forked from paviro/MMM-PIR-Sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
90 lines (76 loc) · 2.69 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
'use strict';
/* Magic Mirror
* Module: MMM-PIR-Sensor
*
* By Paul-Vincent Roll http://paulvincentroll.com
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const Gpio = require('onoff').Gpio;
const exec = require('child_process').exec;
module.exports = NodeHelper.create({
start: function () {
this.started = false;
},
activateMonitor: function () {
if (this.config.relayPIN != false) {
this.relay.writeSync(this.config.relayOnState);
}
else if (this.config.relayPIN == false){
// Check if hdmi output is already on
exec("/opt/vc/bin/tvservice -s").stdout.on('data', function(data) {
if (data.indexOf("0x120002") !== -1)
exec("/opt/vc/bin/tvservice --preferred && chvt 6 && chvt 7", null);
});
}
},
deactivateMonitor: function () {
if (this.config.relayPIN != false) {
this.relay.writeSync(this.config.relayOffState);
}
else if (this.config.relayPIN == false){
exec("/opt/vc/bin/tvservice -o", null);
}
},
// Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === 'CONFIG' && this.started == false) {
const self = this;
this.config = payload;
// Setup value which represent on and off
const valueOn = this.config.invertSensorValue ? 0 : 1;
const valueOff = this.config.invertSensorValue ? 1 : 0;
//Setup pins
this.pir = new Gpio(this.config.sensorPIN, 'in', 'both');
// exec("echo '" + this.config.sensorPIN.toString() + "' > /sys/class/gpio/export", null);
// exec("echo 'in' > /sys/class/gpio/gpio" + this.config.sensorPIN.toString() + "/direction", null);
if (this.config.relayPIN) {
this.relay = new Gpio(this.config.relayPIN, 'out');
this.relay.writeSync(this.config.relayOnState);
exec("/opt/vc/bin/tvservice --preferred && chvt 6 && chvt 7", null);
}
//Detected movement
this.pir.watch(function(err, value) {
if (value == valueOn) {
self.sendSocketNotification("USER_PRESENCE", true);
if (self.config.powerSaving){
clearTimeout(self.deactivateMonitorTimeout);
self.activateMonitor();
}
}
else if (value == valueOff) {
self.sendSocketNotification("USER_PRESENCE", false);
if (!self.config.powerSaving){
return;
}
self.deactivateMonitorTimeout = setTimeout(function() {
self.deactivateMonitor();
}, self.config.powerSavingDelay * 1000);
}
});
this.started = true;
} else if (notification === 'SCREEN_WAKEUP') {
this.activateMonitor();
}
}
});