-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
241 lines (191 loc) · 6.76 KB
/
Copy pathnode_helper.js
File metadata and controls
241 lines (191 loc) · 6.76 KB
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
const NodeHelper = require("node_helper");
const { spawn } = require("child_process");
module.exports = NodeHelper.create({
start: function () {
this.config = null;
this.gpioProcess = null;
// Press state
this._pressed = false;
this._longPressTimer = null;
this._longPressFired = false;
// stdout line buffering
this._stdoutBuf = "";
// gpiomon compatibility
this._useDebounceFlag = true;
this._restartPending = false;
// software debounce fallback (ms)
this._lastAcceptedMs = 0;
// Absolute path (avoids PATH issues under pm2)
this._stdbuf = "/usr/bin/stdbuf";
},
socketNotificationReceived: function (notification, payload) {
if (notification === "INIT") {
this.config = payload || {};
// normalize logging -> debug boolean for helper chatter
const mode = this._logMode();
this.config.debug = mode === "debug";
// reset state
this._pressed = false;
this._longPressFired = false;
if (this._longPressTimer) {
clearTimeout(this._longPressTimer);
this._longPressTimer = null;
}
this._stdoutBuf = "";
this._useDebounceFlag = true;
this._restartPending = false;
this._lastAcceptedMs = 0;
this._startGpioMon();
return;
}
// Operational event logs should land in pm2 logs
if (notification === "EVENT_LOG") {
const mode = this._logMode();
if (mode === "on" || mode === "debug") {
console.log("[MMM-PageBtn] " + String(payload || ""));
}
return;
}
},
_logMode: function () {
const raw = this.config && this.config.logging ? String(this.config.logging).toLowerCase() : "";
if (raw === "off" || raw === "on" || raw === "debug") return raw;
// legacy support
if (this.config && this.config.debug === true) return "debug";
return "off";
},
_debug: function (msg) {
if (this._logMode() === "debug") {
console.log("[MMM-PageBtn] " + msg);
this.sendSocketNotification("DEBUG", msg);
}
},
_stopGpioMon: function () {
if (this.gpioProcess) {
try { this.gpioProcess.kill(); } catch (_) {}
this.gpioProcess = null;
}
},
_spawnGpioMon: function (args) {
this._stdoutBuf = "";
// Force line buffered stdout/stderr:
// stdbuf -oL -eL gpiomon <args...>
return spawn(this._stdbuf, ["-oL", "-eL", "gpiomon", ...args]);
},
_startGpioMon: function () {
const chip = this.config.gpioChip || "gpiochip0";
const gpioLine = this.config.gpioLine || 17;
const debounceUs = (this.config.debounceMs || 50) * 1000;
this._stopGpioMon();
const args = ["--bias=pull-up", "--rising-edge", "--falling-edge"];
// Try gpiomon debounce first; auto-fallback if unsupported.
if (this._useDebounceFlag) {
args.push(`--debounce=${debounceUs}us`);
}
args.push(chip, String(gpioLine));
this._debug("Starting gpiomon with args: " + args.join(" ") + " (via /usr/bin/stdbuf)");
this.gpioProcess = this._spawnGpioMon(args);
this.gpioProcess.stdout.on("data", (data) => {
this._stdoutBuf += data.toString();
let idx;
while ((idx = this._stdoutBuf.indexOf("\n")) !== -1) {
const eventLine = this._stdoutBuf.slice(0, idx).trim();
this._stdoutBuf = this._stdoutBuf.slice(idx + 1);
if (eventLine) this._handleEvent(eventLine);
}
});
this.gpioProcess.stderr.on("data", (data) => {
const msg = data.toString();
this.sendSocketNotification("GPIO_ERROR", msg);
// Detect unsupported --debounce and restart without it
const lower = msg.toLowerCase();
if (
this._useDebounceFlag &&
!this._restartPending &&
lower.includes("unrecognized option") &&
lower.includes("debounce")
) {
this._debug("gpiomon does not support --debounce; restarting without debounce.");
this._useDebounceFlag = false;
this._restartPending = true;
this._stopGpioMon();
setTimeout(() => {
this._restartPending = false;
this._startGpioMon();
}, 250);
}
});
this.gpioProcess.on("close", (code) => {
this._debug("gpiomon exited with code " + code);
if (this._restartPending) return;
if (code !== 0 && code !== null) {
this.sendSocketNotification("GPIO_ERROR", "gpiomon exited with code " + code);
setTimeout(() => this._startGpioMon(), 5000);
}
});
this.gpioProcess.on("error", (err) => {
this.sendSocketNotification("GPIO_ERROR", "Failed to start gpiomon via stdbuf: " + err.message);
setTimeout(() => this._startGpioMon(), 5000);
});
},
_handleEvent: function (rawLine) {
this._debug("GPIO event: " + rawLine);
const s = String(rawLine).toLowerCase();
const now = Date.now();
// Edge-aware debounce:
// - Do NOT time-filter rising edges needed to end a press.
// - Ignore bounce by state: duplicate FALLING while pressed, duplicate RISING while released.
// - Keep a lightweight time filter only for identical-edge spam when state is ambiguous.
const debounceMs = this.config.debounceMs || 50;
const isFalling = s.includes("falling");
const isRising = s.includes("rising");
// If neither rising nor falling, ignore
if (!isFalling && !isRising) return;
// Optional time filter for same-edge chatter only
if (now - this._lastAcceptedMs < debounceMs) {
// Allow a RISING to end a press even if within debounce window
if (!(isRising && this._pressed)) {
this._debug("Debounce: ignored event within " + debounceMs + "ms");
return;
}
}
this._lastAcceptedMs = now;
const longPressMs = this.config.longPressMs || 1000;
// FALLING = press (active-low)
if (s.includes("falling")) {
if (this._pressed) return;
this._pressed = true;
this._longPressFired = false;
if (this._longPressTimer) clearTimeout(this._longPressTimer);
this._longPressTimer = setTimeout(() => {
if (this._pressed && !this._longPressFired) {
this._longPressFired = true;
this.sendSocketNotification("LONG_PRESS");
}
}, longPressMs);
return;
}
// RISING = release
if (s.includes("rising")) {
if (!this._pressed) return;
this._pressed = false;
if (this._longPressTimer) {
clearTimeout(this._longPressTimer);
this._longPressTimer = null;
}
// If long already fired, do not also short-press
if (this._longPressFired) {
this._longPressFired = false;
return;
}
this.sendSocketNotification("SHORT_PRESS");
}
},
stop: function () {
if (this._longPressTimer) {
clearTimeout(this._longPressTimer);
this._longPressTimer = null;
}
this._stopGpioMon();
}
});