2

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



Vedo
  • 976
  • 4
  • 21
  • Did [this](https://stackoverflow.com/search?q=%5Basterisk%5D+get+number+of+active+calls) not help? – Luuk Mar 14 '23 at 14:43

2 Answers2

1

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

}

Vedo
  • 976
  • 4
  • 21
0

You should issue action type "COMMAND" and read a result.

Commands can be

  1. this one shows total of channels

    core show channels count

  2. this one shows channels in human-readable format

    core show channels

  3. this one show in machine/parsable

    core show channels concise

Please note, that it may be not very reliable way. You have count yourself or use some dialplan blockers like function GROUP/GROUP_COUNT to make reliable results.

You can send custom event from your dialplan by UserEvent dialplan command.

Yes, asterisk is PBX which is not manageable without state machine knowledge(dialplan). At least not reliable.

arheops
  • 15,544
  • 1
  • 21
  • 27