This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
node_helper.js
67 lines (58 loc) · 2.01 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
var NodeHelper = require("node_helper");
const exec = require('child_process').exec;
const fs = require('fs');
const Gpio = require('onoff').Gpio;
var commandDict = {
"js": "node",
"py": "python",
"sh": "sh"
};
module.exports = NodeHelper.create({
running: false,
socketNotificationReceived: function (notification, payload) {
const self = this;
if (notification === "CONFIG") {
this.config = payload;
this.pir = new Gpio(this.config.sensorPin, 'in', 'both');
this.pir.watch(function (err, value) {
if (value == 1) {
self.sendSocketNotification("USER_PRESENCE", true);
if (!self.running) {
self.running = true;
if (self.config.turnOffDisplay) {
execute(buildCommand("/default/displayOn.sh"), function (stdout) {
console.log(stdout);
});
}
}
}
});
} else if (notification === "TIMER_EXPIRED") {
self.running = false;
for (var i = 0; i < this.config.callbackScripts.length; i++) {
execute(buildCommand(this.config.callbackScripts[i]), function (stdout) {
console.log(stdout);
});
}
if (self.config.turnOffDisplay) {
execute(buildCommand("/default/displayOff.sh"), function (stdout) {
console.log(stdout);
});
}
}
},
});
function buildCommand(fileName) {
var file = __dirname + "/callbackScripts/" + fileName;
var fileExtension = file.split(".").slice(-1).pop();
return commandDict[fileExtension] + " " + file;
}
function execute(command, callback) {
exec(command, function (error, stdout, stderr) {
if (error) {
console.log(stderr);
} else {
callback(stdout);
}
});
}