-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (66 loc) · 2.55 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
const { getCameras, CameraStream } = require('./modules/ffmpeg');
const MotionDetector = require('./modules/motionDetection');
const DVR = require('./modules/DVR');
const Notifier = require('./modules/notifier');
const path = require('path');
const WebServer = require('./modules/webService');
require('dotenv').config();
const debounceTime = 1 * 1000; //1 seconds
(() => {
const notifier = new Notifier({
token: {
key: './AuthKey.p8',
keyId: process.env.apnKeyId,
teamId: process.env.apnTeamId,
},
production: true,
});
let cameras = getCameras();
if (cameras.length == 0) {
console.log('no cameras could be found... exiting...');
return;
}
let streams = [];
for (let [cameraNumber, camera] of Object.entries(cameras)) {
let stream = new CameraStream(camera, 1280, 720);
streams.push({ stream, cameraNumber });
let dvr = new DVR(stream, 1);
let motionDetector = new MotionDetector(stream, 5);
let cooldown = 0;
let dvrTimeout;
setTimeout(() => {
//wait 2 second otherwise we might detect the camera turning on as motion
console.log('started...');
motionDetector.on('motion', (distance, frame) => {
if (cooldown > Date.now()) return;
cooldown = Date.now() + debounceTime; // set cooldown
if (!dvr.recording) {
let outputPath = path.join(
__dirname,
'clips',
Date.now() + '_' + cameraNumber + '_' + distance + '.mp4'
);
dvr.start(outputPath);
console.log(
'motion detected on camera ' +
cameraNumber +
' recording started at ' +
outputPath
);
notifier.notify(
'Motion Detected on camera ' + cameraNumber,
'Saved DVR, started recording',
'camera' + cameraNumber
);
}
if (dvrTimeout) clearTimeout(dvrTimeout);
dvrTimeout = setTimeout(() => {
dvr.stop();
console.log('motion not detected for 5 seconds dvr stopping...');
}, 5000);
});
}, 5000);
}
const webServer = new WebServer(80, process.env.password, streams);
webServer.start();
})();