goasteriskasteriskami

How can I get number of active calls from Asterisk Manager Interface Event


I've made connection to asterisk, and managed to get number of active and inactive peers from event PeerStatus, but now i need to get number of active calls and channels and display them. I've tried method to look for ChannelStateDesc=Up but it doesn't work. Even when I put logs I don't see that the event is being found. How can i fix it (maybe with event CoreShowChannelsComplete?) Thanks in advance

//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)
        }
    }
}




Solution

  • I've figured it out, here is the code: //server.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
    

    }