forked from TrunkRecorder/trunk-recorder-status-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
273 lines (231 loc) · 8.76 KB
/
index.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
var express = require('express');
var util = require("util");
var app = express(),
http = require('http'),
server = http.createServer(app);
const WebSocket = require('ws');
const clientWss = new WebSocket.Server({
noServer: true
});
const serverWss = new WebSocket.Server({
noServer: true
});
const url = require('url');
var csv = require('csv');
var fs = require('fs');
var path = require('path');
var router = express.Router();
var bodyParser = require('body-parser');
var channels = {};
var clients = [];
var srv = null;
var rec = null;
var stats = {};
server.on('upgrade', (request, socket, head) => {
const pathname = url.parse(request.url).pathname;
if (pathname === '/client') {
console.log("Upgrading Client Connection");
clientWss.handleUpgrade(request, socket, head, (ws) => {
clientWss.emit('connection', ws);
});
} else if (pathname === '/server') {
console.log("Upgrading Server Connection");
serverWss.handleUpgrade(request, socket, head, (ws) => {
serverWss.emit('connection', ws);
});
} else {
socket.destroy();
}
});
app.use(bodyParser());
app.use(express.static(__dirname + '/public'));
function get_clients(req, res) {
if (req.params.shortName) {
var short_name = req.params.shortName.toLowerCase();
} else {
var short_name = null;
}
for (var i = 0; i < clients.length; i++) {
var response = [];
//console.log(util.inspect(clients[i].socket));
if (!short_name || (clients[i].shortName == short_name)) {
var age = (Date.now() - clients[i].timestamp) / 1000;
var obj = {
shortName: clients[i].shortName,
filterCode: clients[i].filterCode,
filterName: clients[i].filterName,
filterType: clients[i].filterType,
talkgroupNums: clients[i].talkgroupNums,
type: clients[i].type,
timestamp: age
}
response.push(obj);
}
}
res.contentType('json');
res.send(JSON.stringify(response));
}
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/public/index.html'));
});
app.get('/:shortName/clients', get_clients);
app.get('/clients', get_clients);
function notify_clients(call) {
call.type = "calls";
var sent = 0;
for (var i = 0; i < clients.length; i++) {
//console.log(util.inspect(clients[i].socket));
if (clients[i].shortName == call.shortName.toLowerCase()) {
if (clients[i].filterCode == "") {
//console.log("Call TG # is set to All");
sent++;
clients[i].socket.send(JSON.stringify(call));
} else if (clients[i].filterType == "unit") {
var codeArray = clients[i].filterCode.split(',');
var success = false;
for (var j = 0; j < codeArray.length; ++j) {
for (var k = 0; k < call.srcList.length; k++) {
if (codeArray[j] == call.srcList[k]) {
sent++;
clients[i].socket.send(JSON.stringify(call));
success = true;
break;
}
}
if (success) {
break;
}
}
} else {
var codeArray = clients[i].talkgroupNums;
//console.log("Group Client: " + i + "\tCodes: " + codeArray + "\tTalkgroupNum: " + call.talkgroupNum);
for (var j = 0; j < codeArray.length; ++j) {
if (codeArray[j] == call.talkgroupNum) {
console.log("[ " + i + " ] - Sending one filtered call");
clients[i].socket.send(JSON.stringify(call));
sent++
break;
}
}
}
}
}
if (sent > 0) {
console.log("Sent calls to " + sent + " clients, System: " + call.shortName.toLowerCase());
}
}
function heartbeat() {
this.isAlive = true;
}
clientWss.on('connection', function connection(ws, req) {
var client = {
socket: ws
};
clients.push(client);
ws.isAlive = true;
ws.on('pong', heartbeat);
console.log((new Date()) + ' WebSocket Connection accepted.');
ws.on('message', function incoming(message) {
console.log("Got message: " + message);
try {
var data = JSON.parse(message);
if (typeof data.type !== "undefined") {
if (data.type == 'add') {
var client = {
socket: ws,
code: null
};
clients.push(client);
console.log("[ " + data.type + " ] Client added");
if (srv){
console.log("Sending Srv config: " + srv.config);
ws.send(JSON.stringify(srv.config));
console.log("Sent");
}
if (rec){
console.log("Sending Recorders config: " + rec.config);
ws.send(JSON.stringify(rec.config));
console.log("Sent");
}
}
var index = clients.indexOf(client);
if (index != -1) {
clients[index].timestamp = new Date();
console.log("[ " + data.type + " ] Client updated: " + index);
} else {
console.log("Error - WebSocket: Client not Found!");
}
}
} catch (err) {
console.log("JSON PArsing Error: " + err);
}
console.log('Received Message: ' + message);
});
ws.on('close', function(reasonCode, description) {
console.log((new Date()) + ' Client ' + connection.remoteAddress + ' disconnected.');
console.log("code: " + reasonCode + " description: " + description);
for (var i = 0; i < clients.length; i++) {
// # Remove from our connections list so we don't send
// # to a dead socket
if (clients[i].socket == ws) {
clients.splice(i);
break;
}
}
});
});
serverWss.on('connection', function connection(ws, req) {
ws.isAlive = true;
ws.on('pong', heartbeat);
console.log((new Date()) + ' WebSocket Connection accepted.');
ws.on('message', function incoming(message) {
try { var data = JSON.parse(message);
if (typeof data.type !== "undefined") {
if (data.type == 'config') {
srv = {
socket: ws,
config: data,
timestamp: new Date()
};
console.log("[ " + data.type + " ] Server Live - config rcv'd");
} else if (data.type == 'calls_active') {
console.log("[ " + data.type + " ] Server - calls_active message ");
} else if (data.type == 'rates') {
console.log("[ " + data.type + " ] Server - rates message ");
} else if (data.type == 'recorders') {
rec = {
socket: ws,
config: data,
timestamp: new Date()
};
console.log("[ " + data.type + " ] Server - recorders message ");
} else if (data.type == 'recorder') {
console.log("[ " + data.type + " ] Server - recorder message ");
} else {
console.log("[ " + data.type + " ] Server - Unknown message type");
}
} else {
console.log("Server - Message type not defined");
}
clientWss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
} catch (err) {
console.log("JSON Parsing Error: " + err);
}
console.log('Received Message: ' + message);
});
ws.on('close', function(reasonCode, description) {
console.log((new Date()) + ' Server ' + connection.remoteAddress + ' disconnected.');
console.log("code: " + reasonCode + " description: " + description);
srv = null;
});
});
server.listen(3010, function() {
console.log('Web interface is available at: ' + server.address().port + '...');
console.log('status socket address is probably: ws://localhost:' + server.address().port + '/server');
console.log(process.env)
});
module.exports = server;