node-opcua-aggregates 2.97.0 → 2.98.1

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/average.js CHANGED
@@ -1,61 +1,61 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getAverageData = void 0;
4
- const node_opcua_data_value_1 = require("node-opcua-data-value");
5
- const node_opcua_variant_1 = require("node-opcua-variant");
6
- const node_opcua_status_code_1 = require("node-opcua-status-code");
7
- const common_1 = require("./common");
8
- function calculateIntervalAverageValue(interval, options) {
9
- const indexStart = interval.index;
10
- let statusCode;
11
- let isPartial = interval.isPartial;
12
- const isRaw = false;
13
- let hasBad = false;
14
- const values = [];
15
- for (let i = indexStart; i < indexStart + interval.count; i++) {
16
- const dataValue = interval.dataValues[i];
17
- if (dataValue.statusCode.equals(node_opcua_status_code_1.StatusCodes.BadNoData)) {
18
- isPartial = true;
19
- continue;
20
- }
21
- if (dataValue.statusCode.isNotGood()) {
22
- hasBad = true;
23
- continue;
24
- }
25
- values.push(dataValue.value.value);
26
- }
27
- if (isRaw) {
28
- if (hasBad) {
29
- statusCode = node_opcua_status_code_1.StatusCodes.UncertainDataSubNormal;
30
- }
31
- else {
32
- statusCode = node_opcua_status_code_1.StatusCodes.Good;
33
- }
34
- }
35
- else if (hasBad) {
36
- statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(node_opcua_status_code_1.StatusCodes.UncertainDataSubNormal, "HistorianCalculated");
37
- }
38
- else {
39
- statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(node_opcua_status_code_1.StatusCodes.Good, "HistorianCalculated");
40
- }
41
- if (values.length === 0) {
42
- return new node_opcua_data_value_1.DataValue({
43
- sourceTimestamp: interval.startTime,
44
- statusCode: node_opcua_status_code_1.StatusCodes.BadNoData
45
- });
46
- }
47
- const mean = values.reduce((p, c) => p + c, 0) / values.length;
48
- return new node_opcua_data_value_1.DataValue({
49
- sourceTimestamp: interval.startTime,
50
- statusCode: statusCode,
51
- value: {
52
- dataType: node_opcua_variant_1.DataType.Double,
53
- value: mean
54
- }
55
- });
56
- }
57
- function getAverageData(node, processingInterval, startDate, endDate, callback) {
58
- (0, common_1.getAggregateData)(node, processingInterval, startDate, endDate, calculateIntervalAverageValue, callback);
59
- }
60
- exports.getAverageData = getAverageData;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAverageData = void 0;
4
+ const node_opcua_data_value_1 = require("node-opcua-data-value");
5
+ const node_opcua_variant_1 = require("node-opcua-variant");
6
+ const node_opcua_status_code_1 = require("node-opcua-status-code");
7
+ const common_1 = require("./common");
8
+ function calculateIntervalAverageValue(interval, options) {
9
+ const indexStart = interval.index;
10
+ let statusCode;
11
+ let isPartial = interval.isPartial;
12
+ const isRaw = false;
13
+ let hasBad = false;
14
+ const values = [];
15
+ for (let i = indexStart; i < indexStart + interval.count; i++) {
16
+ const dataValue = interval.dataValues[i];
17
+ if (dataValue.statusCode.equals(node_opcua_status_code_1.StatusCodes.BadNoData)) {
18
+ isPartial = true;
19
+ continue;
20
+ }
21
+ if (dataValue.statusCode.isNotGood()) {
22
+ hasBad = true;
23
+ continue;
24
+ }
25
+ values.push(dataValue.value.value);
26
+ }
27
+ if (isRaw) {
28
+ if (hasBad) {
29
+ statusCode = node_opcua_status_code_1.StatusCodes.UncertainDataSubNormal;
30
+ }
31
+ else {
32
+ statusCode = node_opcua_status_code_1.StatusCodes.Good;
33
+ }
34
+ }
35
+ else if (hasBad) {
36
+ statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(node_opcua_status_code_1.StatusCodes.UncertainDataSubNormal, "HistorianCalculated");
37
+ }
38
+ else {
39
+ statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(node_opcua_status_code_1.StatusCodes.Good, "HistorianCalculated");
40
+ }
41
+ if (values.length === 0) {
42
+ return new node_opcua_data_value_1.DataValue({
43
+ sourceTimestamp: interval.startTime,
44
+ statusCode: node_opcua_status_code_1.StatusCodes.BadNoData
45
+ });
46
+ }
47
+ const mean = values.reduce((p, c) => p + c, 0) / values.length;
48
+ return new node_opcua_data_value_1.DataValue({
49
+ sourceTimestamp: interval.startTime,
50
+ statusCode: statusCode,
51
+ value: {
52
+ dataType: node_opcua_variant_1.DataType.Double,
53
+ value: mean
54
+ }
55
+ });
56
+ }
57
+ function getAverageData(node, processingInterval, startDate, endDate, callback) {
58
+ (0, common_1.getAggregateData)(node, processingInterval, startDate, endDate, calculateIntervalAverageValue, callback);
59
+ }
60
+ exports.getAverageData = getAverageData;
61
61
  //# sourceMappingURL=average.js.map
@@ -1,10 +1,10 @@
1
- import { StatusCode } from "node-opcua-status-code";
2
- import { Interval, AggregateConfigurationOptions } from "./interval";
3
- export declare function calculateBadAndGood(interval: Interval, options: AggregateConfigurationOptions): {
4
- durationGood: number;
5
- durationBad: number;
6
- durationUnknown: number;
7
- percentBad: number;
8
- percentGood: number;
9
- statusCode: StatusCode;
10
- };
1
+ import { StatusCode } from "node-opcua-status-code";
2
+ import { Interval, AggregateConfigurationOptions } from "./interval";
3
+ export declare function calculateBadAndGood(interval: Interval, options: AggregateConfigurationOptions): {
4
+ durationGood: number;
5
+ durationBad: number;
6
+ durationUnknown: number;
7
+ percentBad: number;
8
+ percentGood: number;
9
+ statusCode: StatusCode;
10
+ };
@@ -1,118 +1,118 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateBadAndGood = void 0;
4
- const node_opcua_status_code_1 = require("node-opcua-status-code");
5
- const interval_1 = require("./interval");
6
- const a = (s, options) => !s || s === node_opcua_status_code_1.StatusCodes.BadNoData
7
- ? node_opcua_status_code_1.StatusCodes.BadNoData
8
- : s.isBad() || (options.treatUncertainAsBad && (0, interval_1.isUncertain)(s))
9
- ? node_opcua_status_code_1.StatusCodes.Bad
10
- : node_opcua_status_code_1.StatusCodes.Good;
11
- function findLowBound(interval, options) {
12
- const indexStart = interval.index;
13
- const initialValue = interval.dataValues[indexStart];
14
- if (initialValue.sourceTimestamp.getTime() === interval.startTime.getTime()) {
15
- return { previousStatus: initialValue.statusCode, previousTime: interval.startTime.getTime(), indexStart: indexStart + 1 };
16
- }
17
- const previousStatus = indexStart === 0 || !interval.dataValues[indexStart - 1]
18
- ? node_opcua_status_code_1.StatusCodes.BadNoData
19
- : a(interval.dataValues[indexStart - 1].statusCode, options);
20
- const previousTime = interval.startTime.getTime();
21
- return { previousStatus, previousTime, indexStart };
22
- }
23
- // eslint-disable-next-line max-statements, complexity
24
- function calculateBadAndGood(interval, options) {
25
- if (interval.count === 0) {
26
- return {
27
- durationGood: 0,
28
- durationBad: 0,
29
- durationUnknown: 0,
30
- percentBad: 0,
31
- percentGood: 0,
32
- statusCode: node_opcua_status_code_1.StatusCodes.BadNoData
33
- };
34
- }
35
- let durationGood = 0;
36
- let durationBad = 0;
37
- let durationUnknown = 0;
38
- let partialFlag = interval.isPartial ? node_opcua_status_code_1.extraStatusCodeBits.HistorianPartial : 0;
39
- let { previousStatus, previousTime, indexStart } = findLowBound(interval, options);
40
- if (previousStatus === node_opcua_status_code_1.StatusCodes.BadNoData) {
41
- partialFlag = node_opcua_status_code_1.extraStatusCodeBits.HistorianPartial;
42
- previousStatus = node_opcua_status_code_1.StatusCodes.Bad;
43
- }
44
- let nbGood = 0;
45
- let nbBad = 0;
46
- let nbUncertain = 0;
47
- indexStart += 0;
48
- for (let i = indexStart; i < interval.index + interval.count; i++) {
49
- const dataValue = interval.dataValues[i];
50
- if (dataValue.statusCode.isGoodish()) {
51
- nbGood++;
52
- }
53
- if ((0, interval_1.isUncertain)(dataValue.statusCode)) {
54
- nbUncertain++;
55
- }
56
- if (dataValue.statusCode.isBad()) {
57
- nbBad++;
58
- }
59
- const currentStatus = a(dataValue.statusCode, options);
60
- if (currentStatus === node_opcua_status_code_1.StatusCodes.BadNoData) {
61
- partialFlag = node_opcua_status_code_1.extraStatusCodeBits.HistorianPartial;
62
- }
63
- const currentTime = dataValue.sourceTimestamp.getTime();
64
- // console.log(" ", dataValue.sourceTimestamp?.toISOString(), dataValue.statusCode.toString(), dataValue.value.value);
65
- if (currentStatus === previousStatus)
66
- continue;
67
- if (previousStatus === node_opcua_status_code_1.StatusCodes.Good) {
68
- // if (isBadWithUncertain(currentStatus, options.treatUncertainAsBad)) {
69
- // durationBad += currentTime - previousTime;
70
- // } else {
71
- durationGood += currentTime - previousTime;
72
- // }
73
- }
74
- else if (previousStatus === node_opcua_status_code_1.StatusCodes.BadNoData) {
75
- durationUnknown += currentTime - previousTime;
76
- }
77
- else {
78
- durationBad += currentTime - previousTime;
79
- }
80
- previousStatus = currentStatus;
81
- previousTime = currentTime;
82
- }
83
- // final step
84
- const currentTime = interval.getEffectiveEndTime();
85
- if (previousStatus === node_opcua_status_code_1.StatusCodes.Good) {
86
- durationGood += currentTime - previousTime;
87
- }
88
- else if (previousStatus === node_opcua_status_code_1.StatusCodes.BadNoData) {
89
- durationUnknown += currentTime - previousTime;
90
- }
91
- else {
92
- durationBad += currentTime - previousTime;
93
- }
94
- if (nbGood === 0) {
95
- if (nbBad > 0) {
96
- // we need at lest a Good Status in the intervale to be good & no bad !
97
- durationGood = -1;
98
- }
99
- else {
100
- durationGood = 0;
101
- }
102
- }
103
- const effectiveProcessingInterval = currentTime - interval.startTime.getTime();
104
- const percentGood = (durationGood / effectiveProcessingInterval) * 100;
105
- const percentBad = (durationBad / effectiveProcessingInterval) * 100;
106
- let percentDataGood = options.percentDataGood === undefined ? 100 : options.percentDataGood;
107
- const percentDataBad = options.percentDataBad === undefined ? 100 : options.percentDataBad;
108
- if (percentBad >= percentDataBad || (nbGood === 0 && nbUncertain === 0)) {
109
- durationGood = 0; // BAD
110
- percentDataGood = -1;
111
- // const statusCode = StatusCodes.Bad;
112
- //return { durationGood, durationBad, durationUnknown, percentBad, percentGood, statusCode };
113
- }
114
- const statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(node_opcua_status_code_1.StatusCodes.Good, node_opcua_status_code_1.extraStatusCodeBits.HistorianCalculated | partialFlag);
115
- return { durationGood, durationBad, durationUnknown, percentBad, percentGood, statusCode };
116
- }
117
- exports.calculateBadAndGood = calculateBadAndGood;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.calculateBadAndGood = void 0;
4
+ const node_opcua_status_code_1 = require("node-opcua-status-code");
5
+ const interval_1 = require("./interval");
6
+ const a = (s, options) => !s || s === node_opcua_status_code_1.StatusCodes.BadNoData
7
+ ? node_opcua_status_code_1.StatusCodes.BadNoData
8
+ : s.isBad() || (options.treatUncertainAsBad && (0, interval_1.isUncertain)(s))
9
+ ? node_opcua_status_code_1.StatusCodes.Bad
10
+ : node_opcua_status_code_1.StatusCodes.Good;
11
+ function findLowBound(interval, options) {
12
+ const indexStart = interval.index;
13
+ const initialValue = interval.dataValues[indexStart];
14
+ if (initialValue.sourceTimestamp.getTime() === interval.startTime.getTime()) {
15
+ return { previousStatus: initialValue.statusCode, previousTime: interval.startTime.getTime(), indexStart: indexStart + 1 };
16
+ }
17
+ const previousStatus = indexStart === 0 || !interval.dataValues[indexStart - 1]
18
+ ? node_opcua_status_code_1.StatusCodes.BadNoData
19
+ : a(interval.dataValues[indexStart - 1].statusCode, options);
20
+ const previousTime = interval.startTime.getTime();
21
+ return { previousStatus, previousTime, indexStart };
22
+ }
23
+ // eslint-disable-next-line max-statements, complexity
24
+ function calculateBadAndGood(interval, options) {
25
+ if (interval.count === 0) {
26
+ return {
27
+ durationGood: 0,
28
+ durationBad: 0,
29
+ durationUnknown: 0,
30
+ percentBad: 0,
31
+ percentGood: 0,
32
+ statusCode: node_opcua_status_code_1.StatusCodes.BadNoData
33
+ };
34
+ }
35
+ let durationGood = 0;
36
+ let durationBad = 0;
37
+ let durationUnknown = 0;
38
+ let partialFlag = interval.isPartial ? node_opcua_status_code_1.extraStatusCodeBits.HistorianPartial : 0;
39
+ let { previousStatus, previousTime, indexStart } = findLowBound(interval, options);
40
+ if (previousStatus === node_opcua_status_code_1.StatusCodes.BadNoData) {
41
+ partialFlag = node_opcua_status_code_1.extraStatusCodeBits.HistorianPartial;
42
+ previousStatus = node_opcua_status_code_1.StatusCodes.Bad;
43
+ }
44
+ let nbGood = 0;
45
+ let nbBad = 0;
46
+ let nbUncertain = 0;
47
+ indexStart += 0;
48
+ for (let i = indexStart; i < interval.index + interval.count; i++) {
49
+ const dataValue = interval.dataValues[i];
50
+ if (dataValue.statusCode.isGoodish()) {
51
+ nbGood++;
52
+ }
53
+ if ((0, interval_1.isUncertain)(dataValue.statusCode)) {
54
+ nbUncertain++;
55
+ }
56
+ if (dataValue.statusCode.isBad()) {
57
+ nbBad++;
58
+ }
59
+ const currentStatus = a(dataValue.statusCode, options);
60
+ if (currentStatus === node_opcua_status_code_1.StatusCodes.BadNoData) {
61
+ partialFlag = node_opcua_status_code_1.extraStatusCodeBits.HistorianPartial;
62
+ }
63
+ const currentTime = dataValue.sourceTimestamp.getTime();
64
+ // console.log(" ", dataValue.sourceTimestamp?.toISOString(), dataValue.statusCode.toString(), dataValue.value.value);
65
+ if (currentStatus === previousStatus)
66
+ continue;
67
+ if (previousStatus === node_opcua_status_code_1.StatusCodes.Good) {
68
+ // if (isBadWithUncertain(currentStatus, options.treatUncertainAsBad)) {
69
+ // durationBad += currentTime - previousTime;
70
+ // } else {
71
+ durationGood += currentTime - previousTime;
72
+ // }
73
+ }
74
+ else if (previousStatus === node_opcua_status_code_1.StatusCodes.BadNoData) {
75
+ durationUnknown += currentTime - previousTime;
76
+ }
77
+ else {
78
+ durationBad += currentTime - previousTime;
79
+ }
80
+ previousStatus = currentStatus;
81
+ previousTime = currentTime;
82
+ }
83
+ // final step
84
+ const currentTime = interval.getEffectiveEndTime();
85
+ if (previousStatus === node_opcua_status_code_1.StatusCodes.Good) {
86
+ durationGood += currentTime - previousTime;
87
+ }
88
+ else if (previousStatus === node_opcua_status_code_1.StatusCodes.BadNoData) {
89
+ durationUnknown += currentTime - previousTime;
90
+ }
91
+ else {
92
+ durationBad += currentTime - previousTime;
93
+ }
94
+ if (nbGood === 0) {
95
+ if (nbBad > 0) {
96
+ // we need at lest a Good Status in the intervale to be good & no bad !
97
+ durationGood = -1;
98
+ }
99
+ else {
100
+ durationGood = 0;
101
+ }
102
+ }
103
+ const effectiveProcessingInterval = currentTime - interval.startTime.getTime();
104
+ const percentGood = (durationGood / effectiveProcessingInterval) * 100;
105
+ const percentBad = (durationBad / effectiveProcessingInterval) * 100;
106
+ let percentDataGood = options.percentDataGood === undefined ? 100 : options.percentDataGood;
107
+ const percentDataBad = options.percentDataBad === undefined ? 100 : options.percentDataBad;
108
+ if (percentBad >= percentDataBad || (nbGood === 0 && nbUncertain === 0)) {
109
+ durationGood = 0; // BAD
110
+ percentDataGood = -1;
111
+ // const statusCode = StatusCodes.Bad;
112
+ //return { durationGood, durationBad, durationUnknown, percentBad, percentGood, statusCode };
113
+ }
114
+ const statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(node_opcua_status_code_1.StatusCodes.Good, node_opcua_status_code_1.extraStatusCodeBits.HistorianCalculated | partialFlag);
115
+ return { durationGood, durationBad, durationUnknown, percentBad, percentGood, statusCode };
116
+ }
117
+ exports.calculateBadAndGood = calculateBadAndGood;
118
118
  //# sourceMappingURL=calculate_bad_good.js.map
package/dist/common.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- /**
2
- * @module node-opca-aggregates
3
- */
4
- import { UAVariable } from "node-opcua-address-space";
5
- import { DataValue } from "node-opcua-data-value";
6
- import { Interval, AggregateConfigurationOptionsEx } from "./interval";
7
- export declare function getAggregateData(node: UAVariable, processingInterval: number, startDate: Date, endDate: Date, lambda: (interval: Interval, aggregateConfiguration: AggregateConfigurationOptionsEx) => DataValue, callback: (err: Error | null, dataValues?: DataValue[]) => void): void;
8
- export declare function interpolateValue(dataValue1: DataValue, dataValue2: DataValue, date: Date): DataValue;
1
+ /**
2
+ * @module node-opca-aggregates
3
+ */
4
+ import { UAVariable } from "node-opcua-address-space";
5
+ import { DataValue } from "node-opcua-data-value";
6
+ import { Interval, AggregateConfigurationOptionsEx } from "./interval";
7
+ export declare function getAggregateData(node: UAVariable, processingInterval: number, startDate: Date, endDate: Date, lambda: (interval: Interval, aggregateConfiguration: AggregateConfigurationOptionsEx) => DataValue, callback: (err: Error | null, dataValues?: DataValue[]) => void): void;
8
+ export declare function interpolateValue(dataValue1: DataValue, dataValue2: DataValue, date: Date): DataValue;
package/dist/common.js CHANGED
@@ -1,94 +1,94 @@
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
- */
18
- function processAggregateData(node, processingInterval, startDate, endDate, dataValues, lambda, callback) {
19
- const aggregateConfiguration = (0, aggregates_1.getAggregateConfiguration)(node);
20
- const results = [];
21
- const tstart = startDate.getTime();
22
- const tend = endDate.getTime();
23
- const indexHint = 0;
24
- for (let t = tstart; t < tend; t += processingInterval) {
25
- const sourceTimestamp = new Date();
26
- sourceTimestamp.setTime(t);
27
- const interval = (0, interval_1.getInterval)(sourceTimestamp, processingInterval, indexHint, dataValues);
28
- const dataValue = lambda(interval, aggregateConfiguration);
29
- /* istanbul ignore next */
30
- if (!dataValue || !dataValue.sourceTimestamp) {
31
- // const dataValue = interval.interpolatedValue(aggregateConfiguration);
32
- throw Error("invalid DataValue");
33
- }
34
- results.push(dataValue);
35
- // console.log(" => ", dataValue.sourceTimestamp.toISOString(), dataValue.statusCode.toString(), dataValue.value.value);
36
- }
37
- setImmediate(() => {
38
- callback(null, results);
39
- });
40
- }
41
- function getAggregateData(node, processingInterval, startDate, endDate, lambda, callback) {
42
- /* istanbul ignore next */
43
- if (node.nodeClass !== node_opcua_data_model_1.NodeClass.Variable) {
44
- throw new Error("node must be UAVariable");
45
- }
46
- /* istanbul ignore next */
47
- if (processingInterval <= 0) {
48
- throw new Error("Invalid processing interval, shall be greater than 0");
49
- }
50
- const continuationPointManager = new node_opcua_address_space_1.ContinuationPointManager();
51
- const context = new node_opcua_address_space_1.SessionContext({
52
- session: {
53
- continuationPointManager,
54
- getSessionId: () => (0, node_opcua_nodeid_1.coerceNodeId)("i=0")
55
- }
56
- });
57
- const historyReadDetails = new node_opcua_service_history_1.ReadRawModifiedDetails({
58
- endTime: endDate,
59
- startTime: startDate,
60
- isReadModified: false,
61
- numValuesPerNode: 0
62
- // returnBounds: true,
63
- });
64
- const indexRange = null;
65
- const dataEncoding = null;
66
- const continuationPoint = null;
67
- node.historyRead(context, historyReadDetails, indexRange, dataEncoding, { continuationPoint }, (err, result) => {
68
- /* istanbul ignore next */
69
- if (err) {
70
- return callback(err);
71
- }
72
- const historyData = result.historyData;
73
- const dataValues = historyData.dataValues || [];
74
- processAggregateData(node, processingInterval, startDate, endDate, dataValues, lambda, callback);
75
- });
76
- }
77
- exports.getAggregateData = getAggregateData;
78
- function interpolateValue(dataValue1, dataValue2, date) {
79
- const t0 = dataValue1.sourceTimestamp.getTime();
80
- const t = date.getTime();
81
- const t1 = dataValue2.sourceTimestamp.getTime();
82
- const coef1 = (t - t0) / (t1 - t0);
83
- const coef2 = (t1 - t) / (t1 - t0);
84
- const value = dataValue1.value.clone();
85
- value.value = coef2 * dataValue1.value.value + coef1 * dataValue2.value.value;
86
- const statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(dataValue1.statusCode, "HistorianInterpolated");
87
- return new node_opcua_data_value_1.DataValue({
88
- sourceTimestamp: date,
89
- statusCode,
90
- value
91
- });
92
- }
93
- 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
+ */
18
+ function processAggregateData(node, processingInterval, startDate, endDate, dataValues, lambda, callback) {
19
+ const aggregateConfiguration = (0, aggregates_1.getAggregateConfiguration)(node);
20
+ const results = [];
21
+ const tstart = startDate.getTime();
22
+ const tend = endDate.getTime();
23
+ const indexHint = 0;
24
+ for (let t = tstart; t < tend; t += processingInterval) {
25
+ const sourceTimestamp = new Date();
26
+ sourceTimestamp.setTime(t);
27
+ const interval = (0, interval_1.getInterval)(sourceTimestamp, processingInterval, indexHint, dataValues);
28
+ const dataValue = lambda(interval, aggregateConfiguration);
29
+ /* istanbul ignore next */
30
+ if (!dataValue || !dataValue.sourceTimestamp) {
31
+ // const dataValue = interval.interpolatedValue(aggregateConfiguration);
32
+ throw Error("invalid DataValue");
33
+ }
34
+ results.push(dataValue);
35
+ // console.log(" => ", dataValue.sourceTimestamp.toISOString(), dataValue.statusCode.toString(), dataValue.value.value);
36
+ }
37
+ setImmediate(() => {
38
+ callback(null, results);
39
+ });
40
+ }
41
+ function getAggregateData(node, processingInterval, startDate, endDate, lambda, callback) {
42
+ /* istanbul ignore next */
43
+ if (node.nodeClass !== node_opcua_data_model_1.NodeClass.Variable) {
44
+ throw new Error("node must be UAVariable");
45
+ }
46
+ /* istanbul ignore next */
47
+ if (processingInterval <= 0) {
48
+ throw new Error("Invalid processing interval, shall be greater than 0");
49
+ }
50
+ const continuationPointManager = new node_opcua_address_space_1.ContinuationPointManager();
51
+ const context = new node_opcua_address_space_1.SessionContext({
52
+ session: {
53
+ continuationPointManager,
54
+ getSessionId: () => (0, node_opcua_nodeid_1.coerceNodeId)("i=0")
55
+ }
56
+ });
57
+ const historyReadDetails = new node_opcua_service_history_1.ReadRawModifiedDetails({
58
+ endTime: endDate,
59
+ startTime: startDate,
60
+ isReadModified: false,
61
+ numValuesPerNode: 0
62
+ // returnBounds: true,
63
+ });
64
+ const indexRange = null;
65
+ const dataEncoding = null;
66
+ const continuationPoint = null;
67
+ node.historyRead(context, historyReadDetails, indexRange, dataEncoding, { continuationPoint }, (err, result) => {
68
+ /* istanbul ignore next */
69
+ if (err) {
70
+ return callback(err);
71
+ }
72
+ const historyData = result.historyData;
73
+ const dataValues = historyData.dataValues || [];
74
+ processAggregateData(node, processingInterval, startDate, endDate, dataValues, lambda, callback);
75
+ });
76
+ }
77
+ exports.getAggregateData = getAggregateData;
78
+ function interpolateValue(dataValue1, dataValue2, date) {
79
+ const t0 = dataValue1.sourceTimestamp.getTime();
80
+ const t = date.getTime();
81
+ const t1 = dataValue2.sourceTimestamp.getTime();
82
+ const coef1 = (t - t0) / (t1 - t0);
83
+ const coef2 = (t1 - t) / (t1 - t0);
84
+ const value = dataValue1.value.clone();
85
+ value.value = coef2 * dataValue1.value.value + coef1 * dataValue2.value.value;
86
+ const statusCode = node_opcua_status_code_1.StatusCode.makeStatusCode(dataValue1.statusCode, "HistorianInterpolated");
87
+ return new node_opcua_data_value_1.DataValue({
88
+ sourceTimestamp: date,
89
+ statusCode,
90
+ value
91
+ });
92
+ }
93
+ exports.interpolateValue = interpolateValue;
94
94
  //# sourceMappingURL=common.js.map
package/dist/count.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { UAVariable } from "node-opcua-address-space";
2
- import { DataValue } from "node-opcua-data-value";
3
- export declare function getCountData(node: UAVariable, processingInterval: number, startDate: Date, endDate: Date, callback: (err: Error | null, dataValues?: DataValue[]) => void): void;
1
+ import { UAVariable } from "node-opcua-address-space";
2
+ import { DataValue } from "node-opcua-data-value";
3
+ export declare function getCountData(node: UAVariable, processingInterval: number, startDate: Date, endDate: Date, callback: (err: Error | null, dataValues?: DataValue[]) => void): void;