首頁 > 後端開發 > Golang > 主體

如何從 Asterisk Manager 介面事件取得活動呼叫數量

王林
發布: 2024-02-09 11:20:18
轉載
950 人瀏覽過

如何从 Asterisk Manager 接口事件获取活动呼叫数量

在使用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中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板