-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
147 lines (107 loc) · 3.48 KB
/
monitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bytes"
"encoding/csv"
"fmt"
"log"
"strings"
"sync"
"github.com/fiorix/go-eventsocket/eventsocket"
)
// c.Send(fmt.Sprintf("bgapi originate %s %s", dest, dialplan))
func monitor(wg *sync.WaitGroup, channel chan message) {
fmt.Println("\nRodando Monitor:")
var msg message
//var body string
var reader *csv.Reader
var record [][]string
var event *eventsocket.Event
c, err := eventsocket.Dial("localhost:8021", "ClueCon")
if err != nil {
log.Fatal(err)
}
c.Send("event plain CHANNEL_CREATE CHANNEL_ANSWER CHANNEL_HANGUP CHANNEL_HANGUP_COMPLETE CALL_UPDATE CHANNEL_BRIDGE")
//c.Send("event plain all")
// c.Send("event plain BACKGROUND_JOB")
//c.Send("event plain CHANNEL_ANSWER BACKGROUND_JOB")
//Only execute when start monitor
event, _ = c.Send("api show calls")
//fmt.Println(event.Body)
reader = csv.NewReader(bytes.NewBufferString(strings.Split(event.Body, "\n\n")[0]))
record, err = reader.ReadAll()
fmt.Println(record)
if err != nil {
fmt.Println("Error:", err)
return
}
for i := 1; i < len(record); i++ {
if record[i][1] == "outbound" {
continue
}
fmt.Println(" ", record[i])
msg.Action = "Add"
msg.Unique_Id = record[i][0]
msg.Call_Direction = record[i][1]
msg.Event_Date_Local = record[i][2]
msg.Caller_Caller_Id_Number = record[i][7]
msg.Caller_Destination_Number = record[i][9]
msg.Channel_Call_State = record[i][12]
log.Println(activeCalls.Add(msg))
log.Println(activeCalls.Calls[0].Unique_Id)
channel <- msg
// fmt.Println(msg)
}
for {
ev, err := c.ReadEvent()
if err != nil {
log.Fatal(err)
}
if ev.Get("Call-Direction") == "outbound" {
continue
}
//ev.PrettyPrint()
fmt.Println("\nNew event: ", ev.Get("Event-Name"))
fmt.Println("Channel-State: ", ev.Get("Channel-State"))
// fmt.Println("Answer-State: ", ev.Get("Answer-State"))
fmt.Println("Channel-Call-State: ", ev.Get("Channel-Call-State"))
// fmt.Println("Call-Direction: ", ev.Get("Call-Direction"))
// fmt.Println("Channel-Call-Uuid: ", ev.Get("Channel-Call-Uuid"))
// fmt.Println("Unique-Id: ", ev.Get("Unique-Id"))
if ev.Get("Event-Name") == "CHANNEL_CREATE" {
fmt.Println("Event-Date-Local: ", ev.Get("Event-Date-Local"))
fmt.Println("Caller-Caller-Id-Number: ", ev.Get("Caller-Caller-Id-Number"))
fmt.Println("Caller-Destination-Number: ", ev.Get("Caller-Destination-Number"))
msg.Action = "Add"
msg.Event_Date_Local = ev.Get("Event-Date-Local")
msg.Caller_Caller_Id_Number = ev.Get("Caller-Caller-Id-Number")
msg.Caller_Destination_Number = ev.Get("Caller-Destination-Number")
}
if ev.Get("Event-Name") == "CHANNEL_HANGUP" || ev.Get("Event-Name") == "CHANNEL_ANSWER" || ev.Get("Event-Name") == "CALL_UPDATE" || ev.Get("Event-Name") == "CHANNEL_BRIDGE" {
msg.Action = "Mod"
}
if ev.Get("Event-Name") == "CHANNEL_HANGUP_COMPLETE" {
msg.Action = "Del"
}
msg.Channel_State = ev.Get("Channel-State")
//msg.Answer_State = ev.Get("Answer-State")
msg.Channel_Call_State = ev.Get("Channel-Call-State")
msg.Call_Direction = ev.Get("Call-Direction")
// msg.Channel_Call_Uuid = ev.Get("Channel-Call-Uuid")
msg.Unique_Id = ev.Get("Unique-Id")
if msg.Action == "Add" {
log.Println(activeCalls.Add(msg))
log.Println(activeCalls.Calls[0].Unique_Id)
}
if msg.Action == "Del" {
log.Println(activeCalls.Del(msg))
}
if msg.Action == "Mod" {
log.Println(activeCalls.Update(msg))
}
log.Println(activeCalls.Calls)
channel <- msg
}
c.Close()
wg.Done()
//close(a)
}