This repository was archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnode_helper.js
108 lines (87 loc) · 2.4 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
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
/* Magic Mirror
* Module: MMM-PIR-Sensor-Lite
*
* Magic Mirror By Michael Teeuw https://magicmirror.builders
* MIT Licensed.
*
* Module MMM-PIR-Sensor-Lite By Grena https://github.com/grenagit
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const spawn = require('child_process').spawn;
const exec = require('child_process').exec;
module.exports = NodeHelper.create({
start: function() {
this.started = false;
this.activated = true;
},
getDataPIR: function() {
//exec('pkill -f "python3 -u ' + __dirname + '/pir.py"', { timeout: 500 });
const process = spawn('python3', ['-u', __dirname + '/pir.py', this.config.sensorPin]);
var self = this;
process.stdout.on('data', function(data) {
if(data.indexOf("PIR_START") === 0) {
self.sendSocketNotification("STARTED", true);
self.started = true;
}
if(data.indexOf("USER_PRESENCE") === 0) {
self.sendSocketNotification("USER_PRESENCE", true);
self.resetTimeout();
if(self.activated == false) {
self.activateMonitor();
}
}
});
},
activateMonitor: function() {
this.sendSocketNotification("POWER_ON", true);
this.activated = true;
if(!this.config.debugMode) {
switch(this.config.commandType) {
case 'vcgencmd':
exec("/usr/bin/vcgencmd display_power 1", null);
break;
case 'xrandr':
exec("xrandr --output " + this.config.hdmiPort + " --rotate " + this.config.rotation + " --auto", null);
break;
case 'xset':
exec("xset dpms force on", null);
break;
}
}
},
deactivateMonitor: function() {
this.sendSocketNotification("POWER_OFF", true);
this.activated = false;
if(!this.config.debugMode) {
switch(this.config.commandType) {
case 'vcgencmd':
exec("/usr/bin/vcgencmd display_power 0", null);
break;
case 'xrandr':
exec("xrandr --output " + this.config.hdmiPort + " --off", null);
break;
case 'xset':
exec("xset dpms force off", null);
break;
}
}
},
resetTimeout: function() {
var self = this;
clearTimeout(self.timeout);
self.timeout = setTimeout(function() {
self.deactivateMonitor();
}, self.config.deactivateDelay);
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if(notification === 'CONFIG' && self.started == false) {
self.config = payload;
self.activateMonitor();
self.getDataPIR();
self.resetTimeout();
}
}
});