nodejs-poolcontroller 7.6.1 → 8.0.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 (102) hide show
  1. package/.eslintrc.json +36 -45
  2. package/.github/ISSUE_TEMPLATE/1-bug-report.yml +84 -0
  3. package/.github/ISSUE_TEMPLATE/2-docs.md +12 -0
  4. package/.github/ISSUE_TEMPLATE/3-proposal.md +28 -0
  5. package/.github/ISSUE_TEMPLATE/config.yml +8 -0
  6. package/CONTRIBUTING.md +74 -74
  7. package/Changelog +242 -215
  8. package/Dockerfile +17 -17
  9. package/Gruntfile.js +40 -40
  10. package/LICENSE +661 -661
  11. package/README.md +195 -191
  12. package/anslq25/MessagesMock.ts +218 -0
  13. package/anslq25/boards/MockBoardFactory.ts +50 -0
  14. package/anslq25/boards/MockEasyTouchBoard.ts +696 -0
  15. package/anslq25/boards/MockSystemBoard.ts +217 -0
  16. package/anslq25/chemistry/MockChlorinator.ts +75 -0
  17. package/anslq25/pumps/MockPump.ts +84 -0
  18. package/app.ts +10 -14
  19. package/config/Config.ts +26 -8
  20. package/config/VersionCheck.ts +8 -4
  21. package/controller/Constants.ts +59 -25
  22. package/controller/Equipment.ts +2667 -2459
  23. package/controller/Errors.ts +181 -180
  24. package/controller/Lockouts.ts +534 -436
  25. package/controller/State.ts +596 -77
  26. package/controller/boards/AquaLinkBoard.ts +1003 -0
  27. package/controller/boards/BoardFactory.ts +53 -45
  28. package/controller/boards/EasyTouchBoard.ts +3079 -2653
  29. package/controller/boards/IntelliCenterBoard.ts +3821 -4230
  30. package/controller/boards/IntelliComBoard.ts +69 -63
  31. package/controller/boards/IntelliTouchBoard.ts +384 -241
  32. package/controller/boards/NixieBoard.ts +1871 -1675
  33. package/controller/boards/SunTouchBoard.ts +393 -0
  34. package/controller/boards/SystemBoard.ts +5244 -4697
  35. package/controller/comms/Comms.ts +905 -541
  36. package/controller/comms/ScreenLogic.ts +1663 -0
  37. package/controller/comms/messages/Messages.ts +382 -54
  38. package/controller/comms/messages/config/ChlorinatorMessage.ts +8 -4
  39. package/controller/comms/messages/config/CircuitGroupMessage.ts +5 -2
  40. package/controller/comms/messages/config/CircuitMessage.ts +82 -13
  41. package/controller/comms/messages/config/ConfigMessage.ts +3 -1
  42. package/controller/comms/messages/config/CoverMessage.ts +2 -1
  43. package/controller/comms/messages/config/CustomNameMessage.ts +31 -30
  44. package/controller/comms/messages/config/EquipmentMessage.ts +5 -1
  45. package/controller/comms/messages/config/ExternalMessage.ts +33 -3
  46. package/controller/comms/messages/config/FeatureMessage.ts +2 -1
  47. package/controller/comms/messages/config/GeneralMessage.ts +2 -1
  48. package/controller/comms/messages/config/HeaterMessage.ts +145 -11
  49. package/controller/comms/messages/config/IntellichemMessage.ts +2 -1
  50. package/controller/comms/messages/config/OptionsMessage.ts +16 -27
  51. package/controller/comms/messages/config/PumpMessage.ts +62 -47
  52. package/controller/comms/messages/config/RemoteMessage.ts +80 -13
  53. package/controller/comms/messages/config/ScheduleMessage.ts +390 -347
  54. package/controller/comms/messages/config/SecurityMessage.ts +2 -1
  55. package/controller/comms/messages/config/ValveMessage.ts +44 -27
  56. package/controller/comms/messages/status/ChlorinatorStateMessage.ts +44 -91
  57. package/controller/comms/messages/status/EquipmentStateMessage.ts +139 -30
  58. package/controller/comms/messages/status/HeaterStateMessage.ts +135 -86
  59. package/controller/comms/messages/status/IntelliChemStateMessage.ts +448 -445
  60. package/controller/comms/messages/status/IntelliValveStateMessage.ts +36 -35
  61. package/controller/comms/messages/status/PumpStateMessage.ts +92 -2
  62. package/controller/comms/messages/status/VersionMessage.ts +2 -1
  63. package/controller/nixie/Nixie.ts +173 -162
  64. package/controller/nixie/NixieEquipment.ts +104 -103
  65. package/controller/nixie/bodies/Body.ts +120 -120
  66. package/controller/nixie/bodies/Filter.ts +135 -135
  67. package/controller/nixie/chemistry/ChemController.ts +2682 -2498
  68. package/controller/nixie/chemistry/ChemDoser.ts +806 -0
  69. package/controller/nixie/chemistry/Chlorinator.ts +367 -314
  70. package/controller/nixie/circuits/Circuit.ts +402 -248
  71. package/controller/nixie/heaters/Heater.ts +815 -649
  72. package/controller/nixie/pumps/Pump.ts +934 -661
  73. package/controller/nixie/schedules/Schedule.ts +319 -257
  74. package/controller/nixie/valves/Valve.ts +170 -170
  75. package/defaultConfig.json +346 -286
  76. package/logger/DataLogger.ts +448 -448
  77. package/logger/Logger.ts +38 -9
  78. package/package.json +60 -56
  79. package/tsconfig.json +25 -25
  80. package/web/Server.ts +275 -117
  81. package/web/bindings/aqualinkD.json +560 -0
  82. package/web/bindings/homeassistant.json +437 -0
  83. package/web/bindings/influxDB.json +1066 -1021
  84. package/web/bindings/mqtt.json +721 -654
  85. package/web/bindings/mqttAlt.json +746 -684
  86. package/web/bindings/rulesManager.json +54 -54
  87. package/web/bindings/smartThings-Hubitat.json +31 -31
  88. package/web/bindings/valveRelays.json +20 -20
  89. package/web/bindings/vera.json +25 -25
  90. package/web/interfaces/baseInterface.ts +188 -136
  91. package/web/interfaces/httpInterface.ts +148 -124
  92. package/web/interfaces/influxInterface.ts +283 -245
  93. package/web/interfaces/mqttInterface.ts +695 -475
  94. package/web/interfaces/ruleInterface.ts +87 -0
  95. package/web/services/config/Config.ts +177 -49
  96. package/web/services/config/ConfigSocket.ts +2 -1
  97. package/web/services/state/State.ts +154 -3
  98. package/web/services/state/StateSocket.ts +69 -18
  99. package/web/services/utilities/Utilities.ts +232 -42
  100. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -52
  101. package/config copy.json +0 -300
  102. package/issue_template.md +0 -52
@@ -1,36 +1,37 @@
1
- /* nodejs-poolController. An application to control pool equipment.
2
- Copyright (C) 2016, 2017, 2018, 2019, 2020. Russell Goldin, tagyoureit. russ.goldin@gmail.com
3
-
4
- This program is free software: you can redistribute it and/or modify
5
- it under the terms of the GNU Affero General Public License as
6
- published by the Free Software Foundation, either version 3 of the
7
- License, or (at your option) any later version.
8
-
9
- This program is distributed in the hope that it will be useful,
10
- but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- GNU Affero General Public License for more details.
13
-
14
- You should have received a copy of the GNU Affero General Public License
15
- along with this program. If not, see <http://www.gnu.org/licenses/>.
16
- */
17
- import { Inbound } from "../Messages";
18
- import { state } from "../../../State";
19
- import { sys, ControllerType } from "../../../Equipment";
20
- import { logger } from "../../../../logger/Logger";
21
-
22
- export class IntelliValveStateMessage {
23
- public static process(msg: Inbound) {
24
- if (sys.controllerType === ControllerType.Unknown) return;
25
- // We only want to process the messages that are coming from IntelliValve.
26
- if (msg.source !== 12) return;
27
- switch (msg.action) {
28
- case 82: // This is hail from the valve that says it is not bound yet.
29
- break;
30
- default:
31
- logger.info(`IntelliValve sent an unknown action ${msg.action}`);
32
- break;
33
- }
34
- state.emitEquipmentChanges();
35
- }
1
+ /* nodejs-poolController. An application to control pool equipment.
2
+ Copyright (C) 2016, 2017, 2018, 2019, 2020, 2021, 2022.
3
+ Russell Goldin, tagyoureit. russ.goldin@gmail.com
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Affero General Public License as
7
+ published by the Free Software Foundation, either version 3 of the
8
+ License, or (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Affero General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Affero General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ import { Inbound } from "../Messages";
19
+ import { state } from "../../../State";
20
+ import { sys, ControllerType } from "../../../Equipment";
21
+ import { logger } from "../../../../logger/Logger";
22
+
23
+ export class IntelliValveStateMessage {
24
+ public static process(msg: Inbound) {
25
+ if (sys.controllerType === ControllerType.Unknown) return;
26
+ // We only want to process the messages that are coming from IntelliValve.
27
+ if (msg.source !== 12) return;
28
+ switch (msg.action) {
29
+ case 82: // This is hail from the valve that says it is not bound yet.
30
+ break;
31
+ default:
32
+ logger.info(`IntelliValve sent an unknown action ${msg.action}`);
33
+ break;
34
+ }
35
+ state.emitEquipmentChanges();
36
+ }
36
37
  }
@@ -1,5 +1,6 @@
1
1
  /* nodejs-poolController. An application to control pool equipment.
2
- Copyright (C) 2016, 2017, 2018, 2019, 2020. Russell Goldin, tagyoureit. russ.goldin@gmail.com
2
+ Copyright (C) 2016, 2017, 2018, 2019, 2020, 2021, 2022.
3
+ Russell Goldin, tagyoureit. russ.goldin@gmail.com
3
4
 
4
5
  This program is free software: you can redistribute it and/or modify
5
6
  it under the terms of the GNU Affero General Public License as
@@ -14,12 +15,51 @@ GNU Affero General Public License for more details.
14
15
  You should have received a copy of the GNU Affero General Public License
15
16
  along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
  */
17
- import { Inbound } from "../Messages";
18
+ import { Inbound, Outbound, Protocol } from "../Messages";
18
19
  import { state } from "../../../State";
19
20
  import { sys, ControllerType } from "../../../Equipment";
21
+ import { conn } from "../../Comms";
22
+ import { logger } from "../../../../logger/Logger";
23
+
20
24
  export class PumpStateMessage {
25
+ private static detectPumpType(msg: Inbound) {
26
+ let pumpType = -1;
27
+ switch (msg.action) {
28
+ case 1:
29
+ {
30
+ let speed = (msg.extractPayloadByte(2) * 256) + msg.extractPayloadByte(3);
31
+ if (speed > 0) {
32
+ pumpType = speed < 300 ? sys.board.valueMaps.pumpTypes.getValue('vf') : sys.board.valueMaps.pumpTypes.getValue('vs');
33
+ }
34
+ }
35
+ break;
36
+ case 9:
37
+ case 10:
38
+ pumpType = sys.board.valueMaps.pumpTypes.getValue('vsf');
39
+ break;
40
+ }
41
+ if (pumpType > 0) {
42
+ let pump = sys.pumps.find(x => x.address === msg.dest);
43
+ if (typeof pump === 'undefined') {
44
+ let id = sys.pumps.filter(elem => elem.master === 0).getMaxId(false, 0) + 1;
45
+ pump = sys.pumps.getItemById(id, true);
46
+ pump.name = `Pump ${msg.dest - 95}`;
47
+ pump.address = msg.dest;
48
+ pump.isActive = true;
49
+ pump.type = pumpType;
50
+ pump.master = 0;
51
+ }
52
+ let spump = state.pumps.getItemById(pump.id, true);
53
+ spump.address = pump.address;
54
+ spump.type = pump.type;
55
+ spump.isActive = pump.isActive;
56
+ spump.name = pump.name;
57
+ spump.type = pump.type;
58
+ }
59
+ }
21
60
  public static process(msg: Inbound) {
22
61
  if (sys.controllerType === ControllerType.Unknown) return;
62
+ if (msg.dest >= 96 && sys.controllerType === ControllerType.SunTouch) PumpStateMessage.detectPumpType(msg);
23
63
 
24
64
  // We only want to process the messages that are coming from the pump not to the pump. At some point
25
65
  // this filter was removed. Any messages that are coming from the panel are simply requests to the pump
@@ -33,6 +73,29 @@ export class PumpStateMessage {
33
73
  let ptype = sys.board.valueMaps.pumpTypes.transform(pumpCfg.type);
34
74
  let pump = state.pumps.getItemById(pumpId, pumpCfg.isActive === true);
35
75
  switch (msg.action) {
76
+ case 1:
77
+ if (msg.source === 96 && sys.controllerType === ControllerType.EasyTouch) {
78
+ if (sys.equipment.modules.getItemByIndex(0, false).type >= 128) {
79
+ // EasyTouch Version 1 controllers do not request the current information about rpms or wattage from the pump. We need to ask in
80
+ // its stead.
81
+ let out = Outbound.create({
82
+ portId: pumpCfg.portId || 0,
83
+ protocol: Protocol.Pump,
84
+ dest: pumpCfg.address,
85
+ action: 7,
86
+ payload: [],
87
+ retries: 1,
88
+ response: true,
89
+ onComplete: (err, _) => {
90
+ if (err) {
91
+ logger.error(`EasyTouch 1 request pump status failed for ${pump.name}: ${err.message}`);
92
+ }
93
+ }
94
+ });
95
+ conn.queueSendMessage(out);
96
+ }
97
+ }
98
+ break;
36
99
  case 7:
37
100
  //[165, 63, 15, 16, 2, 29][11, 47, 32, 0, 0, 0, 0, 0, 0, 32, 0, 0, 2, 0, 59, 59, 0, 241, 56, 121, 24, 246, 0, 0, 0, 0, 0, 23, 0][4, 219]
38
101
  //[165, 0, 96, 16, 1, 4][2, 196, 7, 58][2, 33]
@@ -55,7 +118,34 @@ export class PumpStateMessage {
55
118
  state.emitEquipmentChanges();
56
119
  // if (msg.action !== 7) this.processDirectPumpMessages(msg);
57
120
  }
121
+ public static processHayward(msg: Inbound) {
122
+ switch (msg.action) {
123
+ case 12: // This is a pump status message
124
+ PumpStateMessage.processHaywardStar(msg);
125
+ break;
126
+ }
127
+ }
128
+ public static processHaywardStar(msg: Inbound) {
129
+ // src act dest
130
+ //[0x10, 0x02, 0x00, 0x0C, 0x00][0x00, 0x62, 0x17, 0x81][0x01, 0x18, 0x10, 0x03]
131
+ //[0x10, 0x02, 0x00, 0x0C, 0x00][0x00, 0x2D, 0x02, 0x36][0x00, 0x83, 0x10, 0x03] -- Response from pump
132
+ let ptype = sys.board.valueMaps.pumpTypes.transformByName('hwvs');
133
+ let address = msg.source + 96;
134
+ //console.log({ src: msg.source, dest: msg.dest, action: msg.action, address: address });
58
135
 
136
+ let pump = sys.pumps.find(elem => elem.address === address && elem.type === 6);
137
+ if (typeof pump !== 'undefined') {
138
+ let pstate = state.pumps.getItemById(pump.id, true);
139
+ // 3450 * .5
140
+ pstate.rpm = Math.round(ptype.maxSpeed * (msg.extractPayloadByte(1) / 100));
141
+ // This is really goofy as the watts are actually the hex string from the two bytes.
142
+ pstate.watts = parseInt(msg.extractPayloadByte(2).toString(16) + msg.extractPayloadByte(3).toString(16), 10);
143
+ pstate.isActive = true;
144
+ pstate.command = (pstate.rpm > 0 || pstate.watts > 0) ? 10 : 0;
145
+ pstate.driveState
146
+ state.emitEquipmentChanges();
147
+ }
148
+ }
59
149
  public static processDirectPumpMessages(msg: Inbound) {
60
150
 
61
151
  if (msg.payload.length === 2)
@@ -1,5 +1,6 @@
1
1
  /* nodejs-poolController. An application to control pool equipment.
2
- Copyright (C) 2016, 2017, 2018, 2019, 2020. Russell Goldin, tagyoureit. russ.goldin@gmail.com
2
+ Copyright (C) 2016, 2017, 2018, 2019, 2020, 2021, 2022.
3
+ Russell Goldin, tagyoureit. russ.goldin@gmail.com
3
4
 
4
5
  This program is free software: you can redistribute it and/or modify
5
6
  it under the terms of the GNU Affero General Public License as
@@ -1,163 +1,174 @@
1
- import * as path from 'path';
2
- import * as fs from 'fs';
3
- import * as os from 'os';
4
- import { webApp } from "../../web/Server";
5
- import { logger } from "../../logger/Logger";
6
- import { INixieControlPanel } from "./NixieEquipment";
7
- import { NixieChemControllerCollection } from "./chemistry/ChemController";
8
-
9
- import { sys, PoolSystem } from "../../controller/Equipment";
10
- import { NixieCircuitCollection } from './circuits/Circuit';
11
- import { NixieBodyCollection } from './bodies/Body';
12
- import { NixieValveCollection } from './valves/Valve';
13
- import { NixieHeaterCollection } from './heaters/Heater';
14
- import { config } from '../../config/Config';
15
- import { NixieFilterCollection } from './bodies/Filter';
16
- import { NixieChlorinatorCollection } from './chemistry/Chlorinator';
17
- import { NixiePump, NixiePumpCollection } from './pumps/Pump';
18
- import { NixieScheduleCollection } from './schedules/Schedule';
19
-
20
- /************************************************************************
21
- * Nixie: Nixie is a control panel that controls devices as a master. It
22
- * can extend an existing *Touch or Center control panel. Or it can run
23
- * standalone as a master controller. The NixieControlPanel will always
24
- * be instantiated to control equipment when that equipment is not
25
- * supported or controlled by a traditional OCP. However, if a traditional
26
- * OCP exists, it will be subordinate to that OCP in regard to controlling
27
- * pool bodies.
28
- *
29
- * Equipment: Equipment identified as ncp (Nixie Control Panel) will be
30
- * managed by the Nixie controller. It works hand-in-glove with the REM
31
- * (Relay Equipment Manager) to provide low level hardware support.
32
- *
33
- * RS485: RS485 occurs directly within nixie through the standard RS485
34
- * bus identified for njspc and does not route through REM. This is an
35
- * important distinction to provide robust hardware level communication.
36
- * Items Controlled on this channel include: Pumps, Chlorinators, and
37
- * IntelliChem controllers. When and if IntelliValve functions become
38
- * available these too will be performed through the njspc RS485 bus.
39
- *
40
- * Configuration and State Management:
41
- * Nixie uses the underlying njspc configuration structures. The intent
42
- * is not to replace these rather the intention is to extend them with
43
- * low level control. For this reason, only when a Nixie is the only
44
- * master controller on the pool do commands get routed through NixieBoard.
45
- * this mode is identified at startup. When a particular piece of equipment
46
- * is identified to be controlled by Nixie as a master then the command
47
- * will eventually be marshalled to the NixieControlPanel. Any connected
48
- * OCP will get first crack at it.
49
- *
50
- * LifeCycle: The Nixie controller does not persist data outside of
51
- * the PoolConfig.json and PoolState.json files. It is initialized
52
- * at startup and torn down when the application is stopped.
53
- * */
54
- export class NixieControlPanel implements INixieControlPanel {
55
- // Only equipment controlled by Nixie is represented on the controller. If interaction with
56
- // other equipment is required this should be sent back through the original controller.
57
- // Command sequence is <OCP>Board -> SystemBoard -> NixieController whenever the master is not identified as Nixie.
58
- chemControllers: NixieChemControllerCollection = new NixieChemControllerCollection(this);
59
- chlorinators: NixieChlorinatorCollection = new NixieChlorinatorCollection(this);
60
- circuits: NixieCircuitCollection = new NixieCircuitCollection(this);
61
- bodies: NixieBodyCollection = new NixieBodyCollection(this);
62
- filters: NixieFilterCollection = new NixieFilterCollection(this);
63
- valves: NixieValveCollection = new NixieValveCollection(this);
64
- heaters: NixieHeaterCollection = new NixieHeaterCollection(this);
65
- pumps: NixiePumpCollection = new NixiePumpCollection(this);
66
- schedules: NixieScheduleCollection = new NixieScheduleCollection(this);
67
-
68
- public async initAsync(equipment: PoolSystem) {
69
- try {
70
-
71
- // We need to tell Nixie what her place is. If there is an existing OCP she needs to be a partner. However, if
72
- // she is the only master then she needs to step up and take command. The way we will signify this is
73
- // by using settings in config.json.
74
- // The controller types define the number of bodies and whether they are shared.
75
- logger.info(`Initializing Nixie Controller`);
76
- await this.bodies.initAsync(equipment.bodies);
77
- await this.filters.initAsync(equipment.filters);
78
- await this.circuits.initAsync(equipment.circuits);
79
- await this.valves.initAsync(equipment.valves);
80
- await this.heaters.initAsync(equipment.heaters);
81
- await this.chlorinators.initAsync(equipment.chlorinators);
82
- await this.chemControllers.initAsync(equipment.chemControllers);
83
- await this.pumps.initAsync(equipment.pumps);
84
- await this.schedules.initAsync(equipment.schedules);
85
- logger.info(`Nixie Controller Initialized`)
86
- }
87
- catch (err) { return Promise.reject(err); }
88
- }
89
- public async readLogFile(logFile: string): Promise<string[]> {
90
- try {
91
- let logPath = path.join(process.cwd(), '/logs');
92
- if (!fs.existsSync(logPath)) fs.mkdirSync(logPath);
93
- logPath += (`/${logFile}`);
94
- let lines = [];
95
- if (fs.existsSync(logPath)) {
96
- let buff = fs.readFileSync(logPath);
97
- lines = buff.toString().split('\n');
98
- }
99
- return lines;
100
- } catch (err) { logger.error(err); }
101
- }
102
- public async logData(logFile: string, data: any) {
103
- try {
104
- let logPath = path.join(process.cwd(), '/logs');
105
- if (!fs.existsSync(logPath)) fs.mkdirSync(logPath);
106
- logPath += (`/${logFile}`);
107
- let lines = [];
108
- if (fs.existsSync(logPath)) {
109
- let buff = fs.readFileSync(logPath);
110
- lines = buff.toString().split('\n');
111
- }
112
- if (typeof data === 'object')
113
- lines.unshift(JSON.stringify(data));
114
- else
115
- lines.unshift(data.toString());
116
- fs.writeFileSync(logPath, lines.join('\n'));
117
- } catch (err) { logger.error(err); }
118
- }
119
- public async closeAsync() {
120
- // Close all the associated equipment.
121
- await this.pumps.closeAsync();
122
- await this.chemControllers.closeAsync();
123
- await this.chlorinators.closeAsync();
124
- await this.heaters.closeAsync();
125
- await this.circuits.closeAsync();
126
- await this.filters.closeAsync();
127
- await this.bodies.closeAsync();
128
- await this.valves.closeAsync();
129
- }
130
- /*
131
- * This method is used to obtain a list of existing REM servers for configuration. This returns all servers and
132
- * their potential devices and is not designed to be used at run-time or to detect failure.
133
- *
134
- */
135
- public async getREMServers() {
136
- try {
137
- let srv = [];
138
- let servers = webApp.findServersByType('rem');
139
- if (typeof servers !== 'undefined') {
140
- for (let i = 0; i < servers.length; i++) {
141
- let server = servers[i];
142
- // Sometimes I hate type safety.
143
- let devices = typeof server['getDevices'] === 'function' ? await server['getDevices']() : [];
144
- let int = config.getInterfaceByUuid(servers[i].uuid);
145
- srv.push({
146
- uuid: servers[i].uuid,
147
- name: servers[i].name,
148
- type: servers[i].type,
149
- isRunning: servers[i].isRunning,
150
- isConnected: servers[i].isConnected,
151
- devices: devices,
152
- remoteConnectionId: servers[i].remoteConnectionId,
153
- interface: int
154
- });
155
- }
156
- await ncp.chemControllers.syncRemoteREMFeeds(srv);
157
- }
158
- return srv;
159
- } catch (err) { logger.error(err); }
160
- }
161
- }
162
-
1
+ import * as path from 'path';
2
+ import * as fs from 'fs';
3
+ import * as os from 'os';
4
+ import { webApp } from "../../web/Server";
5
+ import { logger } from "../../logger/Logger";
6
+ import { INixieControlPanel } from "./NixieEquipment";
7
+ import { NixieChemControllerCollection } from "./chemistry/ChemController";
8
+ import { NixieChemDoserCollection } from "./chemistry/ChemDoser";
9
+
10
+ import { sys, PoolSystem } from "../../controller/Equipment";
11
+ import { NixieCircuitCollection } from './circuits/Circuit';
12
+ import { NixieBodyCollection } from './bodies/Body';
13
+ import { NixieValveCollection } from './valves/Valve';
14
+ import { NixieHeaterCollection } from './heaters/Heater';
15
+ import { config } from '../../config/Config';
16
+ import { NixieFilterCollection } from './bodies/Filter';
17
+ import { NixieChlorinatorCollection } from './chemistry/Chlorinator';
18
+ import { NixiePump, NixiePumpCollection } from './pumps/Pump';
19
+ import { NixieScheduleCollection } from './schedules/Schedule';
20
+
21
+ /************************************************************************
22
+ * Nixie: Nixie is a control panel that controls devices as a master. It
23
+ * can extend an existing *Touch or Center control panel. Or it can run
24
+ * standalone as a master controller. The NixieControlPanel will always
25
+ * be instantiated to control equipment when that equipment is not
26
+ * supported or controlled by a traditional OCP. However, if a traditional
27
+ * OCP exists, it will be subordinate to that OCP in regard to controlling
28
+ * pool bodies.
29
+ *
30
+ * Equipment: Equipment identified as ncp (Nixie Control Panel) will be
31
+ * managed by the Nixie controller. It works hand-in-glove with the REM
32
+ * (Relay Equipment Manager) to provide low level hardware support.
33
+ *
34
+ * RS485: RS485 occurs directly within nixie through the standard RS485
35
+ * bus identified for njspc and does not route through REM. This is an
36
+ * important distinction to provide robust hardware level communication.
37
+ * Items Controlled on this channel include: Pumps, Chlorinators, and
38
+ * IntelliChem controllers. When and if IntelliValve functions become
39
+ * available these too will be performed through the njspc RS485 bus.
40
+ *
41
+ * Configuration and State Management:
42
+ * Nixie uses the underlying njspc configuration structures. The intent
43
+ * is not to replace these rather the intention is to extend them with
44
+ * low level control. For this reason, only when a Nixie is the only
45
+ * master controller on the pool do commands get routed through NixieBoard.
46
+ * this mode is identified at startup. When a particular piece of equipment
47
+ * is identified to be controlled by Nixie as a master then the command
48
+ * will eventually be marshalled to the NixieControlPanel. Any connected
49
+ * OCP will get first crack at it.
50
+ *
51
+ * LifeCycle: The Nixie controller does not persist data outside of
52
+ * the PoolConfig.json and PoolState.json files. It is initialized
53
+ * at startup and torn down when the application is stopped.
54
+ * */
55
+ export class NixieControlPanel implements INixieControlPanel {
56
+ // Only equipment controlled by Nixie is represented on the controller. If interaction with
57
+ // other equipment is required this should be sent back through the original controller.
58
+ // Command sequence is <OCP>Board -> SystemBoard -> NixieController whenever the master is not identified as Nixie.
59
+ chemDosers: NixieChemDoserCollection = new NixieChemDoserCollection(this);
60
+ chemControllers: NixieChemControllerCollection = new NixieChemControllerCollection(this);
61
+ chlorinators: NixieChlorinatorCollection = new NixieChlorinatorCollection(this);
62
+ circuits: NixieCircuitCollection = new NixieCircuitCollection(this);
63
+ bodies: NixieBodyCollection = new NixieBodyCollection(this);
64
+ filters: NixieFilterCollection = new NixieFilterCollection(this);
65
+ valves: NixieValveCollection = new NixieValveCollection(this);
66
+ heaters: NixieHeaterCollection = new NixieHeaterCollection(this);
67
+ pumps: NixiePumpCollection = new NixiePumpCollection(this);
68
+ schedules: NixieScheduleCollection = new NixieScheduleCollection(this);
69
+ public async setServiceModeAsync() {
70
+ await this.circuits.setServiceModeAsync();
71
+ await this.heaters.setServiceModeAsync();
72
+ await this.chlorinators.setServiceModeAsync();
73
+ await this.chemControllers.setServiceModeAsync();
74
+ await this.chemDosers.setServiceModeAsync();
75
+ await this.pumps.setServiceModeAsync();
76
+ }
77
+ public async initAsync(equipment: PoolSystem) {
78
+ try {
79
+
80
+ // We need to tell Nixie what her place is. If there is an existing OCP she needs to be a partner. However, if
81
+ // she is the only master then she needs to step up and take command. The way we will signify this is
82
+ // by using settings in config.json.
83
+ // The controller types define the number of bodies and whether they are shared.
84
+ logger.info(`Initializing Nixie Controller`);
85
+ await this.bodies.initAsync(equipment.bodies);
86
+ await this.filters.initAsync(equipment.filters);
87
+ await this.circuits.initAsync(equipment.circuits);
88
+ await this.valves.initAsync(equipment.valves);
89
+ await this.heaters.initAsync(equipment.heaters);
90
+ await this.chlorinators.initAsync(equipment.chlorinators);
91
+ await this.chemControllers.initAsync(equipment.chemControllers);
92
+ await this.chemDosers.initAsync(equipment.chemDosers);
93
+ await this.pumps.initAsync(equipment.pumps);
94
+ await this.schedules.initAsync(equipment.schedules);
95
+ logger.info(`Nixie Controller Initialized`)
96
+ }
97
+ catch (err) { return Promise.reject(err); }
98
+ }
99
+ public async readLogFile(logFile: string): Promise<string[]> {
100
+ try {
101
+ let logPath = path.join(process.cwd(), '/logs');
102
+ if (!fs.existsSync(logPath)) fs.mkdirSync(logPath);
103
+ logPath += (`/${logFile}`);
104
+ let lines = [];
105
+ if (fs.existsSync(logPath)) {
106
+ let buff = fs.readFileSync(logPath);
107
+ lines = buff.toString().split('\n');
108
+ }
109
+ return lines;
110
+ } catch (err) { logger.error(`Error reading log file ${logFile}: ${err.message}`); }
111
+ }
112
+ public async logData(logFile: string, data: any) {
113
+ try {
114
+ let logPath = path.join(process.cwd(), '/logs');
115
+ if (!fs.existsSync(logPath)) fs.mkdirSync(logPath);
116
+ logPath += (`/${logFile}`);
117
+ let lines = [];
118
+ if (fs.existsSync(logPath)) {
119
+ let buff = fs.readFileSync(logPath);
120
+ lines = buff.toString().split('\n');
121
+ }
122
+ if (typeof data === 'object')
123
+ lines.unshift(JSON.stringify(data));
124
+ else
125
+ lines.unshift(data.toString());
126
+ fs.writeFileSync(logPath, lines.join('\n'));
127
+ } catch (err) { logger.error(`Error logging to ${logFile}: ${err.message}`); }
128
+ }
129
+ public async closeAsync() {
130
+ // Close all the associated equipment.
131
+ await this.chemDosers.closeAsync();
132
+ await this.chemControllers.closeAsync();
133
+ await this.chlorinators.closeAsync();
134
+ await this.heaters.closeAsync();
135
+ await this.circuits.closeAsync();
136
+ await this.pumps.closeAsync();
137
+ await this.filters.closeAsync();
138
+ await this.bodies.closeAsync();
139
+ await this.valves.closeAsync();
140
+ }
141
+ /*
142
+ * This method is used to obtain a list of existing REM servers for configuration. This returns all servers and
143
+ * their potential devices and is not designed to be used at run-time or to detect failure.
144
+ *
145
+ */
146
+ public async getREMServers() {
147
+ try {
148
+ let srv = [];
149
+ let servers = webApp.findServersByType('rem');
150
+ if (typeof servers !== 'undefined') {
151
+ for (let i = 0; i < servers.length; i++) {
152
+ let server = servers[i];
153
+ // Sometimes I hate type safety.
154
+ let devices = typeof server['getDevices'] === 'function' ? await server['getDevices']() : [];
155
+ let int = config.getInterfaceByUuid(servers[i].uuid);
156
+ srv.push({
157
+ uuid: servers[i].uuid,
158
+ name: servers[i].name,
159
+ type: servers[i].type,
160
+ isRunning: servers[i].isRunning,
161
+ isConnected: servers[i].isConnected,
162
+ devices: devices,
163
+ remoteConnectionId: servers[i].remoteConnectionId,
164
+ interface: int
165
+ });
166
+ }
167
+ await ncp.chemControllers.syncRemoteREMFeeds(srv);
168
+ }
169
+ return srv;
170
+ } catch (err) { logger.error(`Error gettting REM Servers: ${err.message}`); }
171
+ }
172
+ }
173
+
163
174
  export let ncp = new NixieControlPanel();