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
File without changes
File without changes
File without changes
@@ -1,233 +1,233 @@
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 * as path from "path";
19
- import * as fs from "fs";
20
- import * as multer from 'multer';
21
- import * as express from 'express';
22
- import { SsdpServer} from '../../Server';
23
- import { state } from "../../../controller/State";
24
- import { sys } from "../../../controller/Equipment";
25
- import { webApp } from "../../Server";
26
- import { config } from "../../../config/Config";
27
- import { logger } from "../../../logger/Logger";
28
- import { ServiceParameterError, ServiceProcessError } from "../../../controller/Errors";
29
- import { BindingsFile } from "../../interfaces/baseInterface";
30
- import { Utils, utils } from "../../../controller/Constants";
31
- const extend = require("extend");
32
- export class UtilitiesRoute {
33
-
34
- public static initRoutes(app: express.Application) {
35
- app.use('/upnp.xml', async (req, res, next) => {
36
- try {
37
- // Put together the upnp device description.
38
- let ssdp = webApp.findServer('ssdp') as SsdpServer;
39
- if (typeof ssdp === 'undefined') throw new Error(`SSDP Server not initialized. No upnp information available.`);
40
- res.status(200).set('Content-Type', 'text/xml').send(ssdp.deviceXML());
41
- } catch (err) { next(err); }
42
- });
43
- app.get('/extended/:section', (req, res) => {
44
- let cfg = sys.getSection(req.params.section);
45
- let st = state.getState(req.params.section);
46
- let arr = [];
47
- for (let i = 0; i < cfg.length; i++){
48
- let p = extend(true, {}, cfg[i], st.find(s => s.id === cfg[i].id));
49
- arr.push(p);
50
- }
51
- return res.status(200).send(arr);
52
- });
53
- app.put('/app/interfaces/add', async (req, res, next) => {
54
- try {
55
- let faces = config.getSection('web.interfaces');
56
- let opts: any = {};
57
- switch (req.body.type) {
58
- case 'rule':
59
- opts = {};
60
- break;
61
- case 'rem':
62
- opts = {
63
- options: { protocol: 'http://', host: '', port: 8080, headers: { "content-type": "application/json" } },
64
- socket: {
65
- transports: ['websocket'], allowEIO3: true, upgrade: false,
66
- reconnectionDelay: 2000, reconnection: true, reconnectionDelayMax: 20000
67
- }
68
- }
69
- break;
70
- case 'http':
71
- case 'rest':
72
- opts = {
73
- options: { protocol: 'http://', host: '', port: 80 }
74
- }
75
- break;
76
- case 'influx':
77
- case 'influxdb':
78
- opts = {
79
- options: {
80
- version: 1,
81
- protocol: 'http',
82
- database: 'pool',
83
- port: 8601,
84
- retentionPolicy: 'autogen'
85
- }
86
- }
87
- break;
88
- case 'influxdb2':
89
- opts = {
90
- options: {
91
- version: 2,
92
- protocol: 'http',
93
- port: 9999,
94
- database: 'pool',
95
- bucket: '57ec4eed2d90a50b',
96
- token: '...LuyM84JJx93Qvc7tfaXPbI_mFFjRBjaA==',
97
- org: 'njsPC-org'
98
- }
99
- }
100
- break;
101
- case 'mqtt':
102
- opts = {
103
- options: {
104
- protocol: 'mqtt://', host: '', port: 1883, username: '', password: '',
105
- selfSignedCertificate: false,
106
- rootTopic: "pool/@bind=(state.equipment.model).replace(/ /g,'-').replace(' / ','').toLowerCase();",
107
- retain: true, qos: 0, changesOnly: true
108
- }
109
- }
110
- break;
111
- default:
112
- return Promise.reject(new ServiceParameterError(`An invalid type was specified ${req.body.type}`, 'PUT: /app/interfaces/add', 'type', req.body.type));
113
- }
114
- opts.uuid = utils.uuid();
115
- opts.isCustom = true;
116
- // Now lets create a name for the element. This would have been much easier if it were an array but alas we are stuck.
117
- let name = req.body.name.replace(/^[^a-zA-Z_$]|[^0-9a-zA-Z_$]/g, '_');
118
- if (name.length === 0) return Promise.reject(new ServiceParameterError(`An invalid name was specified ${req.body.name}`, 'PUT: /app/interfaces/add', 'name', req.body.name));
119
- if (name.charAt(0) >= '0' && name.charAt(0) <= '9') name = 'i' + name;
120
- let fnEnsureUnique = (name, ord?: number): string => {
121
- let isUnique = true;
122
- for (let fname in faces) {
123
- if (fname === (typeof ord !== 'undefined' ? `${name}_${ord}` : name)) {
124
- isUnique = false;
125
- break;
126
- }
127
- }
128
- if (!isUnique) name = fnEnsureUnique(name, typeof ord === 'undefined' ? 0 : ord++);
129
- return typeof ord !== 'undefined' ? `${name}_${ord}` : name;
130
- }
131
- name = fnEnsureUnique(name);
132
- opts = extend(true, {}, opts, req.body);
133
- config.setSection(`web.interfaces.${name}`, opts);
134
- res.status(200).send({ id: name, opts: opts });
135
- } catch (err) { next(err); }
136
- });
137
- app.delete('/app/interface', async (req, res, next) => {
138
- try {
139
- let faces = config.getSection('web.interfaces');
140
- let deleted;
141
- for (let fname in faces) {
142
- let iface = faces[fname];
143
- if (typeof req.body.id !== 'undefined') {
144
- if (fname === req.body.id) {
145
- deleted = iface;
146
- iface.enabled = false;
147
- await webApp.updateServerInterface(iface);
148
- config.removeSection(`web.interfaces.${fname}`);
149
- }
150
- }
151
- else if (typeof req.body.uuid !== 'undefined') {
152
- if (req.body.uuid.toLowerCase() === iface.uuid.toLowerCase()) {
153
- deleted = iface;
154
- iface.enabled = false;
155
- await webApp.updateServerInterface(iface);
156
- config.removeSection(`web.interfaces.${fname}`);
157
- }
158
- }
159
- }
160
- return res.status(200).send(deleted);
161
- } catch (err) { next(err); }
162
- });
163
- app.get('/app/options/interfaces', async (req, res, next) => {
164
- try {
165
- // todo: move bytevaluemaps out to a proper location; add additional definitions
166
- let opts = {
167
- interfaces: config.getSection('web.interfaces'),
168
- types: [
169
- { name: 'rule', desc: 'Rule', hasBindings: true, hasUrl: false },
170
- { name: 'rest', desc: 'Rest', hasBindings: true },
171
- { name: 'http', desc: 'Http', hasBindings: true },
172
- { name: 'rem', desc: 'Relay Equipment Manager', hasBindings: false },
173
- { name: 'mqtt', desc: 'MQTT', hasBindings: true },
174
- { name: 'influx', desc: 'InfluxDB', hasBindings: true },
175
- { name: 'influxdb2', desc: 'InfluxDB2', hasBindings: true}
176
- ],
177
- protocols: [
178
- { val: 0, name: 'http://', desc: 'http://' },
179
- { val: 1, name: 'https://', desc: 'https://' },
180
- { val: 2, name: 'mqtt://', desc: 'mqtt://' }
181
- ],
182
- files: []
183
- }
184
- // Read all the files in the custom bindings directory.
185
- let cpath = path.posix.join(process.cwd(), '/web/bindings/custom/');
186
- let files = fs.readdirSync(cpath);
187
- for (let i = 0; i < files.length; i++) {
188
- if (path.extname(files[i]) === '.json') {
189
- let bf = await BindingsFile.fromFile(path.posix.join(process.cwd(), 'web/bindings/'), `custom/${files[i]}`);
190
- if (typeof bf !== 'undefined') opts.files.push(bf.options);
191
- }
192
- }
193
- return res.status(200).send(opts);
194
- } catch (err) { next(err); }
195
- });
196
- app.post('/app/interfaceBindings/file', async (req, res, next) => {
197
- try {
198
- let file = multer({
199
- limits: { fileSize: 1000000 },
200
- storage: multer.memoryStorage()
201
- }).single('bindingsFile');
202
- file(req, res, async (err) => {
203
- try {
204
- if (err) { next(err); }
205
- else {
206
- // Validate the incoming data and save it off only if it is valid.
207
- let bf = await BindingsFile.fromBuffer(req.file.originalname, req.file.buffer);
208
- if (typeof bf === 'undefined') {
209
- err = new ServiceProcessError(`Invalid bindings file: ${req.file.originalname}`, 'POST: app/bindings/file', 'extractBindingOptions');
210
- next(err);
211
- }
212
- else {
213
- if (fs.existsSync(bf.filePath))
214
- return next(new ServiceProcessError(`File already exists ${req.file.originalname}`, 'POST: app/bindings/file', 'writeFile'));
215
- else {
216
- try {
217
- fs.writeFileSync(bf.filePath, req.file.buffer);
218
- } catch (e) { logger.error(`Error writing bindings file ${e.message}`); }
219
- }
220
- return res.status(200).send(bf);
221
- }
222
- }
223
- } catch (e) {
224
- err = new ServiceProcessError(`Error uploading file: ${e.message}`, 'POST: app/backup/file', 'uploadFile');
225
- next(err);
226
- logger.error(`File upload error: ${e.message}`);
227
- }
228
- });
229
- } catch (err) { next(err); }
230
- });
231
-
232
- }
233
- }
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 * as path from "path";
19
+ import * as fs from "fs";
20
+ import * as multer from 'multer';
21
+ import * as express from 'express';
22
+ import { SsdpServer} from '../../Server';
23
+ import { state } from "../../../controller/State";
24
+ import { sys } from "../../../controller/Equipment";
25
+ import { webApp } from "../../Server";
26
+ import { config } from "../../../config/Config";
27
+ import { logger } from "../../../logger/Logger";
28
+ import { ServiceParameterError, ServiceProcessError } from "../../../controller/Errors";
29
+ import { BindingsFile } from "../../interfaces/baseInterface";
30
+ import { Utils, utils } from "../../../controller/Constants";
31
+ const extend = require("extend");
32
+ export class UtilitiesRoute {
33
+
34
+ public static initRoutes(app: express.Application) {
35
+ app.use('/upnp.xml', async (req, res, next) => {
36
+ try {
37
+ // Put together the upnp device description.
38
+ let ssdp = webApp.findServer('ssdp') as SsdpServer;
39
+ if (typeof ssdp === 'undefined') throw new Error(`SSDP Server not initialized. No upnp information available.`);
40
+ res.status(200).set('Content-Type', 'text/xml').send(ssdp.deviceXML());
41
+ } catch (err) { next(err); }
42
+ });
43
+ app.get('/extended/:section', (req, res) => {
44
+ let cfg = sys.getSection(req.params.section);
45
+ let st = state.getState(req.params.section);
46
+ let arr = [];
47
+ for (let i = 0; i < cfg.length; i++){
48
+ let p = extend(true, {}, cfg[i], st.find(s => s.id === cfg[i].id));
49
+ arr.push(p);
50
+ }
51
+ return res.status(200).send(arr);
52
+ });
53
+ app.put('/app/interfaces/add', async (req, res, next) => {
54
+ try {
55
+ let faces = config.getSection('web.interfaces');
56
+ let opts: any = {};
57
+ switch (req.body.type) {
58
+ case 'rule':
59
+ opts = {};
60
+ break;
61
+ case 'rem':
62
+ opts = {
63
+ options: { protocol: 'http://', host: '', port: 8080, headers: { "content-type": "application/json" } },
64
+ socket: {
65
+ transports: ['websocket'], allowEIO3: true, upgrade: false,
66
+ reconnectionDelay: 2000, reconnection: true, reconnectionDelayMax: 20000
67
+ }
68
+ }
69
+ break;
70
+ case 'http':
71
+ case 'rest':
72
+ opts = {
73
+ options: { protocol: 'http://', host: '', port: 80 }
74
+ }
75
+ break;
76
+ case 'influx':
77
+ case 'influxdb':
78
+ opts = {
79
+ options: {
80
+ version: 1,
81
+ protocol: 'http',
82
+ database: 'pool',
83
+ port: 8601,
84
+ retentionPolicy: 'autogen'
85
+ }
86
+ }
87
+ break;
88
+ case 'influxdb2':
89
+ opts = {
90
+ options: {
91
+ version: 2,
92
+ protocol: 'http',
93
+ port: 9999,
94
+ database: 'pool',
95
+ bucket: '57ec4eed2d90a50b',
96
+ token: '...LuyM84JJx93Qvc7tfaXPbI_mFFjRBjaA==',
97
+ org: 'njsPC-org'
98
+ }
99
+ }
100
+ break;
101
+ case 'mqtt':
102
+ opts = {
103
+ options: {
104
+ protocol: 'mqtt://', host: '', port: 1883, username: '', password: '',
105
+ selfSignedCertificate: false,
106
+ rootTopic: "pool/@bind=(state.equipment.model).replace(/ /g,'-').replace(' / ','').toLowerCase();",
107
+ retain: true, qos: 0, changesOnly: true
108
+ }
109
+ }
110
+ break;
111
+ default:
112
+ return Promise.reject(new ServiceParameterError(`An invalid type was specified ${req.body.type}`, 'PUT: /app/interfaces/add', 'type', req.body.type));
113
+ }
114
+ opts.uuid = utils.uuid();
115
+ opts.isCustom = true;
116
+ // Now lets create a name for the element. This would have been much easier if it were an array but alas we are stuck.
117
+ let name = req.body.name.replace(/^[^a-zA-Z_$]|[^0-9a-zA-Z_$]/g, '_');
118
+ if (name.length === 0) return Promise.reject(new ServiceParameterError(`An invalid name was specified ${req.body.name}`, 'PUT: /app/interfaces/add', 'name', req.body.name));
119
+ if (name.charAt(0) >= '0' && name.charAt(0) <= '9') name = 'i' + name;
120
+ let fnEnsureUnique = (name, ord?: number): string => {
121
+ let isUnique = true;
122
+ for (let fname in faces) {
123
+ if (fname === (typeof ord !== 'undefined' ? `${name}_${ord}` : name)) {
124
+ isUnique = false;
125
+ break;
126
+ }
127
+ }
128
+ if (!isUnique) name = fnEnsureUnique(name, typeof ord === 'undefined' ? 0 : ord++);
129
+ return typeof ord !== 'undefined' ? `${name}_${ord}` : name;
130
+ }
131
+ name = fnEnsureUnique(name);
132
+ opts = extend(true, {}, opts, req.body);
133
+ config.setSection(`web.interfaces.${name}`, opts);
134
+ res.status(200).send({ id: name, opts: opts });
135
+ } catch (err) { next(err); }
136
+ });
137
+ app.delete('/app/interface', async (req, res, next) => {
138
+ try {
139
+ let faces = config.getSection('web.interfaces');
140
+ let deleted;
141
+ for (let fname in faces) {
142
+ let iface = faces[fname];
143
+ if (typeof req.body.id !== 'undefined') {
144
+ if (fname === req.body.id) {
145
+ deleted = iface;
146
+ iface.enabled = false;
147
+ await webApp.updateServerInterface(iface);
148
+ config.removeSection(`web.interfaces.${fname}`);
149
+ }
150
+ }
151
+ else if (typeof req.body.uuid !== 'undefined') {
152
+ if (req.body.uuid.toLowerCase() === iface.uuid.toLowerCase()) {
153
+ deleted = iface;
154
+ iface.enabled = false;
155
+ await webApp.updateServerInterface(iface);
156
+ config.removeSection(`web.interfaces.${fname}`);
157
+ }
158
+ }
159
+ }
160
+ return res.status(200).send(deleted);
161
+ } catch (err) { next(err); }
162
+ });
163
+ app.get('/app/options/interfaces', async (req, res, next) => {
164
+ try {
165
+ // todo: move bytevaluemaps out to a proper location; add additional definitions
166
+ let opts = {
167
+ interfaces: config.getSection('web.interfaces'),
168
+ types: [
169
+ { name: 'rule', desc: 'Rule', hasBindings: true, hasUrl: false },
170
+ { name: 'rest', desc: 'Rest', hasBindings: true },
171
+ { name: 'http', desc: 'Http', hasBindings: true },
172
+ { name: 'rem', desc: 'Relay Equipment Manager', hasBindings: false },
173
+ { name: 'mqtt', desc: 'MQTT', hasBindings: true },
174
+ { name: 'influx', desc: 'InfluxDB', hasBindings: true },
175
+ { name: 'influxdb2', desc: 'InfluxDB2', hasBindings: true}
176
+ ],
177
+ protocols: [
178
+ { val: 0, name: 'http://', desc: 'http://' },
179
+ { val: 1, name: 'https://', desc: 'https://' },
180
+ { val: 2, name: 'mqtt://', desc: 'mqtt://' }
181
+ ],
182
+ files: []
183
+ }
184
+ // Read all the files in the custom bindings directory.
185
+ let cpath = path.posix.join(process.cwd(), '/web/bindings/custom/');
186
+ let files = fs.readdirSync(cpath);
187
+ for (let i = 0; i < files.length; i++) {
188
+ if (path.extname(files[i]) === '.json') {
189
+ let bf = await BindingsFile.fromFile(path.posix.join(process.cwd(), 'web/bindings/'), `custom/${files[i]}`);
190
+ if (typeof bf !== 'undefined') opts.files.push(bf.options);
191
+ }
192
+ }
193
+ return res.status(200).send(opts);
194
+ } catch (err) { next(err); }
195
+ });
196
+ app.post('/app/interfaceBindings/file', async (req, res, next) => {
197
+ try {
198
+ let file = multer({
199
+ limits: { fileSize: 1000000 },
200
+ storage: multer.memoryStorage()
201
+ }).single('bindingsFile');
202
+ file(req, res, async (err) => {
203
+ try {
204
+ if (err) { next(err); }
205
+ else {
206
+ // Validate the incoming data and save it off only if it is valid.
207
+ let bf = await BindingsFile.fromBuffer(req.file.originalname, req.file.buffer);
208
+ if (typeof bf === 'undefined') {
209
+ err = new ServiceProcessError(`Invalid bindings file: ${req.file.originalname}`, 'POST: app/bindings/file', 'extractBindingOptions');
210
+ next(err);
211
+ }
212
+ else {
213
+ if (fs.existsSync(bf.filePath))
214
+ return next(new ServiceProcessError(`File already exists ${req.file.originalname}`, 'POST: app/bindings/file', 'writeFile'));
215
+ else {
216
+ try {
217
+ fs.writeFileSync(bf.filePath, req.file.buffer);
218
+ } catch (e) { logger.error(`Error writing bindings file ${e.message}`); }
219
+ }
220
+ return res.status(200).send(bf);
221
+ }
222
+ }
223
+ } catch (e) {
224
+ err = new ServiceProcessError(`Error uploading file: ${e.message}`, 'POST: app/backup/file', 'uploadFile');
225
+ next(err);
226
+ logger.error(`File upload error: ${e.message}`);
227
+ }
228
+ });
229
+ } catch (err) { next(err); }
230
+ });
231
+
232
+ }
233
+ }