@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.
- package/.circleci/config.yml +141 -0
- package/.eslintrc.json +36 -0
- package/.gitattributes +12 -0
- package/CHANGELOG.md +544 -0
- package/README.md +2 -0
- package/index.js +17 -0
- package/lib/config/adapterConfig.js +535 -0
- package/lib/config/common/allowDenyList.js +58 -0
- package/lib/config/common/jsonPath.js +44 -0
- package/lib/config/common/labjackU3T4Common.js +227 -0
- package/lib/config/common/validations.js +142 -0
- package/lib/config/configError.js +32 -0
- package/lib/config/device/adam-6052Config.js +103 -0
- package/lib/config/device/brotherHTTPConfig.js +215 -0
- package/lib/config/device/ethernetIPConfig.js +191 -0
- package/lib/config/device/ifmIotConfig.js +245 -0
- package/lib/config/device/jsonHttpConfig.js +76 -0
- package/lib/config/device/labjackT4Config.js +39 -0
- package/lib/config/device/labjackT7Config.js +192 -0
- package/lib/config/device/labjackU3Config.js +32 -0
- package/lib/config/device/modbusTcpConfig.js +336 -0
- package/lib/config/device/mqttBaseConfig.js +70 -0
- package/lib/config/device/mqttConfig.js +113 -0
- package/lib/config/device/mqttLincolnConfig.js +62 -0
- package/lib/config/device/mtconnectAdapterConfig.js +42 -0
- package/lib/config/device/mtconnectBaseConfig.js +136 -0
- package/lib/config/device/mtconnectConfig.js +52 -0
- package/lib/config/device/mtconnectHaasConfig.js +15 -0
- package/lib/config/device/nullConfig.js +81 -0
- package/lib/config/device/opcuaConfig.js +205 -0
- package/lib/config/device/pcccConfig.js +88 -0
- package/lib/config/engineConfigV1.js +382 -0
- package/lib/config/engineConfigV2.js +106 -0
- package/lib/config/generator/counterGenConfig.js +15 -0
- package/lib/config/generator/cronGenConfig.js +33 -0
- package/lib/config/generator/dateTimeGenConfig.js +33 -0
- package/lib/config/index.js +339 -0
- package/lib/config/transformConfigUtil.js +296 -0
- package/lib/engine/dataOutput.js +357 -0
- package/lib/engine/deviceOutput.js +186 -0
- package/lib/engine/engineV1.js +480 -0
- package/lib/engine/engineV2.js +719 -0
- package/lib/engine/index.js +34 -0
- package/lib/engine/transformBuilderV1.js +111 -0
- package/lib/engine/transformBuilderV2.js +74 -0
- package/lib/expressionService.js +330 -0
- package/lib/math.js +142 -0
- package/lib/transform/accumulate.js +98 -0
- package/lib/transform/average.js +56 -0
- package/lib/transform/debounce.js +152 -0
- package/lib/transform/downsample.js +69 -0
- package/lib/transform/edge.js +34 -0
- package/lib/transform/expression.js +91 -0
- package/lib/transform/fallingEdge.js +70 -0
- package/lib/transform/fromBuffer.js +89 -0
- package/lib/transform/hash.js +41 -0
- package/lib/transform/ignoreValue.js +118 -0
- package/lib/transform/index.js +93 -0
- package/lib/transform/invert.js +25 -0
- package/lib/transform/latch.js +99 -0
- package/lib/transform/latchValue.js +115 -0
- package/lib/transform/logicAnd.js +67 -0
- package/lib/transform/logicOr.js +67 -0
- package/lib/transform/map.js +115 -0
- package/lib/transform/max.js +57 -0
- package/lib/transform/maxLength.js +39 -0
- package/lib/transform/min.js +57 -0
- package/lib/transform/minDelta.js +40 -0
- package/lib/transform/offDelay.js +89 -0
- package/lib/transform/onDelay.js +89 -0
- package/lib/transform/patternEscape.js +24 -0
- package/lib/transform/patternMatch.js +194 -0
- package/lib/transform/patternReplace.js +170 -0
- package/lib/transform/patternTest.js +85 -0
- package/lib/transform/rateOfChange.js +56 -0
- package/lib/transform/reject.js +115 -0
- package/lib/transform/resample.js +96 -0
- package/lib/transform/risingEdge.js +90 -0
- package/lib/transform/risingEdgeCounter.js +179 -0
- package/lib/transform/sampleInterval.js +12 -0
- package/lib/transform/source.js +116 -0
- package/lib/transform/state.js +201 -0
- package/lib/transform/threshold.js +52 -0
- package/lib/transform/toBuffer.js +159 -0
- package/lib/transform/toggle.js +118 -0
- package/lib/transform/transformState.js +193 -0
- package/lib/transform/trim.js +27 -0
- package/lib/transform/util/chainSource.js +96 -0
- package/lib/transform/util/ringBuffer.js +34 -0
- package/lib/transform/valueChange.js +34 -0
- package/lib/transform/valueDecrease.js +38 -0
- package/lib/transform/valueIncrease.js +38 -0
- package/lib/transform/valueIncreaseDiff.js +66 -0
- package/lib/transform/whenUnavailable.js +73 -0
- package/lib/transform/windowCount.js +86 -0
- package/lib/util/fileUtil.js +44 -0
- package/package.json +38 -0
- package/test/.eslintrc.json +15 -0
- package/test/chainedTransform.test.js +88 -0
- package/test/conditions.test.js +118 -0
- package/test/config/ab-pccc.test.js +41 -0
- package/test/config/adam-6052.test.js +16 -0
- package/test/config/adapter.test.js +109 -0
- package/test/config/brother-http.test.js +19 -0
- package/test/config/ethernet-ip.test.js +19 -0
- package/test/config/ifm-iot.test.js +20 -0
- package/test/config/json-http.test.js +47 -0
- package/test/config/labjack-t4.test.js +78 -0
- package/test/config/labjack-t7.test.js +18 -0
- package/test/config/labjack-u3.test.js +17 -0
- package/test/config/modbusTcp.test.js +87 -0
- package/test/config/mqtt.test.js +19 -0
- package/test/config/mqttLincoln.test.js +29 -0
- package/test/config/mtconnect.test.js +63 -0
- package/test/config/mtconnectAdapter.test.js +124 -0
- package/test/config/mtconnectHaas.test.js +15 -0
- package/test/config/null.test.js +16 -0
- package/test/config/opcua.test.js +97 -0
- package/test/config-tests.js +102 -0
- package/test/configFiles/conditions.yml +37 -0
- package/test/configFiles/data-items-legacy.yml +24 -0
- package/test/configFiles/data-items-shorthand.yml +14 -0
- package/test/configFiles/data-items.yml +12 -0
- package/test/configFiles/device/ab-pccc-default.yml +3 -0
- package/test/configFiles/device/ab-pccc-full.yml +13 -0
- package/test/configFiles/device/adam-6052-default.yml +2 -0
- package/test/configFiles/device/brother-http-default.yml +3 -0
- package/test/configFiles/device/ethernet-ip-default.yml +2 -0
- package/test/configFiles/device/ifm-iot-default.yml +2 -0
- package/test/configFiles/device/json-http-bad-prop.yml +13 -0
- package/test/configFiles/device/json-http-bad-prop2.yml +13 -0
- package/test/configFiles/device/json-http-default.yml +3 -0
- package/test/configFiles/device/json-http-std.yml +13 -0
- package/test/configFiles/device/labjack-t4-condition-exprs.yaml +46 -0
- package/test/configFiles/device/labjack-t4-default.yml +3 -0
- package/test/configFiles/device/labjack-t4-pins-alt.yaml +41 -0
- package/test/configFiles/device/labjack-t4-pins.yaml +40 -0
- package/test/configFiles/device/labjack-t4-v1.yaml +29 -0
- package/test/configFiles/device/labjack-t7-default.yml +2 -0
- package/test/configFiles/device/labjack-u3-default.yml +2 -0
- package/test/configFiles/device/modbus-partial.yml +4 -0
- package/test/configFiles/device/modbus-std-extended.yaml +23 -0
- package/test/configFiles/device/modbus-std.yaml +34 -0
- package/test/configFiles/device/modbus-tcp-default.yml +3 -0
- package/test/configFiles/device/mqtt-default.yml +2 -0
- package/test/configFiles/device/mqtt-lincoln-default.yml +3 -0
- package/test/configFiles/device/mqtt-lincoln-full.yml +7 -0
- package/test/configFiles/device/mtconnect-adapter-default.yml +3 -0
- package/test/configFiles/device/mtconnect-adapter-keys.yaml +18 -0
- package/test/configFiles/device/mtconnect-complex-keys.yaml +17 -0
- package/test/configFiles/device/mtconnect-default.yml +3 -0
- package/test/configFiles/device/mtconnect-duplicate-allow-keys.yaml +10 -0
- package/test/configFiles/device/mtconnect-duplicate-declare-keys.yaml +8 -0
- package/test/configFiles/device/mtconnect-duplicate-deny-keys.yaml +10 -0
- package/test/configFiles/device/mtconnect-haas-default.yml +3 -0
- package/test/configFiles/device/mtconnect-std.yaml +18 -0
- package/test/configFiles/device/null-default.yml +1 -0
- package/test/configFiles/device/opcua-bad-tag.yml +18 -0
- package/test/configFiles/device/opcua-bad-tag2.yml +18 -0
- package/test/configFiles/device/opcua-default.yml +3 -0
- package/test/configFiles/device/opcua-std.yml +18 -0
- package/test/configFiles/dump-test.yml +11 -0
- package/test/configFiles/expressionCond.yml +46 -0
- package/test/configFiles/min-config-t4.yaml +4 -0
- package/test/configFiles/min-config-u3.yaml +3 -0
- package/test/configFiles/missing-device.yaml +2 -0
- package/test/configFiles/parse-error1.yml +9 -0
- package/test/configFiles/parse-error2.yml +9 -0
- package/test/configFiles/repro/buffer-convert-repro.yml +15 -0
- package/test/configFiles/repro/chained-delay-timing-repro.yml +13 -0
- package/test/configFiles/repro/count-init-repro.yml +45 -0
- package/test/configFiles/repro/cycle-break-repro.yml +44 -0
- package/test/configFiles/repro/debounce-repro.yml +46 -0
- package/test/configFiles/repro/diff-count-repro.yml +34 -0
- package/test/configFiles/repro/engine-hang-repro.yml +9 -0
- package/test/configFiles/repro/latch-apm-repro.yml +26 -0
- package/test/configFiles/repro/lockout-count-repro.yml +33 -0
- package/test/configFiles/repro/program-extract-repro.yml +38 -0
- package/test/configFiles/repro/state-latch-repro.yml +47 -0
- package/test/configFiles/repro/ternary-repro.yml +26 -0
- package/test/configFiles/transform/debounce.yml +12 -0
- package/test/configFiles/transform/expression.yml +34 -0
- package/test/configFiles/transform/ignoreValue.yml +31 -0
- package/test/configFiles/transform/latch.yml +11 -0
- package/test/configFiles/transform/latchValue.yml +31 -0
- package/test/configFiles/transform/logicAnd.yml +14 -0
- package/test/configFiles/transform/logicOr.yml +14 -0
- package/test/configFiles/transform/map.yml +19 -0
- package/test/configFiles/transform/maxLength.yml +13 -0
- package/test/configFiles/transform/offDelay.yml +12 -0
- package/test/configFiles/transform/pattern-escape.yml +10 -0
- package/test/configFiles/transform/pattern-match.yml +57 -0
- package/test/configFiles/transform/pattern-replace.yml +34 -0
- package/test/configFiles/transform/pattern-test.yml +25 -0
- package/test/configFiles/transform/reject.yml +24 -0
- package/test/configFiles/transform/risingEdgeCounter.yml +36 -0
- package/test/configFiles/transform/source.yml +20 -0
- package/test/configFiles/transform/state.yml +56 -0
- package/test/configFiles/transform/toggle.yml +19 -0
- package/test/configFiles/transform/whenUnavailable.yml +19 -0
- package/test/dataFiles/noisy-pulse.txt +11330 -0
- package/test/dataItems.test.js +140 -0
- package/test/engine-v1-tests.js +418 -0
- package/test/engine-v2-tests.js +284 -0
- package/test/expression-tests.js +171 -0
- package/test/expressionService.test.js +154 -0
- package/test/expressionServiceCondition.test.js +130 -0
- package/test/repro/buffer-convert-repro.test.js +38 -0
- package/test/repro/chained-delay-timing-repro.test.js +34 -0
- package/test/repro/count-init-repro.test.js +46 -0
- package/test/repro/cylce-break-repro.test.js +57 -0
- package/test/repro/debounce-repro.test.js +65 -0
- package/test/repro/diff-count-repro.test.js +79 -0
- package/test/repro/engine-hang-repro.test.js +38 -0
- package/test/repro/latch-apm-repro.test.js +119 -0
- package/test/repro/lockout-count-repro.test.js +84 -0
- package/test/repro/program-extract-repro.test.js +40 -0
- package/test/repro/state-latch-repro.test.js +63 -0
- package/test/repro/ternary-repro.test.js +43 -0
- package/test/transform/accumulte.test.js +18 -0
- package/test/transform/average.test.js +22 -0
- package/test/transform/debounce.test.js +70 -0
- package/test/transform/downsample.test.js +30 -0
- package/test/transform/edge.test.js +27 -0
- package/test/transform/expression.test.js +189 -0
- package/test/transform/fallingEdge.test.js +59 -0
- package/test/transform/fromBuffer.test.js +60 -0
- package/test/transform/hash.test.js +34 -0
- package/test/transform/ignoreValue.test.js +123 -0
- package/test/transform/invert.test.js +26 -0
- package/test/transform/latch.test.js +33 -0
- package/test/transform/latchValue.test.js +126 -0
- package/test/transform/logicAnd.test.js +80 -0
- package/test/transform/logicOr.test.js +80 -0
- package/test/transform/map.test.js +42 -0
- package/test/transform/max.test.js +30 -0
- package/test/transform/maxLength.test.js +32 -0
- package/test/transform/min.test.js +30 -0
- package/test/transform/minDelta.test.js +14 -0
- package/test/transform/offDelay.test.js +123 -0
- package/test/transform/onDelay.test.js +105 -0
- package/test/transform/patternEscape.test.js +18 -0
- package/test/transform/patternMatch.test.js +177 -0
- package/test/transform/patternReplace.test.js +95 -0
- package/test/transform/patternTest.test.js +105 -0
- package/test/transform/rateOfChange.test.js +34 -0
- package/test/transform/reject.test.js +56 -0
- package/test/transform/resample.test.js +193 -0
- package/test/transform/risingEdge.test.js +60 -0
- package/test/transform/risingEdgeCounter.test.js +227 -0
- package/test/transform/sampleInterval.test.js +22 -0
- package/test/transform/source.test.js +137 -0
- package/test/transform/state.test.js +248 -0
- package/test/transform/threshold.test.js +78 -0
- package/test/transform/toBuffer.test.js +60 -0
- package/test/transform/toggle.test.js +92 -0
- package/test/transform/trim.test.js +30 -0
- package/test/transform/valueChange.test.js +14 -0
- package/test/transform/valueDecrease.test.js +32 -0
- package/test/transform/valueIncrease.test.js +32 -0
- package/test/transform/valueIncreaseDiff.test.js +32 -0
- package/test/transform/whenUnavailable.test.js +93 -0
- package/test/transform/windowCount.test.js +26 -0
- package/test/util/testUtils.js +405 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _ = require('lodash');
|
|
4
|
+
const Transform = require('../transform');
|
|
5
|
+
const ConfigError = require('./configError');
|
|
6
|
+
|
|
7
|
+
class TransformConfigUtil {
|
|
8
|
+
constructor(expressionService) {
|
|
9
|
+
this.expressionService = expressionService;
|
|
10
|
+
|
|
11
|
+
this.transformMap = Object.values(Transform).reduce((trmap, transform) => {
|
|
12
|
+
trmap[transform.op] = transform;
|
|
13
|
+
transform.opAliases.forEach((alias) => {
|
|
14
|
+
trmap[alias] = transform;
|
|
15
|
+
});
|
|
16
|
+
return trmap;
|
|
17
|
+
}, {});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
loadTransformList(variable, sectionPath, enforceSource = true) {
|
|
21
|
+
const transformDefn = _.isArray(variable) ? variable : variable.transform;
|
|
22
|
+
if (_.isEmpty(transformDefn) || !_.isArray(transformDefn)) {
|
|
23
|
+
const message = 'Variable must defined as a list of operations';
|
|
24
|
+
throw new ConfigError(message).atPath(sectionPath);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const transform = _.map(transformDefn, (transform, index) => {
|
|
28
|
+
const basePath = `${sectionPath}.${index}`;
|
|
29
|
+
const transformConfig = this.parseTransformConfig(transform, basePath);
|
|
30
|
+
|
|
31
|
+
if (!transformConfig.func) {
|
|
32
|
+
const message = 'Malformed operation';
|
|
33
|
+
throw new ConfigError(message).atPath(sectionPath);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!this.transformMap[transformConfig.func]) {
|
|
37
|
+
const message = `Unsupported operation '${transformConfig.func}'`;
|
|
38
|
+
throw new ConfigError(message).atPath(sectionPath);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (enforceSource) {
|
|
42
|
+
if (index === 0 && !_.includes(['source', 'expression', 'state'], transformConfig.func)) {
|
|
43
|
+
const message = `First line in variable must be 'source' operation, but found '${transformConfig.func}'`;
|
|
44
|
+
throw new ConfigError(message).atPath(sectionPath);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
this.transformMap[transformConfig.func].parseConfig(this, transformConfig);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error instanceof ConfigError) {
|
|
52
|
+
error.atSection(sectionPath);
|
|
53
|
+
}
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return transformConfig;
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return { transform };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
parseTransformConfig(transform, basePath) {
|
|
64
|
+
let tobj = {};
|
|
65
|
+
if (_.isString(transform)) {
|
|
66
|
+
tobj = {
|
|
67
|
+
func: transform,
|
|
68
|
+
args: [],
|
|
69
|
+
path: `${basePath}.${transform}`,
|
|
70
|
+
};
|
|
71
|
+
} else if (_.isObject(transform)) {
|
|
72
|
+
const func = _.first(_.keys(transform));
|
|
73
|
+
let args = [];
|
|
74
|
+
|
|
75
|
+
const primary = transform[func];
|
|
76
|
+
if (_.isEmpty(primary) && transform.length > 1) {
|
|
77
|
+
args = _.rest(transform);
|
|
78
|
+
} else if (_.isObject(primary)) {
|
|
79
|
+
args = primary;
|
|
80
|
+
} else if (!_.isUndefined(primary)) {
|
|
81
|
+
args = {};
|
|
82
|
+
args[func] = primary;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
tobj = {
|
|
86
|
+
func,
|
|
87
|
+
args,
|
|
88
|
+
path: `${basePath}.${func}`,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return tobj;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
getExpressionsFromVariables(variables) {
|
|
96
|
+
return _(variables).map((defn, varName) => {
|
|
97
|
+
return _.map(defn, (transformContainer, index) => {
|
|
98
|
+
return _.map(transformContainer, (body, attribute) => {
|
|
99
|
+
if (this.transformMap[attribute]) {
|
|
100
|
+
return this.transformMap[attribute].getExpressions(this, body, { varName, index, attribute });
|
|
101
|
+
}
|
|
102
|
+
return [];
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
}).flattenDeep().compact().value();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
getExpressions(body, { varName, index, attribute, field }, includeDefault = false) {
|
|
109
|
+
const parts = [];
|
|
110
|
+
|
|
111
|
+
let expression = '';
|
|
112
|
+
if (_.isUndefined(body)) {
|
|
113
|
+
expression = '';
|
|
114
|
+
} else if (_.isObject(body) && field) {
|
|
115
|
+
expression = (body[field] ?? '').toString();
|
|
116
|
+
} else if (!field || includeDefault) {
|
|
117
|
+
expression = body.toString();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (!expression) {
|
|
121
|
+
return parts;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (field) {
|
|
125
|
+
parts.push({
|
|
126
|
+
path: `variables.${varName}.${index}.${attribute}.${field}`,
|
|
127
|
+
expression,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (!field || includeDefault) {
|
|
132
|
+
parts.push({
|
|
133
|
+
path: `variables.${varName}.${index}.${attribute}`,
|
|
134
|
+
expression,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
parts.push({
|
|
139
|
+
path: `variables.${varName}`,
|
|
140
|
+
expression,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
return parts;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
getStringExpressionsFromVariables(variables) {
|
|
147
|
+
return _(variables).map((defn, varName) => {
|
|
148
|
+
return _.map(defn, (transformContainer, index) => {
|
|
149
|
+
return _.map(transformContainer, (body, attribute) => {
|
|
150
|
+
if (this.transformMap[attribute]) {
|
|
151
|
+
return this.transformMap[attribute].getStringExpressions(this, body, { varName, index, attribute });
|
|
152
|
+
}
|
|
153
|
+
return [];
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
}).flattenDeep().compact().value();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
getStringExpressions(body, value, { varName, index, attribute, field }, withThis = true) {
|
|
160
|
+
if (!_.isObject(body) || _.isUndefined(body[field])) {
|
|
161
|
+
return [];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return [{
|
|
165
|
+
path: `variables.${varName}.${index}.${attribute}.${field}`,
|
|
166
|
+
string: value,
|
|
167
|
+
withThis,
|
|
168
|
+
}, {
|
|
169
|
+
path: `variables.${varName}`,
|
|
170
|
+
string: value,
|
|
171
|
+
withThis: true,
|
|
172
|
+
}];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
decomposePattern(pattern, validFlags = 'imus') {
|
|
176
|
+
let flags = '';
|
|
177
|
+
const match = pattern.match(/^\/(.+)\/([a-z]*)$/sm);
|
|
178
|
+
if (match) {
|
|
179
|
+
pattern = match[1];
|
|
180
|
+
flags = (match[2] ?? '').replace(new RegExp(`[^${validFlags}]`, 'g'), '');
|
|
181
|
+
} else {
|
|
182
|
+
const match2 = pattern.match(/^\/(.+)\/$/sm);
|
|
183
|
+
if (match2) {
|
|
184
|
+
pattern = match[0];
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return { pattern, flags };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
testPattern(pattern, flags, attribute) {
|
|
191
|
+
try {
|
|
192
|
+
const regPattern = new RegExp(pattern, flags);
|
|
193
|
+
if (!regPattern) {
|
|
194
|
+
throw new ConfigError(`Invalid regular expression: ${pattern}`).atAttribute(attribute);
|
|
195
|
+
}
|
|
196
|
+
} catch (err) {
|
|
197
|
+
throw new ConfigError(err.message).atAttribute(attribute);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
parseBool(value) {
|
|
202
|
+
if (_.isString(value)) {
|
|
203
|
+
value = value.toLowerCase();
|
|
204
|
+
return value === 'true';
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return !!_.toInteger(value);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
requireObjectParams(args, attribute, {
|
|
211
|
+
optional = true,
|
|
212
|
+
exclusive = false,
|
|
213
|
+
availableAttributes = [],
|
|
214
|
+
defaultAttribute = null
|
|
215
|
+
}) {
|
|
216
|
+
if (defaultAttribute && _.isObject(args) && _.isUndefined(args[defaultAttribute])) {
|
|
217
|
+
args[defaultAttribute] = args[attribute];
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
let attrCount = 0;
|
|
221
|
+
if (_.isObject(args)) {
|
|
222
|
+
attrCount = Object.keys(args).reduce((count, key) => {
|
|
223
|
+
return count + availableAttributes.includes(key) ? 1 : 0;
|
|
224
|
+
}, 0);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (!optional && attrCount === 0) {
|
|
228
|
+
throw new ConfigError(`Expected one of the following attributes: [${availableAttributes.join(', ')}]`).atAttribute(attribute);
|
|
229
|
+
}
|
|
230
|
+
if (exclusive && attrCount > 1) {
|
|
231
|
+
throw new ConfigError(`Expected exactly one of the following attributes: [${availableAttributes.join(', ')}]`).atAttribute(attribute);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (_.isArray(args) && args.length > 0) {
|
|
235
|
+
const filtered = _.filter(args, a => _.isObject(a) && !_.isArray(a));
|
|
236
|
+
if (filtered.length === args.length) {
|
|
237
|
+
const keys = _.keys(_.merge({ }, ...args));
|
|
238
|
+
throw new ConfigError(`Unexpected list. The attributes [${keys.join(', ')}] should not be prefixed with a dash (-).`).atAttribute(attribute);
|
|
239
|
+
} else {
|
|
240
|
+
throw new ConfigError('Unexpected list').atAttribute(attribute);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
getArgOrShorthand(defn, value) {
|
|
246
|
+
if (!_.isUndefined(defn.args[value])) {
|
|
247
|
+
return { value: defn.args[value], usingShorthand: false };
|
|
248
|
+
}
|
|
249
|
+
return { value: defn.args[defn.func], usingShorthand: true };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
requireNumeric(value, field, attribute) {
|
|
253
|
+
if (!_.isNumber(value)) {
|
|
254
|
+
throw new ConfigError(`${field} must have a numeric value`).atAttribute(attribute);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
requirePositive(value, field, attribute) {
|
|
259
|
+
if (!_.isNumber(value)) {
|
|
260
|
+
throw new ConfigError(`${field} must have a numeric value`).atAttribute(attribute);
|
|
261
|
+
}
|
|
262
|
+
if (value <= 0) {
|
|
263
|
+
throw new ConfigError(`${field} must be greater than 0`).atAttribute(attribute);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
requireZeroPositive(value, field, attribute) {
|
|
268
|
+
if (!_.isNumber(value)) {
|
|
269
|
+
throw new ConfigError(`${field} must have a numeric value`).atAttribute(attribute);
|
|
270
|
+
}
|
|
271
|
+
if (value < 0) {
|
|
272
|
+
throw new ConfigError(`${field} must be 0 or greater`).atAttribute(attribute);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
requireValueIn(value, validSet, field, attribute) {
|
|
277
|
+
if (!validSet.includes(value)) {
|
|
278
|
+
throw new ConfigError(`${field} must be on of [${validSet.join(', ')}]`).atAttribute(attribute);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
compileExpression(expression, attribute, scope = { this: 0 }) {
|
|
283
|
+
try {
|
|
284
|
+
return this.expressionService.compileExpression(expression, scope);
|
|
285
|
+
} catch (err) {
|
|
286
|
+
this.throwConfigError(err, attribute);
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
throwConfigError(error, attribute) {
|
|
292
|
+
throw new ConfigError(error).atAttribute(attribute);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
module.exports = TransformConfigUtil;
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _ = require('lodash');
|
|
4
|
+
const EventEmitter = require('eventemitter3');
|
|
5
|
+
|
|
6
|
+
class DataOutput extends EventEmitter {
|
|
7
|
+
constructor(engine, config, options = {}) {
|
|
8
|
+
super();
|
|
9
|
+
|
|
10
|
+
this.logDir = options.logDir || '.';
|
|
11
|
+
this.logger = options.logger || {
|
|
12
|
+
error: () => {},
|
|
13
|
+
warn: () => {},
|
|
14
|
+
info: () => {},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
this.engine = engine;
|
|
18
|
+
this.config = config;
|
|
19
|
+
|
|
20
|
+
this.adapterConfig = config.adapter;
|
|
21
|
+
this.dataItems = this.adapterConfig.dataItems;
|
|
22
|
+
this.conditions = this.adapterConfig.conditions;
|
|
23
|
+
|
|
24
|
+
this.inputState = { ...this.config.expressionService.defaultValues };
|
|
25
|
+
this.inputUnavail = _.mapValues(this.inputState, () => true);
|
|
26
|
+
this.currentConditionCodesByPath = {}; // code path based, related to calculated codes
|
|
27
|
+
this.currentConditionCodes = {};
|
|
28
|
+
this.updateSet = {};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
initiailize() {
|
|
32
|
+
const now = Date.now();
|
|
33
|
+
this.processSampleDataItems(this.dataItems, now);
|
|
34
|
+
this.procesConditionDataItems(this.conditions, now);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
setUnavailable() {
|
|
38
|
+
this.inputUnavail = _.mapValues(this.inputUnavail, () => true);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
handleUpdate(inputName, value, time) {
|
|
42
|
+
this.inputState[inputName] = value;
|
|
43
|
+
this.inputUnavail[inputName] = false;
|
|
44
|
+
|
|
45
|
+
const dataItemNames = this.adapterConfig.triggeredDataItems(inputName);
|
|
46
|
+
const dataItems = _.map(dataItemNames, name => this.dataItems[name]);
|
|
47
|
+
this.processSampleDataItems(dataItems, time * 1000);
|
|
48
|
+
|
|
49
|
+
if (this.lastUpdateTime !== time) {
|
|
50
|
+
this.handleScanSync();
|
|
51
|
+
this.lastUpdateTime = time;
|
|
52
|
+
}
|
|
53
|
+
this.updateSet[inputName] = 1;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
handleItemUnavailable(inputName, time) {
|
|
57
|
+
this.inputState[inputName] = null;
|
|
58
|
+
this.inputUnavail[inputName] = true;
|
|
59
|
+
|
|
60
|
+
const dataItemNames = this.adapterConfig.triggeredDataItems(inputName);
|
|
61
|
+
_.each(dataItemNames, (name) => {
|
|
62
|
+
this.emit('data_unavailable', name, time * 1000);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
if (this.lastUpdateTime !== time) {
|
|
66
|
+
this.handleScanSync();
|
|
67
|
+
this.lastUpdateTime = time;
|
|
68
|
+
}
|
|
69
|
+
this.updateSet[inputName] = 1;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
handleScanSync() {
|
|
73
|
+
if (this.lastUpdateTime > 0) {
|
|
74
|
+
const conditionNames = _(this.updateSet).keys().map((inputName) => {
|
|
75
|
+
return this.adapterConfig.triggeredConditions(inputName);
|
|
76
|
+
}).flatten().uniq().value();
|
|
77
|
+
|
|
78
|
+
const conditions = _.map(conditionNames, name => this.conditions[name]);
|
|
79
|
+
this.procesConditionDataItems(conditions, this.lastUpdateTime * 1000);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this.updateSet = {};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
matchRule(ruleSet, key) {
|
|
86
|
+
return _.reduce(ruleSet, (result, rule) => {
|
|
87
|
+
return result || rule.test(key);
|
|
88
|
+
}, false);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
handleMtconnectSample(name, value, time) {
|
|
92
|
+
this.emit('data', name, value ?? 'UNAVAILABLE', time);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
handleMtconnectCondition(name, fields, time) {
|
|
96
|
+
if (!_.isObject(fields)) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (this.adapterConfig.passthroughConditions[name]) {
|
|
101
|
+
_.each(this.adapterConfig.passthroughConditions[name], (source) => {
|
|
102
|
+
if (fields.code) {
|
|
103
|
+
if (source.allowCodes && !this.matchRule(source.allowCodes, fields.code)) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (source.denyCodes && this.matchRule(source.denyCodes, fields.code)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
this.trackCurrentCondition(name, fields.code);
|
|
112
|
+
this.emit('data', source.name, fields.level || 'NORMAL', time, {
|
|
113
|
+
type: 'condition',
|
|
114
|
+
nativeCode: fields.code,
|
|
115
|
+
severity: fields.severity ?? null,
|
|
116
|
+
qualifier: fields.qualifier ?? null,
|
|
117
|
+
message: fields.message,
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (!_.includes(_.map(this.adapterConfig.passthroughConditions[name], 'name'), name)) {
|
|
123
|
+
this.trackCurrentCondition(name, fields.code);
|
|
124
|
+
this.emit('data', name, fields.level || 'NORMAL', time, {
|
|
125
|
+
type: 'condition',
|
|
126
|
+
nativeCode: fields.code,
|
|
127
|
+
severity: fields.severity ?? null,
|
|
128
|
+
qualifier: fields.qualifier ?? null,
|
|
129
|
+
message: fields.message,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
handleMtconnectMessage(name, fields, time) {
|
|
135
|
+
if (_.isObject(fields)) {
|
|
136
|
+
this.emit('data', name, fields.message ?? 'UNAVAILABLE', time, {
|
|
137
|
+
type: 'message',
|
|
138
|
+
nativeCode: fields.code ?? null,
|
|
139
|
+
});
|
|
140
|
+
} else {
|
|
141
|
+
this.emit('data', name, fields ?? 'UNAVAILABLE', time, {
|
|
142
|
+
type: 'message',
|
|
143
|
+
nativeCode: null,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
nullTriggers(triggers) {
|
|
149
|
+
return _.reduce(triggers, (ac, name) => {
|
|
150
|
+
return ac || this.inputUnavail[name] || this.inputState[name] === null;
|
|
151
|
+
}, false);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
processSampleDataItems(dataItems, time) {
|
|
155
|
+
_.each(dataItems, (item) => {
|
|
156
|
+
if (item.rule) {
|
|
157
|
+
if (this.nullTriggers(item.rule.triggers)) {
|
|
158
|
+
this.emit('data_unavailable', item.name, time);
|
|
159
|
+
} else {
|
|
160
|
+
let result;
|
|
161
|
+
if (item.rule.rawExpression && _.has(this.inputState, item.rule.rawExpression)) {
|
|
162
|
+
result = _.get(this.inputState, item.rule.rawExpression);
|
|
163
|
+
} else {
|
|
164
|
+
result = item.rule.expression.evaluate(this.inputState);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// True floating-value numeric results get precision-adjusted
|
|
168
|
+
if (_.isFinite(result) && !_.isInteger(result)) {
|
|
169
|
+
const floatResult = Number(result);
|
|
170
|
+
if (!Number.isNaN(floatResult)) {
|
|
171
|
+
if (Math.abs(floatResult) < 1) {
|
|
172
|
+
result = floatResult.toPrecision(3);
|
|
173
|
+
} else {
|
|
174
|
+
result = Number(floatResult.toFixed(3));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (_.isUndefined(result) || _.isNull(result)) {
|
|
180
|
+
result = 'UNAVAILABLE';
|
|
181
|
+
}
|
|
182
|
+
this.emit('data', item.name, result, time);
|
|
183
|
+
}
|
|
184
|
+
} else if (item.stateRules) {
|
|
185
|
+
const result = _.reduce(item.stateRules, (res, rule) => {
|
|
186
|
+
if (this.nullTriggers(rule.triggers)) {
|
|
187
|
+
return res || 'UNAVAILABLE';
|
|
188
|
+
}
|
|
189
|
+
return res || (rule.expression.evaluate(this.inputState) ? rule.value : res);
|
|
190
|
+
}, undefined);
|
|
191
|
+
|
|
192
|
+
if (_.isUndefined(result) || _.isNull(result) || result === 'UNAVAILABLE') {
|
|
193
|
+
this.emit('data_unavailable', item.name, time);
|
|
194
|
+
} else {
|
|
195
|
+
this.emit('data', item.name, result, time);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Conditions are processed after a "scansync" event, which typically happens after a scan loop in a device
|
|
202
|
+
// or other appropriate breathing point in data acquisition. This is to ensure that all data related to
|
|
203
|
+
// codes and messages, which are calculated independently, have converged into a consistent state.
|
|
204
|
+
|
|
205
|
+
procesConditionDataItems(conditions, time) {
|
|
206
|
+
_.each(conditions, (condition) => {
|
|
207
|
+
if (condition.arraySource) {
|
|
208
|
+
const active = this.getConditionCodesFromArraySource(condition.arraySource);
|
|
209
|
+
if (active === null) {
|
|
210
|
+
this.emit('data_unavailable', condition.name, time);
|
|
211
|
+
} else {
|
|
212
|
+
this.updateActiveConditions(condition, condition.name, 'FAULT', time, active);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
_.each(condition.codes, (code) => {
|
|
217
|
+
const result = _.reduce(code.stateRules, (res, rule) => {
|
|
218
|
+
if (this.nullTriggers(rule.triggers)) {
|
|
219
|
+
return res || 'UNAVAILABLE';
|
|
220
|
+
}
|
|
221
|
+
return res || (rule.expression.evaluate(this.inputState) ? rule.value : res);
|
|
222
|
+
}, undefined);
|
|
223
|
+
|
|
224
|
+
if (_.isUndefined(result) || _.isNull(result) || result === 'UNAVAILABLE') {
|
|
225
|
+
this.emit('data_unavailable', condition.name, time, {
|
|
226
|
+
type: 'condition',
|
|
227
|
+
nativeCode: code.code,
|
|
228
|
+
});
|
|
229
|
+
} else {
|
|
230
|
+
const nativeCode = code.codeResolver(this.engine);
|
|
231
|
+
const condByPath = this.currentConditionCodesByPath[code.codePath];
|
|
232
|
+
if (condByPath && condByPath !== nativeCode) {
|
|
233
|
+
this.clearCurrentCondition(condition.name, this.currentConditionCodesByPath[code.codePath]);
|
|
234
|
+
this.emit('data', condition.name, 'NORMAL', time, {
|
|
235
|
+
type: 'condition',
|
|
236
|
+
nativeCode: this.currentConditionCodesByPath[code.codePath],
|
|
237
|
+
message: '',
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (_.isNull(nativeCode)) {
|
|
242
|
+
delete this.currentConditionCodesByPath[code.codePath];
|
|
243
|
+
return; // Cancel code
|
|
244
|
+
}
|
|
245
|
+
this.currentConditionCodesByPath[code.codePath] = nativeCode;
|
|
246
|
+
|
|
247
|
+
let message = code.messageResolver(this.engine);
|
|
248
|
+
if (_.isNull(message)) {
|
|
249
|
+
message = 'UNAVAILABLE';
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// TODO: cancel previous code state / track result of computed code
|
|
253
|
+
this.trackCurrentCondition(condition.name, nativeCode);
|
|
254
|
+
this.emit('data', condition.name, result || 'NORMAL', time, {
|
|
255
|
+
type: 'condition',
|
|
256
|
+
nativeCode,
|
|
257
|
+
message,
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
getConditionCodesFromArraySource(arraySource) {
|
|
265
|
+
if (this.inputUnavail[arraySource.codeList] || this.inputUnavail[arraySource.messageList]) {
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
let codeList = this.inputState[arraySource.codeList];
|
|
270
|
+
if (!_.isArray(codeList)) {
|
|
271
|
+
codeList = [codeList];
|
|
272
|
+
}
|
|
273
|
+
let messageList = this.inputState[arraySource.messageList];
|
|
274
|
+
if (!_.isArray(messageList)) {
|
|
275
|
+
messageList = [messageList];
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
let levelList = _.map(codeList, () => 'FAULT');
|
|
279
|
+
if (arraySource.levelList) {
|
|
280
|
+
levelList = this.inputState[arraySource.levelList];
|
|
281
|
+
if (!_.isArray(levelList)) {
|
|
282
|
+
levelList = [levelList];
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return _(_.zip(codeList, messageList, levelList)).map(([code, message, level]) => {
|
|
287
|
+
if (_.isObject(code)) {
|
|
288
|
+
code = _.get(code, 'code');
|
|
289
|
+
}
|
|
290
|
+
if (_.isObject(message)) {
|
|
291
|
+
message = _.get(message, 'message');
|
|
292
|
+
}
|
|
293
|
+
if (_.isObject(level)) {
|
|
294
|
+
level = _.get(level, 'level');
|
|
295
|
+
}
|
|
296
|
+
if (_.isUndefined(code) || _.isUndefined(message)) {
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (_.isUndefined(level)) {
|
|
301
|
+
level = 'FAULT';
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return { code, message, level };
|
|
305
|
+
}).compact().value();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
updateActiveConditions(condition, key, level, time, codeMessageList) {
|
|
309
|
+
const activeCodes = _.map(codeMessageList, 'code');
|
|
310
|
+
const currentCodes = this.currentConditionCodes[key] || [];
|
|
311
|
+
|
|
312
|
+
_.each(currentCodes, (code) => {
|
|
313
|
+
if (!_.isEmpty(code) && !_.includes(activeCodes, code)) {
|
|
314
|
+
this.clearCurrentCondition(key, code);
|
|
315
|
+
this.emit('data', key, 'NORMAL', time, {
|
|
316
|
+
type: 'condition',
|
|
317
|
+
nativeCode: code,
|
|
318
|
+
message: '',
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
_.each(codeMessageList, ({ code, message, localLevel }) => {
|
|
324
|
+
if (condition.allowCodes && !this.matchRule(condition.allowCodes, code)) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
if (condition.denyCodes && this.matchRule(condition.denyCodes, code)) {
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
this.trackCurrentCondition(key, code);
|
|
332
|
+
this.emit('data', key, localLevel ?? level, time, {
|
|
333
|
+
type: 'condition',
|
|
334
|
+
nativeCode: code,
|
|
335
|
+
message: String(message),
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
trackCurrentCondition(key, code) {
|
|
341
|
+
if (!this.currentConditionCodes[key]) {
|
|
342
|
+
this.currentConditionCodes[key] = [];
|
|
343
|
+
}
|
|
344
|
+
if (!_.includes(this.currentConditionCodes, code)) {
|
|
345
|
+
this.currentConditionCodes[key].push(code);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
clearCurrentCondition(key, code) {
|
|
350
|
+
if (!this.currentConditionCodes[key]) {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
_.pull(this.currentConditionCodes[key], code);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
module.exports = DataOutput;
|