node-opcua-aggregates 2.71.0 → 2.72.2

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/dist/common.js CHANGED
@@ -1,100 +1,100 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.interpolateValue = exports.getAggregateData = void 0;
4
- /**
5
- * @module node-opca-aggregates
6
- */
7
- const node_opcua_address_space_1 = require("node-opcua-address-space");
8
- const node_opcua_data_model_1 = require("node-opcua-data-model");
9
- const node_opcua_data_value_1 = require("node-opcua-data-value");
10
- const node_opcua_service_history_1 = require("node-opcua-service-history");
11
- const node_opcua_status_code_1 = require("node-opcua-status-code");
12
- const node_opcua_nodeid_1 = require("node-opcua-nodeid");
13
- const aggregates_1 = require("./aggregates");
14
- const interval_1 = require("./interval");
15
- /**
16
- * @internal
17
- * @param node
18
- * @param processingInterval
19
- * @param startDate
20
- * @param endDate
21
- * @param dataValues
22
- * @param lambda
23
- * @param callback
24
- */
25
- function processAggregateData(node, processingInterval, startDate, endDate, dataValues, lambda, callback) {
26
- const aggregateConfiguration = (0, aggregates_1.getAggregateConfiguration)(node);
27
- const results = [];
28
- const tstart = startDate.getTime();
29
- const tend = endDate.getTime();
30
- const indexHint = 0;
31
- for (let t = tstart; t < tend; t += processingInterval) {
32
- const sourceTimestamp = new Date();
33
- sourceTimestamp.setTime(t);
34
- const interval = (0, interval_1.getInterval)(sourceTimestamp, processingInterval, indexHint, dataValues);
35
- const dataValue = lambda(interval, aggregateConfiguration);
36
- /* istanbul ignore next */
37
- if (!dataValue || !dataValue.sourceTimestamp) {
38
- // const dataValue = interval.interpolatedValue(aggregateConfiguration);
39
- throw Error("invalid DataValue");
40
- }
41
- results.push(dataValue);
42
- }
43
- setImmediate(() => {
44
- callback(null, results);
45
- });
46
- }
47
- function getAggregateData(node, processingInterval, startDate, endDate, lambda, callback) {
48
- /* istanbul ignore next */
49
- if (node.nodeClass !== node_opcua_data_model_1.NodeClass.Variable) {
50
- throw new Error("node must be UAVariable");
51
- }
52
- /* istanbul ignore next */
53
- if (processingInterval <= 0) {
54
- throw new Error("Invalid processing interval, shall be greater than 0");
55
- }
56
- const continuationPointManager = new node_opcua_address_space_1.ContinuationPointManager();
57
- const context = new node_opcua_address_space_1.SessionContext({
58
- session: {
59
- continuationPointManager,
60
- getSessionId: () => (0, node_opcua_nodeid_1.coerceNodeId)("i=0")
61
- }
62
- });
63
- const historyReadDetails = new node_opcua_service_history_1.ReadRawModifiedDetails({
64
- endTime: endDate,
65
- startTime: startDate,
66
- isReadModified: false,
67
- numValuesPerNode: 0
68
- // returnBounds: true,
69
- });
70
- const indexRange = null;
71
- const dataEncoding = null;
72
- const continuationPoint = null;
73
- node.historyRead(context, historyReadDetails, indexRange, dataEncoding, { continuationPoint }, (err, result) => {
74
- /* istanbul ignore next */
75
- if (err) {
76
- return callback(err);
77
- }
78
- const historyData = result.historyData;
79
- const dataValues = historyData.dataValues || [];
80
- processAggregateData(node, processingInterval, startDate, endDate, dataValues, lambda, callback);
81
- });
82
- }
83
- exports.getAggregateData = getAggregateData;
84
- function interpolateValue(dataValue1, dataValue2, date) {
85
- const t0 = dataValue1.sourceTimestamp.getTime();
86
- const t = date.getTime();
87
- const t1 = dataValue2.sourceTimestamp.getTime();
88
- const coef1 = (t - t0) / (t1 - t0);
89
- const coef2 = (t1 - t) / (t1 - t0);
90
- const value = dataValue1.value.clone();
91
- value.value = coef2 * dataValue1.value.value + coef1 * dataValue2.value.value;
92
- const statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(dataValue1.statusCode, "HistorianInterpolated");
93
- return new node_opcua_data_value_1.DataValue({
94
- sourceTimestamp: date,
95
- statusCode,
96
- value
97
- });
98
- }
99
- exports.interpolateValue = interpolateValue;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.interpolateValue = exports.getAggregateData = void 0;
4
+ /**
5
+ * @module node-opca-aggregates
6
+ */
7
+ const node_opcua_address_space_1 = require("node-opcua-address-space");
8
+ const node_opcua_data_model_1 = require("node-opcua-data-model");
9
+ const node_opcua_data_value_1 = require("node-opcua-data-value");
10
+ const node_opcua_service_history_1 = require("node-opcua-service-history");
11
+ const node_opcua_status_code_1 = require("node-opcua-status-code");
12
+ const node_opcua_nodeid_1 = require("node-opcua-nodeid");
13
+ const aggregates_1 = require("./aggregates");
14
+ const interval_1 = require("./interval");
15
+ /**
16
+ * @internal
17
+ * @param node
18
+ * @param processingInterval
19
+ * @param startDate
20
+ * @param endDate
21
+ * @param dataValues
22
+ * @param lambda
23
+ * @param callback
24
+ */
25
+ function processAggregateData(node, processingInterval, startDate, endDate, dataValues, lambda, callback) {
26
+ const aggregateConfiguration = (0, aggregates_1.getAggregateConfiguration)(node);
27
+ const results = [];
28
+ const tstart = startDate.getTime();
29
+ const tend = endDate.getTime();
30
+ const indexHint = 0;
31
+ for (let t = tstart; t < tend; t += processingInterval) {
32
+ const sourceTimestamp = new Date();
33
+ sourceTimestamp.setTime(t);
34
+ const interval = (0, interval_1.getInterval)(sourceTimestamp, processingInterval, indexHint, dataValues);
35
+ const dataValue = lambda(interval, aggregateConfiguration);
36
+ /* istanbul ignore next */
37
+ if (!dataValue || !dataValue.sourceTimestamp) {
38
+ // const dataValue = interval.interpolatedValue(aggregateConfiguration);
39
+ throw Error("invalid DataValue");
40
+ }
41
+ results.push(dataValue);
42
+ }
43
+ setImmediate(() => {
44
+ callback(null, results);
45
+ });
46
+ }
47
+ function getAggregateData(node, processingInterval, startDate, endDate, lambda, callback) {
48
+ /* istanbul ignore next */
49
+ if (node.nodeClass !== node_opcua_data_model_1.NodeClass.Variable) {
50
+ throw new Error("node must be UAVariable");
51
+ }
52
+ /* istanbul ignore next */
53
+ if (processingInterval <= 0) {
54
+ throw new Error("Invalid processing interval, shall be greater than 0");
55
+ }
56
+ const continuationPointManager = new node_opcua_address_space_1.ContinuationPointManager();
57
+ const context = new node_opcua_address_space_1.SessionContext({
58
+ session: {
59
+ continuationPointManager,
60
+ getSessionId: () => (0, node_opcua_nodeid_1.coerceNodeId)("i=0")
61
+ }
62
+ });
63
+ const historyReadDetails = new node_opcua_service_history_1.ReadRawModifiedDetails({
64
+ endTime: endDate,
65
+ startTime: startDate,
66
+ isReadModified: false,
67
+ numValuesPerNode: 0
68
+ // returnBounds: true,
69
+ });
70
+ const indexRange = null;
71
+ const dataEncoding = null;
72
+ const continuationPoint = null;
73
+ node.historyRead(context, historyReadDetails, indexRange, dataEncoding, { continuationPoint }, (err, result) => {
74
+ /* istanbul ignore next */
75
+ if (err) {
76
+ return callback(err);
77
+ }
78
+ const historyData = result.historyData;
79
+ const dataValues = historyData.dataValues || [];
80
+ processAggregateData(node, processingInterval, startDate, endDate, dataValues, lambda, callback);
81
+ });
82
+ }
83
+ exports.getAggregateData = getAggregateData;
84
+ function interpolateValue(dataValue1, dataValue2, date) {
85
+ const t0 = dataValue1.sourceTimestamp.getTime();
86
+ const t = date.getTime();
87
+ const t1 = dataValue2.sourceTimestamp.getTime();
88
+ const coef1 = (t - t0) / (t1 - t0);
89
+ const coef2 = (t1 - t) / (t1 - t0);
90
+ const value = dataValue1.value.clone();
91
+ value.value = coef2 * dataValue1.value.value + coef1 * dataValue2.value.value;
92
+ const statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(dataValue1.statusCode, "HistorianInterpolated");
93
+ return new node_opcua_data_value_1.DataValue({
94
+ sourceTimestamp: date,
95
+ statusCode,
96
+ value
97
+ });
98
+ }
99
+ exports.interpolateValue = interpolateValue;
100
100
  //# sourceMappingURL=common.js.map
package/dist/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- /**
2
- * @module node-opca-aggregates
3
- */
4
- export { addAggregateSupport, installAggregateConfigurationOptions, getAggregateConfiguration } from "./aggregates";
5
- export * from "./interpolate";
6
- export * from "./minmax";
7
- export * from "./interval";
8
- export * from "./common";
9
- export * from "./average";
10
- export * from "./read_processed_details";
11
- export { AggregateFunction } from "node-opcua-constants";
1
+ /**
2
+ * @module node-opca-aggregates
3
+ */
4
+ export { addAggregateSupport, installAggregateConfigurationOptions, getAggregateConfiguration } from "./aggregates";
5
+ export * from "./interpolate";
6
+ export * from "./minmax";
7
+ export * from "./interval";
8
+ export * from "./common";
9
+ export * from "./average";
10
+ export * from "./read_processed_details";
11
+ export { AggregateFunction } from "node-opcua-constants";
package/dist/index.js CHANGED
@@ -1,33 +1,33 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.AggregateFunction = exports.getAggregateConfiguration = exports.installAggregateConfigurationOptions = exports.addAggregateSupport = void 0;
18
- /**
19
- * @module node-opca-aggregates
20
- */
21
- var aggregates_1 = require("./aggregates");
22
- Object.defineProperty(exports, "addAggregateSupport", { enumerable: true, get: function () { return aggregates_1.addAggregateSupport; } });
23
- Object.defineProperty(exports, "installAggregateConfigurationOptions", { enumerable: true, get: function () { return aggregates_1.installAggregateConfigurationOptions; } });
24
- Object.defineProperty(exports, "getAggregateConfiguration", { enumerable: true, get: function () { return aggregates_1.getAggregateConfiguration; } });
25
- __exportStar(require("./interpolate"), exports);
26
- __exportStar(require("./minmax"), exports);
27
- __exportStar(require("./interval"), exports);
28
- __exportStar(require("./common"), exports);
29
- __exportStar(require("./average"), exports);
30
- __exportStar(require("./read_processed_details"), exports);
31
- var node_opcua_constants_1 = require("node-opcua-constants");
32
- Object.defineProperty(exports, "AggregateFunction", { enumerable: true, get: function () { return node_opcua_constants_1.AggregateFunction; } });
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.AggregateFunction = exports.getAggregateConfiguration = exports.installAggregateConfigurationOptions = exports.addAggregateSupport = void 0;
18
+ /**
19
+ * @module node-opca-aggregates
20
+ */
21
+ var aggregates_1 = require("./aggregates");
22
+ Object.defineProperty(exports, "addAggregateSupport", { enumerable: true, get: function () { return aggregates_1.addAggregateSupport; } });
23
+ Object.defineProperty(exports, "installAggregateConfigurationOptions", { enumerable: true, get: function () { return aggregates_1.installAggregateConfigurationOptions; } });
24
+ Object.defineProperty(exports, "getAggregateConfiguration", { enumerable: true, get: function () { return aggregates_1.getAggregateConfiguration; } });
25
+ __exportStar(require("./interpolate"), exports);
26
+ __exportStar(require("./minmax"), exports);
27
+ __exportStar(require("./interval"), exports);
28
+ __exportStar(require("./common"), exports);
29
+ __exportStar(require("./average"), exports);
30
+ __exportStar(require("./read_processed_details"), exports);
31
+ var node_opcua_constants_1 = require("node-opcua-constants");
32
+ Object.defineProperty(exports, "AggregateFunction", { enumerable: true, get: function () { return node_opcua_constants_1.AggregateFunction; } });
33
33
  //# sourceMappingURL=index.js.map
@@ -1,16 +1,16 @@
1
- /**
2
- * @module node-opcua-aggregates
3
- */
4
- import { UAVariable } from "node-opcua-address-space";
5
- import { DataValue } from "node-opcua-data-value";
6
- import { AggregateConfigurationOptionsEx, Interval } from "./interval";
7
- export declare function interpolatedValue(interval: Interval, options: AggregateConfigurationOptionsEx): DataValue;
8
- /**
9
- *
10
- * @param node
11
- * @param processingInterval
12
- * @param startDate
13
- * @param endDate
14
- * @param callback
15
- */
16
- export declare function getInterpolatedData(node: UAVariable, processingInterval: number, startDate: Date, endDate: Date, callback: (err: Error | null, dataValues?: DataValue[]) => void): void;
1
+ /**
2
+ * @module node-opcua-aggregates
3
+ */
4
+ import { UAVariable } from "node-opcua-address-space";
5
+ import { DataValue } from "node-opcua-data-value";
6
+ import { AggregateConfigurationOptionsEx, Interval } from "./interval";
7
+ export declare function interpolatedValue(interval: Interval, options: AggregateConfigurationOptionsEx): DataValue;
8
+ /**
9
+ *
10
+ * @param node
11
+ * @param processingInterval
12
+ * @param startDate
13
+ * @param endDate
14
+ * @param callback
15
+ */
16
+ export declare function getInterpolatedData(node: UAVariable, processingInterval: number, startDate: Date, endDate: Date, callback: (err: Error | null, dataValues?: DataValue[]) => void): void;
@@ -1,131 +1,131 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getInterpolatedData = exports.interpolatedValue = void 0;
4
- const node_opcua_assert_1 = require("node-opcua-assert");
5
- const node_opcua_data_value_1 = require("node-opcua-data-value");
6
- const node_opcua_status_code_1 = require("node-opcua-status-code");
7
- const common_1 = require("./common");
8
- const interval_1 = require("./interval");
9
- /*
10
- For any intervals containing regions where the StatusCodes are Bad,
11
- the total duration of all Bad regions is calculated and divided by the width of the interval.
12
- The resulting ratio is multiplied by 100 and compared to the PercentDataBad parameter.
13
- The StatusCode for the interval is Bad if the ratio is greater than or equal to the PercentDataBad parameter.
14
- For any interval which is not Bad, the total duration of all Good regions is then calculated and divided by
15
- the width of the interval. The resulting ratio is multiplied by 100 and compared to the PercentDataGood parameter.
16
- The StatusCode for the interval is Good if the ratio is greater than or equal to the PercentDataGood parameter.
17
- If for an interval neither ratio applies then that interval is Uncertain_DataSubNormal.
18
- */
19
- function interpolatedValue(interval, options) {
20
- options = (0, interval_1.adjustProcessingOptions)(options);
21
- (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(options, "useSlopedExtrapolation"));
22
- (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(options, "treatUncertainAsBad"));
23
- const bTreatUncertainAsBad = options.treatUncertainAsBad;
24
- const steppedValue = (previousDataValue) => {
25
- if (!previousDataValue.statusCode) {
26
- throw new Error("Expecting statusCode");
27
- }
28
- const interpValue = new node_opcua_data_value_1.DataValue({
29
- sourceTimestamp: interval.startTime,
30
- statusCode: node_opcua_status_code_1.StatusCodes.Bad,
31
- value: previousDataValue.value
32
- });
33
- interpValue.statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(node_opcua_status_code_1.StatusCodes.UncertainDataSubNormal, "HistorianInterpolated");
34
- return interpValue;
35
- };
36
- if (interval.index === -1) {
37
- // the interval is beyond end Data
38
- // we need to find previous good value
39
- // and second previous good value to extrapolate
40
- const prev1 = (0, interval_1._findGoodDataValueBefore)(interval.dataValues, interval.dataValues.length, bTreatUncertainAsBad);
41
- if (prev1.index <= 0) {
42
- return new node_opcua_data_value_1.DataValue({
43
- sourceTimestamp: interval.startTime,
44
- statusCode: node_opcua_status_code_1.StatusCodes.BadNoData,
45
- value: undefined
46
- });
47
- }
48
- if (!options.useSlopedExtrapolation) {
49
- return steppedValue(prev1.dataValue);
50
- }
51
- const prev2 = (0, interval_1._findGoodDataValueBefore)(interval.dataValues, prev1.index, bTreatUncertainAsBad);
52
- if (prev2.index <= 0) {
53
- // use step value
54
- return steppedValue(prev1.dataValue);
55
- }
56
- // else interpolate
57
- const interpVal = (0, common_1.interpolateValue)(prev2.dataValue, prev1.dataValue, interval.startTime);
58
- // tslint:disable:no-bitwise
59
- if (prev2.index + 1 < prev1.index || prev1.index < interval.dataValues.length - 1) {
60
- // some bad data exist in between = change status code
61
- const mask = 0x0000ffffff;
62
- const extraBits = interpVal.statusCode.value & mask;
63
- interpVal.statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(node_opcua_status_code_1.StatusCodes.UncertainDataSubNormal, extraBits);
64
- }
65
- return interpVal;
66
- }
67
- /* istanbul ignore next */
68
- if (interval.index < 0 && interval.count === 0) {
69
- return new node_opcua_data_value_1.DataValue({
70
- sourceTimestamp: interval.startTime,
71
- statusCode: node_opcua_status_code_1.StatusCodes.BadNoData
72
- });
73
- }
74
- const dataValue1 = interval.dataValues[interval.index];
75
- // if a non-Bad Raw value exists at the timestamp then it is the bounding value;
76
- if (!(0, interval_1.isBad)(dataValue1.statusCode) && interval.hasRawDataAsStart()) {
77
- return dataValue1;
78
- }
79
- // find the first non-Bad Raw value before the timestamp;
80
- // find previous good value
81
- const before = interval.beforeStartDataValue(bTreatUncertainAsBad);
82
- if ((0, interval_1.isBad)(before.dataValue.statusCode)) {
83
- return new node_opcua_data_value_1.DataValue({
84
- sourceTimestamp: interval.startTime,
85
- statusCode: node_opcua_status_code_1.StatusCodes.BadNoData
86
- });
87
- }
88
- if (options.stepped) {
89
- if (before.index + 1 === interval.index) {
90
- return new node_opcua_data_value_1.DataValue({
91
- sourceTimestamp: interval.startTime,
92
- statusCode: node_opcua_status_code_1.StatusCode.makeStatusCode(before.dataValue.statusCode, "HistorianInterpolated"),
93
- value: before.dataValue.value
94
- });
95
- }
96
- return steppedValue(before.dataValue);
97
- }
98
- // find the first non-Bad Raw value after the timestamp;
99
- const next = interval.nextStartDataValue(bTreatUncertainAsBad);
100
- // draw a line between before value and after value;
101
- // use point where the line crosses the timestamp as an estimate of the bounding value.
102
- // The calculation can be expressed with the following formula:
103
- // V bound = (T bound – T before)x( V after – V before)/( T after – T before) + V before
104
- // where V
105
- // x is a value at ‘x’ and Tx is the timestamp associated with Vx.
106
- const interpolatedDataValue = (0, common_1.interpolateValue)(before.dataValue, next.dataValue, interval.startTime);
107
- if (before.index + 1 < next.index || !(0, interval_1.isGood)(next.dataValue.statusCode) || !(0, interval_1.isGood)(before.dataValue.statusCode)) {
108
- // tslint:disable:no-bitwise
109
- // some bad data exist in between = change status code
110
- const mask = 0x0000ffffff;
111
- const extraBits = interpolatedDataValue.statusCode.value & mask;
112
- interpolatedDataValue.statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(node_opcua_status_code_1.StatusCodes.UncertainDataSubNormal, extraBits);
113
- }
114
- // check if uncertain or bad value exist between before/next
115
- // todo
116
- return interpolatedDataValue;
117
- }
118
- exports.interpolatedValue = interpolatedValue;
119
- /**
120
- *
121
- * @param node
122
- * @param processingInterval
123
- * @param startDate
124
- * @param endDate
125
- * @param callback
126
- */
127
- function getInterpolatedData(node, processingInterval, startDate, endDate, callback) {
128
- (0, common_1.getAggregateData)(node, processingInterval, startDate, endDate, interpolatedValue, callback);
129
- }
130
- exports.getInterpolatedData = getInterpolatedData;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getInterpolatedData = exports.interpolatedValue = void 0;
4
+ const node_opcua_assert_1 = require("node-opcua-assert");
5
+ const node_opcua_data_value_1 = require("node-opcua-data-value");
6
+ const node_opcua_status_code_1 = require("node-opcua-status-code");
7
+ const common_1 = require("./common");
8
+ const interval_1 = require("./interval");
9
+ /*
10
+ For any intervals containing regions where the StatusCodes are Bad,
11
+ the total duration of all Bad regions is calculated and divided by the width of the interval.
12
+ The resulting ratio is multiplied by 100 and compared to the PercentDataBad parameter.
13
+ The StatusCode for the interval is Bad if the ratio is greater than or equal to the PercentDataBad parameter.
14
+ For any interval which is not Bad, the total duration of all Good regions is then calculated and divided by
15
+ the width of the interval. The resulting ratio is multiplied by 100 and compared to the PercentDataGood parameter.
16
+ The StatusCode for the interval is Good if the ratio is greater than or equal to the PercentDataGood parameter.
17
+ If for an interval neither ratio applies then that interval is Uncertain_DataSubNormal.
18
+ */
19
+ function interpolatedValue(interval, options) {
20
+ options = (0, interval_1.adjustProcessingOptions)(options);
21
+ (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(options, "useSlopedExtrapolation"));
22
+ (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(options, "treatUncertainAsBad"));
23
+ const bTreatUncertainAsBad = options.treatUncertainAsBad;
24
+ const steppedValue = (previousDataValue) => {
25
+ if (!previousDataValue.statusCode) {
26
+ throw new Error("Expecting statusCode");
27
+ }
28
+ const interpValue = new node_opcua_data_value_1.DataValue({
29
+ sourceTimestamp: interval.startTime,
30
+ statusCode: node_opcua_status_code_1.StatusCodes.Bad,
31
+ value: previousDataValue.value
32
+ });
33
+ interpValue.statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(node_opcua_status_code_1.StatusCodes.UncertainDataSubNormal, "HistorianInterpolated");
34
+ return interpValue;
35
+ };
36
+ if (interval.index === -1) {
37
+ // the interval is beyond end Data
38
+ // we need to find previous good value
39
+ // and second previous good value to extrapolate
40
+ const prev1 = (0, interval_1._findGoodDataValueBefore)(interval.dataValues, interval.dataValues.length, bTreatUncertainAsBad);
41
+ if (prev1.index <= 0) {
42
+ return new node_opcua_data_value_1.DataValue({
43
+ sourceTimestamp: interval.startTime,
44
+ statusCode: node_opcua_status_code_1.StatusCodes.BadNoData,
45
+ value: undefined
46
+ });
47
+ }
48
+ if (!options.useSlopedExtrapolation) {
49
+ return steppedValue(prev1.dataValue);
50
+ }
51
+ const prev2 = (0, interval_1._findGoodDataValueBefore)(interval.dataValues, prev1.index, bTreatUncertainAsBad);
52
+ if (prev2.index <= 0) {
53
+ // use step value
54
+ return steppedValue(prev1.dataValue);
55
+ }
56
+ // else interpolate
57
+ const interpVal = (0, common_1.interpolateValue)(prev2.dataValue, prev1.dataValue, interval.startTime);
58
+ // tslint:disable:no-bitwise
59
+ if (prev2.index + 1 < prev1.index || prev1.index < interval.dataValues.length - 1) {
60
+ // some bad data exist in between = change status code
61
+ const mask = 0x0000ffffff;
62
+ const extraBits = interpVal.statusCode.value & mask;
63
+ interpVal.statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(node_opcua_status_code_1.StatusCodes.UncertainDataSubNormal, extraBits);
64
+ }
65
+ return interpVal;
66
+ }
67
+ /* istanbul ignore next */
68
+ if (interval.index < 0 && interval.count === 0) {
69
+ return new node_opcua_data_value_1.DataValue({
70
+ sourceTimestamp: interval.startTime,
71
+ statusCode: node_opcua_status_code_1.StatusCodes.BadNoData
72
+ });
73
+ }
74
+ const dataValue1 = interval.dataValues[interval.index];
75
+ // if a non-Bad Raw value exists at the timestamp then it is the bounding value;
76
+ if (!(0, interval_1.isBad)(dataValue1.statusCode) && interval.hasRawDataAsStart()) {
77
+ return dataValue1;
78
+ }
79
+ // find the first non-Bad Raw value before the timestamp;
80
+ // find previous good value
81
+ const before = interval.beforeStartDataValue(bTreatUncertainAsBad);
82
+ if ((0, interval_1.isBad)(before.dataValue.statusCode)) {
83
+ return new node_opcua_data_value_1.DataValue({
84
+ sourceTimestamp: interval.startTime,
85
+ statusCode: node_opcua_status_code_1.StatusCodes.BadNoData
86
+ });
87
+ }
88
+ if (options.stepped) {
89
+ if (before.index + 1 === interval.index) {
90
+ return new node_opcua_data_value_1.DataValue({
91
+ sourceTimestamp: interval.startTime,
92
+ statusCode: node_opcua_status_code_1.StatusCode.makeStatusCode(before.dataValue.statusCode, "HistorianInterpolated"),
93
+ value: before.dataValue.value
94
+ });
95
+ }
96
+ return steppedValue(before.dataValue);
97
+ }
98
+ // find the first non-Bad Raw value after the timestamp;
99
+ const next = interval.nextStartDataValue(bTreatUncertainAsBad);
100
+ // draw a line between before value and after value;
101
+ // use point where the line crosses the timestamp as an estimate of the bounding value.
102
+ // The calculation can be expressed with the following formula:
103
+ // V bound = (T bound – T before)x( V after – V before)/( T after – T before) + V before
104
+ // where V
105
+ // x is a value at ‘x’ and Tx is the timestamp associated with Vx.
106
+ const interpolatedDataValue = (0, common_1.interpolateValue)(before.dataValue, next.dataValue, interval.startTime);
107
+ if (before.index + 1 < next.index || !(0, interval_1.isGood)(next.dataValue.statusCode) || !(0, interval_1.isGood)(before.dataValue.statusCode)) {
108
+ // tslint:disable:no-bitwise
109
+ // some bad data exist in between = change status code
110
+ const mask = 0x0000ffffff;
111
+ const extraBits = interpolatedDataValue.statusCode.value & mask;
112
+ interpolatedDataValue.statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(node_opcua_status_code_1.StatusCodes.UncertainDataSubNormal, extraBits);
113
+ }
114
+ // check if uncertain or bad value exist between before/next
115
+ // todo
116
+ return interpolatedDataValue;
117
+ }
118
+ exports.interpolatedValue = interpolatedValue;
119
+ /**
120
+ *
121
+ * @param node
122
+ * @param processingInterval
123
+ * @param startDate
124
+ * @param endDate
125
+ * @param callback
126
+ */
127
+ function getInterpolatedData(node, processingInterval, startDate, endDate, callback) {
128
+ (0, common_1.getAggregateData)(node, processingInterval, startDate, endDate, interpolatedValue, callback);
129
+ }
130
+ exports.getInterpolatedData = getInterpolatedData;
131
131
  //# sourceMappingURL=interpolate.js.map