nodejs-poolcontroller 8.3.0 → 8.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/.eslintrc.json +36 -36
  2. package/.github/ISSUE_TEMPLATE/1-bug-report.yml +84 -84
  3. package/.github/ISSUE_TEMPLATE/2-docs.md +12 -12
  4. package/.github/ISSUE_TEMPLATE/3-proposal.md +28 -28
  5. package/.github/ISSUE_TEMPLATE/config.yml +8 -8
  6. package/.github/copilot-instructions.md +63 -63
  7. package/.github/workflows/ghcr-publish.yml +67 -67
  8. package/AGENTS.md +597 -0
  9. package/CONTRIBUTING.md +74 -74
  10. package/Changelog +292 -284
  11. package/Dockerfile +62 -62
  12. package/Gruntfile.js +40 -40
  13. package/LICENSE +661 -661
  14. package/README.md +318 -309
  15. package/anslq25/MessagesMock.ts +221 -221
  16. package/anslq25/boards/MockBoardFactory.ts +49 -49
  17. package/anslq25/boards/MockEasyTouchBoard.ts +696 -696
  18. package/anslq25/boards/MockSystemBoard.ts +216 -216
  19. package/anslq25/chemistry/MockChlorinator.ts +98 -98
  20. package/anslq25/pumps/MockPump.ts +83 -83
  21. package/app.ts +115 -115
  22. package/config/Config.ts +0 -0
  23. package/config/VersionCheck.ts +0 -0
  24. package/controller/Constants.ts +809 -805
  25. package/controller/Equipment.ts +2688 -2664
  26. package/controller/Errors.ts +181 -181
  27. package/controller/Lockouts.ts +549 -549
  28. package/controller/State.ts +3738 -3701
  29. package/controller/boards/AquaLinkBoard.ts +1003 -1003
  30. package/controller/boards/BoardFactory.ts +53 -53
  31. package/controller/boards/EasyTouchBoard.ts +3202 -3202
  32. package/controller/boards/IntelliCenterBoard.ts +4393 -3899
  33. package/controller/boards/IntelliComBoard.ts +69 -69
  34. package/controller/boards/IntelliTouchBoard.ts +382 -382
  35. package/controller/boards/NixieBoard.ts +1944 -1944
  36. package/controller/boards/SunTouchBoard.ts +400 -400
  37. package/controller/boards/SystemBoard.ts +5268 -5268
  38. package/controller/comms/Comms.ts +1272 -1255
  39. package/controller/comms/ScreenLogic.ts +1665 -1665
  40. package/controller/comms/messages/Messages.ts +1433 -1406
  41. package/controller/comms/messages/config/ChlorinatorMessage.ts +5 -0
  42. package/controller/comms/messages/config/CircuitGroupMessage.ts +0 -0
  43. package/controller/comms/messages/config/CircuitMessage.ts +0 -0
  44. package/controller/comms/messages/config/ConfigMessage.ts +6 -0
  45. package/controller/comms/messages/config/CoverMessage.ts +0 -0
  46. package/controller/comms/messages/config/CustomNameMessage.ts +31 -31
  47. package/controller/comms/messages/config/EquipmentMessage.ts +216 -210
  48. package/controller/comms/messages/config/ExternalMessage.ts +96 -10
  49. package/controller/comms/messages/config/FeatureMessage.ts +0 -0
  50. package/controller/comms/messages/config/GeneralMessage.ts +0 -0
  51. package/controller/comms/messages/config/HeaterMessage.ts +0 -0
  52. package/controller/comms/messages/config/IntellichemMessage.ts +0 -0
  53. package/controller/comms/messages/config/OptionsMessage.ts +194 -174
  54. package/controller/comms/messages/config/PumpMessage.ts +0 -0
  55. package/controller/comms/messages/config/RemoteMessage.ts +0 -0
  56. package/controller/comms/messages/config/ScheduleMessage.ts +401 -390
  57. package/controller/comms/messages/config/SecurityMessage.ts +0 -0
  58. package/controller/comms/messages/config/ValveMessage.ts +0 -0
  59. package/controller/comms/messages/status/ChlorinatorStateMessage.ts +0 -0
  60. package/controller/comms/messages/status/EquipmentStateMessage.ts +1158 -822
  61. package/controller/comms/messages/status/HeaterStateMessage.ts +135 -135
  62. package/controller/comms/messages/status/IntelliChemStateMessage.ts +448 -448
  63. package/controller/comms/messages/status/IntelliValveStateMessage.ts +36 -36
  64. package/controller/comms/messages/status/PumpStateMessage.ts +0 -0
  65. package/controller/comms/messages/status/RegalModbusStateMessage.ts +410 -410
  66. package/controller/comms/messages/status/VersionMessage.ts +103 -41
  67. package/controller/nixie/Nixie.ts +173 -173
  68. package/controller/nixie/NixieEquipment.ts +104 -104
  69. package/controller/nixie/bodies/Body.ts +120 -120
  70. package/controller/nixie/bodies/Filter.ts +135 -135
  71. package/controller/nixie/chemistry/ChemController.ts +2724 -2724
  72. package/controller/nixie/chemistry/ChemDoser.ts +806 -806
  73. package/controller/nixie/chemistry/Chlorinator.ts +367 -367
  74. package/controller/nixie/circuits/Circuit.ts +478 -478
  75. package/controller/nixie/heaters/Heater.ts +834 -834
  76. package/controller/nixie/pumps/Pump.ts +1193 -1193
  77. package/controller/nixie/schedules/Schedule.ts +401 -401
  78. package/controller/nixie/valves/Valve.ts +170 -170
  79. package/defaultConfig.json +352 -352
  80. package/docker-compose.yml +31 -31
  81. package/logger/DataLogger.ts +448 -448
  82. package/logger/Logger.ts +448 -436
  83. package/package.json +58 -58
  84. package/sendSocket.js +32 -32
  85. package/tsconfig.json +25 -25
  86. package/types/express-multer.d.ts +32 -32
  87. package/web/Server.ts +1937 -1927
  88. package/web/bindings/aqualinkD.json +559 -559
  89. package/web/bindings/influxDB.json +1066 -1066
  90. package/web/bindings/mqtt.json +721 -721
  91. package/web/bindings/mqttAlt.json +746 -746
  92. package/web/bindings/rulesManager.json +54 -54
  93. package/web/bindings/smartThings-Hubitat.json +31 -31
  94. package/web/bindings/valveRelays.json +20 -20
  95. package/web/bindings/vera.json +25 -25
  96. package/web/interfaces/baseInterface.ts +188 -188
  97. package/web/interfaces/httpInterface.ts +148 -148
  98. package/web/interfaces/influxInterface.ts +283 -283
  99. package/web/interfaces/mqttInterface.ts +695 -695
  100. package/web/interfaces/ruleInterface.ts +101 -87
  101. package/web/services/config/Config.ts +1063 -1053
  102. package/web/services/config/ConfigSocket.ts +0 -0
  103. package/web/services/state/State.ts +0 -0
  104. package/web/services/state/StateSocket.ts +0 -0
  105. package/web/services/utilities/Utilities.ts +233 -233
@@ -1,834 +1,834 @@
1
- import { EquipmentNotFoundError, InvalidEquipmentDataError, InvalidEquipmentIdError, InvalidOperationError, ParameterOutOfRangeError } from '../../Errors';
2
- import { utils, Timestamp } from '../../Constants';
3
- import { logger } from '../../../logger/Logger';
4
-
5
- import { NixieEquipment, NixieChildEquipment, NixieEquipmentCollection, INixieControlPanel } from "../NixieEquipment";
6
- import { Body, Heater, HeaterCollection, sys } from "../../../controller/Equipment";
7
- import { BodyTempState, HeaterState, state, } from "../../State";
8
- import { setTimeout, clearTimeout } from 'timers';
9
- import { NixieControlPanel } from '../Nixie';
10
- import { webApp, InterfaceServerResponse } from "../../../web/Server";
11
- import { conn } from '../../../controller/comms/Comms';
12
- import { Inbound, Outbound, Protocol, Response } from '../../../controller/comms/messages/Messages';
13
- import { delayMgr } from '../../Lockouts';
14
-
15
- export class NixieHeaterCollection extends NixieEquipmentCollection<NixieHeaterBase> {
16
- public async deleteHeaterAsync(id: number) {
17
- try {
18
- for (let i = this.length - 1; i >= 0; i--) {
19
- let heater = this[i];
20
- if (heater.id === id) {
21
- await heater.closeAsync();
22
- this.splice(i, 1);
23
- }
24
- }
25
- } catch (err) { return Promise.reject(`Nixie Control Panel deleteHeaterAsync ${err.message}`); }
26
- }
27
- public async setHeaterStateAsync(hstate: HeaterState, val: boolean, isCooling: boolean) {
28
- try {
29
- let h: NixieHeaterBase = this.find(elem => elem.id === hstate.id) as NixieHeaterBase;
30
- if (typeof h === 'undefined') {
31
- return Promise.reject(new Error(`NCP: Heater ${hstate.id}-${hstate.name} could not be found to set the state to ${val}.`));
32
- }
33
- await h.setHeaterStateAsync(hstate, val, isCooling);
34
- }
35
- catch (err) { return logger.error(`NCP: setHeaterStateAsync ${hstate.id}-${hstate.name}: ${err.message}`); }
36
- }
37
- public async setHeaterAsync(heater: Heater, data: any) {
38
- // By the time we get here we know that we are in control and this is a Nixie heater.
39
- try {
40
- let h: NixieHeaterBase = this.find(elem => elem.id === heater.id) as NixieHeaterBase;
41
- if (typeof h === 'undefined') {
42
- heater.master = 1;
43
- h = NixieHeaterBase.create(this.controlPanel, heater);
44
- this.push(h);
45
- await h.setHeaterAsync(data);
46
- logger.info(`A Heater was not found for id #${heater.id} creating Heater`);
47
- }
48
- else {
49
- await h.setHeaterAsync(data);
50
- }
51
- }
52
- catch (err) { logger.error(`setHeaterAsync: ${err.message}`); return Promise.reject(err); }
53
- }
54
- public async initAsync(heaters: HeaterCollection) {
55
- try {
56
- for (let i = 0; i < heaters.length; i++) {
57
- let heater = heaters.getItemByIndex(i);
58
- if (heater.master === 1) {
59
- if (typeof this.find(elem => elem.id === heater.id) === 'undefined') {
60
- logger.info(`Initializing Heater ${heater.name}`);
61
- let nHeater = NixieHeaterBase.create(this.controlPanel, heater);
62
- this.push(nHeater);
63
- }
64
- }
65
- }
66
- }
67
- catch (err) { logger.error(`Nixie Heater initAsync: ${err.message}`); return Promise.reject(err); }
68
- }
69
- public async closeAsync() {
70
- try {
71
- for (let i = this.length - 1; i >= 0; i--) {
72
- try {
73
- await this[i].closeAsync();
74
- this.splice(i, 1);
75
- } catch (err) { logger.error(`Error stopping Nixie Heater ${err}`); }
76
- }
77
- } catch (err) { } // Don't bail if we have an errror.
78
- }
79
- public async initHeaterAsync(heater: Heater): Promise<NixieHeaterBase> {
80
- try {
81
- let c: NixieHeaterBase = this.find(elem => elem.id === heater.id) as NixieHeaterBase;
82
- if (typeof c === 'undefined') {
83
- c = NixieHeaterBase.create(this.controlPanel, heater);
84
- this.push(c);
85
- }
86
- return c;
87
- } catch (err) { logger.error(`initHeaterAsync: ${err.message}`); return Promise.reject(err); }
88
- }
89
- public async setServiceModeAsync() {
90
- try {
91
- for (let i = this.length - 1; i >= 0; i--) {
92
- let heater = this[i] as NixieHeaterBase;
93
- await heater.setServiceModeAsync();
94
- }
95
- } catch (err) { return Promise.reject(`Nixie Control Panel setServiceMode ${err.message}`); }
96
- }
97
- }
98
- export class NixieHeaterBase extends NixieEquipment {
99
- protected _suspendPolling: number = 0;
100
- public pollingInterval: number = 10000;
101
- public heater: Heater;
102
- protected _pollTimer: NodeJS.Timeout = null;
103
- protected _lastState;
104
- protected closing = false;
105
- protected bodyOnTime: number;
106
- protected isOn: boolean = false;
107
- protected isCooling: boolean = false;
108
- protected lastHeatCycle: Date;
109
- protected lastCoolCycle: Date;
110
- constructor(ncp: INixieControlPanel, heater: Heater) {
111
- super(ncp);
112
- this.heater = heater;
113
- }
114
- public get suspendPolling(): boolean { return this._suspendPolling > 0; }
115
- public set suspendPolling(val: boolean) { this._suspendPolling = Math.max(0, this._suspendPolling + (val ? 1 : -1)); }
116
- public get id(): number { return typeof this.heater !== 'undefined' ? this.heater.id : -1; }
117
- public getCooldownTime() { return 0; }
118
- public static create(ncp: INixieControlPanel, heater: Heater): NixieHeaterBase {
119
- let type = sys.board.valueMaps.heaterTypes.transform(heater.type);
120
- switch (type.name) {
121
- case 'heatpump':
122
- return new NixieHeatpump(ncp, heater);
123
- case 'ultratemp':
124
- return new NixieUltratemp(ncp, heater);
125
- case 'gas':
126
- return new NixieGasHeater(ncp, heater);
127
- case 'mastertemp':
128
- return new NixieMastertemp(ncp, heater);
129
- case 'solar':
130
- return new NixieSolarHeater(ncp, heater);
131
- case 'hybrid':
132
- return new NixieUltraTempETi(ncp, heater);
133
- default:
134
- return new NixieHeaterBase(ncp, heater);
135
- }
136
- }
137
- public isBodyOn() {
138
- let isOn = sys.board.bodies.isBodyOn(this.heater.body);
139
- if (isOn && typeof this.bodyOnTime === 'undefined') {
140
- this.bodyOnTime = new Date().getTime();
141
- }
142
- else if (!isOn) this.bodyOnTime = undefined;
143
- return isOn;
144
- }
145
- public async setHeaterStateAsync(hstate: HeaterState, isOn: boolean, isCooling: boolean) {
146
- try {
147
- return Promise.reject(new InvalidOperationError(`You cannot change the state on this type of heater ${hstate.name}`, 'setHeaterStateAsync'));
148
- } catch (err) { return logger.error(`Nixie Error setting heater state ${hstate.id}-${hstate.name}: ${err.message}`); }
149
- }
150
- public async setHeaterAsync(data: any) {
151
- try {
152
- let heater = this.heater;
153
-
154
- }
155
- catch (err) { logger.error(`Nixie setHeaterAsync: ${err.message}`); return Promise.reject(err); }
156
- }
157
- public async closeAsync() { }
158
- public async setServiceModeAsync() {
159
- let hstate = state.heaters.getItemById(this.heater.id);
160
- await this.setHeaterStateAsync(hstate, false, false);
161
- }
162
- }
163
- export class NixieGasHeater extends NixieHeaterBase {
164
- public pollingInterval: number = 10000;
165
- //declare heater: Heater;
166
- constructor(ncp: INixieControlPanel, heater: Heater) {
167
- super(ncp, heater);
168
- this.heater = heater;
169
- if (typeof this.heater.stopTempDelta === 'undefined') this.heater.stopTempDelta = 1;
170
- if (typeof this.heater.minCycleTime === 'undefined') this.heater.minCycleTime = 2;
171
- this.pollEquipmentAsync();
172
- }
173
- public get id(): number { return typeof this.heater !== 'undefined' ? this.heater.id : -1; }
174
- public getCooldownTime(): number {
175
- // Delays are always in terms of seconds so convert the minute to seconds.
176
- if (this.heater.cooldownDelay === 0 || typeof this.lastHeatCycle === 'undefined') return 0;
177
- let now = new Date().getTime();
178
- let cooldown = this.isOn ? this.heater.cooldownDelay * 60000 : Math.round(((this.lastHeatCycle.getDate() + this.heater.cooldownDelay * 60000) - now) / 1000);
179
- return Math.min(Math.max(0, cooldown), this.heater.cooldownDelay * 60);
180
- }
181
- public async setHeaterStateAsync(hstate: HeaterState, isOn: boolean) {
182
- try {
183
- // Initialize the desired state.
184
- this.isOn = isOn;
185
- this.isCooling = false;
186
- let target = hstate.startupDelay === false && isOn;
187
- if (target && typeof hstate.endTime !== 'undefined') {
188
- // Calculate a short cycle time so that the gas heater does not cycle
189
- // too often. For gas heaters this is 60 seconds. This gives enough time
190
- // for the heater control circuit to make a full cycle.
191
- if (new Date().getTime() - hstate.endTime.getTime() < this.heater.minCycleTime * 60000) {
192
- logger.verbose(`${hstate.name} short cycle detected deferring turn on state`);
193
- target = false;
194
- }
195
- }
196
- // Here we go we need to set the firemans switch state.
197
- if (hstate.isOn !== target) {
198
- logger.info(`Nixie: Set Heater ${hstate.id}-${hstate.name} to ${isOn}`);
199
- }
200
- if (typeof this._lastState === 'undefined' || target || this._lastState !== target) {
201
- if (utils.isNullOrEmpty(this.heater.connectionId) || utils.isNullOrEmpty(this.heater.deviceBinding)) {
202
- this._lastState = hstate.isOn = target;
203
- }
204
- else {
205
- let res = await NixieEquipment.putDeviceService(this.heater.connectionId, `/state/device/${this.heater.deviceBinding}`,
206
- { isOn: target, latch: target ? 10000 : undefined });
207
- if (res.status.code === 200) this._lastState = hstate.isOn = target;
208
- else logger.error(`Nixie Error setting heater state: ${res.status.code} -${res.status.message} ${res.error.message}`);
209
- }
210
- if (target) this.lastHeatCycle = new Date();
211
- }
212
- } catch (err) { return logger.error(`Nixie Error setting heater state ${hstate.id}-${hstate.name}: ${err.message}`); }
213
- }
214
- public async pollEquipmentAsync() {
215
- let self = this;
216
- try {
217
- if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
218
- this._pollTimer = null;
219
- let success = false;
220
- }
221
- catch (err) { logger.error(`Nixie Error polling Heater - ${err}`); }
222
- finally { this._pollTimer = setTimeout(async () => await self.pollEquipmentAsync(), this.pollingInterval || 10000); }
223
- }
224
- private async checkHardwareStatusAsync(connectionId: string, deviceBinding: string) {
225
- try {
226
- let dev = await NixieEquipment.getDeviceService(connectionId, `/status/device/${deviceBinding}`);
227
- return dev;
228
- } catch (err) { logger.error(`Nixie Heater Error checkHardwareStatusAsync: ${err.message}`); return { hasFault: true } }
229
- }
230
- public async validateSetupAsync(heater: Heater, hstate: HeaterState) {
231
- try {
232
- if (typeof heater.connectionId !== 'undefined' && heater.connectionId !== ''
233
- && typeof heater.deviceBinding !== 'undefined' && heater.deviceBinding !== '') {
234
- try {
235
- let stat = await this.checkHardwareStatusAsync(heater.connectionId, heater.deviceBinding);
236
- // If we have a status check the return.
237
- hstate.commStatus = stat.hasFault ? 1 : 0;
238
- } catch (err) { hstate.commStatus = 1; }
239
- }
240
- else
241
- hstate.commStatus = 0;
242
- } catch (err) { logger.error(`Nixie Error checking heater Hardware ${this.heater.name}: ${err.message}`); hstate.commStatus = 1; return Promise.reject(err); }
243
- }
244
- public async closeAsync() {
245
- try {
246
- if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
247
- this._pollTimer = null;
248
- let hstate = state.heaters.getItemById(this.heater.id);
249
- await this.setHeaterStateAsync(hstate, false);
250
- hstate.emitEquipmentChange();
251
- }
252
- catch (err) { logger.error(`Nixie Heater closeAsync: ${err.message}`); return Promise.reject(err); }
253
- }
254
- public logData(filename: string, data: any) { this.controlPanel.logData(filename, data); }
255
- }
256
- export class NixieSolarHeater extends NixieHeaterBase {
257
- public pollingInterval: number = 10000;
258
- declare heater: Heater;
259
- constructor(ncp: INixieControlPanel, heater: Heater) {
260
- super(ncp, heater);
261
- this.heater = heater;
262
- this.pollEquipmentAsync();
263
- }
264
- public get id(): number { return typeof this.heater !== 'undefined' ? this.heater.id : -1; }
265
- public async setHeaterStateAsync(hstate: HeaterState, isOn: boolean, isCooling: boolean) {
266
- try {
267
- let origState = hstate.isOn;
268
- // Initialize the desired state.
269
- this.isOn = isOn;
270
- this.isCooling = isCooling;
271
- let target = hstate.startupDelay === false && isOn;
272
- if (target && typeof hstate.endTime !== 'undefined') {
273
- // Calculate a short cycle time so that the solar heater does not cycle
274
- // too often. For solar heaters this is 60 seconds. This gives enough time
275
- // for the valve to rotate and start heating. If the solar and water sensors are
276
- // not having issues this should be plenty of time.
277
- if (new Date().getTime() - hstate.endTime.getTime() < 60000) {
278
- logger.verbose(`${hstate.name} short cycle detected deferring turn on state`);
279
- target = false;
280
- }
281
- }
282
-
283
- // Here we go we need to set the valve status that is attached to solar.
284
- if (hstate.isOn !== target) {
285
- logger.info(`Nixie: Set Heater ${hstate.id}-${hstate.name} to ${isOn}`);
286
- }
287
- if (typeof this._lastState === 'undefined' || target || this._lastState !== target) {
288
- if (utils.isNullOrEmpty(this.heater.connectionId) || utils.isNullOrEmpty(this.heater.deviceBinding)) {
289
- this._lastState = hstate.isOn = target;
290
- hstate.isCooling = target && isCooling;
291
- }
292
- else {
293
- let res = await NixieEquipment.putDeviceService(this.heater.connectionId, `/state/device/${this.heater.deviceBinding}`,
294
- { isOn: target, latch: target ? 10000 : undefined });
295
- if (res.status.code === 200) {
296
- this._lastState = hstate.isOn = target;
297
- hstate.isCooling = target && isCooling;
298
- }
299
- else logger.error(`Nixie Error setting heater state: ${res.status.code} -${res.status.message} ${res.error.message}`);
300
- }
301
- if (target) {
302
- if (isCooling) this.lastCoolCycle = new Date();
303
- else if (isOn) this.lastHeatCycle = new Date();
304
- }
305
- }
306
- // In this instance we need to see if there are cleaner circuits that we need to turn off
307
- // then delay for the current body because the solar just came on.
308
- if (hstate.isOn && sys.general.options.cleanerSolarDelay && !origState) {
309
- let arrTypes = sys.board.valueMaps.circuitFunctions.toArray().filter(x => { return x.name.indexOf('cleaner') !== -1 && x.body === hstate.bodyId });
310
- let cleaners = sys.circuits.filter(x => { return arrTypes.findIndex(t => { return t.val === x.type }) !== -1 });
311
- // Turn off all the cleaner circuits and set an on delay if they are on.
312
- for (let i = 0; i < cleaners.length; i++) {
313
- let cleaner = cleaners.getItemByIndex(i);
314
- if (cleaner.isActive) {
315
- let cstate = state.circuits.getItemById(cleaner.id);
316
- if (cstate.isOn && sys.general.options.cleanerSolarDelayTime > 0) {
317
- // Turn off the circuit then set a delay.
318
- logger.info(`Setting cleaner solar delay for ${cleaner.name} to ${sys.general.options.cleanerSolarDelayTime}`);
319
- await sys.board.circuits.setCircuitStateAsync(cstate.id, false);
320
- delayMgr.setCleanerStartDelay(cstate, hstate.bodyId, sys.general.options.cleanerSolarDelayTime);
321
- }
322
- }
323
- }
324
- }
325
- } catch (err) { return logger.error(`Nixie Error setting heater state ${hstate.id}-${hstate.name}: ${err.message}`); }
326
- }
327
- public async pollEquipmentAsync() {
328
- let self = this;
329
- try {
330
- if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
331
- this._pollTimer = null;
332
- let success = false;
333
- }
334
- catch (err) { logger.error(`Nixie Error polling Heater - ${err}`); }
335
- finally { this._pollTimer = setTimeout(async () => await self.pollEquipmentAsync(), this.pollingInterval || 10000); }
336
- }
337
- private async checkHardwareStatusAsync(connectionId: string, deviceBinding: string) {
338
- try {
339
- let dev = await NixieEquipment.getDeviceService(connectionId, `/status/device/${deviceBinding}`);
340
- return dev;
341
- } catch (err) { logger.error(`Nixie Heater Error checkHardwareStatusAsync: ${err.message}`); return { hasFault: true } }
342
- }
343
- public async validateSetupAsync(heater: Heater, hstate: HeaterState) {
344
- try {
345
- if (typeof heater.connectionId !== 'undefined' && heater.connectionId !== ''
346
- && typeof heater.deviceBinding !== 'undefined' && heater.deviceBinding !== '') {
347
- try {
348
- let stat = await this.checkHardwareStatusAsync(heater.connectionId, heater.deviceBinding);
349
- // If we have a status check the return.
350
- hstate.commStatus = stat.hasFault ? 1 : 0;
351
- } catch (err) { hstate.commStatus = 1; }
352
- }
353
- else
354
- hstate.commStatus = 0;
355
- } catch (err) { logger.error(`Nixie Error checking heater Hardware ${this.heater.name}: ${err.message}`); hstate.commStatus = 1; return Promise.reject(err); }
356
- }
357
- public async closeAsync() {
358
- try {
359
- if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
360
- this._pollTimer = null;
361
- let hstate = state.heaters.getItemById(this.heater.id);
362
- await this.setHeaterStateAsync(hstate, false, false);
363
- hstate.emitEquipmentChange();
364
- }
365
- catch (err) { logger.error(`Nixie Heater closeAsync: ${err.message}`); return Promise.reject(err); }
366
- }
367
- public logData(filename: string, data: any) { this.controlPanel.logData(filename, data); }
368
- }
369
- export class NixieHeatpump extends NixieHeaterBase {
370
- public pollingInterval: number = 10000;
371
- //declare heater: Heater;
372
- constructor(ncp: INixieControlPanel, heater: Heater) {
373
- super(ncp, heater);
374
- this.heater = heater;
375
- if (typeof this.heater.stopTempDelta === 'undefined') this.heater.stopTempDelta = 1;
376
- if (typeof this.heater.minCycleTime === 'undefined') this.heater.minCycleTime = 2;
377
- this.pollEquipmentAsync();
378
- }
379
- public get id(): number { return typeof this.heater !== 'undefined' ? this.heater.id : -1; }
380
- public getCooldownTime(): number { return 0; } // There is no cooldown delay at this time for a heatpump
381
- public async setHeaterStateAsync(hstate: HeaterState, isOn: boolean) {
382
- try {
383
- // Initialize the desired state.
384
- this.isOn = isOn;
385
- this.isCooling = false;
386
- let target = hstate.startupDelay === false && isOn;
387
- if (target && typeof hstate.endTime !== 'undefined') {
388
- // Calculate a short cycle time so that the gas heater does not cycle
389
- // too often. For gas heaters this is 60 seconds. This gives enough time
390
- // for the heater control circuit to make a full cycle.
391
- if (new Date().getTime() - hstate.endTime.getTime() < this.heater.minCycleTime * 60000) {
392
- logger.verbose(`${hstate.name} short cycle detected deferring turn on state`);
393
- target = false;
394
- }
395
- }
396
- // Here we go we need to set the firemans switch state.
397
- if (hstate.isOn !== target) {
398
- logger.info(`Nixie: Set Heatpump ${hstate.id}-${hstate.name} to ${isOn}`);
399
- }
400
- if (typeof this._lastState === 'undefined' || target || this._lastState !== target) {
401
- if (utils.isNullOrEmpty(this.heater.connectionId) || utils.isNullOrEmpty(this.heater.deviceBinding)) {
402
- this._lastState = hstate.isOn = target;
403
- }
404
- else {
405
- let res = await NixieEquipment.putDeviceService(this.heater.connectionId, `/state/device/${this.heater.deviceBinding}`,
406
- { isOn: target, latch: target ? 10000 : undefined });
407
- if (res.status.code === 200) this._lastState = hstate.isOn = target;
408
- else logger.error(`Nixie Error setting heatpump state: ${res.status.code} -${res.status.message} ${res.error.message}`);
409
- }
410
- if (target) this.lastHeatCycle = new Date();
411
- }
412
- } catch (err) { return logger.error(`Nixie Error setting heatpump state ${hstate.id}-${hstate.name}: ${err.message}`); }
413
- }
414
- public async pollEquipmentAsync() {
415
- let self = this;
416
- try {
417
- if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
418
- this._pollTimer = null;
419
- let success = false;
420
- }
421
- catch (err) { logger.error(`Nixie Error polling Heatpump - ${err}`); }
422
- finally { this._pollTimer = setTimeout(async () => await self.pollEquipmentAsync(), this.pollingInterval || 10000); }
423
- }
424
- private async checkHardwareStatusAsync(connectionId: string, deviceBinding: string) {
425
- try {
426
- let dev = await NixieEquipment.getDeviceService(connectionId, `/status/device/${deviceBinding}`);
427
- return dev;
428
- } catch (err) { logger.error(`Nixie Heatpump Error checkHardwareStatusAsync: ${err.message}`); return { hasFault: true } }
429
- }
430
- public async validateSetupAsync(heater: Heater, hstate: HeaterState) {
431
- try {
432
- if (typeof heater.connectionId !== 'undefined' && heater.connectionId !== ''
433
- && typeof heater.deviceBinding !== 'undefined' && heater.deviceBinding !== '') {
434
- try {
435
- let stat = await this.checkHardwareStatusAsync(heater.connectionId, heater.deviceBinding);
436
- // If we have a status check the return.
437
- hstate.commStatus = stat.hasFault ? 1 : 0;
438
- } catch (err) { hstate.commStatus = 1; }
439
- }
440
- else
441
- hstate.commStatus = 0;
442
- } catch (err) { logger.error(`Nixie Error checking heatpump Hardware ${this.heater.name}: ${err.message}`); hstate.commStatus = 1; return Promise.reject(err); }
443
- }
444
- public async closeAsync() {
445
- try {
446
- if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
447
- this._pollTimer = null;
448
- let hstate = state.heaters.getItemById(this.heater.id);
449
- await this.setHeaterStateAsync(hstate, false);
450
- hstate.emitEquipmentChange();
451
- }
452
- catch (err) { logger.error(`Nixie Heatpump closeAsync: ${err.message}`); return Promise.reject(err); }
453
- }
454
- public logData(filename: string, data: any) { this.controlPanel.logData(filename, data); }
455
- }
456
- export class NixieUltratemp extends NixieHeaterBase {
457
- constructor(ncp: INixieControlPanel, heater: Heater) {
458
- super(ncp, heater);
459
- // Set the polling interval to 3 seconds.
460
- this.pollEquipmentAsync();
461
- }
462
- public async setServiceModeAsync() {
463
- let hstate = state.heaters.getItemById(this.heater.id);
464
- await this.setHeaterStateAsync(hstate, false, false);
465
- await this.releaseHeater(hstate);
466
- }
467
-
468
- public async pollEquipmentAsync() {
469
- let self = this;
470
- try {
471
- this.suspendPolling = true;
472
- if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
473
- this._pollTimer = null;
474
- if (this._suspendPolling > 1) return;
475
- let sheater = state.heaters.getItemById(this.heater.id, !this.closing);
476
- // If the body isn't on then we won't communicate with the chem controller. There is no need
477
- // since most of the time these are attached to the filter relay.
478
- if (!this.closing) {
479
- await this.setStatus(sheater);
480
- }
481
- }
482
- catch (err) { logger.error(`Error polling UltraTemp heater - ${err}`); }
483
- finally {
484
- this.suspendPolling = false; if (!this.closing) this._pollTimer = setTimeout(async () => {
485
- try { await self.pollEquipmentAsync() } catch (err) { }
486
- }, this.pollingInterval || 10000);
487
- }
488
- }
489
- public async setHeaterStateAsync(hstate: HeaterState, isOn: boolean, isCooling: boolean) {
490
- try {
491
- // Initialize the desired state.
492
- this.isCooling = isCooling;
493
- if (hstate.isOn !== isOn) {
494
- logger.info(`Nixie: Set Heater ${hstate.id}-${hstate.name} to ${isCooling ? 'cooling' : isOn ? 'heating' : 'off'}`);
495
-
496
- }
497
- if (isOn && !hstate.startupDelay) this.lastHeatCycle = new Date();
498
- this.isOn = hstate.isOn = isOn;
499
- } catch (err) { return logger.error(`Nixie Error setting heater state ${hstate.id}-${hstate.name}: ${err.message}`); }
500
- }
501
- public async releaseHeater(sheater: HeaterState): Promise<boolean> {
502
- try {
503
- let out = Outbound.create({
504
- portId: this.heater.portId || 0,
505
- protocol: Protocol.Heater,
506
- source: 16,
507
- dest: this.heater.address,
508
- action: 114,
509
- payload: [],
510
- retries: 3, // We are going to try 4 times.
511
- response: Response.create({ protocol: Protocol.Heater, action: 115 }),
512
- onAbort: () => { }
513
- });
514
- out.appendPayloadBytes(0, 10);
515
- out.setPayloadByte(0, 144);
516
- out.setPayloadByte(1, 0, 0);
517
- await out.sendAsync();
518
- return true;
519
-
520
- } catch (err) {
521
- // If the Ultratemp is not responding we need to store that off but at this point we know none of the codes. If a 115 does
522
- // come across this will be cleared by the processing of that message.
523
- sheater.commStatus = sys.board.valueMaps.equipmentCommStatus.getValue('commerr');
524
- state.equipment.messages.setMessageByCode(`heater:${sheater.id}:comms`, 'error', `Communication error with ${sheater.name}`);
525
- logger.error(`Communication error with Ultratemp : ${err.message}`);
526
- return false;
527
- }
528
- }
529
- public async setStatus(sheater: HeaterState): Promise<boolean> {
530
- try {
531
- let out = Outbound.create({
532
- portId: this.heater.portId || 0,
533
- protocol: Protocol.Heater,
534
- source: 16,
535
- dest: this.heater.address,
536
- action: 114,
537
- payload: [],
538
- retries: 3, // We are going to try 4 times.
539
- response: Response.create({ protocol: Protocol.Heater, action: 115 }),
540
- onAbort: () => { }
541
- });
542
- out.appendPayloadBytes(0, 10);
543
- out.setPayloadByte(0, 144);
544
- // If we are in startup delay simply tell the heater that it is off.
545
- if (sheater.startupDelay || this.closing)
546
- out.setPayloadByte(1, 0, 0);
547
- else {
548
- if (this.isOn) {
549
- if (!this.isCooling) this.lastHeatCycle = new Date();
550
- else this.lastCoolCycle = new Date();
551
- }
552
- //console.log(`Setting the heater byte ${this.isOn} ${sheater.isOn} to ${this.isOn ? (this.isCooling ? 2 : 1) : 0}`);
553
- out.setPayloadByte(1, this.isOn ? (this.isCooling ? 2 : 1) : 0, 0);
554
- }
555
- let success = await out.sendAsync();
556
- return success;
557
- } catch (err) {
558
- // If the Ultratemp is not responding we need to store that off but at this point we know none of the codes. If a 115 does
559
- // come across this will be cleared by the processing of that message.
560
- sheater.commStatus = sys.board.valueMaps.equipmentCommStatus.getValue('commerr');
561
- state.equipment.messages.setMessageByCode(`heater:${sheater.id}:comms`, 'error', `Communication error with ${sheater.name}`);
562
- logger.error(`Communication error with Ultratemp : ${err.message}`);
563
- return false;
564
- }
565
- }
566
- public async closeAsync() {
567
- try {
568
- this.suspendPolling = true;
569
- this.closing = true;
570
- if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
571
- this._pollTimer = null;
572
- let sheater = state.heaters.getItemById(this.id);
573
- await this.releaseHeater(sheater);
574
- logger.info(`Closing Heater ${this.heater.name}`);
575
- }
576
- catch (err) { logger.error(`Ultratemp closeAsync: ${err.message}`); return Promise.reject(err); }
577
- }
578
- }
579
- export class NixieMastertemp extends NixieGasHeater {
580
- constructor(ncp: INixieControlPanel, heater: Heater) {
581
- super(ncp, heater);
582
- // Set the polling interval to 3 seconds.
583
- this.pollEquipmentAsync();
584
- this.pollingInterval = 3000;
585
-
586
- }
587
- /* public getCooldownTime(): number {
588
- // Delays are always in terms of seconds so convert the minute to seconds.
589
- if (this.heater.cooldownDelay === 0 || typeof this.lastHeatCycle === 'undefined') return 0;
590
- let now = new Date().getTime();
591
- let cooldown = this.isOn ? this.heater.cooldownDelay * 60000 : Math.round(((this.lastHeatCycle.getDate() + this.heater.cooldownDelay * 60000) - now) / 1000);
592
- return Math.min(Math.max(0, cooldown), this.heater.cooldownDelay * 60);
593
- } */
594
- public async setHeaterStateAsync(hstate: HeaterState, isOn: boolean) {
595
- try {
596
- // Initialize the desired state.
597
- this.isOn = isOn;
598
- this.isCooling = false;
599
- let target = hstate.startupDelay === false && isOn;
600
- if (target && typeof hstate.endTime !== 'undefined') {
601
- // Calculate a short cycle time so that the gas heater does not cycle
602
- // too often. For gas heaters this is 60 seconds. This gives enough time
603
- // for the heater control circuit to make a full cycle.
604
- if (new Date().getTime() - hstate.endTime.getTime() < this.heater.minCycleTime * 60000) {
605
- logger.verbose(`${hstate.name} short cycle detected deferring turn on state`);
606
- target = false;
607
- }
608
- }
609
- // Here we go we need to set the state.
610
- if (hstate.isOn !== target) {
611
- logger.info(`Nixie: Set Mastertemp ${hstate.id}-${hstate.name} to ${isOn}`);
612
- }
613
- if (typeof this._lastState === 'undefined' || target || this._lastState !== target) {
614
- this._lastState = hstate.isOn = target;
615
- if (target) this.lastHeatCycle = new Date();
616
- }
617
- hstate.isOn = isOn;
618
- } catch (err) { return logger.error(`Nixie Error setting heater state ${hstate.id}-${hstate.name}: ${err.message}`); }
619
- }
620
- public async pollEquipmentAsync() {
621
- let self = this;
622
- try {
623
- this.suspendPolling = true;
624
- if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
625
- this._pollTimer = null;
626
- if (this._suspendPolling > 1) return;
627
- let sheater = state.heaters.getItemById(this.heater.id, !this.closing);
628
- if (!this.closing) await this.setStatus(sheater);
629
- }
630
- catch (err) { logger.error(`Error polling MasterTemp heater - ${err}`); }
631
- finally {
632
- this.suspendPolling = false; if (!this.closing) this._pollTimer = setTimeout(async () => {
633
- try { await self.pollEquipmentAsync() } catch (err) { }
634
- }, this.pollingInterval || 3000);
635
- }
636
- }
637
- public async setStatus(sheater: HeaterState): Promise<boolean> {
638
- try {
639
- let out = Outbound.create({
640
- portId: this.heater.portId || 0,
641
- protocol: Protocol.Heater,
642
- source: 16,
643
- dest: this.heater.address,
644
- action: 112,
645
- payload: [],
646
- retries: 3, // We are going to try 4 times.
647
- response: Response.create({ protocol: Protocol.Heater, action: 116 }),
648
- onAbort: () => { }
649
- });
650
- out.appendPayloadBytes(0, 11);
651
- // If we have a startup delay we need to simply send 0 to the heater to make sure that it is off.
652
- if (sheater.startupDelay)
653
- out.setPayloadByte(0, 0);
654
- else {
655
- // The cooldown delay is a bit hard to figure out here since I think the heater does it on its own.
656
- out.setPayloadByte(0, sheater.bodyId <= 2 ? sheater.bodyId : 0);
657
- }
658
- out.setPayloadByte(1, sys.bodies.getItemById(1).heatSetpoint || 0);
659
- out.setPayloadByte(2, sys.bodies.getItemById(2).heatSetpoint || 0);
660
- let success = await out.sendAsync();
661
- return success;
662
- } catch (err) {
663
- // If the MasterTemp is not responding we need to store that off but at this point we know none of the codes. If a 115 does
664
- // come across this will be cleared by the processing of that message.
665
- sheater.commStatus = sys.board.valueMaps.equipmentCommStatus.getValue('commerr');
666
- state.equipment.messages.setMessageByCode(`heater:${sheater.id}:comms`, 'error', `Communication error with ${sheater.name}`);
667
- logger.error(`Communication error with MasterTemp : ${err.message}`);
668
- return false;
669
- }
670
- }
671
- public async setServiceModeAsync() {
672
- let hstate = state.heaters.getItemById(this.heater.id);
673
- await this.setHeaterStateAsync(hstate, false);
674
- }
675
- public async closeAsync() {
676
- try {
677
- this.suspendPolling = true;
678
- this.closing = true;
679
- if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
680
- this._pollTimer = null;
681
- logger.info(`Closing Heater ${this.heater.name}`);
682
-
683
- }
684
- catch (err) { logger.error(`MasterTemp closeAsync: ${err.message}`); return Promise.reject(err); }
685
- }
686
- }
687
- export class NixieUltraTempETi extends NixieHeaterBase {
688
- constructor(ncp: INixieControlPanel, heater: Heater) {
689
- super(ncp, heater);
690
- // Set the polling interval to 3 seconds.
691
- this.pollEquipmentAsync();
692
- }
693
- public async pollEquipmentAsync() {
694
- let self = this;
695
- try {
696
- this.suspendPolling = true;
697
- if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
698
- this._pollTimer = null;
699
- if (this._suspendPolling > 1) return;
700
- let sheater = state.heaters.getItemById(this.heater.id, !this.closing);
701
- // If the body isn't on then we won't communicate with the chem controller. There is no need
702
- // since most of the time these are attached to the filter relay.
703
- if (!this.closing) {
704
- await this.setStatus(sheater);
705
- }
706
- }
707
- catch (err) { logger.error(`Error polling UltraTemp ETi heater - ${err}`); }
708
- finally {
709
- this.suspendPolling = false; if (!this.closing) this._pollTimer = setTimeout(async () => {
710
- try { await self.pollEquipmentAsync() } catch (err) { }
711
- }, this.pollingInterval || 10000);
712
- }
713
- }
714
- public async setHeaterStateAsync(hstate: HeaterState, isOn: boolean, isCooling: boolean) {
715
- try {
716
- // Initialize the desired state.
717
- this.isCooling = isCooling;
718
- if (hstate.isOn !== isOn) {
719
- logger.info(`Nixie: Set Heater ${hstate.id}-${hstate.name} to ${isCooling ? 'cooling' : isOn ? 'heating' : 'off'}`);
720
-
721
- }
722
- if (isOn && !hstate.startupDelay) this.lastHeatCycle = new Date();
723
- this.isOn = hstate.isOn = isOn;
724
- } catch (err) { return logger.error(`Nixie Error setting heater state ${hstate.id}-${hstate.name}: ${err.message}`); }
725
- }
726
- public async releaseHeater(sheater: HeaterState): Promise<boolean> {
727
- try {
728
- let out = Outbound.create({
729
- portId: this.heater.portId || 0,
730
- protocol: Protocol.Heater,
731
- source: 16,
732
- dest: this.heater.address,
733
- action: 112,
734
- payload: [],
735
- retries: 3, // We are going to try 4 times.
736
- response: Response.create({ protocol: Protocol.Heater, action: 113 }),
737
- onAbort: () => { }
738
- });
739
- out.appendPayloadBytes(0, 10);
740
- out.setPayloadByte(0, 0);
741
- out.setPayloadByte(1, 0);
742
- out.setPayloadByte(2, 78);
743
- out.setPayloadByte(3, 1);
744
- out.setPayloadByte(4, 5);
745
- let success = await out.sendAsync();
746
- return success;
747
- } catch (err) {
748
- // If the Ultratemp is not responding we need to store that off but at this point we know none of the codes. If a 113 does
749
- // come across this will be cleared by the processing of that message.
750
- sheater.commStatus = sys.board.valueMaps.equipmentCommStatus.getValue('commerr');
751
- state.equipment.messages.setMessageByCode(`heater:${sheater.id}:comms`, 'error', `Communication error with ${sheater.name}`);
752
- logger.error(`Communication error with Ultratemp : ${err.message}`);
753
- return false;
754
- }
755
- }
756
- protected calcHeatModeByte(body: Body): number {
757
- let byte = 0;
758
- if (this.closing) return 0; // We are closing so just set the heat mode to off.
759
- let mode = sys.board.valueMaps.heatModes.transform(body.heatMode || 0);
760
- switch (mode.name) {
761
- case 'hpump':
762
- case 'heatpump':
763
- byte = 1;
764
- break;
765
- case 'heater':
766
- byte = 2;
767
- break;
768
- case 'heatpumpref':
769
- case 'heatpumppref':
770
- case 'hybrid':
771
- byte = 3;
772
- break;
773
- case 'dual':
774
- byte = 4;
775
- break;
776
- }
777
- return byte;
778
- }
779
- public async setServiceModeAsync() {
780
- let hstate = state.heaters.getItemById(this.heater.id);
781
- await this.setHeaterStateAsync(hstate, false, false);
782
- await this.releaseHeater(hstate);
783
- }
784
- public async setStatus(sheater: HeaterState): Promise<boolean> {
785
- try {
786
- let out = Outbound.create({
787
- portId: this.heater.portId || 0,
788
- protocol: Protocol.Heater,
789
- source: 16,
790
- dest: this.heater.address,
791
- action: 112,
792
- payload: [],
793
- retries: 3, // We are going to try 4 times.
794
- response: Response.create({ protocol: Protocol.Heater, action: 113 }),
795
- onAbort: () => { }
796
- });
797
- out.appendPayloadBytes(0, 10);
798
- out.setPayloadByte(0, this.isOn && !sheater.startupDelay && !this.closing ? 1 : 0);
799
- if (sheater.bodyId > 0) {
800
- let body = sys.bodies.getItemById(sheater.bodyId);
801
- out.setPayloadByte(1, this.calcHeatModeByte(body));
802
- out.setPayloadByte(2, body.setPoint);
803
- }
804
- else out.setPayloadByte(2, utils.convert.temperature.convertUnits(78, 'F', sys.board.valueMaps.tempUnits.getName(state.temps.units) || 'F')); // Just set it to a valid setpoint and call it a day.
805
- out.setPayloadByte(3, this.heater.economyTime, 1);
806
- out.setPayloadByte(4, this.heater.maxBoostTemp, 5);
807
- if (this.isOn) {
808
- if (!this.isCooling) this.lastHeatCycle = new Date();
809
- else this.lastCoolCycle = new Date();
810
- }
811
- let success = await out.sendAsync();
812
- return success;
813
- } catch (err) {
814
- // If the Ultratemp ETi is not responding we need to store that off but at this point we know none of the codes. If a 113 does
815
- // come across this will be cleared by the processing of that message.
816
- sheater.commStatus = sys.board.valueMaps.equipmentCommStatus.getValue('commerr');
817
- state.equipment.messages.setMessageByCode(`heater:${sheater.id}:comms`, 'error', `Communication error with ${sheater.name}`);
818
- logger.error(`Communication error with Ultratemp ETi : ${err.message}`);
819
- return false;
820
- }
821
- }
822
- public async closeAsync() {
823
- try {
824
- this.suspendPolling = true;
825
- this.closing = true;
826
- if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
827
- this._pollTimer = null;
828
- let sheater = state.heaters.getItemById(this.id);
829
- await this.releaseHeater(sheater);
830
- logger.info(`Closing Heater ${this.heater.name}`);
831
- }
832
- catch (err) { logger.error(`Ultratemp closeAsync: ${err.message}`); return Promise.reject(err); }
833
- }
834
- }
1
+ import { EquipmentNotFoundError, InvalidEquipmentDataError, InvalidEquipmentIdError, InvalidOperationError, ParameterOutOfRangeError } from '../../Errors';
2
+ import { utils, Timestamp } from '../../Constants';
3
+ import { logger } from '../../../logger/Logger';
4
+
5
+ import { NixieEquipment, NixieChildEquipment, NixieEquipmentCollection, INixieControlPanel } from "../NixieEquipment";
6
+ import { Body, Heater, HeaterCollection, sys } from "../../../controller/Equipment";
7
+ import { BodyTempState, HeaterState, state, } from "../../State";
8
+ import { setTimeout, clearTimeout } from 'timers';
9
+ import { NixieControlPanel } from '../Nixie';
10
+ import { webApp, InterfaceServerResponse } from "../../../web/Server";
11
+ import { conn } from '../../../controller/comms/Comms';
12
+ import { Inbound, Outbound, Protocol, Response } from '../../../controller/comms/messages/Messages';
13
+ import { delayMgr } from '../../Lockouts';
14
+
15
+ export class NixieHeaterCollection extends NixieEquipmentCollection<NixieHeaterBase> {
16
+ public async deleteHeaterAsync(id: number) {
17
+ try {
18
+ for (let i = this.length - 1; i >= 0; i--) {
19
+ let heater = this[i];
20
+ if (heater.id === id) {
21
+ await heater.closeAsync();
22
+ this.splice(i, 1);
23
+ }
24
+ }
25
+ } catch (err) { return Promise.reject(`Nixie Control Panel deleteHeaterAsync ${err.message}`); }
26
+ }
27
+ public async setHeaterStateAsync(hstate: HeaterState, val: boolean, isCooling: boolean) {
28
+ try {
29
+ let h: NixieHeaterBase = this.find(elem => elem.id === hstate.id) as NixieHeaterBase;
30
+ if (typeof h === 'undefined') {
31
+ return Promise.reject(new Error(`NCP: Heater ${hstate.id}-${hstate.name} could not be found to set the state to ${val}.`));
32
+ }
33
+ await h.setHeaterStateAsync(hstate, val, isCooling);
34
+ }
35
+ catch (err) { return logger.error(`NCP: setHeaterStateAsync ${hstate.id}-${hstate.name}: ${err.message}`); }
36
+ }
37
+ public async setHeaterAsync(heater: Heater, data: any) {
38
+ // By the time we get here we know that we are in control and this is a Nixie heater.
39
+ try {
40
+ let h: NixieHeaterBase = this.find(elem => elem.id === heater.id) as NixieHeaterBase;
41
+ if (typeof h === 'undefined') {
42
+ heater.master = 1;
43
+ h = NixieHeaterBase.create(this.controlPanel, heater);
44
+ this.push(h);
45
+ await h.setHeaterAsync(data);
46
+ logger.info(`A Heater was not found for id #${heater.id} creating Heater`);
47
+ }
48
+ else {
49
+ await h.setHeaterAsync(data);
50
+ }
51
+ }
52
+ catch (err) { logger.error(`setHeaterAsync: ${err.message}`); return Promise.reject(err); }
53
+ }
54
+ public async initAsync(heaters: HeaterCollection) {
55
+ try {
56
+ for (let i = 0; i < heaters.length; i++) {
57
+ let heater = heaters.getItemByIndex(i);
58
+ if (heater.master === 1) {
59
+ if (typeof this.find(elem => elem.id === heater.id) === 'undefined') {
60
+ logger.info(`Initializing Heater ${heater.name}`);
61
+ let nHeater = NixieHeaterBase.create(this.controlPanel, heater);
62
+ this.push(nHeater);
63
+ }
64
+ }
65
+ }
66
+ }
67
+ catch (err) { logger.error(`Nixie Heater initAsync: ${err.message}`); return Promise.reject(err); }
68
+ }
69
+ public async closeAsync() {
70
+ try {
71
+ for (let i = this.length - 1; i >= 0; i--) {
72
+ try {
73
+ await this[i].closeAsync();
74
+ this.splice(i, 1);
75
+ } catch (err) { logger.error(`Error stopping Nixie Heater ${err}`); }
76
+ }
77
+ } catch (err) { } // Don't bail if we have an errror.
78
+ }
79
+ public async initHeaterAsync(heater: Heater): Promise<NixieHeaterBase> {
80
+ try {
81
+ let c: NixieHeaterBase = this.find(elem => elem.id === heater.id) as NixieHeaterBase;
82
+ if (typeof c === 'undefined') {
83
+ c = NixieHeaterBase.create(this.controlPanel, heater);
84
+ this.push(c);
85
+ }
86
+ return c;
87
+ } catch (err) { logger.error(`initHeaterAsync: ${err.message}`); return Promise.reject(err); }
88
+ }
89
+ public async setServiceModeAsync() {
90
+ try {
91
+ for (let i = this.length - 1; i >= 0; i--) {
92
+ let heater = this[i] as NixieHeaterBase;
93
+ await heater.setServiceModeAsync();
94
+ }
95
+ } catch (err) { return Promise.reject(`Nixie Control Panel setServiceMode ${err.message}`); }
96
+ }
97
+ }
98
+ export class NixieHeaterBase extends NixieEquipment {
99
+ protected _suspendPolling: number = 0;
100
+ public pollingInterval: number = 10000;
101
+ public heater: Heater;
102
+ protected _pollTimer: NodeJS.Timeout = null;
103
+ protected _lastState;
104
+ protected closing = false;
105
+ protected bodyOnTime: number;
106
+ protected isOn: boolean = false;
107
+ protected isCooling: boolean = false;
108
+ protected lastHeatCycle: Date;
109
+ protected lastCoolCycle: Date;
110
+ constructor(ncp: INixieControlPanel, heater: Heater) {
111
+ super(ncp);
112
+ this.heater = heater;
113
+ }
114
+ public get suspendPolling(): boolean { return this._suspendPolling > 0; }
115
+ public set suspendPolling(val: boolean) { this._suspendPolling = Math.max(0, this._suspendPolling + (val ? 1 : -1)); }
116
+ public get id(): number { return typeof this.heater !== 'undefined' ? this.heater.id : -1; }
117
+ public getCooldownTime() { return 0; }
118
+ public static create(ncp: INixieControlPanel, heater: Heater): NixieHeaterBase {
119
+ let type = sys.board.valueMaps.heaterTypes.transform(heater.type);
120
+ switch (type.name) {
121
+ case 'heatpump':
122
+ return new NixieHeatpump(ncp, heater);
123
+ case 'ultratemp':
124
+ return new NixieUltratemp(ncp, heater);
125
+ case 'gas':
126
+ return new NixieGasHeater(ncp, heater);
127
+ case 'mastertemp':
128
+ return new NixieMastertemp(ncp, heater);
129
+ case 'solar':
130
+ return new NixieSolarHeater(ncp, heater);
131
+ case 'hybrid':
132
+ return new NixieUltraTempETi(ncp, heater);
133
+ default:
134
+ return new NixieHeaterBase(ncp, heater);
135
+ }
136
+ }
137
+ public isBodyOn() {
138
+ let isOn = sys.board.bodies.isBodyOn(this.heater.body);
139
+ if (isOn && typeof this.bodyOnTime === 'undefined') {
140
+ this.bodyOnTime = new Date().getTime();
141
+ }
142
+ else if (!isOn) this.bodyOnTime = undefined;
143
+ return isOn;
144
+ }
145
+ public async setHeaterStateAsync(hstate: HeaterState, isOn: boolean, isCooling: boolean) {
146
+ try {
147
+ return Promise.reject(new InvalidOperationError(`You cannot change the state on this type of heater ${hstate.name}`, 'setHeaterStateAsync'));
148
+ } catch (err) { return logger.error(`Nixie Error setting heater state ${hstate.id}-${hstate.name}: ${err.message}`); }
149
+ }
150
+ public async setHeaterAsync(data: any) {
151
+ try {
152
+ let heater = this.heater;
153
+
154
+ }
155
+ catch (err) { logger.error(`Nixie setHeaterAsync: ${err.message}`); return Promise.reject(err); }
156
+ }
157
+ public async closeAsync() { }
158
+ public async setServiceModeAsync() {
159
+ let hstate = state.heaters.getItemById(this.heater.id);
160
+ await this.setHeaterStateAsync(hstate, false, false);
161
+ }
162
+ }
163
+ export class NixieGasHeater extends NixieHeaterBase {
164
+ public pollingInterval: number = 10000;
165
+ //declare heater: Heater;
166
+ constructor(ncp: INixieControlPanel, heater: Heater) {
167
+ super(ncp, heater);
168
+ this.heater = heater;
169
+ if (typeof this.heater.stopTempDelta === 'undefined') this.heater.stopTempDelta = 1;
170
+ if (typeof this.heater.minCycleTime === 'undefined') this.heater.minCycleTime = 2;
171
+ this.pollEquipmentAsync();
172
+ }
173
+ public get id(): number { return typeof this.heater !== 'undefined' ? this.heater.id : -1; }
174
+ public getCooldownTime(): number {
175
+ // Delays are always in terms of seconds so convert the minute to seconds.
176
+ if (this.heater.cooldownDelay === 0 || typeof this.lastHeatCycle === 'undefined') return 0;
177
+ let now = new Date().getTime();
178
+ let cooldown = this.isOn ? this.heater.cooldownDelay * 60000 : Math.round(((this.lastHeatCycle.getDate() + this.heater.cooldownDelay * 60000) - now) / 1000);
179
+ return Math.min(Math.max(0, cooldown), this.heater.cooldownDelay * 60);
180
+ }
181
+ public async setHeaterStateAsync(hstate: HeaterState, isOn: boolean) {
182
+ try {
183
+ // Initialize the desired state.
184
+ this.isOn = isOn;
185
+ this.isCooling = false;
186
+ let target = hstate.startupDelay === false && isOn;
187
+ if (target && typeof hstate.endTime !== 'undefined') {
188
+ // Calculate a short cycle time so that the gas heater does not cycle
189
+ // too often. For gas heaters this is 60 seconds. This gives enough time
190
+ // for the heater control circuit to make a full cycle.
191
+ if (new Date().getTime() - hstate.endTime.getTime() < this.heater.minCycleTime * 60000) {
192
+ logger.verbose(`${hstate.name} short cycle detected deferring turn on state`);
193
+ target = false;
194
+ }
195
+ }
196
+ // Here we go we need to set the firemans switch state.
197
+ if (hstate.isOn !== target) {
198
+ logger.info(`Nixie: Set Heater ${hstate.id}-${hstate.name} to ${isOn}`);
199
+ }
200
+ if (typeof this._lastState === 'undefined' || target || this._lastState !== target) {
201
+ if (utils.isNullOrEmpty(this.heater.connectionId) || utils.isNullOrEmpty(this.heater.deviceBinding)) {
202
+ this._lastState = hstate.isOn = target;
203
+ }
204
+ else {
205
+ let res = await NixieEquipment.putDeviceService(this.heater.connectionId, `/state/device/${this.heater.deviceBinding}`,
206
+ { isOn: target, latch: target ? 10000 : undefined });
207
+ if (res.status.code === 200) this._lastState = hstate.isOn = target;
208
+ else logger.error(`Nixie Error setting heater state: ${res.status.code} -${res.status.message} ${res.error.message}`);
209
+ }
210
+ if (target) this.lastHeatCycle = new Date();
211
+ }
212
+ } catch (err) { return logger.error(`Nixie Error setting heater state ${hstate.id}-${hstate.name}: ${err.message}`); }
213
+ }
214
+ public async pollEquipmentAsync() {
215
+ let self = this;
216
+ try {
217
+ if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
218
+ this._pollTimer = null;
219
+ let success = false;
220
+ }
221
+ catch (err) { logger.error(`Nixie Error polling Heater - ${err}`); }
222
+ finally { this._pollTimer = setTimeout(async () => await self.pollEquipmentAsync(), this.pollingInterval || 10000); }
223
+ }
224
+ private async checkHardwareStatusAsync(connectionId: string, deviceBinding: string) {
225
+ try {
226
+ let dev = await NixieEquipment.getDeviceService(connectionId, `/status/device/${deviceBinding}`);
227
+ return dev;
228
+ } catch (err) { logger.error(`Nixie Heater Error checkHardwareStatusAsync: ${err.message}`); return { hasFault: true } }
229
+ }
230
+ public async validateSetupAsync(heater: Heater, hstate: HeaterState) {
231
+ try {
232
+ if (typeof heater.connectionId !== 'undefined' && heater.connectionId !== ''
233
+ && typeof heater.deviceBinding !== 'undefined' && heater.deviceBinding !== '') {
234
+ try {
235
+ let stat = await this.checkHardwareStatusAsync(heater.connectionId, heater.deviceBinding);
236
+ // If we have a status check the return.
237
+ hstate.commStatus = stat.hasFault ? 1 : 0;
238
+ } catch (err) { hstate.commStatus = 1; }
239
+ }
240
+ else
241
+ hstate.commStatus = 0;
242
+ } catch (err) { logger.error(`Nixie Error checking heater Hardware ${this.heater.name}: ${err.message}`); hstate.commStatus = 1; return Promise.reject(err); }
243
+ }
244
+ public async closeAsync() {
245
+ try {
246
+ if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
247
+ this._pollTimer = null;
248
+ let hstate = state.heaters.getItemById(this.heater.id);
249
+ await this.setHeaterStateAsync(hstate, false);
250
+ hstate.emitEquipmentChange();
251
+ }
252
+ catch (err) { logger.error(`Nixie Heater closeAsync: ${err.message}`); return Promise.reject(err); }
253
+ }
254
+ public logData(filename: string, data: any) { this.controlPanel.logData(filename, data); }
255
+ }
256
+ export class NixieSolarHeater extends NixieHeaterBase {
257
+ public pollingInterval: number = 10000;
258
+ declare heater: Heater;
259
+ constructor(ncp: INixieControlPanel, heater: Heater) {
260
+ super(ncp, heater);
261
+ this.heater = heater;
262
+ this.pollEquipmentAsync();
263
+ }
264
+ public get id(): number { return typeof this.heater !== 'undefined' ? this.heater.id : -1; }
265
+ public async setHeaterStateAsync(hstate: HeaterState, isOn: boolean, isCooling: boolean) {
266
+ try {
267
+ let origState = hstate.isOn;
268
+ // Initialize the desired state.
269
+ this.isOn = isOn;
270
+ this.isCooling = isCooling;
271
+ let target = hstate.startupDelay === false && isOn;
272
+ if (target && typeof hstate.endTime !== 'undefined') {
273
+ // Calculate a short cycle time so that the solar heater does not cycle
274
+ // too often. For solar heaters this is 60 seconds. This gives enough time
275
+ // for the valve to rotate and start heating. If the solar and water sensors are
276
+ // not having issues this should be plenty of time.
277
+ if (new Date().getTime() - hstate.endTime.getTime() < 60000) {
278
+ logger.verbose(`${hstate.name} short cycle detected deferring turn on state`);
279
+ target = false;
280
+ }
281
+ }
282
+
283
+ // Here we go we need to set the valve status that is attached to solar.
284
+ if (hstate.isOn !== target) {
285
+ logger.info(`Nixie: Set Heater ${hstate.id}-${hstate.name} to ${isOn}`);
286
+ }
287
+ if (typeof this._lastState === 'undefined' || target || this._lastState !== target) {
288
+ if (utils.isNullOrEmpty(this.heater.connectionId) || utils.isNullOrEmpty(this.heater.deviceBinding)) {
289
+ this._lastState = hstate.isOn = target;
290
+ hstate.isCooling = target && isCooling;
291
+ }
292
+ else {
293
+ let res = await NixieEquipment.putDeviceService(this.heater.connectionId, `/state/device/${this.heater.deviceBinding}`,
294
+ { isOn: target, latch: target ? 10000 : undefined });
295
+ if (res.status.code === 200) {
296
+ this._lastState = hstate.isOn = target;
297
+ hstate.isCooling = target && isCooling;
298
+ }
299
+ else logger.error(`Nixie Error setting heater state: ${res.status.code} -${res.status.message} ${res.error.message}`);
300
+ }
301
+ if (target) {
302
+ if (isCooling) this.lastCoolCycle = new Date();
303
+ else if (isOn) this.lastHeatCycle = new Date();
304
+ }
305
+ }
306
+ // In this instance we need to see if there are cleaner circuits that we need to turn off
307
+ // then delay for the current body because the solar just came on.
308
+ if (hstate.isOn && sys.general.options.cleanerSolarDelay && !origState) {
309
+ let arrTypes = sys.board.valueMaps.circuitFunctions.toArray().filter(x => { return x.name.indexOf('cleaner') !== -1 && x.body === hstate.bodyId });
310
+ let cleaners = sys.circuits.filter(x => { return arrTypes.findIndex(t => { return t.val === x.type }) !== -1 });
311
+ // Turn off all the cleaner circuits and set an on delay if they are on.
312
+ for (let i = 0; i < cleaners.length; i++) {
313
+ let cleaner = cleaners.getItemByIndex(i);
314
+ if (cleaner.isActive) {
315
+ let cstate = state.circuits.getItemById(cleaner.id);
316
+ if (cstate.isOn && sys.general.options.cleanerSolarDelayTime > 0) {
317
+ // Turn off the circuit then set a delay.
318
+ logger.info(`Setting cleaner solar delay for ${cleaner.name} to ${sys.general.options.cleanerSolarDelayTime}`);
319
+ await sys.board.circuits.setCircuitStateAsync(cstate.id, false);
320
+ delayMgr.setCleanerStartDelay(cstate, hstate.bodyId, sys.general.options.cleanerSolarDelayTime);
321
+ }
322
+ }
323
+ }
324
+ }
325
+ } catch (err) { return logger.error(`Nixie Error setting heater state ${hstate.id}-${hstate.name}: ${err.message}`); }
326
+ }
327
+ public async pollEquipmentAsync() {
328
+ let self = this;
329
+ try {
330
+ if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
331
+ this._pollTimer = null;
332
+ let success = false;
333
+ }
334
+ catch (err) { logger.error(`Nixie Error polling Heater - ${err}`); }
335
+ finally { this._pollTimer = setTimeout(async () => await self.pollEquipmentAsync(), this.pollingInterval || 10000); }
336
+ }
337
+ private async checkHardwareStatusAsync(connectionId: string, deviceBinding: string) {
338
+ try {
339
+ let dev = await NixieEquipment.getDeviceService(connectionId, `/status/device/${deviceBinding}`);
340
+ return dev;
341
+ } catch (err) { logger.error(`Nixie Heater Error checkHardwareStatusAsync: ${err.message}`); return { hasFault: true } }
342
+ }
343
+ public async validateSetupAsync(heater: Heater, hstate: HeaterState) {
344
+ try {
345
+ if (typeof heater.connectionId !== 'undefined' && heater.connectionId !== ''
346
+ && typeof heater.deviceBinding !== 'undefined' && heater.deviceBinding !== '') {
347
+ try {
348
+ let stat = await this.checkHardwareStatusAsync(heater.connectionId, heater.deviceBinding);
349
+ // If we have a status check the return.
350
+ hstate.commStatus = stat.hasFault ? 1 : 0;
351
+ } catch (err) { hstate.commStatus = 1; }
352
+ }
353
+ else
354
+ hstate.commStatus = 0;
355
+ } catch (err) { logger.error(`Nixie Error checking heater Hardware ${this.heater.name}: ${err.message}`); hstate.commStatus = 1; return Promise.reject(err); }
356
+ }
357
+ public async closeAsync() {
358
+ try {
359
+ if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
360
+ this._pollTimer = null;
361
+ let hstate = state.heaters.getItemById(this.heater.id);
362
+ await this.setHeaterStateAsync(hstate, false, false);
363
+ hstate.emitEquipmentChange();
364
+ }
365
+ catch (err) { logger.error(`Nixie Heater closeAsync: ${err.message}`); return Promise.reject(err); }
366
+ }
367
+ public logData(filename: string, data: any) { this.controlPanel.logData(filename, data); }
368
+ }
369
+ export class NixieHeatpump extends NixieHeaterBase {
370
+ public pollingInterval: number = 10000;
371
+ //declare heater: Heater;
372
+ constructor(ncp: INixieControlPanel, heater: Heater) {
373
+ super(ncp, heater);
374
+ this.heater = heater;
375
+ if (typeof this.heater.stopTempDelta === 'undefined') this.heater.stopTempDelta = 1;
376
+ if (typeof this.heater.minCycleTime === 'undefined') this.heater.minCycleTime = 2;
377
+ this.pollEquipmentAsync();
378
+ }
379
+ public get id(): number { return typeof this.heater !== 'undefined' ? this.heater.id : -1; }
380
+ public getCooldownTime(): number { return 0; } // There is no cooldown delay at this time for a heatpump
381
+ public async setHeaterStateAsync(hstate: HeaterState, isOn: boolean) {
382
+ try {
383
+ // Initialize the desired state.
384
+ this.isOn = isOn;
385
+ this.isCooling = false;
386
+ let target = hstate.startupDelay === false && isOn;
387
+ if (target && typeof hstate.endTime !== 'undefined') {
388
+ // Calculate a short cycle time so that the gas heater does not cycle
389
+ // too often. For gas heaters this is 60 seconds. This gives enough time
390
+ // for the heater control circuit to make a full cycle.
391
+ if (new Date().getTime() - hstate.endTime.getTime() < this.heater.minCycleTime * 60000) {
392
+ logger.verbose(`${hstate.name} short cycle detected deferring turn on state`);
393
+ target = false;
394
+ }
395
+ }
396
+ // Here we go we need to set the firemans switch state.
397
+ if (hstate.isOn !== target) {
398
+ logger.info(`Nixie: Set Heatpump ${hstate.id}-${hstate.name} to ${isOn}`);
399
+ }
400
+ if (typeof this._lastState === 'undefined' || target || this._lastState !== target) {
401
+ if (utils.isNullOrEmpty(this.heater.connectionId) || utils.isNullOrEmpty(this.heater.deviceBinding)) {
402
+ this._lastState = hstate.isOn = target;
403
+ }
404
+ else {
405
+ let res = await NixieEquipment.putDeviceService(this.heater.connectionId, `/state/device/${this.heater.deviceBinding}`,
406
+ { isOn: target, latch: target ? 10000 : undefined });
407
+ if (res.status.code === 200) this._lastState = hstate.isOn = target;
408
+ else logger.error(`Nixie Error setting heatpump state: ${res.status.code} -${res.status.message} ${res.error.message}`);
409
+ }
410
+ if (target) this.lastHeatCycle = new Date();
411
+ }
412
+ } catch (err) { return logger.error(`Nixie Error setting heatpump state ${hstate.id}-${hstate.name}: ${err.message}`); }
413
+ }
414
+ public async pollEquipmentAsync() {
415
+ let self = this;
416
+ try {
417
+ if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
418
+ this._pollTimer = null;
419
+ let success = false;
420
+ }
421
+ catch (err) { logger.error(`Nixie Error polling Heatpump - ${err}`); }
422
+ finally { this._pollTimer = setTimeout(async () => await self.pollEquipmentAsync(), this.pollingInterval || 10000); }
423
+ }
424
+ private async checkHardwareStatusAsync(connectionId: string, deviceBinding: string) {
425
+ try {
426
+ let dev = await NixieEquipment.getDeviceService(connectionId, `/status/device/${deviceBinding}`);
427
+ return dev;
428
+ } catch (err) { logger.error(`Nixie Heatpump Error checkHardwareStatusAsync: ${err.message}`); return { hasFault: true } }
429
+ }
430
+ public async validateSetupAsync(heater: Heater, hstate: HeaterState) {
431
+ try {
432
+ if (typeof heater.connectionId !== 'undefined' && heater.connectionId !== ''
433
+ && typeof heater.deviceBinding !== 'undefined' && heater.deviceBinding !== '') {
434
+ try {
435
+ let stat = await this.checkHardwareStatusAsync(heater.connectionId, heater.deviceBinding);
436
+ // If we have a status check the return.
437
+ hstate.commStatus = stat.hasFault ? 1 : 0;
438
+ } catch (err) { hstate.commStatus = 1; }
439
+ }
440
+ else
441
+ hstate.commStatus = 0;
442
+ } catch (err) { logger.error(`Nixie Error checking heatpump Hardware ${this.heater.name}: ${err.message}`); hstate.commStatus = 1; return Promise.reject(err); }
443
+ }
444
+ public async closeAsync() {
445
+ try {
446
+ if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
447
+ this._pollTimer = null;
448
+ let hstate = state.heaters.getItemById(this.heater.id);
449
+ await this.setHeaterStateAsync(hstate, false);
450
+ hstate.emitEquipmentChange();
451
+ }
452
+ catch (err) { logger.error(`Nixie Heatpump closeAsync: ${err.message}`); return Promise.reject(err); }
453
+ }
454
+ public logData(filename: string, data: any) { this.controlPanel.logData(filename, data); }
455
+ }
456
+ export class NixieUltratemp extends NixieHeaterBase {
457
+ constructor(ncp: INixieControlPanel, heater: Heater) {
458
+ super(ncp, heater);
459
+ // Set the polling interval to 3 seconds.
460
+ this.pollEquipmentAsync();
461
+ }
462
+ public async setServiceModeAsync() {
463
+ let hstate = state.heaters.getItemById(this.heater.id);
464
+ await this.setHeaterStateAsync(hstate, false, false);
465
+ await this.releaseHeater(hstate);
466
+ }
467
+
468
+ public async pollEquipmentAsync() {
469
+ let self = this;
470
+ try {
471
+ this.suspendPolling = true;
472
+ if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
473
+ this._pollTimer = null;
474
+ if (this._suspendPolling > 1) return;
475
+ let sheater = state.heaters.getItemById(this.heater.id, !this.closing);
476
+ // If the body isn't on then we won't communicate with the chem controller. There is no need
477
+ // since most of the time these are attached to the filter relay.
478
+ if (!this.closing) {
479
+ await this.setStatus(sheater);
480
+ }
481
+ }
482
+ catch (err) { logger.error(`Error polling UltraTemp heater - ${err}`); }
483
+ finally {
484
+ this.suspendPolling = false; if (!this.closing) this._pollTimer = setTimeout(async () => {
485
+ try { await self.pollEquipmentAsync() } catch (err) { }
486
+ }, this.pollingInterval || 10000);
487
+ }
488
+ }
489
+ public async setHeaterStateAsync(hstate: HeaterState, isOn: boolean, isCooling: boolean) {
490
+ try {
491
+ // Initialize the desired state.
492
+ this.isCooling = isCooling;
493
+ if (hstate.isOn !== isOn) {
494
+ logger.info(`Nixie: Set Heater ${hstate.id}-${hstate.name} to ${isCooling ? 'cooling' : isOn ? 'heating' : 'off'}`);
495
+
496
+ }
497
+ if (isOn && !hstate.startupDelay) this.lastHeatCycle = new Date();
498
+ this.isOn = hstate.isOn = isOn;
499
+ } catch (err) { return logger.error(`Nixie Error setting heater state ${hstate.id}-${hstate.name}: ${err.message}`); }
500
+ }
501
+ public async releaseHeater(sheater: HeaterState): Promise<boolean> {
502
+ try {
503
+ let out = Outbound.create({
504
+ portId: this.heater.portId || 0,
505
+ protocol: Protocol.Heater,
506
+ source: 16,
507
+ dest: this.heater.address,
508
+ action: 114,
509
+ payload: [],
510
+ retries: 3, // We are going to try 4 times.
511
+ response: Response.create({ protocol: Protocol.Heater, action: 115 }),
512
+ onAbort: () => { }
513
+ });
514
+ out.appendPayloadBytes(0, 10);
515
+ out.setPayloadByte(0, 144);
516
+ out.setPayloadByte(1, 0, 0);
517
+ await out.sendAsync();
518
+ return true;
519
+
520
+ } catch (err) {
521
+ // If the Ultratemp is not responding we need to store that off but at this point we know none of the codes. If a 115 does
522
+ // come across this will be cleared by the processing of that message.
523
+ sheater.commStatus = sys.board.valueMaps.equipmentCommStatus.getValue('commerr');
524
+ state.equipment.messages.setMessageByCode(`heater:${sheater.id}:comms`, 'error', `Communication error with ${sheater.name}`);
525
+ logger.error(`Communication error with Ultratemp : ${err.message}`);
526
+ return false;
527
+ }
528
+ }
529
+ public async setStatus(sheater: HeaterState): Promise<boolean> {
530
+ try {
531
+ let out = Outbound.create({
532
+ portId: this.heater.portId || 0,
533
+ protocol: Protocol.Heater,
534
+ source: 16,
535
+ dest: this.heater.address,
536
+ action: 114,
537
+ payload: [],
538
+ retries: 3, // We are going to try 4 times.
539
+ response: Response.create({ protocol: Protocol.Heater, action: 115 }),
540
+ onAbort: () => { }
541
+ });
542
+ out.appendPayloadBytes(0, 10);
543
+ out.setPayloadByte(0, 144);
544
+ // If we are in startup delay simply tell the heater that it is off.
545
+ if (sheater.startupDelay || this.closing)
546
+ out.setPayloadByte(1, 0, 0);
547
+ else {
548
+ if (this.isOn) {
549
+ if (!this.isCooling) this.lastHeatCycle = new Date();
550
+ else this.lastCoolCycle = new Date();
551
+ }
552
+ //console.log(`Setting the heater byte ${this.isOn} ${sheater.isOn} to ${this.isOn ? (this.isCooling ? 2 : 1) : 0}`);
553
+ out.setPayloadByte(1, this.isOn ? (this.isCooling ? 2 : 1) : 0, 0);
554
+ }
555
+ let success = await out.sendAsync();
556
+ return success;
557
+ } catch (err) {
558
+ // If the Ultratemp is not responding we need to store that off but at this point we know none of the codes. If a 115 does
559
+ // come across this will be cleared by the processing of that message.
560
+ sheater.commStatus = sys.board.valueMaps.equipmentCommStatus.getValue('commerr');
561
+ state.equipment.messages.setMessageByCode(`heater:${sheater.id}:comms`, 'error', `Communication error with ${sheater.name}`);
562
+ logger.error(`Communication error with Ultratemp : ${err.message}`);
563
+ return false;
564
+ }
565
+ }
566
+ public async closeAsync() {
567
+ try {
568
+ this.suspendPolling = true;
569
+ this.closing = true;
570
+ if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
571
+ this._pollTimer = null;
572
+ let sheater = state.heaters.getItemById(this.id);
573
+ await this.releaseHeater(sheater);
574
+ logger.info(`Closing Heater ${this.heater.name}`);
575
+ }
576
+ catch (err) { logger.error(`Ultratemp closeAsync: ${err.message}`); return Promise.reject(err); }
577
+ }
578
+ }
579
+ export class NixieMastertemp extends NixieGasHeater {
580
+ constructor(ncp: INixieControlPanel, heater: Heater) {
581
+ super(ncp, heater);
582
+ // Set the polling interval to 3 seconds.
583
+ this.pollEquipmentAsync();
584
+ this.pollingInterval = 3000;
585
+
586
+ }
587
+ /* public getCooldownTime(): number {
588
+ // Delays are always in terms of seconds so convert the minute to seconds.
589
+ if (this.heater.cooldownDelay === 0 || typeof this.lastHeatCycle === 'undefined') return 0;
590
+ let now = new Date().getTime();
591
+ let cooldown = this.isOn ? this.heater.cooldownDelay * 60000 : Math.round(((this.lastHeatCycle.getDate() + this.heater.cooldownDelay * 60000) - now) / 1000);
592
+ return Math.min(Math.max(0, cooldown), this.heater.cooldownDelay * 60);
593
+ } */
594
+ public async setHeaterStateAsync(hstate: HeaterState, isOn: boolean) {
595
+ try {
596
+ // Initialize the desired state.
597
+ this.isOn = isOn;
598
+ this.isCooling = false;
599
+ let target = hstate.startupDelay === false && isOn;
600
+ if (target && typeof hstate.endTime !== 'undefined') {
601
+ // Calculate a short cycle time so that the gas heater does not cycle
602
+ // too often. For gas heaters this is 60 seconds. This gives enough time
603
+ // for the heater control circuit to make a full cycle.
604
+ if (new Date().getTime() - hstate.endTime.getTime() < this.heater.minCycleTime * 60000) {
605
+ logger.verbose(`${hstate.name} short cycle detected deferring turn on state`);
606
+ target = false;
607
+ }
608
+ }
609
+ // Here we go we need to set the state.
610
+ if (hstate.isOn !== target) {
611
+ logger.info(`Nixie: Set Mastertemp ${hstate.id}-${hstate.name} to ${isOn}`);
612
+ }
613
+ if (typeof this._lastState === 'undefined' || target || this._lastState !== target) {
614
+ this._lastState = hstate.isOn = target;
615
+ if (target) this.lastHeatCycle = new Date();
616
+ }
617
+ hstate.isOn = isOn;
618
+ } catch (err) { return logger.error(`Nixie Error setting heater state ${hstate.id}-${hstate.name}: ${err.message}`); }
619
+ }
620
+ public async pollEquipmentAsync() {
621
+ let self = this;
622
+ try {
623
+ this.suspendPolling = true;
624
+ if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
625
+ this._pollTimer = null;
626
+ if (this._suspendPolling > 1) return;
627
+ let sheater = state.heaters.getItemById(this.heater.id, !this.closing);
628
+ if (!this.closing) await this.setStatus(sheater);
629
+ }
630
+ catch (err) { logger.error(`Error polling MasterTemp heater - ${err}`); }
631
+ finally {
632
+ this.suspendPolling = false; if (!this.closing) this._pollTimer = setTimeout(async () => {
633
+ try { await self.pollEquipmentAsync() } catch (err) { }
634
+ }, this.pollingInterval || 3000);
635
+ }
636
+ }
637
+ public async setStatus(sheater: HeaterState): Promise<boolean> {
638
+ try {
639
+ let out = Outbound.create({
640
+ portId: this.heater.portId || 0,
641
+ protocol: Protocol.Heater,
642
+ source: 16,
643
+ dest: this.heater.address,
644
+ action: 112,
645
+ payload: [],
646
+ retries: 3, // We are going to try 4 times.
647
+ response: Response.create({ protocol: Protocol.Heater, action: 116 }),
648
+ onAbort: () => { }
649
+ });
650
+ out.appendPayloadBytes(0, 11);
651
+ // If we have a startup delay we need to simply send 0 to the heater to make sure that it is off.
652
+ if (sheater.startupDelay)
653
+ out.setPayloadByte(0, 0);
654
+ else {
655
+ // The cooldown delay is a bit hard to figure out here since I think the heater does it on its own.
656
+ out.setPayloadByte(0, sheater.bodyId <= 2 ? sheater.bodyId : 0);
657
+ }
658
+ out.setPayloadByte(1, sys.bodies.getItemById(1).heatSetpoint || 0);
659
+ out.setPayloadByte(2, sys.bodies.getItemById(2).heatSetpoint || 0);
660
+ let success = await out.sendAsync();
661
+ return success;
662
+ } catch (err) {
663
+ // If the MasterTemp is not responding we need to store that off but at this point we know none of the codes. If a 115 does
664
+ // come across this will be cleared by the processing of that message.
665
+ sheater.commStatus = sys.board.valueMaps.equipmentCommStatus.getValue('commerr');
666
+ state.equipment.messages.setMessageByCode(`heater:${sheater.id}:comms`, 'error', `Communication error with ${sheater.name}`);
667
+ logger.error(`Communication error with MasterTemp : ${err.message}`);
668
+ return false;
669
+ }
670
+ }
671
+ public async setServiceModeAsync() {
672
+ let hstate = state.heaters.getItemById(this.heater.id);
673
+ await this.setHeaterStateAsync(hstate, false);
674
+ }
675
+ public async closeAsync() {
676
+ try {
677
+ this.suspendPolling = true;
678
+ this.closing = true;
679
+ if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
680
+ this._pollTimer = null;
681
+ logger.info(`Closing Heater ${this.heater.name}`);
682
+
683
+ }
684
+ catch (err) { logger.error(`MasterTemp closeAsync: ${err.message}`); return Promise.reject(err); }
685
+ }
686
+ }
687
+ export class NixieUltraTempETi extends NixieHeaterBase {
688
+ constructor(ncp: INixieControlPanel, heater: Heater) {
689
+ super(ncp, heater);
690
+ // Set the polling interval to 3 seconds.
691
+ this.pollEquipmentAsync();
692
+ }
693
+ public async pollEquipmentAsync() {
694
+ let self = this;
695
+ try {
696
+ this.suspendPolling = true;
697
+ if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
698
+ this._pollTimer = null;
699
+ if (this._suspendPolling > 1) return;
700
+ let sheater = state.heaters.getItemById(this.heater.id, !this.closing);
701
+ // If the body isn't on then we won't communicate with the chem controller. There is no need
702
+ // since most of the time these are attached to the filter relay.
703
+ if (!this.closing) {
704
+ await this.setStatus(sheater);
705
+ }
706
+ }
707
+ catch (err) { logger.error(`Error polling UltraTemp ETi heater - ${err}`); }
708
+ finally {
709
+ this.suspendPolling = false; if (!this.closing) this._pollTimer = setTimeout(async () => {
710
+ try { await self.pollEquipmentAsync() } catch (err) { }
711
+ }, this.pollingInterval || 10000);
712
+ }
713
+ }
714
+ public async setHeaterStateAsync(hstate: HeaterState, isOn: boolean, isCooling: boolean) {
715
+ try {
716
+ // Initialize the desired state.
717
+ this.isCooling = isCooling;
718
+ if (hstate.isOn !== isOn) {
719
+ logger.info(`Nixie: Set Heater ${hstate.id}-${hstate.name} to ${isCooling ? 'cooling' : isOn ? 'heating' : 'off'}`);
720
+
721
+ }
722
+ if (isOn && !hstate.startupDelay) this.lastHeatCycle = new Date();
723
+ this.isOn = hstate.isOn = isOn;
724
+ } catch (err) { return logger.error(`Nixie Error setting heater state ${hstate.id}-${hstate.name}: ${err.message}`); }
725
+ }
726
+ public async releaseHeater(sheater: HeaterState): Promise<boolean> {
727
+ try {
728
+ let out = Outbound.create({
729
+ portId: this.heater.portId || 0,
730
+ protocol: Protocol.Heater,
731
+ source: 16,
732
+ dest: this.heater.address,
733
+ action: 112,
734
+ payload: [],
735
+ retries: 3, // We are going to try 4 times.
736
+ response: Response.create({ protocol: Protocol.Heater, action: 113 }),
737
+ onAbort: () => { }
738
+ });
739
+ out.appendPayloadBytes(0, 10);
740
+ out.setPayloadByte(0, 0);
741
+ out.setPayloadByte(1, 0);
742
+ out.setPayloadByte(2, 78);
743
+ out.setPayloadByte(3, 1);
744
+ out.setPayloadByte(4, 5);
745
+ let success = await out.sendAsync();
746
+ return success;
747
+ } catch (err) {
748
+ // If the Ultratemp is not responding we need to store that off but at this point we know none of the codes. If a 113 does
749
+ // come across this will be cleared by the processing of that message.
750
+ sheater.commStatus = sys.board.valueMaps.equipmentCommStatus.getValue('commerr');
751
+ state.equipment.messages.setMessageByCode(`heater:${sheater.id}:comms`, 'error', `Communication error with ${sheater.name}`);
752
+ logger.error(`Communication error with Ultratemp : ${err.message}`);
753
+ return false;
754
+ }
755
+ }
756
+ protected calcHeatModeByte(body: Body): number {
757
+ let byte = 0;
758
+ if (this.closing) return 0; // We are closing so just set the heat mode to off.
759
+ let mode = sys.board.valueMaps.heatModes.transform(body.heatMode || 0);
760
+ switch (mode.name) {
761
+ case 'hpump':
762
+ case 'heatpump':
763
+ byte = 1;
764
+ break;
765
+ case 'heater':
766
+ byte = 2;
767
+ break;
768
+ case 'heatpumpref':
769
+ case 'heatpumppref':
770
+ case 'hybrid':
771
+ byte = 3;
772
+ break;
773
+ case 'dual':
774
+ byte = 4;
775
+ break;
776
+ }
777
+ return byte;
778
+ }
779
+ public async setServiceModeAsync() {
780
+ let hstate = state.heaters.getItemById(this.heater.id);
781
+ await this.setHeaterStateAsync(hstate, false, false);
782
+ await this.releaseHeater(hstate);
783
+ }
784
+ public async setStatus(sheater: HeaterState): Promise<boolean> {
785
+ try {
786
+ let out = Outbound.create({
787
+ portId: this.heater.portId || 0,
788
+ protocol: Protocol.Heater,
789
+ source: 16,
790
+ dest: this.heater.address,
791
+ action: 112,
792
+ payload: [],
793
+ retries: 3, // We are going to try 4 times.
794
+ response: Response.create({ protocol: Protocol.Heater, action: 113 }),
795
+ onAbort: () => { }
796
+ });
797
+ out.appendPayloadBytes(0, 10);
798
+ out.setPayloadByte(0, this.isOn && !sheater.startupDelay && !this.closing ? 1 : 0);
799
+ if (sheater.bodyId > 0) {
800
+ let body = sys.bodies.getItemById(sheater.bodyId);
801
+ out.setPayloadByte(1, this.calcHeatModeByte(body));
802
+ out.setPayloadByte(2, body.setPoint);
803
+ }
804
+ else out.setPayloadByte(2, utils.convert.temperature.convertUnits(78, 'F', sys.board.valueMaps.tempUnits.getName(state.temps.units) || 'F')); // Just set it to a valid setpoint and call it a day.
805
+ out.setPayloadByte(3, this.heater.economyTime, 1);
806
+ out.setPayloadByte(4, this.heater.maxBoostTemp, 5);
807
+ if (this.isOn) {
808
+ if (!this.isCooling) this.lastHeatCycle = new Date();
809
+ else this.lastCoolCycle = new Date();
810
+ }
811
+ let success = await out.sendAsync();
812
+ return success;
813
+ } catch (err) {
814
+ // If the Ultratemp ETi is not responding we need to store that off but at this point we know none of the codes. If a 113 does
815
+ // come across this will be cleared by the processing of that message.
816
+ sheater.commStatus = sys.board.valueMaps.equipmentCommStatus.getValue('commerr');
817
+ state.equipment.messages.setMessageByCode(`heater:${sheater.id}:comms`, 'error', `Communication error with ${sheater.name}`);
818
+ logger.error(`Communication error with Ultratemp ETi : ${err.message}`);
819
+ return false;
820
+ }
821
+ }
822
+ public async closeAsync() {
823
+ try {
824
+ this.suspendPolling = true;
825
+ this.closing = true;
826
+ if (typeof this._pollTimer !== 'undefined' || this._pollTimer) clearTimeout(this._pollTimer);
827
+ this._pollTimer = null;
828
+ let sheater = state.heaters.getItemById(this.id);
829
+ await this.releaseHeater(sheater);
830
+ logger.info(`Closing Heater ${this.heater.name}`);
831
+ }
832
+ catch (err) { logger.error(`Ultratemp closeAsync: ${err.message}`); return Promise.reject(err); }
833
+ }
834
+ }