-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMMM-MotionDetector.js
112 lines (100 loc) · 3.62 KB
/
MMM-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
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
/* global DiffCamEngine */
Module.register("MMM-MotionDetector", {
defaults: {
captureIntervalTime: 1000, // 1 second
scoreThreshold: 20,
timeout: 120000, // 2 minutes,
deviceId: null,
},
lastScoreDetected: null,
lastTimeMotionDetected: null,
lastTimePoweredOff: null,
percentagePoweredOff: 0,
poweredOff: false,
poweredOffTime: 0,
timeStarted: null,
error: null,
getHeader: function () {
return "MMM-MotionDetector";
},
getScripts: function () {
return ["moment.js", "diff-cam-engine.js"];
},
getTemplate: function () {
return "MMM-MotionDetector.njk";
},
getTemplateData: function () {
return {
duration: moment.duration(this.poweredOffTime).humanize(),
lastScoreDetected: this.lastScoreDetected,
lastTimeMotionDetected: this.lastTimeMotionDetected.toLocaleTimeString(),
percentagePoweredOff: this.percentagePoweredOff,
timeout: this.config.timeout,
error: this.error,
};
},
// Override socket notification handler.
socketNotificationReceived: function (notification, payload) {
if (notification === "USER_PRESENCE") {
this.sendNotification(notification, payload);
}
},
start: function () {
Log.info("MMM-MotionDetector: starting up");
this.data.header = "MMM-MotionDetector";
this.lastScoreDetected = 0;
this.lastTimeMotionDetected = new Date();
this.lastTimePoweredOff = new Date();
this.timeStarted = new Date().getTime();
// make sure that the monitor is on when starting
this.sendSocketNotification("ACTIVATE_MONITOR");
const canvas = document.createElement("canvas");
const video = document.createElement("video");
const cameraPreview = document.createElement("div");
cameraPreview.id = "cameraPreview";
cameraPreview.style = "visibility:hidden;";
cameraPreview.appendChild(video);
DiffCamEngine.init({
video: video,
deviceId: this.config.deviceId,
captureIntervalTime: this.config.captureIntervalTime,
motionCanvas: canvas,
scoreThreshold: this.config.scoreThreshold,
initSuccessCallback: () => {
Log.info("MMM-MotionDetector: DiffCamEngine init successful.");
DiffCamEngine.start();
},
initErrorCallback: (error) => {
Log.error("MMM-MotionDetector: DiffCamEngine init failed: " + error);
this.error = error;
this.updateDom();
},
captureCallback: ({ score, hasMotion }) => {
const currentDate = new Date();
this.percentagePoweredOff = ((100 * this.poweredOffTime) / (currentDate.getTime() - this.timeStarted)).toFixed(
2
);
if (hasMotion) {
Log.info("MMM-MotionDetector: Motion detected, score " + score);
this.sendSocketNotification("MOTION_DETECTED", { score: score });
this.sendNotification("MOTION_DETECTED", { score: score });
if (this.poweredOff) {
this.sendSocketNotification("ACTIVATE_MONITOR");
this.poweredOffTime = this.poweredOffTime + (currentDate.getTime() - this.lastTimePoweredOff.getTime());
this.poweredOff = false;
}
this.lastTimeMotionDetected = currentDate;
} else {
const time = currentDate.getTime() - this.lastTimeMotionDetected.getTime();
if (this.config.timeout >= 0 && time > this.config.timeout && !this.poweredOff) {
this.sendSocketNotification("DEACTIVATE_MONITOR");
this.lastTimePoweredOff = currentDate;
this.poweredOff = true;
}
}
this.lastScoreDetected = score;
if (this.data.position) this.updateDom();
},
});
},
});