-
Notifications
You must be signed in to change notification settings - Fork 9
/
motiondetector.js
78 lines (61 loc) · 1.91 KB
/
motiondetector.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
'use strict';
Module.register('motiondetector',{
defaults: {
powerSaving: true,
timeout: 120000 // 5 mins
},
lastTimeMotionDetected: null,
poweredOff: false,
getScripts: function() {
return ["diff-cam-engine.js"];
},
// Override socket notification handler.
socketNotificationReceived: function(notification, payload) {
if (notification === "USER_PRESENCE"){
this.sendNotification(notification, payload)
}
},
start: function() {
Log.info('Starting module: ' + this.name);
this.lastTimeMotionDetected = new Date();
var _this = this;
// make sure that the monitor is on when starting
_this.sendSocketNotification('MOTION_DETECTED', _this.config);
var video = document.createElement('video');
var cameraPreview = document.createElement("div");
cameraPreview.id = "cameraPreview";
cameraPreview.style = "visibility:hidden;"
cameraPreview.appendChild(video);
var canvas = document.createElement('canvas');
DiffCamEngine.init({
video: video,
motionCanvas: canvas,
initSuccessCallback: function () {
DiffCamEngine.start();
},
initErrorCallback: function () {
console.log('error init cam engine');
},
captureCallback: function(payload){
var score = payload.score;
if (score > 20) {
_this.lastTimeMotionDetected = new Date();
if (_this.poweredOff) {
_this.poweredOff = false;
_this.sendSocketNotification('MOTION_DETECTED', _this.config);
}
}
else {
var currentDate = new Date();
var time = currentDate.getTime() - _this.lastTimeMotionDetected;
if ((time > _this.config.timeout) && (!_this.poweredOff)) {
_this.sendSocketNotification('DEACTIVATE_MONITOR', _this.config);
_this.sendNotification('DEACTIVATE_MONITOR', _this.config);
_this.poweredOff = true;
}
}
console.log('score:' + score);
}
});
},
});