-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (57 loc) · 1.97 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
var http = require('http'),
express = require('express'),
app = express(),
apn = require('apn'),
apnConnection,
deviceTokens = [];
const options = {
"production" : false,
"cert" : __dirname + "/certs/cert.pem",
"key" : __dirname + "/certs/key.pem"
};
apnConnection = new apn.Connection(options);
function transmissionError(errCode, notification, device)
{
console.log('encountered a transmission error with data: ' , errCode + ', ' , notification + ', ', device);
}
apnConnection.on('transmissionError', transmissionError);
app.set('port', 3001);
app.get('/registerDeviceToken/:token', function(req, res) {
console.log('request info: ' , req);
var token = req.params.token;
deviceTokens.push(token);
console.log('registered device token: ' + token);
res.status(201).send({"status" : "OK"});
});
app.get('/deregisterDeviceToken/:token', function(req, res) {
var params = req.params,
token = params.token;
for (var i = 0; i < deviceTokens.length; i++) {
var existToken = deviceTokens[i];
if (existToken == token) {
deviceTokens.splice(i, 1);
break;
}
}
console.log('deregistered token');
res.status(201).send({"status" : "OK"});
});
app.get('/motionDetected/:cameraName', function(req, res) {
var params = req.params,
cameraName = params.cameraName;
for (var i = 0; i < deviceTokens.length; i++) {
var token = deviceTokens[i];
var notification = new apn.Notification();
notification.badge = 0;
notification.sound = 'default';
notification.alert = 'Motion Detected from ' + cameraName;
notification.payload = {'messageFrom' : 'Raspberry Pi'};
var device = new apn.Device(token);
apnConnection.pushNotification(notification, device);
}
console.log('detected motion, sent ' + deviceTokens.length + ' push notifications');
res.status(201).send({"status" : "OK"});
});
http.createServer(app).listen(app.get('port'), function() {
console.log('initiated HTTP server');
});