-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
executable file
·271 lines (258 loc) · 7.82 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
var NodeHelper = require("node_helper");
var fs = require("fs");
var exec = require("child_process").exec;
const path = require("path");
module.exports = NodeHelper.create({
config: null,
timeractve : null,
sleeping: false,
hdmi: false,
init: function(){
console.log("handler helper in init");
},
start: function(){
console.log("handler helper in start");
//self=this;
},
stop: function(){
console.log("handler helper in stop");
},
noUser: function(self)
{
//if(self.sleeping==false)
//{
self.socketNotificationReceived("start_sleep");
//}
} ,
socketNotificationReceived: function(notification, payload)
{
let self = this;
console.log("sleep-wake helper in socket notification="+notification);
switch(notification.toUpperCase())
{
case "CONFIG":
self.config=payload;
self.timeractive=setTimeout(()=>{self.noUser(self)},self.config.delay*(60*1000));
if(true) { // if(self.config.source.toUpperCase() === "EXTERNAL"){
exec("/bin/chmod +x "+path.join(__dirname,"external_motion"), function (error, stdout, stderr) {
if(error!=null)
{
console.log("change permissions failed "+JSON.stringify(error));
}
});
if(self.config.ndetectionDir=="/motion")
{
// its the default folder
//console.log("full path="+path.join(__dirname,self.config.detectionDir))
self.config.ndetectionDir= path.join(__dirname,self.config.ndetectionDir);
//console.log("setting detectionDir path from local folder ="+self.config.detectionDir);
}
if(self.config.debug)
console.log(" external source defined dir="+self.config.ndetectionDir);
// check to see if the external motion event folder exists
fs.access(self.config.ndetectionDir, function(err) {
// if not
if (err && err.code === "ENOENT") {
// create it
fs.mkdir(self.config.ndetectionDir, (err)=> {
if(self.config.debug)
console.log("created motion directory", self.config.ndetectionDir);
exec("/bin/chmod 777 "+self.config.ndetectionDir, function (error, stdout, stderr) {
if(error!=null)
{
if(self.config.debug)
console.log("change permissions failed "+JSON.stringify(error));
}
});
})
}
else{
// make sure the directory is empty
self.rmDir(self.config.ndetectionDir,false);
}
// change detector function
// watch for a file to appear in the folder
fs.watch(self.config.ndetectionDir, (eventType, filename) => {
if (filename) {
// remove the file
fs.unlink(path.join(self.config.ndetectionDir,filename), function(error) {
// consume the enonet error
if(error == null){
if(self.config.debug)
console.log("motion detected from external source");
// if the start motion file
if(filename === self.config.ndetectionFile) {
// signal motion started
if(self.config.debug)
console.log("!s:","motionstart");
clearTimeout(self.timeractive);
//self.sleeping=false;
self.timeractive=null;
//if(self.sleeping){
self.socketNotificationReceived("end_sleep");
//}
}
else {
// signal motion ended
if(self.config.debug)
console.log("!e:","motionend");
self.timeractive=setTimeout(()=>{self.noUser(self)},self.config.delay*(60*1000));
if(self.config.debug)
console.log("idle timer started for "+self.config.delay+" minutes")
}
}
});
} else {
if(self.config.debug)
console.log("filename not provided");
}
});
});
}
break;
case "USER_PRESENCE":
if(payload==true)
{self.sleeping==false;}
break;
case "START_SLEEP":
if(self.config.debug)
console.log("processing start sleep")
// if we are not already sleeping
if(!self.sleeping){
self.sleeping=true;
switch(self.config.mode.toUpperCase())
{
case "PI":
if(self.config.debug)
console.log("using PI approach (tvservice)='"+self.config.pi_off+"'")
exec(self.config.pi_off, function (error, stdout, stderr) {
if(error!=null)
{
console.log(self.config.pi_off +" failed "+JSON.stringify(error));
}
});
self.hdmi = false;
break;
case "HIDE":
if(self.config.debug)
console.log("using HIDE approach")
// tell the module so it can hide the others
self.sendSocketNotification("SLEEP_HIDE");
break;
case "DPMS":
if(self.config.debug)
console.log("using DPMS approach (xset)='"+self.config.dmps_off+"'")
/////////// Turns off laptop display and desktop PC with DVI @ Mykle ///////////////
exec(self.config.dpms_off, function (error, stdout, stderr) {
if(error!=null)
{
console.log(self.config.dpms_off+" failed "+JSON.stringify(error));
}
});
break;
case "CEC":
if(self.config.debug)
console.log("using CEC approach ()='"+self.config.cec_off+"'")
/////////// Turns off laptop display and desktop PC with DVI @ Mykle ///////////////
exec(self.config.cec_off, function (error, stdout, stderr) {
if(error!=null)
{
console.log(self.config.cec_off+" failed "+JSON.stringify(error));
}
});
break;
}
}
else{
if(self.config.debug)
console.log("start sleep, already sleeping")
}
break;
case "END_SLEEP":
if(self.config.debug)
console.log("waking up")
// if sleeping
if(self.sleeping){
// wake up
self.sleeping=false;
switch(self.config.mode.toUpperCase())
{
case "PI":
if(self.config.debug)
console.log("waking up using PI approach='"+self.config.pi_on+"'")
exec(self.config.pi_on, function (error, stdout, stderr) {
if(error!=null)
{
console.log(self.config.pi_on+" failed "+JSON.stringify(error));
}
});
self.hdmi = true;
break;
case "HIDE":
if(self.config.debug)
console.log("waking up using HIDE approach")
// tell the module so it can unhide the others
self.sendSocketNotification("SLEEP_WAKE");
break;
case "DPMS":
/////////// Turns on laptop display and desktop PC with DVI @ Mykle ///////////////
if(self.config.debug)
console.log("waking up using DPMS approach='"+self.config.dpms_on+"'")
exec(self.config.dpms_on, function (error, stdout, stderr) {
if(error!=null)
{
console.log(self.config.dpms_on +" failed "+JSON.stringify(error));
}
});
break;
case "CEC":
/////////// Turns on laptop display and desktop PC with DVI @ Mykle ///////////////
if(self.config.debug)
console.log("waking up using CEC approach='"+self.config.cec_on+"'")
exec(self.config.cec_on, function (error, stdout, stderr) {
if(error!=null)
{
console.log(self.config.cec_on +" failed "+JSON.stringify(error));
}
});
break;
}
}
else {
if(self.config.debug)
console.log("waking up, already awake")
}
break;
case "STAND_BY":
if(payload.status === false) {
self.sleeping=false;
self.timeractive=setTimeout(()=>{self.noUser(self)},self.config.delay*(60*1000));
} else if (payload.status === true) {
self.sleeping=true;
if(self.timeractive)
{clearTimeout(self.timeractive);}
}
break;
default:
break;
}
},
// empty the specified folder. remove if requested
rmDir : function(dirPath, removeself) {
if (removeself === undefined)
{removeself = true;}
try { var files = fs.readdirSync(dirPath); }
catch(e) { return; }
if (files.length > 0)
{for (var i = 0; i < files.length; i++) {
var filePath = dirPath + "/" + files[i];
if (fs.statSync(filePath).isFile())
{fs.unlinkSync(filePath);}
else
{rmDir(filePath);}
}}
if (removeself)
{fs.rmdirSync(dirPath);}
},
}
);