forked from bugsounet/MMM-Pir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
82 lines (76 loc) · 2.35 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
/**************************
* node_helper for MMM-Pir *
* BuGsounet *
***************************/
var log = (...args) => { /* do nothing */ };
const NodeHelper = require("node_helper");
const LibScreen = require("./components/screenLib.js");
const LibPir = require("./components/pirLib.js");
module.exports = NodeHelper.create({
start () {
this.pir = null;
this.screen = null;
},
socketNotificationReceived (notification, payload) {
switch (notification) {
case "INIT":
this.config = payload;
this.parse();
break;
case "WAKEUP":
this.screen.wakeup();
break;
case "FORCE_END":
this.screen.forceEnd();
break;
case "LOCK":
this.screen.lock();
break;
case "UNLOCK":
this.screen.unlock();
break;
case "LOCK_FORCE_END":
this.screen.forceLockOFF();
break;
case "LOCK_FORCE_WAKEUP":
this.screen.forceLockON();
break;
}
},
async parse () {
if (this.config.debug) log = (...args) => { console.log("[MMM-Pir]", ...args); };
console.log("[MMM-Pir] Version:", require("./package.json").version, "rev:", require("./package.json").rev);
var callbacks = {
screen: (noti, params) => {
log("[CALLBACK] Screen:", noti, params || "");
this.sendSocketNotification(noti, params);
},
pir: (noti, params) => {
log("[CALLBACK] Pir:", noti, params || "");
if (noti === "PIR_DETECTED") this.screen.wakeup();
else this.sendSocketNotification(noti, params);
}
};
let pirConfig = {
debug: this.config.debug,
gpio: this.config.pir_gpio,
mode: this.config.pir_mode
};
let screenConfig = {
debug: this.config.debug,
delay: this.config.delay,
mode: this.config.mode,
gpio: this.config.mode6_gpio,
clearGpioValue: this.config.mode6_clearGpioValue,
xrandrForceRotation: this.config.xrandrForceRotation,
wrandrForceRotation: this.config.wrandrForceRotation,
wrandrForceMode: this.config.wrandrForceMode
};
this.pir = new LibPir(pirConfig, callbacks.pir);
this.pir.start();
this.screen = new LibScreen(screenConfig, callbacks.screen);
this.screen.activate();
console.log("[MMM-Pir] Started!");
this.sendSocketNotification("INITIALIZED");
}
});