forked from rejas/MMM-MotionDetector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
94 lines (86 loc) · 2.28 KB
/
node_helper.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
const NodeHelper = require("node_helper");
const http = require('http');
const exec = require("child_process").exec;
const Log = require("../../js/logger");
module.exports = NodeHelper.create({
/**
*
*/
start: function () {
this.isMonitorOn(function (result) {
Log.info("MMM-MotionDetector: monitor is " + (result ? "ON" : "OFF") + ".");
});
},
/**
*
*/
activateMonitor: function () {
self = this;
this.isMonitorOn(function (result) {
if (!result) {
exec("vcgencmd display_power 1", function (err, out, code) {
if (err) {
Log.error("MMM-MotionDetector: error activating monitor: " + code);
} else {
Log.info("MMM-MotionDetector: monitor has been activated.");
}
});
self.blink();
}
});
this.started = false;
},
blink: function () {
http.get('http://localhost:7878/state?state=alert', (resp) => {
Log.debug("blinked");
}).on("error", (err) => {
Log.error("failed to blink: " + err.message);
});
},
/**
*
*/
deactivateMonitor: function () {
this.isMonitorOn(function (result) {
if (result) {
exec("vcgencmd display_power 0", function (err, out, code) {
if (err) {
Log.error("MMM-MotionDetector: error deactivating monitor: " + code);
} else {
Log.info("MMM-MotionDetector: monitor has been deactivated.");
}
});
}
});
this.started = false;
},
/**
*
* @param resultCallback
*/
isMonitorOn: function (resultCallback) {
exec("vcgencmd display_power", function (err, out, code) {
if (err) {
Log.error("MMM-MotionDetector: error calling monitor status: " + code);
return;
}
Log.info("MMM-MotionDetector: monitor status is " + out);
resultCallback(out.includes("=1"));
});
},
/**
*
* @param notification
* @param payload
*/
socketNotificationReceived: function (notification, payload) {
if (notification === "ACTIVATE_MONITOR") {
Log.info("MMM-MotionDetector: activating monitor.");
this.activateMonitor();
}
if (notification === "DEACTIVATE_MONITOR") {
Log.info("MMM-MotionDetector: deactivating monitor.");
this.deactivateMonitor();
}
},
});