-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
379 lines (340 loc) · 12 KB
/
server.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright 2014-2015 the project authors as listed in the AUTHORS file.
// All rights reserved. Use of this source code is governed by the
// license that can be found in the LICENSE file.
var mqtt = require('mqtt');
var https = require('https');
var http = require('http');
var path = require('path');
var fs = require('fs');
var auth = require('basic-auth');
var twilio = require('twilio');
var WebSocketServer = require('websocket').server;
// constants
var NUM_ZONES = 8;
var ZONE_STATUS = '/status';
var ZONE_NAME = '/name';
var CAMERA_CAPTURE = '/capture';
var CAMERA_NEWPICTURE = '/newpicture';
// exit codes
var INVALID_SERVER_PORT = -1;
// server configurations
var ssl_options = null;
var serverPort = 8080;
var mqttServerUrl = '';
var mqttRootTopic = null;
var alarmStatusTopic = null;
var zoneTopicPrefix = null;
var eventLogPrevix = null;
var eventLogFile = null;
var title = '';
var webserverUrlForPictures = '';
// alarm state and data
var alarmSite = '';
var latestData = {};
var zoneMapping = {};
// client connections data
var numClients = 0;
var clientArray = {};
// twillio configuration data
var twilioAccountSID;
var twilioAccountAuthToken;
var twilioFromNumber;
var twilioToNumber
// camera configuration
var cameraTopic;
var cameraIRtopic = undefined;
var cameraIRon = undefined;
var cameraIRoff = undefined;
var newPictureTopic;
var cameraCaptureTopic;
var pictureTimer = null;
var picture1 = '';
var picture2 = '';
var picture3 = '';
var picture4 = '';
// for secure connection to mqtt server
var mqttOptions = '';
// authenticate requests to the alarm console
var username = '';
var password;
function authenticate(request,response) {
var authInfo = auth(request);
if (!authInfo || username=='' || authInfo.name !== 'alarm' || authInfo.pass !== password ) {
if (response !== undefined) {
response.writeHead(401, {'WWW-Authenticate': 'Basic realm="alarm"'});
response.end();
}
return false;
}
return true;
}
// function to request that a picture be taken
var irTimer = undefined;
function takePicture() {
irOn();
irOn();
irOn();
// leave some time for the ir light to illuminate
setTimeout(function() {
client.publish(cameraCaptureTopic, 'take');
}, 10);
// now give the pi 60 seconds to take the picture
// and then turn off the ir light. If another
// picture request comes before the first one
// is complete the timer is reset so we should
// always get 60s after the request for the last
// picture
if (irTimer != undefined) {
clearInterval(irTimer);
}
irTimer = setInterval(function() {
irOff();
irOff();
irOff();
clearInterval(irTimer);
irTimer = undefined;
}, 30000);
}
// used to log alarm events
function logEvent(event) {
// not goint to provide error function as we won't do
// anything in case of an error
fs.appendFile(eventLogFile, new Date() + ' :' + event + '\n');
}
// read in the configuration data
function readConfig(configFile) {
ssl_options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem'),
}
data = fs.readFileSync(configFile);
var lines = data.toString().split('\n');
for (line in lines) {
var configParts = lines[line].split('=');
if (1 < configParts.length) {
var configKey = configParts[0];
var configValue = configParts[1];
if ('port' == configKey) {
serverPort = configValue;
if(isNaN(serverPort)) {
logEvent('Invalid server port:' + serverPort);
process.exit(INVALID_SERVER_PORT);
}
} else if ('zone' == configKey) {
var parts = configValue.split(':');
if (1 < parts.length) {
var sensorTopic = parts[0];
var zone = parts[1];
if ((0 <zone) && ( NUM_ZONES >= zone )) {
zoneMapping[sensorTopic] = zoneTopicPrefix + zone + ZONE_STATUS;
if (2 < parts.length) {
// add the description
latestData[zoneTopicPrefix + zone + ZONE_NAME] = parts[2];
}
} else {
logEvent('Invalid zone:' + zone);
}
}
} else if ('mqttRootTopic' == configKey) {
mqttRootTopic = configValue;
alarmStatusTopic = mqttRootTopic + '/alarm/status';
zoneTopicPrefix = mqttRootTopic + '/alarm/zone/';
} else if ('mqttServerUrl' == configKey) {
mqttServerUrl = configValue;
} else if ('eventLogPrefix' == configKey) {
eventLogPrefix = configValue;
eventLogFile = eventLogPrefix + path.sep + 'alarm_event_log';
} else if ('twilioAccountSID' == configKey) {
twilioAccountSID = configValue;
} else if ('twilioAccountAuthToken' == configKey) {
twilioAccountAuthToken = configValue;
} else if ('twilioFromNumber' == configKey) {
twilioFromNumber = configValue;
} else if ('twilioToNumber' == configKey) {
twilioToNumber = configValue;
} else if ('alarmSite' == configKey) {
alarmSite = configValue;
} else if ('cameraTopic' == configKey) {
cameraTopic = configValue;
newPictureTopic = cameraTopic + CAMERA_NEWPICTURE;
cameraCaptureTopic = cameraTopic + CAMERA_CAPTURE;
} else if ('username' == configKey) {
username = configValue;
} else if ('cameraIRtopic' == configKey) {
cameraIRtopic = configValue;
} else if ('cameraIRon' == configKey) {
cameraIRon = configValue;
} else if ('cameraIRoff' == configKey) {
cameraIRoff = configValue;
} else if ('password' == configKey) {
password = configValue;
} else if ('title' == configKey) {
title = configValue;
} else if ('webserverUrlForPictures' == configKey) {
webserverUrlForPictures = configValue;
} else if ('certsDir' == configKey) {
mqttOptions = {
key: fs.readFileSync(configValue + '/client.key'),
cert: fs.readFileSync(configValue + '/client.cert'),
ca: fs.readFileSync(configValue + '/ca.cert'),
checkServerIdentity: function() { return undefined }
}
}
}
}
}
function irOn() {
if ((undefined != cameraIRtopic) && (undefined != cameraIRon)) {
client.publish(cameraIRtopic, cameraIRon);
}
}
function irOff() {
if ((undefined != cameraIRtopic) && (undefined != cameraIRoff)) {
client.publish(cameraIRtopic, cameraIRoff);
}
}
readConfig(process.argv[2]);
logEvent('Read configuration');
// setup the websocket/server enpoint
var mainPage = fs.readFileSync('page.html').toString();
mainPage = mainPage.replace('<ALARM DASHBOARD TITLE>', title);
mainPage = mainPage.replace('<UNIQUE_WINDOW_ID>', title);
mainPage = mainPage.replace('<WEBSERVER_URL>', webserverUrlForPictures);
mainPage = mainPage.replace('<WEBSERVER_URL>', webserverUrlForPictures);
mainPage = mainPage.replace('<WEBSERVER_URL>', webserverUrlForPictures);
var server = https.createServer(ssl_options, function(request,response) {
if (!authenticate(request, response)) {
return;
}
// ok now server the appropriate page base on the request type
if (request.url.indexOf("getlog") > -1) {
var logFile = fs.readFileSync(eventLogFile);
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(logFile);
return;
}
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(mainPage);
});
server.listen(serverPort,function(){
});;
wsServ = new WebSocketServer({
httpServer:server
});
wsServ.on('request', function(newRequest) {
if (!authenticate(newRequest.httpRequest)) {
return;
}
// ok now server the appropriate page base on the request type
// accept connection and add to the list of clients
var newConnection = newRequest.accept('text',newRequest.origin);
var id = numClients++;
clientArray[id] = newConnection;
for (key in latestData) {
if (key != newPictureTopic) {
newConnection.sendUTF(key + ":" + latestData[key]);
}
}
// send the latest pictures
newConnection.sendUTF(newPictureTopic + ":" + picture4);
newConnection.sendUTF(newPictureTopic + ":" + picture3);
newConnection.sendUTF(newPictureTopic + ":" + picture2);
newConnection.sendUTF(newPictureTopic + ":" + picture1);
// when client disconnections remove it from the list
newConnection.on('close', function(reason,description) {
delete clientArray[id];
});
newConnection.on('message', function(message) {
var parts = message.utf8Data.split(":");
var topic = parts[0];
var value = parts[1];
if (topic == alarmStatusTopic) {
client.publish(topic, value);
} else if (topic == cameraCaptureTopic) {
takePicture();
}
});
});
var client = mqtt.connect(mqttServerUrl, mqttOptions);
/* each time we connect register on all topics we are interested
* in. This must be done after a reconnect as well as the
* initial connect
*/
client.on('connect',function() {
client.subscribe(alarmStatusTopic);
client.subscribe(zoneTopicPrefix + '+/+');
client.subscribe(newPictureTopic);
for(topic in zoneMapping) {
client.subscribe(topic);
}
});
client.on('message', function(topic, message) {
latestData[topic] = message;
if (alarmStatusTopic == topic) {
if (('arm' == message) || ('disarm' == message)) {
// first clear the state of all of the zones
for(i=1;i < NUM_ZONES+1; i++) {
client.publish(zoneTopicPrefix + i + ZONE_STATUS, 'off');
}
// stop taking pictures if we are as we are reseting the alarm state
if (null != pictureTimer) {
clearInterval(pictureTimer);
}
if ('arm' == message) {
logEvent('Armed');
} else {
logEvent('Dis-Armed');
}
} else if ('triggered' == message) {
logEvent('Alarm Triggered:' + Date());
// take pictures every 10 seconds for 5 minutes after the alarm is triggered
var count = 0;
pictureTimer = setInterval(function() {
takePicture();
count++;
if (30 < count) {
clearInterval(pictureTimer);
}
}, 10000);
// send sms message indicating alarm has been triggered
var twilioClient = new twilio.RestClient(twilioAccountSID, twilioAccountAuthToken);
twilioClient.sendMessage({
to: twilioToNumber,
from: twilioFromNumber,
body: 'Alarm triggered:' + alarmSite
}, function(err, message) {
if (err) {
logEvent('Failed to send sms:' + err.message);
} else {
logEvent('SMS Sent:' + message.sid);
}
});
}
} else if (topic == newPictureTopic) {
// seems like we sometimes get duplicates, avoid adding these to the picture rotation
if ((message != picture1) &&
(message != picture2) &&
(message != picture3) &&
(message != picture4)) {
picture4 = picture3
picture3 = picture2
picture2 = picture1
picture1 = message;
}
}
if (undefined != zoneMapping[topic]) {
// sensor alarm, map and publish alarm event
client.publish(zoneMapping[topic], 'on');
logEvent('Zone triggered - ' + zoneMapping[topic]);
// if we are armed then system has been triggered
if ('arm' == latestData[alarmStatusTopic]) {
client.publish(alarmStatusTopic, 'triggered');
}
} else {
// other message publish directly to clients
for (var i in clientArray) {
clientArray[i].sendUTF(topic + ":" + message);
}
}
});
logEvent('Alarm active');