@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,70 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+ const ConfigError = require('../configError');
5
+
6
+ class MqttBaseConfig {
7
+ constructor(expressionService) {
8
+ this.expressionService = expressionService;
9
+
10
+ _.each(this.getIdentifiers(expressionService.config), name => this.expressionService.addName(name));
11
+ }
12
+
13
+ getIdentifiers() {
14
+ return [];
15
+ }
16
+
17
+ parse(config = {}) {
18
+ this.endpoint = this.checkEndpoint(config);
19
+ this.telemetryRate = this.checkTelemetryRate(config);
20
+
21
+ this.username = this.checkUsername(config);
22
+ this.password = this.checkPassword(config);
23
+ }
24
+
25
+ checkEndpoint(defn) {
26
+ const endpoint = defn.endpoint;
27
+ if (_.isEmpty(endpoint)) {
28
+ throw new ConfigError('An MQTT endpoint must be specified').atPath('endpoint');
29
+ }
30
+
31
+ return endpoint;
32
+ }
33
+
34
+ checkTelemetryRate(defn) {
35
+ const rate = _.get(defn, 'telemetry-rate', 1);
36
+ if (!_.isNumber(rate) || rate <= 0) {
37
+ throw new ConfigError('telemetry-rate must be >= 0').atPath('telemetry-rate');
38
+ }
39
+
40
+ return rate;
41
+ }
42
+
43
+ checkUsername(defn) {
44
+ const username = defn.username;
45
+ if (_.isEmpty(username)) {
46
+ return null;
47
+ }
48
+
49
+ if (!_.isString(username) && !_.isNumber(username)) {
50
+ throw new ConfigError('Username must be a string').atPath('username');
51
+ }
52
+
53
+ return String(username);
54
+ }
55
+
56
+ checkPassword(defn) {
57
+ const password = defn.password;
58
+ if (_.isEmpty(password)) {
59
+ return null;
60
+ }
61
+
62
+ if (!_.isString(password) && !_.isNumber(password)) {
63
+ throw new ConfigError('Password must be a string').atPath('password');
64
+ }
65
+
66
+ return String(password);
67
+ }
68
+ }
69
+
70
+ module.exports = MqttBaseConfig;
@@ -0,0 +1,113 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+ const ConfigError = require('../configError');
5
+ const MqttBaseConfig = require('./mqttBaseConfig');
6
+ const validations = require('../common/validations');
7
+ const { checkJsonPropPath } = require('../common/jsonPath');
8
+
9
+ class MqttConfig extends MqttBaseConfig {
10
+ // eslint-disable-next-line no-useless-constructor
11
+ constructor(expressionService) {
12
+ super(expressionService);
13
+ }
14
+
15
+ getIdentifiers(config = {}) {
16
+ const keys = [];
17
+ _.each(config.topics, (val) => {
18
+ if (_.isString(val)) {
19
+ keys.push(val);
20
+ } else if (_.isObject(val?.json)) {
21
+ keys.push(...Object.keys(val.json));
22
+ }
23
+ });
24
+
25
+ return _.map(keys, (name) => {
26
+ return {
27
+ name,
28
+ channel: 'device',
29
+ defaultValue: 0,
30
+ };
31
+ });
32
+ }
33
+
34
+ device() {
35
+ return 'mqtt';
36
+ }
37
+
38
+ parse(config = {}) {
39
+ this.endpoint = this.checkEndpoint(config);
40
+ this.telemetryRate = this.checkTelemetryRate(config);
41
+
42
+ this.username = this.checkUsername(config);
43
+ this.password = this.checkPassword(config);
44
+
45
+ this.topics = this.loadTopics(config.topics);
46
+ this.reset = validations.checkReset(config);
47
+ }
48
+
49
+ loadTopics(topics = {}) {
50
+ return _(topics).map((defn, topic) => {
51
+ let subSection = '';
52
+ try {
53
+ if (_.isString(defn)) {
54
+ defn = { type: 'raw', key: defn };
55
+ }
56
+
57
+ if (!_.isObject(defn)) {
58
+ throw new ConfigError('Unexpected topic structure');
59
+ }
60
+
61
+ const type = this.checkType(defn);
62
+ if (type === 'raw') {
63
+ // Simple raw topic value
64
+ return {
65
+ topic,
66
+ type: 'raw',
67
+ key: defn.key,
68
+ };
69
+ } else if (type === 'json') {
70
+ subSection = 'json';
71
+ this.checkJson(defn);
72
+
73
+ // JSON prop collection
74
+ return {
75
+ topic,
76
+ type: 'json',
77
+ json: defn.json,
78
+ };
79
+ }
80
+
81
+ throw new ConfigError('Unexpected topic type');
82
+ } catch (error) {
83
+ if (error instanceof ConfigError) {
84
+ let section = `topics.${topic}`;
85
+ if (subSection) {
86
+ section += `.${subSection}`;
87
+ }
88
+ error.atSection(section);
89
+ }
90
+ throw error;
91
+ }
92
+ }).keyBy('topic').value();
93
+ }
94
+
95
+ checkType(defn) {
96
+ const type = defn.type ?? 'json';
97
+ if (!_.includes(['raw', 'json'], type)) {
98
+ throw new ConfigError('Type must be one of [raw, json]').atAttribute('type');
99
+ }
100
+ return type;
101
+ }
102
+
103
+ checkJson(defn) {
104
+ const json = defn.json;
105
+ if (!_.isObject(json)) {
106
+ throw new ConfigError('JSON block must be collection of prop names and JSON paths').atAttribute('json');
107
+ }
108
+
109
+ _.each(json, (prop, key) => checkJsonPropPath(prop, key));
110
+ }
111
+ }
112
+
113
+ module.exports = MqttConfig;
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+ const ConfigError = require('../configError');
5
+ const MqttBaseConfig = require('./mqttBaseConfig');
6
+
7
+ const fixedKeys = [
8
+ // telemetry
9
+ 'current', 'voltage', 'wire_speed', 'motor_current', 'true_energy', 'weld_record',
10
+ // summary
11
+ 'summary_avg_current', 'summary_avg_voltage', 'summary_avg_wire_speed', 'summary_avg_motor_current',
12
+ 'summary_avg_gas_flow', 'consumable_density', 'consumable_diameter', 'summary_duration',
13
+ // derived
14
+ 'execution', 'weldcount', 'consumable_rate', 'control_board_serial', 'observed_serials',
15
+ 'summary_wire_length', 'summary_wire_mass',
16
+ ];
17
+
18
+ class MqttLincolnConfig extends MqttBaseConfig {
19
+ // eslint-disable-next-line no-useless-constructor
20
+ constructor(expressionService) {
21
+ super(expressionService);
22
+ }
23
+
24
+ getIdentifiers() {
25
+ return _.map(fixedKeys, (name) => {
26
+ return {
27
+ name,
28
+ channel: 'device',
29
+ defaultValue: 0,
30
+ };
31
+ });
32
+ }
33
+
34
+ parse(config = {}) {
35
+ this.endpoint = this.checkEndpoint(config);
36
+ this.telemetryRate = this.checkTelemetryRate(config);
37
+ this.serial = this.checkSerial(config);
38
+ this.username = this.checkUsername(config);
39
+ this.password = this.checkPassword(config);
40
+ }
41
+
42
+ postParse(config) {
43
+ _.each(fixedKeys, (key) => {
44
+ config.adapter.loadDataItem(key, key);
45
+ });
46
+ }
47
+
48
+ checkSerial(defn) {
49
+ const serial = defn.serial;
50
+ if (_.isNil(serial)) {
51
+ return null;
52
+ }
53
+
54
+ if (!_.isString(serial) && !_.isNumber(serial)) {
55
+ throw new ConfigError('Invalid serial').atPath('serial');
56
+ }
57
+
58
+ return String(serial);
59
+ }
60
+ }
61
+
62
+ module.exports = MqttLincolnConfig;
@@ -0,0 +1,42 @@
1
+ 'use strict';
2
+
3
+ const MTConnectBaseConfig = require('./mtconnectBaseConfig');
4
+ const ConfigError = require('../configError');
5
+
6
+ class MTConnectAdapterConfig extends MTConnectBaseConfig {
7
+ parse(config = {}) {
8
+ super.parse(config);
9
+
10
+ if (config.device !== this.device()) {
11
+ return;
12
+ }
13
+
14
+ this.parseEndpoint(config.endpoint);
15
+ // TODO: Chase down local address translation
16
+ }
17
+
18
+ device() {
19
+ return 'mtconnect-adapter';
20
+ }
21
+
22
+ parseEndpoint(endpoint) {
23
+ if (!endpoint) {
24
+ throw new ConfigError('No endpoint defined').atAttribute('endpoint');
25
+ }
26
+
27
+ if (endpoint.match(/^local\s+/)) {
28
+ this.localAddress = endpoint.replace(/^local\s+/, '');
29
+ return;
30
+ }
31
+
32
+ if (!endpoint.match(/^[\w.]+(:\d+)?$/)) {
33
+ throw new ConfigError('Invalid endpoint').atAttribute('endpoint');
34
+ }
35
+
36
+ const [host, port] = endpoint.split(':');
37
+ this.host = host;
38
+ this.port = port || 7878;
39
+ }
40
+ }
41
+
42
+ module.exports = MTConnectAdapterConfig;
@@ -0,0 +1,136 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+ const AllowDenyList = require('../common/allowDenyList');
5
+ const ConfigError = require('../configError');
6
+ const validations = require('../common/validations');
7
+
8
+ const allowDeny = new AllowDenyList();
9
+
10
+ class MTConnectBaseConfig {
11
+ constructor(expressionService) {
12
+ this.expressionService = expressionService;
13
+
14
+ _.each(this.getIdentifiers(expressionService.config), name => this.expressionService.addName(name));
15
+ }
16
+
17
+ getIdentifiers(config = {}) {
18
+ return _.map(config['declare-keys'], (name) => {
19
+ if (_.isObject(name)) {
20
+ name = _.first(_.keys(name));
21
+ }
22
+
23
+ return {
24
+ name,
25
+ channel: 'device',
26
+ defaultValue: 0,
27
+ };
28
+ });
29
+ }
30
+
31
+ parse(config = {}) {
32
+ if (config.device !== this.device()) {
33
+ return;
34
+ }
35
+
36
+ this.mtconnectPassthrough = this.parsePassthrough(config);
37
+ this.allowKeyPatterns = allowDeny.parseWildcardKeyList(config['allow-keys'], 'allow-keys');
38
+ this.denyKeyPatterns = allowDeny.parseWildcardKeyList(config['deny-keys'], 'deny-keys');
39
+ this.keys = this.parseDeclaredKeysList(config);
40
+ this.enforceAvailCheck = validations.checkBoolean(config, 'enforce-avail-check', { defaultValue: true });
41
+
42
+ this.sourceKeys = _(this.keys).map('source').uniq().value();
43
+
44
+ this.aliasMap = this.createAliasMap(this.keys);
45
+ }
46
+
47
+ mtconnectKeys() {
48
+ return this.keys;
49
+ }
50
+
51
+ parsePassthrough(config) {
52
+ if (_.isUndefined(config['mtconnect-passthrough'])) {
53
+ return true;
54
+ }
55
+ return !!config['mtconnect-passthrough'];
56
+ }
57
+
58
+ parseDeclaredKeysList(config) {
59
+ let attribute = 'declare-keys';
60
+ if (!config[attribute] && config.keys) {
61
+ attribute = 'keys';
62
+ }
63
+
64
+ const keys = config[attribute];
65
+ if (_.isEmpty(keys)) {
66
+ return [];
67
+ }
68
+ if (!_.isArray(keys)) {
69
+ throw new ConfigError('list expected').atAttribute(attribute);
70
+ }
71
+
72
+ const parsedKeys = _(keys).map((key) => {
73
+ if (_.isObject(key)) {
74
+ const name = _.first(_.keys(key));
75
+ const body = key[name];
76
+ if (_.isObject(body)) {
77
+ let sourceName = name;
78
+ if (_.has(body, 'key')) {
79
+ sourceName = body.key;
80
+ }
81
+ const type = body.type ?? 'value';
82
+
83
+ return {
84
+ name,
85
+ source: sourceName,
86
+ type,
87
+ };
88
+ }
89
+
90
+ return {
91
+ name,
92
+ source: body,
93
+ type: 'value',
94
+ };
95
+ }
96
+
97
+ return {
98
+ name: key,
99
+ source: key,
100
+ type: 'value',
101
+ };
102
+ }).value();
103
+
104
+ // Check for duplicate key names
105
+ const keyNames = _.map(parsedKeys, 'name');
106
+ const duplicates = _(keyNames)
107
+ .groupBy()
108
+ .pickBy(x => x.length > 1)
109
+ .keys()
110
+ .value();
111
+
112
+ if (duplicates.length > 0) {
113
+ throw new ConfigError(`duplicate key names found: ${duplicates.join(', ')}`).atAttribute(attribute);
114
+ }
115
+
116
+ return _.keyBy(parsedKeys, 'name');
117
+ }
118
+
119
+ createAliasMap(keys) {
120
+ const aliasMap = {};
121
+ const sourceKeys = _(keys).map('source').uniq().value();
122
+
123
+ const aliasNames = _.uniq(_.map(_.filter(keys, k => k.name !== k.source), 'name'));
124
+ _.each(sourceKeys, (sourceKey) => {
125
+ aliasMap[sourceKey] = [];
126
+ });
127
+ _.each(aliasNames, (alias) => {
128
+ const key = keys[alias];
129
+ aliasMap[key.source].push(alias);
130
+ });
131
+
132
+ return aliasMap;
133
+ }
134
+ }
135
+
136
+ module.exports = MTConnectBaseConfig;
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+
3
+ const MTConnectBaseConfig = require('./mtconnectBaseConfig');
4
+ const ConfigError = require('../configError');
5
+
6
+ class MTConnectConfig extends MTConnectBaseConfig {
7
+ constructor(expressionService) {
8
+ super(expressionService);
9
+ this.defaultPort = '5000';
10
+ }
11
+
12
+ parse(config = {}) {
13
+ super.parse(config);
14
+
15
+ if (config.device !== this.device()) {
16
+ return;
17
+ }
18
+
19
+ this.endpoint = this.parseEndpoint(config.endpoint);
20
+ this.verboseScanning = !!config['verbose-scanning'];
21
+ this.haasMode = !!config['haas-mode'];
22
+ }
23
+
24
+ device() {
25
+ return 'mtconnect';
26
+ }
27
+
28
+ parseEndpoint(endpointString) {
29
+ if (!endpointString) {
30
+ throw new ConfigError('No endpoint defined').atAttribute('endpoint');
31
+ }
32
+
33
+ // HTTP/S URLs are returned directly
34
+ if (endpointString.match(/^https?:\/\//)) {
35
+ return endpointString;
36
+ } else if (endpointString.match(/^\w+:\/\//)) {
37
+ throw new ConfigError('Unexpected endpoint protocol').atAttribute('endpoint');
38
+ }
39
+
40
+ let endpoint = endpointString;
41
+ // If no port is provided, specify 5000 by default
42
+ if (!endpointString.match(/:\d+/)) {
43
+ const [baseEndpoint, ...path] = endpointString.split('/');
44
+ endpoint = `${baseEndpoint}:${this.defaultPort}/${path.join('/')}`;
45
+ }
46
+
47
+ // Assume HTTP protocol by default
48
+ return `http://${endpoint}`;
49
+ }
50
+ }
51
+
52
+ module.exports = MTConnectConfig;
@@ -0,0 +1,15 @@
1
+ const MTConnectConfig = require('./mtconnectConfig');
2
+
3
+ class MTConnectHaas extends MTConnectConfig {
4
+ constructor(expressionService) {
5
+ super(expressionService);
6
+ this.defaultPort = '8082';
7
+ this.haasMode = 1;
8
+ }
9
+
10
+ device() {
11
+ return 'mtconnect-haas';
12
+ }
13
+ }
14
+
15
+ module.exports = MTConnectHaas;
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+ const ConfigError = require('../configError');
5
+
6
+ class NullConfig {
7
+ constructor(expressionService) {
8
+ this.expressionService = expressionService;
9
+
10
+ _.each(this.getIdentifiers(expressionService.config), name => this.expressionService.addName(name));
11
+ }
12
+
13
+ getIdentifiers(config = {}) {
14
+ return _.map(config['declare-keys'], (name) => {
15
+ if (_.isObject(name)) {
16
+ name = _.first(_.keys(name));
17
+ }
18
+
19
+ return {
20
+ name,
21
+ channel: 'device',
22
+ defaultValue: 0,
23
+ };
24
+ });
25
+ }
26
+
27
+ parse(config = {}) {
28
+ if (config.device !== 'local') {
29
+ return;
30
+ }
31
+
32
+ this.keys = this.parseDeclaredKeysList(config);
33
+ this.sourceKeys = _(this.keys).map('source').uniq().value();
34
+ }
35
+
36
+ parseDeclaredKeysList(config) {
37
+ let attribute = 'declare-keys';
38
+ if (!config[attribute] && config.keys) {
39
+ attribute = 'keys';
40
+ }
41
+
42
+ const keys = config[attribute];
43
+ if (_.isEmpty(keys)) {
44
+ return [];
45
+ }
46
+ if (!_.isArray(keys)) {
47
+ throw new ConfigError('list expected').atAttribute(attribute);
48
+ }
49
+
50
+ return _(keys).map((key) => {
51
+ if (_.isObject(key)) {
52
+ const name = _.first(_.keys(key));
53
+ const body = key[name];
54
+ if (_.isObject(body)) {
55
+ const sourceName = body.key;
56
+ const type = body.type;
57
+
58
+ return {
59
+ name,
60
+ source: sourceName,
61
+ type,
62
+ };
63
+ }
64
+
65
+ return {
66
+ name,
67
+ source: name,
68
+ type: body,
69
+ };
70
+ }
71
+
72
+ return {
73
+ name: key,
74
+ source: key,
75
+ type: 'value',
76
+ };
77
+ }).keyBy('name').value();
78
+ }
79
+ }
80
+
81
+ module.exports = NullConfig;