-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
491 lines (441 loc) · 14.6 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
var NodeHelper = require("node_helper");
const { FindUnits, Unit } = require("node-intellicenter");
const messages = require("node-intellicenter/messages");
const Log = require("logger");
const path = require("path");
const fs = require("fs");
const filename = "/lastData.json";
var configFilename = path.resolve(__dirname + filename);
const reconnectDelayMs = 10 * 1000;
const unitFinderTimeoutMs = 5 * 1000;
let foundUnit;
const poolData = {
poolTemp: 0,
spaTemp: 0,
poolSetPoint: 0,
spaSetPoint: 0,
poolHeaterStatus: false,
spaHeaterStatus: false,
poolStatus: false,
spaStatus: false,
phVal: 0,
lastPHVal: 0,
phTank: 0,
orp: 0,
lastOrpVal: 0,
saltPPM: 0,
saturation: 0,
freezeMode: false,
circuits: {},
};
let poolObjnam = "B1101";
let spaObjnam = "B1202";
let refreshTimer;
let unitFinderRetry;
let unitReconnectTimer;
let intellichemObjnam = "";
let chlorinatorObjnam = "";
let freezeObjnam = "";
let initialConnectDone = false;
module.exports = NodeHelper.create({
start() {
Log.info("##### Starting node helper for: " + this.name);
try {
const data = fs.readFileSync(configFilename);
if (data && data.length > 0) {
const parsed = JSON.parse(data);
poolData.lastPHVal = parsed.lastPH ?? 0;
poolData.lastOrpVal = parsed.lastOrp ?? 0;
}
} catch {
// no existing config
}
},
saveLast() {
const obj = {
lastPH: poolData.lastPHVal,
lastOrp: poolData.lastOrpVal,
};
const json = JSON.stringify(obj);
fs.writeFileSync(configFilename, json, "utf8");
},
setCircuit(circuitState) {
if (!foundUnit) {
return;
}
Log.info(
`[MMM-IntelliCenter] setting circuit ${circuitState.id} to ${!!circuitState.state}`,
);
foundUnit.send(
messages.SetObjectStatus(circuitState.id, !!circuitState.state),
);
},
setHeatpoint(heatpoint) {
if (!foundUnit) {
return;
}
let heatObjnam = "";
if (heatpoint.body === "spa") {
heatObjnam = spaObjnam;
} else if (heatpoint.body === "pool") {
heatObjnam = poolObjnam;
}
if (!heatObjnam) {
Log.error(
`[MMM-IntelliCenter] unable to determine objnam from given heatpoint body ${heatpoint.body} - expected "spa" or "pool"`,
);
return;
}
Log.info(
`[MMM-IntelliCenter] setting heatpoint for body ${heatObjnam} to ${heatpoint.temperature} deg`,
);
foundUnit.send(messages.SetSetpoint(heatObjnam, heatpoint.temperature));
},
setHeatstate(heatstate) {
if (!foundUnit) {
return;
}
let heatObjnam = "";
if (heatstate.body === "spa") {
heatObjnam = spaObjnam;
} else if (heatstate.body === "pool") {
heatObjnam = poolObjnam;
}
if (!heatObjnam) {
Log.error(
`[MMM-IntelliCenter] unable to determine objnam from given heatstate body ${heatstate.body} - expected "spa" or "pool"`,
);
return;
}
Log.info(
`[MMM-IntelliCenter] setting heat state for body ${heatObjnam} to ${!!heatstate.state}`,
);
foundUnit.send(messages.SetHeatMode(heatObjnam, !!heatstate.state));
},
setLightcmd(lightCmd) {
if (!foundUnit) {
return;
}
Log.info(`[MMM-IntelliCenter] sending light command ${lightCmd}`);
// NYI in node-intellicenter
// foundUnit.send(messages.SendLightCommand(lightCmd));
},
notifyReconnecting() {
this.sendSocketNotification("INTELLICENTER_RECONNECTING");
},
socketNotificationReceived(notification, payload) {
if (notification === "INTELLICENTER_CONFIG") {
if (!this.config) {
this.config = payload;
this.connect(
(status) => {
this.sendSocketNotification("INTELLICENTER_RESULT", status);
},
() => {
this.notifyReconnecting();
},
);
} else if (poolData) {
this.sendSocketNotification("INTELLICENTER_RESULT", poolData);
}
// If we don't have a status yet, assume the initial connection is still in progress and this socket notification will be delivered when setup is done
}
if (notification === "INTELLICENTER_CIRCUIT") {
this.setCircuit(payload);
}
if (notification === "INTELLICENTER_HEATPOINT") {
this.setHeatpoint(payload);
}
if (notification === "INTELLICENTER_HEATSTATE") {
this.setHeatstate(payload);
}
if (notification === "INTELLICENTER_LIGHTCMD") {
this.setLightcmd(payload);
}
},
resetFoundUnit() {
foundUnit = null;
if (refreshTimer) {
clearInterval(refreshTimer);
refreshTimer = null;
}
if (unitFinderRetry) {
clearInterval(unitFinderRetry);
unitFinderRetry = null;
}
if (unitReconnectTimer) {
clearTimeout(unitReconnectTimer);
unitReconnectTimer = null;
}
},
setupUnit(cb, reconnectCb) {
Log.info("[MMM-IntelliCenter] initial connection to unit...");
initialConnectDone = false;
const reset = () => {
reconnectCb();
this.resetFoundUnit();
unitReconnectTimer = setTimeout(() => {
this.connect(cb, reconnectCb);
}, reconnectDelayMs);
};
foundUnit
.on("error", (e) => {
Log.error("[MMM-IntelliCenter] error in unit connection.");
Log.error(e);
})
.on("close", () => {
Log.error(
`[MMM-IntelliCenter] unit connection closed unexpectedly. restarting the connection process in ${reconnectDelayMs / 1000} seconds`,
);
reset();
})
.on("timeout", () => {
Log.error("[MMM-IntelliCenter] unit connection timed out.");
})
.on("notify", (msg) => {
for (const obj of msg.objectList) {
if (obj.objnam === intellichemObjnam) {
Log.info("[MMM-IntelliCenter] received chemical update");
if (obj.params.ORPVAL) {
poolData.orp = parseInt(obj.params.ORPVAL);
}
if (obj.params.PHVAL) {
poolData.phVal = parseFloat(obj.params.PHVAL);
}
if (obj.params.PHTNK) {
poolData.phTank = parseInt(obj.params.PHTNK);
}
if (obj.params.QUALTY) {
poolData.saturation = parseFloat(obj.params.QUALTY);
}
if (poolData.phVal !== 0) {
poolData.lastPHVal = poolData.phVal;
this.saveLast();
}
if (poolData.orp !== 0) {
poolData.lastOrpVal = poolData.orp;
this.saveLast();
}
} else if (obj.objnam === poolObjnam) {
Log.info("[MMM-IntelliCenter] received pool update");
if (obj.params.LOTMP) {
poolData.poolSetPoint = parseInt(obj.params.LOTMP);
}
// todo: is MODE the right check for this?
if (obj.params.MODE) {
poolData.poolHeaterStatus = obj.params.MODE === "11";
}
if (obj.params.STATUS) {
poolData.poolStatus = obj.params.STATUS === "ON";
}
if (obj.params.LSTTMP) {
poolData.poolTemp = parseInt(obj.params.LSTTMP);
}
} else if (obj.objnam === spaObjnam) {
Log.info("[MMM-IntelliCenter] received spa update");
if (obj.params.LOTMP) {
poolData.spaSetPoint = parseInt(obj.params.LOTMP);
}
// todo: is MODE the right check for this?
if (obj.params.MODE) {
poolData.spaHeaterStatus = obj.params.MODE === "11";
}
if (obj.params.STATUS) {
poolData.spaStatus = obj.params.STATUS === "ON";
}
if (obj.params.LSTTMP) {
poolData.spaTemp = parseInt(obj.params.LSTTMP);
}
} else if (obj.objnam === chlorinatorObjnam) {
Log.info("[MMM-IntelliCenter] received chlorinator update");
if (obj.params.SALT) {
poolData.saltPPM = parseInt(obj.params.SALT);
}
} else if (obj.objnam === freezeObjnam) {
Log.info("[MMM-IntelliCenter] received freeze-protection update");
if (obj.params.STATUS) {
poolData.freezeMode = obj.params.STATUS === "ON";
}
} else if (Object.keys(poolData.circuits).includes(obj.objnam)) {
Log.info(
`[MMM-IntelliCenter] received update for circuit ${obj.objnam} (${poolData.circuits[obj.objnam].name})`,
);
if (obj.params.STATUS) {
poolData.circuits[obj.objnam].status = obj.params.STATUS === "ON";
}
} else {
Log.info(
`[MMM-IntelliCenter] received update for untracked object: ${obj.objnam}`,
);
}
}
if (initialConnectDone) {
cb(poolData);
}
})
.once("connected", async () => {
Log.info(
"[MMM-IntelliCenter] logged into unit. getting system configuration...",
);
const sysinfo = await foundUnit.send(messages.GetSystemConfiguration());
const bodyUpdates = [];
for (const obj of sysinfo.answer) {
if (obj.params.OBJTYP === "BODY" && obj.params.SUBTYP === "POOL") {
const ichem = obj.params.OBJLIST?.find(
(obj) => obj.params.SUBTYP === "ICHEM",
);
intellichemObjnam = ichem?.objnam;
poolObjnam = obj.objnam;
bodyUpdates.push(obj.objnam);
} else if (
obj.params.OBJTYP === "BODY" &&
obj.params.SUBTYP === "SPA"
) {
spaObjnam = obj.objnam;
bodyUpdates.push(obj.objnam);
}
}
Log.info("[MMM-IntelliCenter] getting chemical status...");
const chemstatus = await foundUnit.send(messages.GetChemicalStatus());
for (const obj of chemstatus.objectList) {
if (obj.params.SUBTYP === "ICHLOR") {
chlorinatorObjnam = obj.objnam;
}
}
Log.info("[MMM-IntelliCenter] getting circuit information...");
const circuits = await foundUnit.send(messages.GetCircuitStatus());
const freezeCirc = circuits.objectList?.find(
(obj) => obj.params?.SUBTYP === "FRZ",
);
if (freezeCirc) {
freezeObjnam = freezeCirc.objnam;
Log.info(
`[MMM-IntelliCenter] registering for freeze-protection updates...`,
);
await foundUnit.send(
messages.SubscribeToUpdates(freezeObjnam, "STATUS"),
);
}
if (this.config.controls?.length > 0) {
for (const circuit of circuits.objectList) {
const wantedControl = this.config.controls.find(
(c) => c.id === circuit.objnam,
);
if (!wantedControl) {
continue;
}
poolData.circuits[circuit.objnam] = {
status: false,
name: circuit.params.SNAME,
};
}
}
for (const circuit of Object.keys(poolData.circuits)) {
Log.info(`[MMM-IntelliCenter] registering for ${circuit} updates...`);
await foundUnit.send(messages.SubscribeToUpdates(circuit, "STATUS"));
}
if (bodyUpdates.length > 0) {
for (const obj of bodyUpdates) {
Log.info(
`[MMM-IntelliCenter] registering for ${obj === poolObjnam ? "pool" : obj === spaObjnam ? "spa" : obj} updates...`,
);
await foundUnit.send(
messages.SubscribeToUpdates(obj, [
"LOTMP",
"MODE",
"STATUS",
"LSTTMP",
]),
);
}
}
if (chlorinatorObjnam) {
Log.info(
"[MMM-IntelliCenter] registering for chlorinator updates...",
);
// can also check PRIM, SEC, and SUPER
// PRIM: percentage output going to primary body (probably pool) on 1-100 scale
// SEC: percentage output going to secondary body (probably spa) on 1-100 scale
// SUPER: "ON" or "OFF" for whether currently in superchlorination mode or not
await foundUnit.send(
messages.SubscribeToUpdates(chlorinatorObjnam, "SALT"),
);
}
if (intellichemObjnam) {
Log.info("[MMM-IntelliCenter] registering for chemical updates...");
await foundUnit.send(
messages.SubscribeToUpdates(intellichemObjnam, [
"PHVAL",
"PHTNK",
"ORPVAL",
"QUALTY",
]),
);
}
Log.info("[MMM-IntelliCenter] finished initial setup.");
initialConnectDone = true;
cb(poolData);
});
foundUnit.connect().catch((ex) => {
Log.error(
`[MMM-IntelliCenter] error attempting to connect to unit: ${ex}`,
);
Log.error(
`[MMM-IntelliCenter] restarting the connection process in ${reconnectDelayMs / 1000} seconds`,
);
unitReconnectTimer = setTimeout(() => {
this.connect(cb, reconnectCb);
}, reconnectDelayMs);
});
},
findServer(cb, reconnectCb) {
Log.info("[MMM-IntelliCenter] starting search for local units");
const finder = new FindUnits(this.config.multicastInterface);
finder
.on("serverFound", (server) => {
finder.close();
Log.info(
`[MMM-IntelliCenter] local unit found at ${server.addressStr}:${server.port}`,
);
foundUnit = new Unit(server.addressStr, server.port);
this.setupUnit(cb, reconnectCb);
clearInterval(unitFinderRetry);
unitFinderRetry = null;
})
.on("error", (e) => {
Log.error(
`[MMM-IntelliCenter] error trying to find a server. scheduling a retry in ${reconnectDelayMs / 1000} seconds`,
);
Log.error(e);
this.resetFoundUnit();
setTimeout(() => {
this.findServer(cb);
}, reconnectDelayMs);
});
finder.search();
unitFinderRetry = setInterval(() => {
Log.info(
`[MMM-IntelliCenter] didn't find any units within ${unitFinderTimeoutMs / 1000} seconds, trying again...`,
);
finder.search();
}, unitFinderTimeoutMs);
},
connect(cb, reconnectCb) {
if (
!foundUnit &&
typeof this.config !== "undefined" &&
this.config.serverAddress &&
this.config.serverPort
) {
Log.info(
`[MMM-IntelliCenter] connecting directly to configured unit at ${this.config.serverAddress}:${this.config.serverPort}`,
);
foundUnit = new Unit(this.config.serverAddress, this.config.serverPort);
}
if (foundUnit) {
this.setupUnit(cb, reconnectCb);
} else {
this.findServer(cb, reconnectCb);
}
},
});