@machinemetrics/io-adapter-lib 2.32.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 (264) hide show
  1. package/.circleci/config.yml +141 -0
  2. package/.eslintrc.json +36 -0
  3. package/.gitattributes +12 -0
  4. package/CHANGELOG.md +544 -0
  5. package/README.md +2 -0
  6. package/index.js +17 -0
  7. package/lib/config/adapterConfig.js +535 -0
  8. package/lib/config/common/allowDenyList.js +58 -0
  9. package/lib/config/common/jsonPath.js +44 -0
  10. package/lib/config/common/labjackU3T4Common.js +227 -0
  11. package/lib/config/common/validations.js +142 -0
  12. package/lib/config/configError.js +32 -0
  13. package/lib/config/device/adam-6052Config.js +103 -0
  14. package/lib/config/device/brotherHTTPConfig.js +215 -0
  15. package/lib/config/device/ethernetIPConfig.js +191 -0
  16. package/lib/config/device/ifmIotConfig.js +245 -0
  17. package/lib/config/device/jsonHttpConfig.js +76 -0
  18. package/lib/config/device/labjackT4Config.js +39 -0
  19. package/lib/config/device/labjackT7Config.js +192 -0
  20. package/lib/config/device/labjackU3Config.js +32 -0
  21. package/lib/config/device/modbusTcpConfig.js +336 -0
  22. package/lib/config/device/mqttBaseConfig.js +70 -0
  23. package/lib/config/device/mqttConfig.js +113 -0
  24. package/lib/config/device/mqttLincolnConfig.js +62 -0
  25. package/lib/config/device/mtconnectAdapterConfig.js +42 -0
  26. package/lib/config/device/mtconnectBaseConfig.js +136 -0
  27. package/lib/config/device/mtconnectConfig.js +52 -0
  28. package/lib/config/device/mtconnectHaasConfig.js +15 -0
  29. package/lib/config/device/nullConfig.js +81 -0
  30. package/lib/config/device/opcuaConfig.js +205 -0
  31. package/lib/config/device/pcccConfig.js +88 -0
  32. package/lib/config/engineConfigV1.js +382 -0
  33. package/lib/config/engineConfigV2.js +106 -0
  34. package/lib/config/generator/counterGenConfig.js +15 -0
  35. package/lib/config/generator/cronGenConfig.js +33 -0
  36. package/lib/config/generator/dateTimeGenConfig.js +33 -0
  37. package/lib/config/index.js +339 -0
  38. package/lib/config/transformConfigUtil.js +296 -0
  39. package/lib/engine/dataOutput.js +357 -0
  40. package/lib/engine/deviceOutput.js +186 -0
  41. package/lib/engine/engineV1.js +480 -0
  42. package/lib/engine/engineV2.js +719 -0
  43. package/lib/engine/index.js +34 -0
  44. package/lib/engine/transformBuilderV1.js +111 -0
  45. package/lib/engine/transformBuilderV2.js +74 -0
  46. package/lib/expressionService.js +330 -0
  47. package/lib/math.js +142 -0
  48. package/lib/transform/accumulate.js +98 -0
  49. package/lib/transform/average.js +56 -0
  50. package/lib/transform/debounce.js +152 -0
  51. package/lib/transform/downsample.js +69 -0
  52. package/lib/transform/edge.js +34 -0
  53. package/lib/transform/expression.js +91 -0
  54. package/lib/transform/fallingEdge.js +70 -0
  55. package/lib/transform/fromBuffer.js +89 -0
  56. package/lib/transform/hash.js +41 -0
  57. package/lib/transform/ignoreValue.js +118 -0
  58. package/lib/transform/index.js +93 -0
  59. package/lib/transform/invert.js +25 -0
  60. package/lib/transform/latch.js +99 -0
  61. package/lib/transform/latchValue.js +115 -0
  62. package/lib/transform/logicAnd.js +67 -0
  63. package/lib/transform/logicOr.js +67 -0
  64. package/lib/transform/map.js +115 -0
  65. package/lib/transform/max.js +57 -0
  66. package/lib/transform/maxLength.js +39 -0
  67. package/lib/transform/min.js +57 -0
  68. package/lib/transform/minDelta.js +40 -0
  69. package/lib/transform/offDelay.js +89 -0
  70. package/lib/transform/onDelay.js +89 -0
  71. package/lib/transform/patternEscape.js +24 -0
  72. package/lib/transform/patternMatch.js +194 -0
  73. package/lib/transform/patternReplace.js +170 -0
  74. package/lib/transform/patternTest.js +85 -0
  75. package/lib/transform/rateOfChange.js +56 -0
  76. package/lib/transform/reject.js +115 -0
  77. package/lib/transform/resample.js +96 -0
  78. package/lib/transform/risingEdge.js +90 -0
  79. package/lib/transform/risingEdgeCounter.js +179 -0
  80. package/lib/transform/sampleInterval.js +12 -0
  81. package/lib/transform/source.js +116 -0
  82. package/lib/transform/state.js +201 -0
  83. package/lib/transform/threshold.js +52 -0
  84. package/lib/transform/toBuffer.js +159 -0
  85. package/lib/transform/toggle.js +118 -0
  86. package/lib/transform/transformState.js +193 -0
  87. package/lib/transform/trim.js +27 -0
  88. package/lib/transform/util/chainSource.js +96 -0
  89. package/lib/transform/util/ringBuffer.js +34 -0
  90. package/lib/transform/valueChange.js +34 -0
  91. package/lib/transform/valueDecrease.js +38 -0
  92. package/lib/transform/valueIncrease.js +38 -0
  93. package/lib/transform/valueIncreaseDiff.js +66 -0
  94. package/lib/transform/whenUnavailable.js +73 -0
  95. package/lib/transform/windowCount.js +86 -0
  96. package/lib/util/fileUtil.js +44 -0
  97. package/package.json +38 -0
  98. package/test/.eslintrc.json +15 -0
  99. package/test/chainedTransform.test.js +88 -0
  100. package/test/conditions.test.js +118 -0
  101. package/test/config/ab-pccc.test.js +41 -0
  102. package/test/config/adam-6052.test.js +16 -0
  103. package/test/config/adapter.test.js +109 -0
  104. package/test/config/brother-http.test.js +19 -0
  105. package/test/config/ethernet-ip.test.js +19 -0
  106. package/test/config/ifm-iot.test.js +20 -0
  107. package/test/config/json-http.test.js +47 -0
  108. package/test/config/labjack-t4.test.js +78 -0
  109. package/test/config/labjack-t7.test.js +18 -0
  110. package/test/config/labjack-u3.test.js +17 -0
  111. package/test/config/modbusTcp.test.js +87 -0
  112. package/test/config/mqtt.test.js +19 -0
  113. package/test/config/mqttLincoln.test.js +29 -0
  114. package/test/config/mtconnect.test.js +63 -0
  115. package/test/config/mtconnectAdapter.test.js +124 -0
  116. package/test/config/mtconnectHaas.test.js +15 -0
  117. package/test/config/null.test.js +16 -0
  118. package/test/config/opcua.test.js +97 -0
  119. package/test/config-tests.js +102 -0
  120. package/test/configFiles/conditions.yml +37 -0
  121. package/test/configFiles/data-items-legacy.yml +24 -0
  122. package/test/configFiles/data-items-shorthand.yml +14 -0
  123. package/test/configFiles/data-items.yml +12 -0
  124. package/test/configFiles/device/ab-pccc-default.yml +3 -0
  125. package/test/configFiles/device/ab-pccc-full.yml +13 -0
  126. package/test/configFiles/device/adam-6052-default.yml +2 -0
  127. package/test/configFiles/device/brother-http-default.yml +3 -0
  128. package/test/configFiles/device/ethernet-ip-default.yml +2 -0
  129. package/test/configFiles/device/ifm-iot-default.yml +2 -0
  130. package/test/configFiles/device/json-http-bad-prop.yml +13 -0
  131. package/test/configFiles/device/json-http-bad-prop2.yml +13 -0
  132. package/test/configFiles/device/json-http-default.yml +3 -0
  133. package/test/configFiles/device/json-http-std.yml +13 -0
  134. package/test/configFiles/device/labjack-t4-condition-exprs.yaml +46 -0
  135. package/test/configFiles/device/labjack-t4-default.yml +3 -0
  136. package/test/configFiles/device/labjack-t4-pins-alt.yaml +41 -0
  137. package/test/configFiles/device/labjack-t4-pins.yaml +40 -0
  138. package/test/configFiles/device/labjack-t4-v1.yaml +29 -0
  139. package/test/configFiles/device/labjack-t7-default.yml +2 -0
  140. package/test/configFiles/device/labjack-u3-default.yml +2 -0
  141. package/test/configFiles/device/modbus-partial.yml +4 -0
  142. package/test/configFiles/device/modbus-std-extended.yaml +23 -0
  143. package/test/configFiles/device/modbus-std.yaml +34 -0
  144. package/test/configFiles/device/modbus-tcp-default.yml +3 -0
  145. package/test/configFiles/device/mqtt-default.yml +2 -0
  146. package/test/configFiles/device/mqtt-lincoln-default.yml +3 -0
  147. package/test/configFiles/device/mqtt-lincoln-full.yml +7 -0
  148. package/test/configFiles/device/mtconnect-adapter-default.yml +3 -0
  149. package/test/configFiles/device/mtconnect-adapter-keys.yaml +18 -0
  150. package/test/configFiles/device/mtconnect-complex-keys.yaml +17 -0
  151. package/test/configFiles/device/mtconnect-default.yml +3 -0
  152. package/test/configFiles/device/mtconnect-duplicate-allow-keys.yaml +10 -0
  153. package/test/configFiles/device/mtconnect-duplicate-declare-keys.yaml +8 -0
  154. package/test/configFiles/device/mtconnect-duplicate-deny-keys.yaml +10 -0
  155. package/test/configFiles/device/mtconnect-haas-default.yml +3 -0
  156. package/test/configFiles/device/mtconnect-std.yaml +18 -0
  157. package/test/configFiles/device/null-default.yml +1 -0
  158. package/test/configFiles/device/opcua-bad-tag.yml +18 -0
  159. package/test/configFiles/device/opcua-bad-tag2.yml +18 -0
  160. package/test/configFiles/device/opcua-default.yml +3 -0
  161. package/test/configFiles/device/opcua-std.yml +18 -0
  162. package/test/configFiles/dump-test.yml +11 -0
  163. package/test/configFiles/expressionCond.yml +46 -0
  164. package/test/configFiles/min-config-t4.yaml +4 -0
  165. package/test/configFiles/min-config-u3.yaml +3 -0
  166. package/test/configFiles/missing-device.yaml +2 -0
  167. package/test/configFiles/parse-error1.yml +9 -0
  168. package/test/configFiles/parse-error2.yml +9 -0
  169. package/test/configFiles/repro/buffer-convert-repro.yml +15 -0
  170. package/test/configFiles/repro/chained-delay-timing-repro.yml +13 -0
  171. package/test/configFiles/repro/count-init-repro.yml +45 -0
  172. package/test/configFiles/repro/cycle-break-repro.yml +44 -0
  173. package/test/configFiles/repro/debounce-repro.yml +46 -0
  174. package/test/configFiles/repro/diff-count-repro.yml +34 -0
  175. package/test/configFiles/repro/engine-hang-repro.yml +9 -0
  176. package/test/configFiles/repro/latch-apm-repro.yml +26 -0
  177. package/test/configFiles/repro/lockout-count-repro.yml +33 -0
  178. package/test/configFiles/repro/program-extract-repro.yml +38 -0
  179. package/test/configFiles/repro/state-latch-repro.yml +47 -0
  180. package/test/configFiles/repro/ternary-repro.yml +26 -0
  181. package/test/configFiles/transform/debounce.yml +12 -0
  182. package/test/configFiles/transform/expression.yml +34 -0
  183. package/test/configFiles/transform/ignoreValue.yml +31 -0
  184. package/test/configFiles/transform/latch.yml +11 -0
  185. package/test/configFiles/transform/latchValue.yml +31 -0
  186. package/test/configFiles/transform/logicAnd.yml +14 -0
  187. package/test/configFiles/transform/logicOr.yml +14 -0
  188. package/test/configFiles/transform/map.yml +19 -0
  189. package/test/configFiles/transform/maxLength.yml +13 -0
  190. package/test/configFiles/transform/offDelay.yml +12 -0
  191. package/test/configFiles/transform/pattern-escape.yml +10 -0
  192. package/test/configFiles/transform/pattern-match.yml +57 -0
  193. package/test/configFiles/transform/pattern-replace.yml +34 -0
  194. package/test/configFiles/transform/pattern-test.yml +25 -0
  195. package/test/configFiles/transform/reject.yml +24 -0
  196. package/test/configFiles/transform/risingEdgeCounter.yml +36 -0
  197. package/test/configFiles/transform/source.yml +20 -0
  198. package/test/configFiles/transform/state.yml +56 -0
  199. package/test/configFiles/transform/toggle.yml +19 -0
  200. package/test/configFiles/transform/whenUnavailable.yml +19 -0
  201. package/test/dataFiles/noisy-pulse.txt +11330 -0
  202. package/test/dataItems.test.js +140 -0
  203. package/test/engine-v1-tests.js +418 -0
  204. package/test/engine-v2-tests.js +284 -0
  205. package/test/expression-tests.js +171 -0
  206. package/test/expressionService.test.js +154 -0
  207. package/test/expressionServiceCondition.test.js +130 -0
  208. package/test/repro/buffer-convert-repro.test.js +38 -0
  209. package/test/repro/chained-delay-timing-repro.test.js +34 -0
  210. package/test/repro/count-init-repro.test.js +46 -0
  211. package/test/repro/cylce-break-repro.test.js +57 -0
  212. package/test/repro/debounce-repro.test.js +65 -0
  213. package/test/repro/diff-count-repro.test.js +79 -0
  214. package/test/repro/engine-hang-repro.test.js +38 -0
  215. package/test/repro/latch-apm-repro.test.js +119 -0
  216. package/test/repro/lockout-count-repro.test.js +84 -0
  217. package/test/repro/program-extract-repro.test.js +40 -0
  218. package/test/repro/state-latch-repro.test.js +63 -0
  219. package/test/repro/ternary-repro.test.js +43 -0
  220. package/test/transform/accumulte.test.js +18 -0
  221. package/test/transform/average.test.js +22 -0
  222. package/test/transform/debounce.test.js +70 -0
  223. package/test/transform/downsample.test.js +30 -0
  224. package/test/transform/edge.test.js +27 -0
  225. package/test/transform/expression.test.js +189 -0
  226. package/test/transform/fallingEdge.test.js +59 -0
  227. package/test/transform/fromBuffer.test.js +60 -0
  228. package/test/transform/hash.test.js +34 -0
  229. package/test/transform/ignoreValue.test.js +123 -0
  230. package/test/transform/invert.test.js +26 -0
  231. package/test/transform/latch.test.js +33 -0
  232. package/test/transform/latchValue.test.js +126 -0
  233. package/test/transform/logicAnd.test.js +80 -0
  234. package/test/transform/logicOr.test.js +80 -0
  235. package/test/transform/map.test.js +42 -0
  236. package/test/transform/max.test.js +30 -0
  237. package/test/transform/maxLength.test.js +32 -0
  238. package/test/transform/min.test.js +30 -0
  239. package/test/transform/minDelta.test.js +14 -0
  240. package/test/transform/offDelay.test.js +123 -0
  241. package/test/transform/onDelay.test.js +105 -0
  242. package/test/transform/patternEscape.test.js +18 -0
  243. package/test/transform/patternMatch.test.js +177 -0
  244. package/test/transform/patternReplace.test.js +95 -0
  245. package/test/transform/patternTest.test.js +105 -0
  246. package/test/transform/rateOfChange.test.js +34 -0
  247. package/test/transform/reject.test.js +56 -0
  248. package/test/transform/resample.test.js +193 -0
  249. package/test/transform/risingEdge.test.js +60 -0
  250. package/test/transform/risingEdgeCounter.test.js +227 -0
  251. package/test/transform/sampleInterval.test.js +22 -0
  252. package/test/transform/source.test.js +137 -0
  253. package/test/transform/state.test.js +248 -0
  254. package/test/transform/threshold.test.js +78 -0
  255. package/test/transform/toBuffer.test.js +60 -0
  256. package/test/transform/toggle.test.js +92 -0
  257. package/test/transform/trim.test.js +30 -0
  258. package/test/transform/valueChange.test.js +14 -0
  259. package/test/transform/valueDecrease.test.js +32 -0
  260. package/test/transform/valueIncrease.test.js +32 -0
  261. package/test/transform/valueIncreaseDiff.test.js +32 -0
  262. package/test/transform/whenUnavailable.test.js +93 -0
  263. package/test/transform/windowCount.test.js +26 -0
  264. package/test/util/testUtils.js +405 -0
@@ -0,0 +1,159 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+ const Buffer = require('buffer/').Buffer;
5
+ const TransformState = require('./transformState');
6
+
7
+ const TYPE_PATTERN = /(type=)?(?<type>((u?int)(8|16|32|64))|(float(32|64)?)|double|string)/i;
8
+ const ORDER_PATTERN = /((order=)|(8|16|32|64|float|double)|,\s*|\s+)(?<order>be|le)(,|,?\s+|$)/i;
9
+
10
+ const INT_TYPES = ['INT8', 'UINT8', 'INT16', 'UINT16', 'INT32', 'UINT32', 'INT64', 'UINT64'];
11
+ const FLOAT_TYPES = ['FLOAT', 'FLOAT32', 'DOUBLE', 'FLOAT64'];
12
+ const STRING_TYPES = ['STRING'];
13
+
14
+ /**
15
+ * Converts a value to a buffer
16
+ */
17
+ class ToBufferFilter extends TransformState {
18
+ constructor(spec) {
19
+ super();
20
+
21
+ this.spec = spec;
22
+ this.type = (this.matchPattern(spec, TYPE_PATTERN, 'type') ?? 'uint8').toUpperCase();
23
+ this.order = (this.matchPattern(spec, ORDER_PATTERN, 'order') ?? 'be').toUpperCase();
24
+ }
25
+
26
+ static op = 'to-buffer';
27
+
28
+ static create(args) {
29
+ return new ToBufferFilter(args.args.spec);
30
+ }
31
+
32
+ static parseConfig(configUtil, defn) {
33
+ const spec = _.get(defn.args, 'spec', defn.args[defn.func]);
34
+
35
+ defn.args = { spec };
36
+ }
37
+
38
+ filter(context, value, time) {
39
+ try {
40
+ if (_.isArray(value)) {
41
+ const buf = Buffer.concat(value.map(v => this.writeValue(v)));
42
+ this.commitValue(context, buf, time);
43
+ } else {
44
+ this.commitValue(context, this.writeValue(value), time);
45
+ }
46
+ } catch (err) {
47
+ this.recordError(context, err, time);
48
+ this.setUnavailable(context, time);
49
+ }
50
+ }
51
+
52
+ matchPattern(spec, pattern, group) {
53
+ const match = spec.match(pattern) ?? { };
54
+ return match?.groups?.[group];
55
+ }
56
+
57
+ writeValue(value) {
58
+ if (Buffer.isBuffer(value)) {
59
+ return value;
60
+ }
61
+
62
+ if (_.isString(value)) {
63
+ if (INT_TYPES.includes(this.type)) {
64
+ value = parseInt(value, 10);
65
+ } else if (FLOAT_TYPES.includes(this.type)) {
66
+ value = parseFloat(value);
67
+ }
68
+ } else if (STRING_TYPES.includes(this.type)) {
69
+ value = value.toString();
70
+ }
71
+
72
+ let buf;
73
+ switch (this.type) {
74
+ case 'INT8':
75
+ buf = Buffer.alloc(1);
76
+ buf.writeInt8(value, 0);
77
+ break;
78
+ case 'UINT8':
79
+ buf = Buffer.alloc(1);
80
+ buf.writeUInt8(value, 0);
81
+ break;
82
+ case 'INT16':
83
+ buf = Buffer.alloc(2);
84
+ if (this.order === 'BE') {
85
+ buf.writeInt16BE(value, 0);
86
+ } else {
87
+ buf.writeInt16LE(value, 0);
88
+ }
89
+ break;
90
+ case 'UINT16':
91
+ buf = Buffer.alloc(2);
92
+ if (this.order === 'BE') {
93
+ buf.writeUInt16BE(value, 0);
94
+ } else {
95
+ buf.writeUInt16LE(value, 0);
96
+ }
97
+ break;
98
+ case 'INT32':
99
+ buf = Buffer.alloc(4);
100
+ if (this.order === 'BE') {
101
+ buf.writeInt32BE(value, 0);
102
+ } else {
103
+ buf.writeInt32LE(value, 0);
104
+ }
105
+ break;
106
+ case 'UINT32':
107
+ buf = Buffer.alloc(4);
108
+ if (this.order === 'BE') {
109
+ buf.writeUInt32BE(value, 0);
110
+ } else {
111
+ buf.writeUInt32LE(value, 0);
112
+ }
113
+ break;
114
+ case 'INT64':
115
+ buf = Buffer.alloc(8);
116
+ if (this.order === 'BE') {
117
+ buf.writeBigInt64BE(value, 0);
118
+ } else {
119
+ buf.writeBigInt64LE(value, 0);
120
+ }
121
+ break;
122
+ case 'UINT64':
123
+ buf = Buffer.alloc(8);
124
+ if (this.order === 'BE') {
125
+ buf.writeBigUInt64BE(value, 0);
126
+ } else {
127
+ buf.writeBigUInt64LE(value, 0);
128
+ }
129
+ break;
130
+ case 'FLOAT':
131
+ case 'FLOAT32':
132
+ buf = Buffer.alloc(4);
133
+ if (this.order === 'BE') {
134
+ buf.writeFloatBE(value, 0);
135
+ } else {
136
+ buf.writeFloatLE(value, 0);
137
+ }
138
+ break;
139
+ case 'DOUBLE':
140
+ case 'FLOAT64':
141
+ buf = Buffer.alloc(8);
142
+ if (this.order === 'BE') {
143
+ buf.writeDoubleBE(value, 0);
144
+ } else {
145
+ buf.writeDoubleLE(value, 0);
146
+ }
147
+ break;
148
+ case 'STRING':
149
+ buf = Buffer.alloc(value.length);
150
+ buf.write(value, 0, value.length, 'utf8');
151
+ break;
152
+ default:
153
+ throw new Error(`Unsupported type: ${this.type}`);
154
+ }
155
+ return buf;
156
+ }
157
+ }
158
+
159
+ module.exports = ToBufferFilter;
@@ -0,0 +1,118 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+ const TransformState = require('./transformState');
5
+
6
+ /**
7
+ * Alternates between T and F on every rising edge (F->T) transition
8
+ *
9
+ * For every incoming sample, the current toggle state will be emitted. When a rising edge is found,
10
+ * the toggle will flip before the output value is emitted.
11
+ */
12
+ class ToggleFilter extends TransformState {
13
+ constructor({ engine, path, args: { init = false, reset = {} } }) {
14
+ super();
15
+
16
+ this.engine = engine;
17
+ this.resetExpression = reset.compiledExpression;
18
+ this.resetSources = engine.expressionService.expressionTriggers(`${path}.reset`);
19
+
20
+ this.defaultValue = init;
21
+ this.initReset();
22
+ }
23
+
24
+ static op = 'toggle';
25
+
26
+ static create(args) {
27
+ return new ToggleFilter(args);
28
+ }
29
+
30
+ static parseConfig(configUtil, defn) {
31
+ configUtil.requireObjectParams(defn.args, this.op, {
32
+ optional: true,
33
+ availableAttributes: ['default', 'reset'],
34
+ });
35
+
36
+ let init = defn.args.default;
37
+ if (!_.isUndefined(init)) {
38
+ init = configUtil.parseBool(init);
39
+ }
40
+
41
+ let reset = defn.args.reset;
42
+ if (!_.isUndefined(reset)) {
43
+ try {
44
+ const expression = reset.toString();
45
+ const compiledExpression = configUtil.compileExpression(expression, this.op);
46
+ reset = { expression, compiledExpression };
47
+ } catch (err) {
48
+ configUtil.throwConfigError(`Problem evaluating expression: ${err.message}`, this.op);
49
+ }
50
+ }
51
+
52
+ defn.args = { init, reset };
53
+ }
54
+
55
+ static getExpressions(configUtil, body, info) {
56
+ return configUtil.getExpressions(body, { ...info, field: 'reset' });
57
+ }
58
+
59
+ initReset() {
60
+ this.value = this.defaultValue;
61
+ this.previousValue = this.defaultValue;
62
+ this.previousInputValue = this.defaultValue;
63
+ this.toggle = this.defaultValue;
64
+ }
65
+
66
+ update(context, value, time) {
67
+ this.lastSampleTime = time;
68
+ if (!this.available) {
69
+ this.available = true;
70
+ this.previousInputValue = value;
71
+
72
+ // Don't change when becoming available already in a high state
73
+ this.emitUpdate(context);
74
+ return;
75
+ }
76
+
77
+ this.filter(context, value, time);
78
+ this.emitUpdate(context);
79
+ }
80
+
81
+ filter(context, value, time) {
82
+ if (value && value !== this.previousInputValue) {
83
+ this.commitValue(context, !this.value, time);
84
+ }
85
+ this.previousInputValue = value;
86
+ }
87
+
88
+ emitUpdate(context) {
89
+ // While resetExpression evals true, force count to 0
90
+ if (this.resetExpression) {
91
+ const localState = { this: this.value };
92
+ const unavailTerm = this.resetSources.reduce((res, name) => {
93
+ const state = this.engine.getState(name);
94
+ localState[name] = state?.value;
95
+ return res || !state || !state.available;
96
+ }, false);
97
+
98
+ try {
99
+ if (!unavailTerm && this.resetExpression.evaluate(localState)) {
100
+ this.value = this.defaultValue;
101
+ this.emit('update', this.value, this.lastSampleTime, context);
102
+ return;
103
+ }
104
+ } catch (err) {
105
+ this.recordError(context, err, this.lastSampleTime);
106
+ }
107
+ }
108
+
109
+ super.emitUpdate(context);
110
+ }
111
+
112
+ setUnavailable(context, time) {
113
+ super.setUnavailable(context, time);
114
+ this.initReset();
115
+ }
116
+ }
117
+
118
+ module.exports = ToggleFilter;
@@ -0,0 +1,193 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+ const EventEmitter = require('eventemitter3');
5
+
6
+ class TransformState extends EventEmitter {
7
+ constructor() {
8
+ super();
9
+
10
+ this.available = false;
11
+ this.forceEmit = false;
12
+ this.defaultValue = 0;
13
+ this.value = 0;
14
+ this.valueTime = -1;
15
+ this.previousValue = 0;
16
+ this.previousValueTime = -1;
17
+ this.lastSampleTime = -1;
18
+ this.lastEmitValue = -1;
19
+ this.lastEmitTime = -1;
20
+ this.pendingChangeTime = null;
21
+ this.next = null;
22
+ this.lastSuppliedValue = undefined;
23
+ this.lastError = null;
24
+ this.lastErrorTime = 0;
25
+ this.errorCount = 0;
26
+ }
27
+
28
+ static op = 'no-op';
29
+
30
+ static opAliases = [];
31
+
32
+ static parseConfig(_configUtil, _defn) {
33
+ // Each class implements its own static ParseConfig
34
+ }
35
+
36
+ static getExpressions(_configUtil, _body, _info) {
37
+ return [];
38
+ }
39
+
40
+ static getStringExpressions(_configUtil, _body, _info) {
41
+ return [];
42
+ }
43
+
44
+ // A transform state that supports pending (future) state changes not necessarily tied to a specific input
45
+ // should override this to return true. For example, a transform that reverts its state to another value
46
+ // after 3 seconds may have a pending change.
47
+ supportsPendingChanges() {
48
+ return false;
49
+ }
50
+
51
+ // The exact time in the future that the transform's state should change if it receives no further input.
52
+ // If there is no pending change, return null.
53
+ // Optional time argument lets a transform decide if a pending change should still be reported based on the
54
+ // amount of time that has elapsed since the last update.
55
+ nextPendingChange(_time) {
56
+ return this.pendingChangeTime;
57
+ }
58
+
59
+ update(context, value, time) {
60
+ if (!this.available) {
61
+ this.available = true;
62
+ this.forceEmit = true;
63
+ }
64
+
65
+ this.lastSuppliedValue = value;
66
+ this.lastSampleTime = time;
67
+ this.filter(context, value, time);
68
+ if (this.available) {
69
+ this.emitUpdate(context);
70
+ }
71
+ }
72
+
73
+ probe(context, time) {
74
+ if (!_.isUndefined(this.lastSuppliedValue)) {
75
+ this.forceEmit = true;
76
+ this.lastSampleTime = time;
77
+ this.filter(context, this.lastSuppliedValue, time);
78
+ this.emitUpdate(context);
79
+ } else {
80
+ this.setUnavailable(context, time);
81
+ }
82
+ }
83
+
84
+ probeOrForward(context, time) {
85
+ if (!this.next) {
86
+ this.probe(context, time);
87
+ return;
88
+ }
89
+
90
+ const thisChange = this.nextPendingChange(time);
91
+ const nextChange = this.next.chainNextPendingChange(time);
92
+ if (nextChange && (!thisChange || nextChange < thisChange)) {
93
+ this.next.probeOrForward(context, time);
94
+ } else {
95
+ this.probe(context, time);
96
+ }
97
+ }
98
+
99
+ filter(context, value, time) {
100
+ this.commitValue(context, value, time);
101
+ }
102
+
103
+ commitValue(context, value, time) {
104
+ if (value !== this.value) {
105
+ this.previousValue = this.value;
106
+ this.previousValueTime = this.valueTime;
107
+ this.value = value;
108
+ this.valueTime = time;
109
+ }
110
+ }
111
+
112
+ setUnavailable(context, time) {
113
+ this.available = false;
114
+ this.previousValue = this.value;
115
+ this.previousValueTime = this.valueTime;
116
+ this.value = this.defaultValue;
117
+ this.valueTime = time;
118
+ this.emit('unavailable', time, context);
119
+ }
120
+
121
+ emitUpdate(context) {
122
+ if (this.forceEmit || this.value !== this.lastEmitValue || this.lastSampleTime !== this.lastEmitTime) {
123
+ this.forceEmit = false;
124
+ this.lastEmitValue = this.value;
125
+ this.lastEmitTime = this.lastSampleTime;
126
+ this.emit('update', this.value, this.lastSampleTime, context);
127
+ }
128
+ }
129
+
130
+ emitCommittedUpdate(context) {
131
+ if (this.forceEmit || this.value !== this.lastEmitValue || this.valueTime !== this.lastEmitTime) {
132
+ this.forceEmit = false;
133
+ this.lastEmitValue = this.value;
134
+ this.lastEmitTime = this.valueTime;
135
+ this.emit('update', this.value, this.valueTime, context);
136
+ }
137
+ }
138
+
139
+ chain(nextFilter) {
140
+ if (this.next) {
141
+ this.next.chain(nextFilter);
142
+ } else {
143
+ this.next = nextFilter;
144
+ this.on('update', (value, time, context) => nextFilter.update(context, value, time));
145
+ this.on('unavailable', (time, context) => nextFilter.setUnavailable(context, time));
146
+ }
147
+
148
+ return this;
149
+ }
150
+
151
+ chainEnd() {
152
+ if (this.next === null) {
153
+ return this;
154
+ }
155
+ return this.next.chainEnd();
156
+ }
157
+
158
+ chainSupportsPendingChange() {
159
+ if (this.supportsPendingChanges()) {
160
+ return true;
161
+ } else if (this.next) {
162
+ return this.next.chainSupportsPendingChange();
163
+ }
164
+ return false;
165
+ }
166
+
167
+ // Find the soonest pending change for all transforms remaining in this chain, if any
168
+ chainNextPendingChange(time) {
169
+ if (!this.next) {
170
+ return this.nextPendingChange(time);
171
+ }
172
+
173
+ const nextChange = this.next.chainNextPendingChange(time);
174
+ const thisChange = this.nextPendingChange(time);
175
+ if (nextChange === null) {
176
+ return thisChange;
177
+ } else if (thisChange === null) {
178
+ return nextChange;
179
+ }
180
+
181
+ return Math.min(thisChange, nextChange);
182
+ }
183
+
184
+ recordError(context, err, time = 0) {
185
+ this.errorCount += 1;
186
+ this.lastError = err;
187
+ this.lastErrorContext = context;
188
+ this.lastEmitTime = time;
189
+ this.emit('error', err, time, context);
190
+ }
191
+ }
192
+
193
+ module.exports = TransformState;
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+
3
+ /* eslint-disable no-control-regex */
4
+
5
+ const _ = require('lodash');
6
+ const TransformState = require('./transformState');
7
+
8
+ /**
9
+ * Trims any whitespace or nonprintable characters from ends of string samples.
10
+ */
11
+ class TrimFilter extends TransformState {
12
+ static op = 'trim';
13
+
14
+ static create(_args) {
15
+ return new TrimFilter();
16
+ }
17
+
18
+ filter(context, value, time) {
19
+ if (_.isString(value)) {
20
+ value = value.replace(/^[\x00-\x20\x7F\xA0]*/, '');
21
+ value = value.replace(/[\x00-\x20\x7F\xA0]*$/, '');
22
+ }
23
+ this.commitValue(context, value, time);
24
+ }
25
+ }
26
+
27
+ module.exports = TrimFilter;
@@ -0,0 +1,96 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+
5
+ class ChainSource {
6
+ constructor(transform, { engine, path, rootPath }, { onWatchSource, onWatchChainSource } = {}) {
7
+ this.transform = transform;
8
+ this.onWatchSource = onWatchSource;
9
+ this.onWatchChainSource = onWatchChainSource;
10
+ this.rootPath = rootPath;
11
+
12
+ if (!_.isArray(path)) {
13
+ path = [path];
14
+ }
15
+
16
+ this.selfSources = _(path).map(p => engine.expressionService.expressionTriggers(p)).flatten().union().value();
17
+ this.triggerSources = engine.expressionService.expressionTriggers(rootPath);
18
+ this.watchedSources = [];
19
+
20
+ const checkName = (name) => {
21
+ if (_.includes(this.selfSources, name)) {
22
+ this.watchSource(engine.getState(name));
23
+ } else if (_.includes(this.triggerSources, name)) {
24
+ this.watchChainSource(engine.getState(name));
25
+ }
26
+ };
27
+
28
+ _.each(this.triggerSources, checkName);
29
+
30
+ engine.on('add-item', state => checkName(state.name));
31
+ }
32
+
33
+ watchSource(state) {
34
+ if (!state || _.includes(this.watchedSources, state.name)) {
35
+ return;
36
+ }
37
+
38
+ state.on('update-unfiltered', (value, time, context) => {
39
+ const newContext = {
40
+ sourceType: 'chainRoot-update',
41
+ path: this.rootPath,
42
+ trigger: state.name,
43
+ prevContext: context,
44
+ };
45
+ this.transform.update(newContext, value, time);
46
+ });
47
+
48
+ state.on('unavailable', (time, context) => {
49
+ const newContext = {
50
+ sourceType: 'chainRoot-unavail',
51
+ path: this.rootPath,
52
+ trigger: state.name,
53
+ prevContext: context,
54
+ };
55
+ this.transform.setUnavailable(newContext, time);
56
+ });
57
+
58
+ this.watchedSources.push(state.name);
59
+ if (this.onWatchSource) {
60
+ this.onWatchSource(state.name);
61
+ }
62
+ }
63
+
64
+ watchChainSource(state) {
65
+ if (!state || _.includes(this.watchedSources, state.name)) {
66
+ return;
67
+ }
68
+
69
+ state.on('update-unfiltered', (value, time, context) => {
70
+ const newContext = {
71
+ sourceType: 'chain-update',
72
+ path: this.rootPath,
73
+ trigger: state.name,
74
+ prevContext: context,
75
+ };
76
+ this.transform.probe(newContext, time);
77
+ });
78
+
79
+ state.on('unavailable', (time, context) => {
80
+ const newContext = {
81
+ sourceType: 'chain-unavail',
82
+ path: this.rootPath,
83
+ trigger: state.name,
84
+ prevContext: context,
85
+ };
86
+ this.transform.probe(newContext, time);
87
+ });
88
+
89
+ this.watchedSources.push(state.name);
90
+ if (this.onWatchChainSource) {
91
+ this.onWatchChainSource(state.name);
92
+ }
93
+ }
94
+ }
95
+
96
+ module.exports = ChainSource;
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+
5
+ class RingBuffer {
6
+ constructor(size) {
7
+ this.size = size;
8
+ this.buffer = _.times(size, _.constant(0));
9
+ this.pointer = 0;
10
+ }
11
+
12
+ get(key) {
13
+ return this.buffer[key % this.size];
14
+ }
15
+
16
+ getHeadIndex() {
17
+ return (this.pointer + (this.size - 1)) % this.size;
18
+ }
19
+
20
+ getTailIndex() {
21
+ return this.pointer;
22
+ }
23
+
24
+ push(value) {
25
+ this.buffer[this.pointer] = value;
26
+ this.pointer = (this.size + this.pointer + 1) % this.size;
27
+ }
28
+
29
+ values() {
30
+ return this.buffer;
31
+ }
32
+ }
33
+
34
+ module.exports = RingBuffer;
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ const TransformState = require('./transformState');
4
+
5
+ /**
6
+ * Emits a T signal each time the value changes in a stream.
7
+ */
8
+ class ValueChangeFilter extends TransformState {
9
+ constructor() {
10
+ super();
11
+
12
+ this.defaultValue = false;
13
+ this.value = false;
14
+ this.previousValue = false;
15
+ }
16
+
17
+ static op = 'value-change';
18
+
19
+ static create(_args) {
20
+ return new ValueChangeFilter();
21
+ }
22
+
23
+ emitUpdate(context) {
24
+ if (this.forceEmit || this.valueTime === this.lastSampleTime) {
25
+ this.forceEmit = false;
26
+ this.lastEmitValue = this.value;
27
+ this.lastEmitTime = this.lastSampleTime;
28
+ this.emit('update', true, this.lastSampleTime, context);
29
+ }
30
+ this.emit('update', false, this.lastSampleTime, context);
31
+ }
32
+ }
33
+
34
+ module.exports = ValueChangeFilter;
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+
3
+ const TransformState = require('./transformState');
4
+
5
+ /**
6
+ * Emits a T signal each time the value decreases in a stream.
7
+ */
8
+ class ValueDecreaseFilter extends TransformState {
9
+ constructor() {
10
+ super();
11
+
12
+ this.defaultValue = false;
13
+ this.value = false;
14
+ this.previousValue = false;
15
+ }
16
+
17
+ static op = 'value-decrease';
18
+
19
+ static create(_args) {
20
+ return new ValueDecreaseFilter();
21
+ }
22
+
23
+ filter(context, value, time) {
24
+ this.commitValue(context, +value, time);
25
+ }
26
+
27
+ emitUpdate(context) {
28
+ if (this.forceEmit || (this.value < this.previousValue && this.valueTime === this.lastSampleTime)) {
29
+ this.forceEmit = false;
30
+ this.lastEmitValue = this.value;
31
+ this.lastEmitTime = this.lastSampleTime;
32
+ this.emit('update', true, this.lastSampleTime, context);
33
+ }
34
+ this.emit('update', false, this.lastSampleTime, context);
35
+ }
36
+ }
37
+
38
+ module.exports = ValueDecreaseFilter;