-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradar.js
94 lines (81 loc) · 2.64 KB
/
radar.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
///////////////////////////////////////////////
// ORIGINAL POC. DO NOT USE, WILL BE DELETED //
///////////////////////////////////////////////
let Gpio = require('onoff').Gpio,
RaspiCam = require('raspicam'),
pir = new Gpio(4, 'in', 'both'),
https = require('https'),
FROM_PHONE_NUMBER = process.env.RADAR_TWILIO_FROM_PHONE_NUMBER,
TO_PHONE_NUMBER = process.env.RADAR_TWILIO_TO_PHONE_NUMBER,
accountSid = process.env.RADAR_TWILIO_ACCOUNTSID,
authToken = process.env.RADAR_TWILIO_AUTHTOKEN,
mediaUrl = `${process.env.RADAR_HTTP_PROTO}${process.env.RADAR_HTTP_HOST}/`,
client = require('twilio')(accountSid, authToken),
uuidV4 = require('uuid/v4');
function motionWatcher (err, value) {
let fileName;
if (err) {
console.error(err);
// SEND Err SMS
notifyFault(err);
throw err;
}
pir.unwatch();
console.info(`Motion detected... ${value}`);
var camera = new RaspiCam({
mode: "photo",
output: `/var/www/html/pi-photos/image-${uuidV4()}.jpg`,
encoding: "jpg",
timeout: 2,
w: 640,
h: 480
});
camera.on("start", function( err, timestamp ){
console.log("photo started at " + timestamp );
});
camera.on("read", function( err, timestamp, filename ) {
console.log("photo image captured with filename: " + filename );
if (filename.indexOf('~') > -1) {
// Fucking rename operation hoses the MMS file capture!
return;
}
fileName = filename;
notify(fileName);
});
camera.on("exit", function( timestamp ){
console.log("photo child process has exited at " + timestamp );
});
camera.start();
}
function notify (fileName) {
client.messages.post({
to: TO_PHONE_NUMBER,
from: FROM_PHONE_NUMBER,
body: `Motion detected @ ${new Date()}\n${mediaUrl}${fileName}`,
// mediaUrl: `${mediaUrl}${fileName}` // MMS, but expensive
}, function (err, message) {
if (err) {
return console.error('Cannot send SMS', err);
pir.watch(motionWatcher);
}
console.log(message);
pir.watch(motionWatcher);
});
}
function notifyFault (smsMessage) {
client.messages.post({
to: TO_PHONE_NUMBER,
from: FROM_PHONE_NUMBER,
body: `RADAR ERROR at ${new Date()}: ${smsMessage}`
}, function (err, message) {
if (err) {
return console.error('Cannot send fault SMS', err);
}
console.log(message);
pir.watch(motionWatcher);
});
}
pir.watch(motionWatcher);
process.on('SIGINT', function () {
pir.unexport();
});