nodejs-poolcontroller 7.6.1 → 7.7.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 (91) hide show
  1. package/.eslintrc.json +44 -44
  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 +220 -215
  8. package/Dockerfile +17 -17
  9. package/Gruntfile.js +40 -40
  10. package/LICENSE +661 -661
  11. package/README.md +191 -191
  12. package/app.ts +1 -1
  13. package/config/Config.ts +14 -0
  14. package/config/VersionCheck.ts +2 -2
  15. package/controller/Constants.ts +2 -1
  16. package/controller/Equipment.ts +2484 -2459
  17. package/controller/Errors.ts +180 -180
  18. package/controller/Lockouts.ts +502 -436
  19. package/controller/State.ts +106 -30
  20. package/controller/boards/AquaLinkBoard.ts +1000 -0
  21. package/controller/boards/BoardFactory.ts +49 -45
  22. package/controller/boards/EasyTouchBoard.ts +2859 -2653
  23. package/controller/boards/IntelliCenterBoard.ts +4198 -4230
  24. package/controller/boards/IntelliComBoard.ts +63 -63
  25. package/controller/boards/IntelliTouchBoard.ts +273 -241
  26. package/controller/boards/NixieBoard.ts +1728 -1675
  27. package/controller/boards/SystemBoard.ts +4925 -4697
  28. package/controller/comms/Comms.ts +442 -479
  29. package/controller/comms/messages/Messages.ts +171 -25
  30. package/controller/comms/messages/config/ChlorinatorMessage.ts +5 -2
  31. package/controller/comms/messages/config/CircuitGroupMessage.ts +0 -0
  32. package/controller/comms/messages/config/CircuitMessage.ts +1 -0
  33. package/controller/comms/messages/config/ConfigMessage.ts +0 -0
  34. package/controller/comms/messages/config/CoverMessage.ts +0 -0
  35. package/controller/comms/messages/config/CustomNameMessage.ts +30 -30
  36. package/controller/comms/messages/config/EquipmentMessage.ts +0 -0
  37. package/controller/comms/messages/config/ExternalMessage.ts +0 -0
  38. package/controller/comms/messages/config/FeatureMessage.ts +0 -0
  39. package/controller/comms/messages/config/GeneralMessage.ts +0 -0
  40. package/controller/comms/messages/config/HeaterMessage.ts +142 -10
  41. package/controller/comms/messages/config/IntellichemMessage.ts +0 -0
  42. package/controller/comms/messages/config/OptionsMessage.ts +4 -21
  43. package/controller/comms/messages/config/PumpMessage.ts +53 -35
  44. package/controller/comms/messages/config/RemoteMessage.ts +0 -0
  45. package/controller/comms/messages/config/ScheduleMessage.ts +350 -347
  46. package/controller/comms/messages/config/SecurityMessage.ts +0 -0
  47. package/controller/comms/messages/config/ValveMessage.ts +1 -1
  48. package/controller/comms/messages/status/ChlorinatorStateMessage.ts +38 -86
  49. package/controller/comms/messages/status/EquipmentStateMessage.ts +58 -22
  50. package/controller/comms/messages/status/HeaterStateMessage.ts +116 -86
  51. package/controller/comms/messages/status/IntelliChemStateMessage.ts +445 -445
  52. package/controller/comms/messages/status/IntelliValveStateMessage.ts +35 -35
  53. package/controller/comms/messages/status/PumpStateMessage.ts +23 -1
  54. package/controller/comms/messages/status/VersionMessage.ts +0 -0
  55. package/controller/nixie/Nixie.ts +162 -162
  56. package/controller/nixie/NixieEquipment.ts +103 -103
  57. package/controller/nixie/bodies/Body.ts +120 -120
  58. package/controller/nixie/bodies/Filter.ts +135 -135
  59. package/controller/nixie/chemistry/ChemController.ts +2511 -2498
  60. package/controller/nixie/chemistry/Chlorinator.ts +363 -314
  61. package/controller/nixie/circuits/Circuit.ts +261 -248
  62. package/controller/nixie/heaters/Heater.ts +650 -648
  63. package/controller/nixie/pumps/Pump.ts +906 -661
  64. package/controller/nixie/schedules/Schedule.ts +313 -257
  65. package/controller/nixie/valves/Valve.ts +170 -170
  66. package/defaultConfig.json +306 -286
  67. package/logger/DataLogger.ts +448 -448
  68. package/logger/Logger.ts +0 -0
  69. package/package.json +56 -56
  70. package/tsconfig.json +25 -25
  71. package/web/Server.ts +92 -47
  72. package/web/bindings/aqualinkD.json +505 -0
  73. package/web/bindings/influxDB.json +1051 -1021
  74. package/web/bindings/mqtt.json +702 -654
  75. package/web/bindings/mqttAlt.json +731 -684
  76. package/web/bindings/rulesManager.json +54 -54
  77. package/web/bindings/smartThings-Hubitat.json +31 -31
  78. package/web/bindings/valveRelays.json +20 -20
  79. package/web/bindings/vera.json +25 -25
  80. package/web/interfaces/baseInterface.ts +137 -136
  81. package/web/interfaces/httpInterface.ts +145 -124
  82. package/web/interfaces/influxInterface.ts +276 -245
  83. package/web/interfaces/mqttInterface.ts +535 -475
  84. package/web/services/config/Config.ts +39 -18
  85. package/web/services/config/ConfigSocket.ts +0 -0
  86. package/web/services/state/State.ts +10 -0
  87. package/web/services/state/StateSocket.ts +4 -4
  88. package/web/services/utilities/Utilities.ts +44 -42
  89. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -52
  90. package/config copy.json +0 -300
  91. package/issue_template.md +0 -52
@@ -1,245 +1,276 @@
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
-
18
- import extend = require("extend");
19
- import { ClientOptions, InfluxDB, Point, WriteApi, WritePrecisionType } from '@influxdata/influxdb-client';
20
- import { utils, Timestamp } from '../../controller/Constants';
21
- import { logger } from "../../logger/Logger";
22
- import { BaseInterfaceBindings, InterfaceContext, InterfaceEvent } from "./baseInterface";
23
- export class InfluxInterfaceBindings extends BaseInterfaceBindings {
24
- constructor(cfg) {
25
- super(cfg);
26
- }
27
- private writeApi: WriteApi;
28
- declare context: InterfaceContext;
29
- declare cfg;
30
- declare events: InfluxInterfaceEvent[];
31
- private init = () => {
32
- let baseOpts = extend(true, this.cfg.options, this.context.options);
33
- let url = 'http://';
34
- if (typeof baseOpts.protocol !== 'undefined' && baseOpts.protocol) url = baseOpts.protocol;
35
- if (!url.endsWith('://')) url += '://';
36
- url = `${url}${baseOpts.host}:${baseOpts.port}`;
37
- let influxDB: InfluxDB;
38
- let bucket;
39
- let org;
40
- if (typeof baseOpts.host === 'undefined' || !baseOpts.host) {
41
- logger.warn(`Interface: ${this.cfg.name} has not resolved to a valid host.`);
42
- return;
43
- }
44
- if (baseOpts.version === 1) {
45
- if (typeof baseOpts.database === 'undefined' || !baseOpts.database) {
46
- logger.warn(`Interface: ${this.cfg.name} has not resolved to a valid database.`);
47
- return;
48
- }
49
- bucket = `${baseOpts.database}/${baseOpts.retentionPolicy}`;
50
- const clientOptions: ClientOptions = {
51
- url,
52
- token: `${baseOpts.username}:${baseOpts.password}`,
53
- }
54
- influxDB = new InfluxDB(clientOptions);
55
- }
56
- else if (baseOpts.version === 2) {
57
- org = baseOpts.org;
58
- bucket = baseOpts.bucket;
59
- const clientOptions: ClientOptions = {
60
- url,
61
- token: baseOpts.token,
62
- }
63
- influxDB = new InfluxDB(clientOptions);
64
- }
65
- this.writeApi = influxDB.getWriteApi(org, bucket, 'ms');
66
-
67
-
68
- // set global tags from context
69
- let baseTags = {}
70
- baseOpts.tags.forEach(tag => {
71
- let toks = {};
72
- let sname = this.tokensReplacer(tag.name, undefined, toks, undefined, {})
73
- let svalue = this.tokensReplacer(tag.value, undefined, toks, { vars: {} } as any, {});
74
- if (typeof sname !== 'undefined' && typeof svalue !== 'undefined' && !sname.includes('@bind') && !svalue.includes('@bind'))
75
- baseTags[sname] = svalue;
76
- })
77
- this.writeApi.useDefaultTags(baseTags);
78
- }
79
- public bindEvent(evt: string, ...data: any) {
80
-
81
- // if (state.status.value !== sys.board.valueMaps.controllerStatus.getValue('ready')) return; // miss values? or show errors? or?
82
- if (typeof this.events !== 'undefined') {
83
- if (typeof this.writeApi === 'undefined') this.init();
84
- let evts = this.events.filter(elem => elem.name === evt);
85
- if (evts.length > 0) {
86
- let toks = {};
87
- for (let i = 0; i < evts.length; i++) {
88
- let e = evts[i];
89
- if (typeof e.enabled !== 'undefined' && !e.enabled) continue;
90
- // Figure out whether we need to check the filter.
91
- if (typeof e.filter !== 'undefined') {
92
- this.buildTokens(e.filter, evt, toks, e, data[0]);
93
- if (eval(this.replaceTokens(e.filter, toks)) === false) continue;
94
- }
95
- for (let j = 0; j < e.points.length; j++) {
96
- let _point = e.points[j];
97
- // Figure out whether we need to check the filter for each point.
98
- if (typeof _point.filter !== 'undefined') {
99
- this.buildTokens(_point.filter, evt, toks, e, data[0]);
100
- if (eval(this.replaceTokens(_point.filter, toks)) === false) continue;
101
- }
102
- // iterate through points array
103
- let point = new Point(_point.measurement)
104
- let point2 = new Point(_point.measurement);
105
- _point.tags.forEach(_tag => {
106
- let sname = _tag.name;
107
- this.buildTokens(sname, evt, toks, e, data[0]);
108
- sname = this.replaceTokens(sname, toks);
109
- let svalue = _tag.value;
110
- this.buildTokens(svalue, evt, toks, e, data[0]);
111
- svalue = this.replaceTokens(svalue, toks);
112
- if (typeof sname !== 'undefined' && typeof svalue !== 'undefined' && !sname.includes('@bind') && !svalue.includes('@bind') && svalue !== null) {
113
- point.tag(sname, svalue);
114
- if (typeof _point.storePrevState !== 'undefined' && _point.storePrevState) point2.tag(sname, svalue);
115
- }
116
- else {
117
- logger.error(`InfluxDB tag binding failure on ${evt}:${_tag.name}/${_tag.value} --> ${svalue || 'undefined'} ${JSON.stringify(data[0])}`);
118
- if (typeof sname === 'undefined') logger.error(`InfluxDB tag name is undefined`);
119
- if (typeof svalue === 'undefined') logger.error(`InfluxDB value is undefined`);
120
- if (svalue.includes('@bind')) logger.error(`InfluxDB value not bound`);
121
- if (svalue === null) logger.error(`InfluxDB value is null`);
122
- }
123
- })
124
- _point.fields.forEach(_field => {
125
- try {
126
- let sname = _field.name;
127
- this.buildTokens(sname, evt, toks, e, data[0]);
128
- //console.log(toks);
129
- sname = this.replaceTokens(sname, toks);
130
- let svalue = _field.value;
131
- this.buildTokens(svalue, evt, toks, e, data[0]);
132
- svalue = this.replaceTokens(svalue, toks);
133
- if (typeof sname !== 'undefined' && typeof svalue !== 'undefined' && !sname.includes('@bind') && !svalue.includes('@bind') && svalue !== null)
134
- switch (_field.type) {
135
- case 'int':
136
- case 'integer':
137
- let int = parseInt(svalue, 10);
138
- if (!isNaN(int)) point.intField(sname, int);
139
- // if (!isNaN(int) && typeof _point.storePrevState !== 'undefined' && _point.storePrevState) point2.intField(sname, int);
140
- break;
141
- case 'string':
142
- point.stringField(sname, svalue);
143
- // if (typeof _point.storePrevState !== 'undefined' && _point.storePrevState) point2.stringField(sname, svalue);
144
- break;
145
- case 'boolean':
146
- point.booleanField(sname, utils.makeBool(svalue));
147
- if (typeof _point.storePrevState !== 'undefined' && _point.storePrevState) point2.booleanField(sname, !utils.makeBool(svalue));
148
- break;
149
- case 'float':
150
- let float = parseFloat(svalue);
151
- if (!isNaN(float)) point.floatField(sname, float);
152
- // if (!isNaN(float) && typeof _point.storePrevState !== 'undefined' && _point.storePrevState) point2.intField(sname, int);
153
- break;
154
- case 'timestamp':
155
- case 'datetime':
156
- case 'date':
157
- //let dt = Date.parse(svalue.replace(/^["'](.+(?=["']$))["']$/, '$1'));
158
- // RKS: 07-06-21 - I think this is missing the eval function around all of this. The strings still have the quotes around them. I think
159
- // maybe we need to create a closure and execute it as a code segment for variable data.
160
- let sdt = eval(svalue);
161
- if (sdt !== null && typeof sdt !== 'undefined') {
162
- let dt = Date.parse(sdt);
163
- if (!isNaN(dt)) point.intField(sname, dt);
164
- else if (svalue !== '') logger.warn(`Influx error parsing date from ${sname}: ${svalue}`);
165
- }
166
- break;
167
- }
168
- else {
169
- logger.error(`InfluxDB point binding failure on ${evt}:${_field.name}/${_field.value} --> ${svalue || 'undefined'}`);
170
- }
171
- } catch (err) { logger.error(`Error binding InfluxDB point fields ${err.message}`); }
172
- });
173
- if (typeof _point.series !== 'undefined') {
174
- try {
175
- this.buildTokens(_point.series.value, evt, toks, e, data[0]);
176
- let ser = eval(this.replaceTokens(_point.series.value, toks));
177
- let ts = Date.parse(ser);
178
- if (isNaN(ts)) {
179
- logger.error(`Influx series timestamp is invalid ${ser}`);
180
- }
181
- else
182
- point.timestamp(new Date(ts));
183
- } catch (err) { logger.error(`Error parsing Influx point series for ${evt} - ${_point.series.value}`); }
184
- }
185
- else
186
- point.timestamp(new Date());
187
- try {
188
-
189
- if (typeof _point.storePrevState !== 'undefined' && _point.storePrevState) {
190
- // copy the point and subtract a second and keep inverse value
191
- let ts = new Date();
192
- let sec = ts.getSeconds() - 1;
193
- ts.setSeconds(sec);
194
- point2.timestamp(ts);
195
- logger.silly(`Writing influx ${e.name} inverse data point ${point2.toString()})`)
196
- this.writeApi.writePoint(point2);
197
- }
198
- if (typeof point.toLineProtocol() !== 'undefined') {
199
- logger.silly(`Writing influx ${e.name} data point ${point.toString()}`)
200
- this.writeApi.writePoint(point);
201
- this.writeApi.flush()
202
- .catch(error => { logger.error(`Error flushing Influx data point ${point.toString()} ${error}`); });
203
- //logger.info(`INFLUX: ${point.toLineProtocol()}`)
204
- }
205
- else {
206
- logger.silly(`Skipping INFLUX write because some data is missing with ${e.name} event on measurement ${_point.measurement}.`)
207
- }
208
- }
209
- catch (err) {
210
- logger.error(`Error writing to Influx: ${err.message}`)
211
- }
212
- }
213
- }
214
- }
215
- }
216
- }
217
- public close = () => {
218
-
219
- }
220
- }
221
-
222
- class InfluxInterfaceEvent extends InterfaceEvent {
223
- public points: IPoint[];
224
- }
225
-
226
- export interface IPoint {
227
- measurement: string;
228
- series?: ISeries;
229
- tags: ITag[];
230
- fields: IFields[];
231
- storePrevState?: boolean;
232
- filter?: any;
233
- }
234
- export interface ITag {
235
- name: string;
236
- value: string;
237
- }
238
- export interface IFields {
239
- name: string;
240
- value: string;
241
- type: string;
242
- }
243
- export interface ISeries {
244
- value: string;
245
- }
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
+
18
+ import extend = require("extend");
19
+ import { ClientOptions, DEFAULT_WriteOptions, InfluxDB, Point, WriteApi, WriteOptions, WritePrecisionType } from '@influxdata/influxdb-client';
20
+ import { utils, Timestamp } from '../../controller/Constants';
21
+ import { logger } from "../../logger/Logger";
22
+ import { BaseInterfaceBindings, InterfaceContext, InterfaceEvent } from "./baseInterface";
23
+ export class InfluxInterfaceBindings extends BaseInterfaceBindings {
24
+ constructor(cfg) {
25
+ super(cfg);
26
+ }
27
+ private writeApi: WriteApi;
28
+ declare context: InterfaceContext;
29
+ declare cfg;
30
+ declare events: InfluxInterfaceEvent[];
31
+ private init = () => {
32
+ let baseOpts = extend(true, this.cfg.options, this.context.options);
33
+ let url = 'http://';
34
+ if (typeof baseOpts.protocol !== 'undefined' && baseOpts.protocol) url = baseOpts.protocol;
35
+ if (!url.endsWith('://')) url += '://';
36
+ url = `${url}${baseOpts.host}`;
37
+ if(typeof baseOpts.port !== 'undefined' && baseOpts.port !== null && !isNaN(baseOpts.port)) url = `${url}:${baseOpts.port}`;
38
+ let influxDB: InfluxDB;
39
+ let bucket;
40
+ let org;
41
+ if (typeof baseOpts.host === 'undefined' || !baseOpts.host) {
42
+ logger.warn(`Interface: ${this.cfg.name} has not resolved to a valid host.`);
43
+ return;
44
+ }
45
+ if (baseOpts.version === 1) {
46
+ if (typeof baseOpts.database === 'undefined' || !baseOpts.database) {
47
+ logger.warn(`Interface: ${this.cfg.name} has not resolved to a valid database.`);
48
+ return;
49
+ }
50
+ bucket = `${baseOpts.database}/${baseOpts.retentionPolicy}`;
51
+ const clientOptions: ClientOptions = {
52
+ url,
53
+ token: `${baseOpts.username}:${baseOpts.password}`,
54
+ }
55
+ influxDB = new InfluxDB(clientOptions);
56
+ }
57
+ else if (baseOpts.version === 2) {
58
+ org = baseOpts.org;
59
+ bucket = baseOpts.bucket;
60
+ const clientOptions: ClientOptions = {
61
+ url,
62
+ token: baseOpts.token,
63
+ }
64
+ influxDB = new InfluxDB(clientOptions);
65
+ }
66
+ // set global tags from context
67
+ let baseTags = {}
68
+ baseOpts.tags.forEach(tag => {
69
+ let toks = {};
70
+ let sname = this.tokensReplacer(tag.name, undefined, toks, undefined, {})
71
+ let svalue = this.tokensReplacer(tag.value, undefined, toks, { vars: {} } as any, {});
72
+ if (typeof sname !== 'undefined' && typeof svalue !== 'undefined' && !sname.includes('@bind') && !svalue.includes('@bind'))
73
+ baseTags[sname] = svalue;
74
+ })
75
+ //this.writeApi.useDefaultTags(baseTags);
76
+ const writeOptions:WriteOptions = {
77
+ /* the maximum points/line to send in a single batch to InfluxDB server */
78
+ batchSize: baseOpts.batchSize || 100,
79
+ /* default tags to add to every point */
80
+ defaultTags: baseTags,
81
+ /* maximum time in millis to keep points in an unflushed batch, 0 means don't periodically flush */
82
+ flushInterval: DEFAULT_WriteOptions.flushInterval,
83
+ /* maximum size of the retry buffer - it contains items that could not be sent for the first time */
84
+ maxBufferLines: DEFAULT_WriteOptions.maxBufferLines,
85
+ /* the count of retries, the delays between retries follow an exponential backoff strategy if there is no Retry-After HTTP header */
86
+ maxRetries: DEFAULT_WriteOptions.maxRetries,
87
+ /* maximum delay between retries in milliseconds */
88
+ maxRetryDelay: DEFAULT_WriteOptions.maxRetryDelay,
89
+ /* minimum delay between retries in milliseconds */
90
+ minRetryDelay: DEFAULT_WriteOptions.minRetryDelay, // minimum delay between retries
91
+ /* a random value of up to retryJitter is added when scheduling next retry */
92
+ retryJitter: DEFAULT_WriteOptions.retryJitter,
93
+ // ... or you can customize what to do on write failures when using a writeFailed fn, see the API docs for details
94
+ writeFailed: function(error, lines, failedAttempts){
95
+ /** return promise or void */
96
+ logger.error(`InfluxDB batch write failed writing ${lines.length} lines with ${failedAttempts} failed attempts. ${error.message}`);
97
+ },
98
+ writeSuccess: function(lines){
99
+ logger.silly(`InfluxDB successfully wrote ${lines.length} lines.`)
100
+ },
101
+ maxRetryTime: DEFAULT_WriteOptions.maxRetryTime,
102
+ exponentialBase: DEFAULT_WriteOptions.exponentialBase,
103
+ randomRetry: DEFAULT_WriteOptions.randomRetry
104
+ }
105
+ this.writeApi = influxDB.getWriteApi(org, bucket, 'ms', writeOptions);
106
+
107
+
108
+
109
+ }
110
+ public bindEvent(evt: string, ...data: any) {
111
+
112
+ // if (state.status.value !== sys.board.valueMaps.controllerStatus.getValue('ready')) return; // miss values? or show errors? or?
113
+ if (typeof this.events !== 'undefined') {
114
+ if (typeof this.writeApi === 'undefined') this.init();
115
+ let evts = this.events.filter(elem => elem.name === evt);
116
+ if (evts.length > 0) {
117
+ let toks = {};
118
+ for (let i = 0; i < evts.length; i++) {
119
+ let e = evts[i];
120
+ if (typeof e.enabled !== 'undefined' && !e.enabled) continue;
121
+ // Figure out whether we need to check the filter.
122
+ if (typeof e.filter !== 'undefined') {
123
+ this.buildTokens(e.filter, evt, toks, e, data[0]);
124
+ if (eval(this.replaceTokens(e.filter, toks)) === false) continue;
125
+ }
126
+ for (let j = 0; j < e.points.length; j++) {
127
+ let _point = e.points[j];
128
+ // Figure out whether we need to check the filter for each point.
129
+ if (typeof _point.filter !== 'undefined') {
130
+ this.buildTokens(_point.filter, evt, toks, e, data[0]);
131
+ if (eval(this.replaceTokens(_point.filter, toks)) === false) continue;
132
+ }
133
+ // iterate through points array
134
+ let point = new Point(_point.measurement)
135
+ let point2 = new Point(_point.measurement);
136
+ _point.tags.forEach(_tag => {
137
+ let sname = _tag.name;
138
+ this.buildTokens(sname, evt, toks, e, data[0]);
139
+ sname = this.replaceTokens(sname, toks);
140
+ let svalue = _tag.value;
141
+ this.buildTokens(svalue, evt, toks, e, data[0]);
142
+ svalue = this.replaceTokens(svalue, toks);
143
+ if (typeof sname !== 'undefined' && typeof svalue !== 'undefined' && !sname.includes('@bind') && !svalue.includes('@bind') && svalue !== null) {
144
+ point.tag(sname, svalue);
145
+ if (typeof _point.storePrevState !== 'undefined' && _point.storePrevState) point2.tag(sname, svalue);
146
+ }
147
+ else {
148
+ logger.error(`InfluxDB tag binding failure on ${evt}:${_tag.name}/${_tag.value} --> ${svalue || 'undefined'} ${JSON.stringify(data[0])}`);
149
+ if (typeof sname === 'undefined') logger.error(`InfluxDB tag name is undefined`);
150
+ if (typeof svalue === 'undefined') logger.error(`InfluxDB value is undefined`);
151
+ if (svalue.includes('@bind')) logger.error(`InfluxDB value not bound`);
152
+ if (svalue === null) logger.error(`InfluxDB value is null`);
153
+ }
154
+ })
155
+ _point.fields.forEach(_field => {
156
+ try {
157
+ let sname = _field.name;
158
+ this.buildTokens(sname, evt, toks, e, data[0]);
159
+ //console.log(toks);
160
+ sname = this.replaceTokens(sname, toks);
161
+ let svalue = _field.value;
162
+ this.buildTokens(svalue, evt, toks, e, data[0]);
163
+ svalue = this.replaceTokens(svalue, toks);
164
+ if (typeof sname !== 'undefined' && typeof svalue !== 'undefined' && !sname.includes('@bind') && !svalue.includes('@bind') && svalue !== null)
165
+ switch (_field.type) {
166
+ case 'int':
167
+ case 'integer':
168
+ let int = parseInt(svalue, 10);
169
+ if (!isNaN(int)) point.intField(sname, int);
170
+ // if (!isNaN(int) && typeof _point.storePrevState !== 'undefined' && _point.storePrevState) point2.intField(sname, int);
171
+ break;
172
+ case 'string':
173
+ point.stringField(sname, svalue);
174
+ // if (typeof _point.storePrevState !== 'undefined' && _point.storePrevState) point2.stringField(sname, svalue);
175
+ break;
176
+ case 'boolean':
177
+ point.booleanField(sname, utils.makeBool(svalue));
178
+ if (typeof _point.storePrevState !== 'undefined' && _point.storePrevState) point2.booleanField(sname, !utils.makeBool(svalue));
179
+ break;
180
+ case 'float':
181
+ let float = parseFloat(svalue);
182
+ if (!isNaN(float)) point.floatField(sname, float);
183
+ // if (!isNaN(float) && typeof _point.storePrevState !== 'undefined' && _point.storePrevState) point2.intField(sname, int);
184
+ break;
185
+ case 'timestamp':
186
+ case 'datetime':
187
+ case 'date':
188
+ //let dt = Date.parse(svalue.replace(/^["'](.+(?=["']$))["']$/, '$1'));
189
+ // RKS: 07-06-21 - I think this is missing the eval function around all of this. The strings still have the quotes around them. I think
190
+ // maybe we need to create a closure and execute it as a code segment for variable data.
191
+ let sdt = eval(svalue);
192
+ if (sdt !== null && typeof sdt !== 'undefined') {
193
+ let dt = Date.parse(sdt);
194
+ if (!isNaN(dt)) point.intField(sname, dt);
195
+ else if (svalue !== '') logger.warn(`Influx error parsing date from ${sname}: ${svalue}`);
196
+ }
197
+ break;
198
+ }
199
+ else {
200
+ logger.error(`InfluxDB point binding failure on ${evt}:${_field.name}/${_field.value} --> ${svalue || 'undefined'}`);
201
+ }
202
+ } catch (err) { logger.error(`Error binding InfluxDB point fields ${err.message}`); }
203
+ });
204
+ if (typeof _point.series !== 'undefined') {
205
+ try {
206
+ this.buildTokens(_point.series.value, evt, toks, e, data[0]);
207
+ let ser = eval(this.replaceTokens(_point.series.value, toks));
208
+ let ts = Date.parse(ser);
209
+ if (isNaN(ts)) {
210
+ logger.error(`Influx series timestamp is invalid ${ser}`);
211
+ }
212
+ else
213
+ point.timestamp(new Date(ts));
214
+ } catch (err) { logger.error(`Error parsing Influx point series for ${evt} - ${_point.series.value}`); }
215
+ }
216
+ else
217
+ point.timestamp(new Date());
218
+ try {
219
+
220
+ if (typeof _point.storePrevState !== 'undefined' && _point.storePrevState) {
221
+ // copy the point and subtract a second and keep inverse value
222
+ let ts = new Date();
223
+ let sec = ts.getSeconds() - 1;
224
+ ts.setSeconds(sec);
225
+ point2.timestamp(ts);
226
+ logger.silly(`Batching influx ${e.name} inverse data point ${point2.toString()})`)
227
+ this.writeApi.writePoint(point2);
228
+ }
229
+ if (typeof point.toLineProtocol() !== 'undefined') {
230
+ logger.silly(`Batching influx ${e.name} data point ${point.toString()}`)
231
+ this.writeApi.writePoint(point);
232
+ // this.writeApi.flush()
233
+ // .catch(error => { logger.error(`Error flushing Influx data point ${point.toString()} ${error}`); });
234
+ //logger.info(`INFLUX: ${point.toLineProtocol()}`)
235
+ }
236
+ else {
237
+ logger.silly(`Skipping INFLUX write because some data is missing with ${e.name} event on measurement ${_point.measurement}.`)
238
+ }
239
+ }
240
+ catch (err) {
241
+ logger.error(`Error writing to Influx: ${err.message}`)
242
+ }
243
+ }
244
+ }
245
+ }
246
+ }
247
+ }
248
+ public close = () => {
249
+
250
+ }
251
+ }
252
+
253
+ class InfluxInterfaceEvent extends InterfaceEvent {
254
+ public points: IPoint[];
255
+ }
256
+
257
+ export interface IPoint {
258
+ measurement: string;
259
+ series?: ISeries;
260
+ tags: ITag[];
261
+ fields: IFields[];
262
+ storePrevState?: boolean;
263
+ filter?: any;
264
+ }
265
+ export interface ITag {
266
+ name: string;
267
+ value: string;
268
+ }
269
+ export interface IFields {
270
+ name: string;
271
+ value: string;
272
+ type: string;
273
+ }
274
+ export interface ISeries {
275
+ value: string;
276
+ }