node-opcua-aggregates 2.55.0 → 2.58.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.
@@ -1,139 +1,149 @@
1
- import * as async from "async";
2
- import { AggregateFunction } from "node-opcua-constants";
3
- import { ISessionContext, ContinuationPoint, UAVariable } from "node-opcua-address-space";
4
- import { NumericRange } from "node-opcua-numeric-range";
5
- import { QualifiedNameLike } from "node-opcua-data-model";
6
- import { CallbackT, StatusCodes } from "node-opcua-status-code";
7
- import { DataValue } from "node-opcua-data-value";
8
- import { ObjectIds } from "node-opcua-constants";
9
- import { NodeId } from "node-opcua-nodeid";
10
- import { getMinData, getMaxData } from "./minmax";
11
- import {
12
- HistoryData,
13
- HistoryReadResult,
14
- ReadAtTimeDetails,
15
- ReadEventDetails,
16
- ReadProcessedDetails,
17
- ReadRawModifiedDetails
18
- } from "node-opcua-service-history";
19
-
20
- import { getInterpolatedData } from "./interpolate";
21
- import { getAverageData } from "./average";
22
-
23
- export function readProcessedDetails(
24
- variable: UAVariable,
25
- context: ISessionContext,
26
- historyReadDetails: ReadProcessedDetails,
27
- indexRange: NumericRange | null,
28
- dataEncoding: QualifiedNameLike | null,
29
- continuationPoint: ContinuationPoint | null,
30
- callback: CallbackT<HistoryReadResult>
31
- ) {
32
- // OPC Unified Architecture, Part 11 27 Release 1.03
33
- //
34
- // This structure is used to compute Aggregate values, qualities, and timestamps from data in
35
- // the history database for the specified time domain for one or more HistoricalDataNodes. The
36
- // time domain is divided into intervals of duration ProcessingInterval. The specified Aggregate
37
- // Type is calculated for each interval beginning with startTime by using the data within the next
38
- // ProcessingInterval.
39
- // For example, this function can provide hourly statistics such as Maximum, Minimum , and
40
- // Average for each item during the specified time domain when ProcessingInterval is 1 hour.
41
- // The domain of the request is defined by startTime, endTime, and ProcessingInterval. All three
42
- // shall be specified. If endTime is less than startTime then the data shall be returned in reverse
43
- // order with the later data coming first. If startTime and endTime are the same then the Server
44
- // shall return Bad_InvalidArgument as there is no meaningful way to interpret such a case. If
45
- // the ProcessingInterval is specified as 0 then Aggregates shall be calculated using one interval
46
- // starting at startTime and ending at endTime.
47
- // The aggregateType[] parameter allows a Client to request multiple Aggregate calculations per
48
- // requested NodeId. If multiple Aggregates are requested then a corresponding number of
49
- // entries are required in the NodesToRead array.
50
- // For example, to request Min Aggregate for NodeId FIC101, FIC102, and both Min and Max
51
- // Aggregates for NodeId FIC103 would require NodeId FIC103 to appear twice in the
52
- // NodesToRead array request parameter.
53
- // aggregateType[] NodesToRead[]
54
- // Min FIC101
55
- // Min FIC102
56
- // Min FIC103
57
- // Max FIC103
58
- // If the array of Aggregates does not match the array of NodesToRead then the Server shall
59
- // return a StatusCode of Bad_AggregateListMismatch.
60
- // The aggregateConfiguration parameter allows a Client to override the Aggregate configuration
61
- // settings supplied by the AggregateConfiguration Object on a per call basis. See Part 13 for
62
- // more information on Aggregate configurations. If the Server does not support the ability to
63
- // override the Aggregate configuration settings then it shall return a StatusCode of Bad_
64
- // AggregateConfigurationRejected. If the Aggregate is not valid for the Node then the
65
- // StatusCode shall be Bad_AggregateNotSupported.
66
- // The values used in computing the Aggregate for each interval shall include any value that
67
- // falls exactly on the timestamp at the beginning of the interval, but shall not include any value
68
- // that falls directly on the timestamp ending the interval. Thus, each value shall be included
69
- // only once in the calculation. If the time domain is in reverse order then we consider the later
70
- // timestamp to be the one beginning the sub interval, and the earlier timestamp to be the one
71
- // ending it. Note that this means that simply swapping the start and end times will not result in
72
- // getting the same values back in reverse order as the intervals being requested in the two
73
- // cases are not the same.
74
- // If an Aggregate is taking a long time to calculate then the Server can return partial results
75
- // with a continuation point. This might be done if the calculation is going to take more time th an
76
- // the Client timeout hint. In some cases it may take longer than the Client timeout hint to
77
- // calculate even one Aggregate result. Then the Server may return zero results with a
78
- // continuation point that allows the Server to resume the calculation on the next Client read
79
- // call.
80
- const startTime = historyReadDetails.startTime;
81
- const endTime = historyReadDetails.endTime;
82
- if (!startTime || !endTime) {
83
- return callback(null, new HistoryReadResult({ statusCode: StatusCodes.BadInvalidArgument }));
84
- }
85
- if (startTime.getTime() === endTime.getTime()) {
86
- // Start = End Int = Anything No intervals. Returns a Bad_InvalidArgument StatusCode,
87
- // regardless of whether there is data at the specified time or not
88
- return callback(null, new HistoryReadResult({ statusCode: StatusCodes.BadInvalidArgument }));
89
- }
90
-
91
- const aggregateTypes: NodeId[] = historyReadDetails.aggregateType || [];
92
-
93
- // If the ProcessingInterval is specified as 0 then Aggregates shall be calculated using one interval
94
- // starting at startTime and ending at endTime.
95
- const processingInterval = historyReadDetails.processingInterval || endTime.getTime() - startTime.getTime();
96
-
97
- // tslint:disable-next-line: prefer-for-of
98
-
99
- function applyAggregate(aggregateType: NodeId, callback2: (err: Error | null, result: HistoryReadResult) => void) {
100
- function buildResult(err: Error | null, dataValues?: DataValue[]) {
101
- if (err) {
102
- return callback2(null, new HistoryReadResult({ statusCode: StatusCodes.BadInternalError }));
103
- }
104
- const result = new HistoryReadResult({
105
- historyData: new HistoryData({
106
- dataValues
107
- }),
108
- statusCode: StatusCodes.Good
109
- });
110
- return callback2(null, result);
111
- }
112
-
113
- if (!startTime || !endTime) {
114
- return buildResult(new Error("Invalid date time"));
115
- }
116
- switch (aggregateType.value) {
117
- case AggregateFunction.Minimum:
118
- getMinData(variable, processingInterval, startTime, endTime, buildResult);
119
- break;
120
- case AggregateFunction.Maximum:
121
- getMaxData(variable, processingInterval, startTime, endTime, buildResult);
122
- break;
123
- case AggregateFunction.Interpolative:
124
- getInterpolatedData(variable, processingInterval, startTime, endTime, buildResult);
125
- break;
126
- case AggregateFunction.Average:
127
- getAverageData(variable, processingInterval, startTime, endTime, buildResult);
128
- break;
129
- case AggregateFunction.Count:
130
- default:
131
- // todo provide correct implementation
132
- return callback2(null, new HistoryReadResult({ statusCode: StatusCodes.BadAggregateNotSupported }));
133
- }
134
- }
135
- if (historyReadDetails.aggregateType?.length !== 1) {
136
- return callback(null, new HistoryReadResult({ statusCode: StatusCodes.BadInternalError }));
137
- }
138
- return applyAggregate(aggregateTypes[0], callback);
139
- }
1
+ import * as async from "async";
2
+ import { AggregateFunction } from "node-opcua-constants";
3
+ import { ISessionContext, ContinuationPoint, UAVariable, ContinuationData } from "node-opcua-address-space";
4
+ import { NumericRange } from "node-opcua-numeric-range";
5
+ import { QualifiedNameLike } from "node-opcua-data-model";
6
+ import { CallbackT, StatusCodes } from "node-opcua-status-code";
7
+ import { DataValue } from "node-opcua-data-value";
8
+ import { ObjectIds } from "node-opcua-constants";
9
+ import { NodeId } from "node-opcua-nodeid";
10
+ import {
11
+ HistoryData,
12
+ HistoryReadResult,
13
+ ReadAtTimeDetails,
14
+ ReadEventDetails,
15
+ ReadProcessedDetails,
16
+ ReadRawModifiedDetails
17
+ } from "node-opcua-service-history";
18
+
19
+ import { getMinData, getMaxData } from "./minmax";
20
+
21
+ import { getInterpolatedData } from "./interpolate";
22
+ import { getAverageData } from "./average";
23
+
24
+ function _buildResult(err: Error | null, dataValues: DataValue[] | undefined, callback2: CallbackT<HistoryReadResult>) {
25
+ if (err) {
26
+ return callback2(null, new HistoryReadResult({ statusCode: StatusCodes.BadInternalError }));
27
+ }
28
+ const result = new HistoryReadResult({
29
+ historyData: new HistoryData({
30
+ dataValues
31
+ }),
32
+ statusCode: StatusCodes.Good
33
+ });
34
+ return callback2(null, result);
35
+ }
36
+
37
+ function applyAggregate(
38
+ variable: UAVariable,
39
+ processingInterval: number,
40
+ startTime: Date,
41
+ endTime: Date,
42
+ aggregateType: NodeId,
43
+ callback2: CallbackT<HistoryReadResult>
44
+ ) {
45
+ if (!startTime || !endTime) {
46
+ return _buildResult(new Error("Invalid date time"), undefined, callback2);
47
+ }
48
+ const buildResult = (err: Error | null, dataValues: DataValue[] | undefined) => {
49
+ _buildResult(err, dataValues, callback2);
50
+ };
51
+ switch (aggregateType.value) {
52
+ case AggregateFunction.Minimum:
53
+ getMinData(variable, processingInterval, startTime, endTime, buildResult);
54
+ break;
55
+ case AggregateFunction.Maximum:
56
+ getMaxData(variable, processingInterval, startTime, endTime, buildResult);
57
+ break;
58
+ case AggregateFunction.Interpolative:
59
+ getInterpolatedData(variable, processingInterval, startTime, endTime, buildResult);
60
+ break;
61
+ case AggregateFunction.Average:
62
+ getAverageData(variable, processingInterval, startTime, endTime, buildResult);
63
+ break;
64
+ case AggregateFunction.Count:
65
+ default:
66
+ // todo provide correct implementation
67
+ return callback2(null, new HistoryReadResult({ statusCode: StatusCodes.BadAggregateNotSupported }));
68
+ }
69
+ }
70
+ export function readProcessedDetails(
71
+ variable: UAVariable,
72
+ context: ISessionContext,
73
+ historyReadDetails: ReadProcessedDetails,
74
+ indexRange: NumericRange | null,
75
+ dataEncoding: QualifiedNameLike | null,
76
+ continuationData: ContinuationData,
77
+ callback: CallbackT<HistoryReadResult>
78
+ ): void {
79
+ // OPC Unified Architecture, Part 11 27 Release 1.03
80
+ //
81
+ // This structure is used to compute Aggregate values, qualities, and timestamps from data in
82
+ // the history database for the specified time domain for one or more HistoricalDataNodes. The
83
+ // time domain is divided into intervals of duration ProcessingInterval. The specified Aggregate
84
+ // Type is calculated for each interval beginning with startTime by using the data within the next
85
+ // ProcessingInterval.
86
+ // For example, this function can provide hourly statistics such as Maximum, Minimum , and
87
+ // Average for each item during the specified time domain when ProcessingInterval is 1 hour.
88
+ // The domain of the request is defined by startTime, endTime, and ProcessingInterval. All three
89
+ // shall be specified. If endTime is less than startTime then the data shall be returned in reverse
90
+ // order with the later data coming first. If startTime and endTime are the same then the Server
91
+ // shall return Bad_InvalidArgument as there is no meaningful way to interpret such a case. If
92
+ // the ProcessingInterval is specified as 0 then Aggregates shall be calculated using one interval
93
+ // starting at startTime and ending at endTime.
94
+ // The aggregateType[] parameter allows a Client to request multiple Aggregate calculations per
95
+ // requested NodeId. If multiple Aggregates are requested then a corresponding number of
96
+ // entries are required in the NodesToRead array.
97
+ // For example, to request Min Aggregate for NodeId FIC101, FIC102, and both Min and Max
98
+ // Aggregates for NodeId FIC103 would require NodeId FIC103 to appear twice in the
99
+ // NodesToRead array request parameter.
100
+ // aggregateType[] NodesToRead[]
101
+ // Min FIC101
102
+ // Min FIC102
103
+ // Min FIC103
104
+ // Max FIC103
105
+ // If the array of Aggregates does not match the array of NodesToRead then the Server shall
106
+ // return a StatusCode of Bad_AggregateListMismatch.
107
+ // The aggregateConfiguration parameter allows a Client to override the Aggregate configuration
108
+ // settings supplied by the AggregateConfiguration Object on a per call basis. See Part 13 for
109
+ // more information on Aggregate configurations. If the Server does not support the ability to
110
+ // override the Aggregate configuration settings then it shall return a StatusCode of Bad_
111
+ // AggregateConfigurationRejected. If the Aggregate is not valid for the Node then the
112
+ // StatusCode shall be Bad_AggregateNotSupported.
113
+ // The values used in computing the Aggregate for each interval shall include any value that
114
+ // falls exactly on the timestamp at the beginning of the interval, but shall not include any value
115
+ // that falls directly on the timestamp ending the interval. Thus, each value shall be included
116
+ // only once in the calculation. If the time domain is in reverse order then we consider the later
117
+ // timestamp to be the one beginning the sub interval, and the earlier timestamp to be the one
118
+ // ending it. Note that this means that simply swapping the start and end times will not result in
119
+ // getting the same values back in reverse order as the intervals being requested in the two
120
+ // cases are not the same.
121
+ // If an Aggregate is taking a long time to calculate then the Server can return partial results
122
+ // with a continuation point. This might be done if the calculation is going to take more time th an
123
+ // the Client timeout hint. In some cases it may take longer than the Client timeout hint to
124
+ // calculate even one Aggregate result. Then the Server may return zero results with a
125
+ // continuation point that allows the Server to resume the calculation on the next Client read
126
+ // call.
127
+ const startTime = historyReadDetails.startTime;
128
+ const endTime = historyReadDetails.endTime;
129
+ if (!startTime || !endTime) {
130
+ return callback(null, new HistoryReadResult({ statusCode: StatusCodes.BadInvalidArgument }));
131
+ }
132
+ if (startTime.getTime() === endTime.getTime()) {
133
+ // Start = End Int = Anything No intervals. Returns a Bad_InvalidArgument StatusCode,
134
+ // regardless of whether there is data at the specified time or not
135
+ return callback(null, new HistoryReadResult({ statusCode: StatusCodes.BadInvalidArgument }));
136
+ }
137
+
138
+ const aggregateTypes: NodeId[] = historyReadDetails.aggregateType || [];
139
+
140
+ // If the ProcessingInterval is specified as 0 then Aggregates shall be calculated using one interval
141
+ // starting at startTime and ending at endTime.
142
+ const processingInterval = historyReadDetails.processingInterval || endTime.getTime() - startTime.getTime();
143
+
144
+ // tslint:disable-next-line: prefer-for-of
145
+ if (historyReadDetails.aggregateType?.length !== 1) {
146
+ return callback(null, new HistoryReadResult({ statusCode: StatusCodes.BadInternalError }));
147
+ }
148
+ return applyAggregate(variable, processingInterval, startTime, endTime, aggregateTypes[0], callback);
149
+ }