This repository has been archived by the owner on Jun 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatomix.js
325 lines (316 loc) · 11.8 KB
/
atomix.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
var sock = null;
var ellog = null;
window.onload = function() {
ellog = document.getElementById('log');
var wsuri;
if (window.location.protocol === "file:") {
wsuri = "ws://127.0.0.1:8080/ws?a=23&foo=bar";
} else {
wsuri = "ws://" + window.location.hostname + ":8080/ws?a=23&foo=bar";
}
if ("WebSocket" in window) {
sock = new WebSocket(wsuri);
} else if ("MozWebSocket" in window) {
sock = new MozWebSocket(wsuri);
} else {
log("Browser does not support WebSocket!");
}
if (sock) {
sock.onopen = function() {
log("CLI> Connected to " + wsuri);
};
sock.onclose = function(e) {
log("CLI> Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')");
sock = null;
};
sock.onmessage = function(e) {
var obj = JSON.parse(e.data);
switch (obj.Event) {
case "Command":
log(obj.Data)
break;
case "Peers":
for (var key in obj.Data){
PJSipContact(key);
if (obj.Data[key] == "OK") {
ContactRegistered(key);
}
}
break;
case "Dahdi":
DahdiContact(obj.Data);
break;
case "Newchannel":
Newchannel(obj.Data);
break;
case "Newstate":
Updatechannel(obj.Data);
break;
case "Hangup":
Destroychannel(obj.Data);
break;
case "PeerStatus":
UpdatePJSipContact(obj.Data);
break;
case "Alarm":
case "AlarmClear":
UpdateDAHDI(obj.Data);
break;
}
}
}
}
function sendCommand() {
command = document.getElementById( "command").value
log("CLI> "+command)
sock.send(command);
};
function DahdiContact(line) {
//creates DIV Element, and assign ID
var div = document.createElement("DIV");
div.setAttribute("id", line.dahdichannel );
//creates DIV Title Element, then asigns class and inner text
var title = document.createElement("DIV")
title.setAttribute("class", "contact-title" );
title.innerText= "DAHDI/"+line.dahdichannel;
//creates DIV alarm Element, then assigns id and innter text
var alarm = document.createElement("TEXT")
alarm.setAttribute("id", line.dahdichannel+"-status" );
alarm.innerText= line.alarm;
//set class by Alarm Status
switch (line.alarm) {
case "Red Alarm":
div.setAttribute("class", "btn btn-danger" );
break;
case "No Alarm":
div.setAttribute("class", "btn btn-success" );
break;
}
//append div to document
div.appendChild(title);
div.appendChild(alarm);
document.getElementById("dahdi").appendChild(div);
}
function UpdateDAHDI(line) {
if (!!line.alarm) {
div = document.getElementById(line.dahdichannel);
div.setAttribute("class", "btn btn-danger" );
text = document.getElementById(line.dahdichannel+"-status");
text.innerText= line.alarm;
}
else {
div = document.getElementById(line.channel);
div.setAttribute("class", "btn btn-success" );
text = document.getElementById(line.channel+"-status");
text.innerText= "No Alarm";
}
}
function PJSipContact(id) {
//create DIV element, then asigns id
var btn = document.createElement("DIV");
btn.setAttribute("id", id );
//create DIV title element, then assigns inner text and class
var title = document.createElement("DIV")
title.setAttribute("class", "contact-title" );
title.innerText= id;
//creates DIV alarm Element, then assigns id
var alarm = document.createElement("TEXT")
alarm.setAttribute("id", id+"-status" );
//eppend div to document
btn.appendChild(title);
btn.appendChild(alarm);
document.getElementById("pjsip").appendChild(btn);
//set contact as Unregistered
ContactNotRegistered(id);
}
function ContactNotRegistered(id){
//gets contact btn and alarm by id, set innerText and assigns classes
var btn = document.getElementById(id);
var alarm = document.getElementById(id+"-status");
alarm.innerText= "No Registered";
btn.setAttribute("class", "btn btn-default" );
}
function ContactRegistered(id) {
//gets contact btn and alarm by id, set innerText and assigns classes
var btn=document.getElementById(id);
var alarm = document.getElementById(id+'-status');
//set classes to contact button and alarm div if exists
if (!!btn) {
btn.setAttribute("class", "btn btn-success" );
}
if (!!alarm){
//check if contact is dahdi or sip
if (id.length <=2 ){
alarm.innerText="No Alarm";
}
else {
alarm.innerText="Registered";
}
}
}
function ContactUp(id) {
//gets contact btn and alarm by id, set innerText and assigns classes
var btn = document.getElementById(id);
var alarm = document.getElementById(id+'-status');
//set classes to contact button and alarm div if exists
if(!!btn){
btn.setAttribute("class", "btn btn-info" );
}
if (!!alarm){
alarm.innerText="Up";
}
}
function UpdatePJSipContact(chan){
//gets peer number from channel
var id= chan.peer.substring(6,9);
//sets contact as Registered or Unregistered
if ( chan.peerstatus == "Unreachable") {
ContactNotRegistered(id)
}
else if (chan.peerstatus == "Reachable"){
ContactRegistered(id);
}
}
function Newchannel(chan) {
//active caller contact
ContactUp(chan.calleridnum);
//sets id as the last three numbers of the channel linkedid
var id = chan.linkedid.substr(chan.linkedid.length - 4)
//gets link, call and btn_time by id
var link = document.getElementById(chan.linkedid);
var call = document.getElementById("call"+chan.linkedid);
var btn_time = document.getElementById(id);
//if don't exists, create link call div
if (link == null){
var link = document.createElement("DIV");
link.setAttribute("id", chan.linkedid );
link.setAttribute("class", "row" );
var title = document.createElement("DIV");
title.setAttribute("class", "row" );
var src = document.createElement("DIV");
src.setAttribute("class", "col-md-4 link-title " );
src.innerText = "SRC channel";
var dst = document.createElement("DIV");
dst.setAttribute("class", "col-md-4 link-title " );
dst.innerText = "DST channel";
var time = document.createElement("DIV");
time.setAttribute("class", "col-md-2 link-title " );
time.innerText = "Time";
var btn_time = document.createElement("DIV");
btn_time.innerText = "00:00:00";
btn_time.setAttribute("id", id);
var call = document.createElement("DIV");
call.setAttribute("class", "row" );
call.setAttribute("id", "call"+chan.linkedid );
title.appendChild(time);
title.appendChild(src);
title.appendChild(dst);
call.appendChild(btn_time);
link.appendChild(title);
link.appendChild(call);
}
//create channel button
var btn = document.createElement("DIV");
btn.innerText = chan.channel+" "+chan.channelstatedesc;
btn.setAttribute("id",chan.channel)
//append button to call row
call.appendChild(btn);
//appends call to channels div
document.getElementById("channels").appendChild(link);
//create Date object, if chan.seconds exists get its value if not set it to 0
var curdate = new Date(null);
if( !!chan.seconds){
seconds = curdate.setTime(chan.seconds*1000);
}
else {
seconds =curdate.setTime(0*1000);
}
//create instance of object Timer, send id and seconds
var timer = new Timer(id,seconds);
//call start method of Timer object
timer.start();
Updatechannel(chan);
}
function Updatechannel(chan) {
var id = chan.linkedid.substr(chan.linkedid.length - 4)
var btn_time = document.getElementById(id) ;
var btn= document.getElementById(chan.channel);
btn.innerText = chan.channel+ " "+chan.channelstatedesc;
// change class by status
switch (chan.channelstatedesc) {
case "Ringing":
btn.setAttribute("class", "col-md-4 alert alert-danger blink" );
break;
case "Up":
//update contact status
btn.setAttribute("class", "col-md-4 alert alert-info" );
btn_time.setAttribute("class", "col-md-2 alert alert-info" );
ch = chan.channel.substring(0,5);
//check if destination contact is DAHDI
if (ch == "DAHDI") {
ContactUp(chan.channel.substring(6,7)); //gets character[7] from string
}
else {
ContactUp(chan.calleridnum); //gets character[7:9] from string
}
break;
default:
btn_time.setAttribute("class", "col-md-2 alert alert-warning" );
btn.setAttribute("class", "col-md-4 alert alert-warning" );
break;
}
}
function Destroychannel(chan) {
//get chan from likedid and change status
var link = document.getElementById(chan.linkedid);
//update contact status
ContactRegistered(chan.calleridnum);
ch = chan.channel.substring(0,5);
//check if destination contact is DAHDI
if (ch == "DAHDI") {
ContactRegistered(chan.channel.substring(6,7));
}
else {
ContactRegistered(chan.calleridnum);
}
//remove link div
if(!!link){
link.remove();
}
}
function ShowPanel(id){
var div = document.getElementById(id+"-panel");
var btn= document.getElementById(id+"-btn");
if (div.style.display !== 'none') {
div.style.display = 'none';
btn.setAttribute("class","btn")
}
else {
div.style.display = 'block';
btn.setAttribute("class","btn btn-primary")
}
}
function log(m) {
ellog.innerHTML += m + '\n';
ellog.scrollTop = ellog.scrollHeight;
}
//declare funcion class Timer
var Timer = function startTime(id,seconds) {
//saves this to variable me
var me = this;
//gets time div of current channel
var btn_timer = document.getElementById(id);
//get starting time
inicio = new Date().getTime();
//function that changes value of btn_timer every second
this.start = function(){
this.actual = new Date().getTime();
this.diff=new Date(this.actual -inicio+seconds);
this.result=LeadingZero(this.diff.getUTCHours())+":"+LeadingZero(this.diff.getUTCMinutes())+":"+LeadingZero(this.diff.getUTCSeconds());
btn_timer.innerHTML = this.result;
setTimeout(function() {me.start(id)}, 1000);
}
}
function LeadingZero(Time) {
return (Time < 10) ? "0" + Time : + Time;
}