在使用Asterisk通信系统时,了解活动呼叫数量对于监控和管理呼叫流量非常重要。而通过Asterisk Manager接口可以获取到各种事件信息,包括呼叫数量。本文将向您介绍如何使用Asterisk Manager接口来获取活动呼叫数量的方法和步骤。无论您是Asterisk的新手还是有经验的用户,本文都会为您提供详细的指导。让我们一起来看看吧!
我已经连接到星号,并设法从事件 peerstatus 中获取活动和非活动对等点的数量,但现在我需要获取活动呼叫和通道的数量并显示它们。我尝试过寻找 channelstatedesc=up 的方法,但它不起作用。即使我放置日志,我也看不到该事件被发现。我该如何修复它(也许通过事件 coreshowchannelscomplete?)提前致谢
//server.go package server import ( "bufio" "fmt" "net" "strings" "data" ) func connecttoami(address, username, password string) error { conn, err := net.dial("tcp", address) if err != nil { return err } defer conn.close() fmt.fprintf(conn, "action: login\r\nusername: %s\r\nsecret: %s\r\n\r\n", username, password) peerstatus := &data.peerstatus{} callstatus := &data.callstatus{} scanner := bufio.newscanner(conn) for scanner.scan() { line := scanner.text() fmt.println(line) if strings.hasprefix(line, "peerstatus") { data.getpeerstatus(line, peerstatus) fmt.println("active peers:", peerstatus.active) fmt.println("inactive peers:", peerstatus.inactive) } else if strings.hasprefix(line, "coreshowchannel") { data.getchannelstatus(line, callstatus) fmt.println("active peers:", peerstatus.active) fmt.println("inactive peers:", peerstatus.inactive) } } if err := scanner.err(); err != nil { return err } return nil }
//calls.go package data import ( "encoding/json" "fmt" "strings" ) type CallStatus struct { ActiveCalls int `json:"active_calls"` ActiveChannels int `json:"active_channels"` } func (cs *CallStatus) UpdateCallStatus(state string) { fmt.Println("UpdateCallStatus function", state) switch state { case "Up": cs.ActiveCalls++ cs.ActiveChannels = cs.ActiveCalls * 2 case "Down": cs.ActiveCalls-- cs.ActiveChannels=cs.ActiveChannels-2 default: } } func GetChannelStatus(event string, callStatus *CallStatus) { fmt.Println("GetChannelStatus function", event) for _, line := range strings.Split(event, "\r\n") { if strings.HasPrefix(line, "ChannelStateDesc: ") { state := strings.TrimSpace(strings.TrimPrefix(line, "ChannelStateDesc: ")) callStatus.UpdateCallStatus(state) } } }
我已经弄清楚了,代码如下: //服务器.go
parts := strings.split(line, ": ") if len(parts) == 2 { key := parts[0] value := parts[1] if key == "event" { object.event = value } if key == "channelstate" { object.channelstate = value } if key == "linkedid" { object.linkedid = value } } data.handleevent(object, activecalls)
calls.go
package data import ( "encoding/json" "fmt" ) type Data struct { Event string `json:"Event"` ChannelState string `json:"ChannelState"` Linkedid string `json:"Linkedid"` } type ActiveCalls struct { Count int `json:"active_calls"` } func HandleEvent(data Data, activeCalls *ActiveCalls) { if data.Event == "Newstate" { fmt.Println(data.ChannelState) if data.ChannelState == "6" { activeCalls.Count++ fmt.Println("Newstate count active calls", activeCalls.Count) } } else if data.Event == "Hangup" { fmt.Println(data.ChannelState) activeCalls.Count-- if activeCalls.Count < 0 { activeCalls.Count = 0 } fmt.Println("Newstate count active calls after hangup", activeCalls.Count) } } func ActiveCallsToJSON(activeCalls *ActiveCalls) (string, error) { jsonBytes, err := json.Marshal(activeCalls) if err != nil { return "", err } return string(jsonBytes), nil
}
以上是如何从 Asterisk Manager 接口事件获取活动呼叫数量的详细内容。更多信息请关注PHP中文网其他相关文章!