-
-
Notifications
You must be signed in to change notification settings - Fork 478
/
address_space_historical_data_node.ts
907 lines (809 loc) · 39.4 KB
/
address_space_historical_data_node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
/**
* @module node-opcua-address-space
* @class AddressSpace
*/
// tslint:disable:no-console
import * as chalk from "chalk";
import * as _ from "underscore";
import { assert } from "node-opcua-assert";
import { AccessLevelFlag, NodeClass, QualifiedNameLike } from "node-opcua-data-model";
import { DataValue } from "node-opcua-data-value";
import { isMinDate } from "node-opcua-date-time";
import { NumericRange } from "node-opcua-numeric-range";
import {
HistoryData,
HistoryReadResult, ReadAtTimeDetails, ReadEventDetails, ReadProcessedDetails,
ReadRawModifiedDetails
} from "node-opcua-service-history";
import { StatusCodes } from "node-opcua-status-code";
import { DataType } from "node-opcua-variant";
import {
AddressSpace as AddressSpacePublic,
Callback,
ContinuationPoint,
HistoricalDataConfiguration,
IVariableHistorian,
IVariableHistorianOptions
} from "../../source";
import { AddressSpace } from "../address_space";
import { SessionContext } from "../session_context";
import { UAVariable } from "../ua_variable";
// tslint:disable:no-var-requires
const Dequeue = require("dequeue");
/* interface Historian */
// {
//
// }
function inInTimeRange(
historyReadDetails: any,
dataValue: DataValue
): boolean {
if (historyReadDetails.startTime &&
!isMinDate(historyReadDetails.startTime)
&& dataValue.sourceTimestamp! < historyReadDetails.startTime) {
return false;
}
if (historyReadDetails.endTime
&& !isMinDate(historyReadDetails.endTime)
&& dataValue.sourceTimestamp! > historyReadDetails.endTime) {
return false;
}
return true;
}
function inInTimeRange2(
historyReadDetails: any,
dataValue: DataValue
): boolean {
if (historyReadDetails.endTime &&
!isMinDate(historyReadDetails.endTime) &&
dataValue.sourceTimestamp! > historyReadDetails.endTime) {
return false;
}
return !(historyReadDetails.startTime &&
!isMinDate(historyReadDetails.startTime) &&
dataValue.sourceTimestamp! < historyReadDetails.startTime);
}
function filter_dequeue(
q: any,
historyReadRawModifiedDetails: ReadRawModifiedDetails,
onlyThisNumber: number,
isReversed: boolean
) {
const r: any[] = [];
const predicate = isReversed ?
inInTimeRange2.bind(null, historyReadRawModifiedDetails)
: inInTimeRange.bind(null, historyReadRawModifiedDetails);
if (isReversed) {
let c = q.head.prev;
while (c.data) {
if (predicate(c.data)) {
r.push(c.data);
}
c = c.prev;
if (onlyThisNumber && r.length === onlyThisNumber) {
return r;
}
}
} else {
let c = q.head.next;
while (c.data) {
if (predicate(c.data)) {
r.push(c.data);
}
c = c.next;
if (onlyThisNumber && r.length === onlyThisNumber) {
return r;
}
}
}
return r;
}
export class VariableHistorian implements IVariableHistorian {
public readonly node: UAVariable;
private readonly _timeline: any /* Dequeue */;
private readonly _maxOnlineValues: number;
private lastDate: Date;
private lastDatePicoSeconds: number;
constructor(
node: UAVariable,
options: IVariableHistorianOptions
) {
this._timeline = new Dequeue(); // is is ordered here ??????
this._maxOnlineValues = options.maxOnlineValues || 1000;
this.lastDate = new Date(1600, 0, 1, 0, 0, 0);
this.lastDatePicoSeconds = 0;
this.node = node;
}
/* public */
public push(newDataValue: DataValue): any {
this._timeline.push(newDataValue);
const sourceTime = newDataValue.sourceTimestamp || new Date();
const sourcePicoSeconds = newDataValue.sourcePicoseconds || 0;
// ensure that values are set with date increasing
if (sourceTime.getTime() <= this.lastDate.getTime()) {
if (!(sourceTime.getTime() === this.lastDate.getTime() && sourcePicoSeconds > this.lastDatePicoSeconds)) {
console.log(chalk.red("Warning date not increasing "),
newDataValue.toString(), " last known date = ", this.lastDate);
}
}
this.lastDate = sourceTime;
this.lastDatePicoSeconds = newDataValue.sourcePicoseconds || 0;
// we keep only a limited amount in main memory
if (this._timeline.length > this._maxOnlineValues) {
assert(_.isNumber(this._maxOnlineValues) && this._maxOnlineValues > 0);
while (this._timeline.length > this._maxOnlineValues) {
this._timeline.shift();
}
}
if (this._timeline.length >= this._maxOnlineValues || this._timeline.length === 1) {
const first = this._timeline.first();
this.node._update_startOfOnlineArchive(first.sourceTimestamp);
// we update the node startOnlineDate
}
}
public extractDataValues(
historyReadRawModifiedDetails: any,
maxNumberToExtract: number,
isReversed: boolean,
reverseDataValue: boolean,
callback: Callback<DataValue[]>
) {
assert(callback instanceof Function);
let dataValues = filter_dequeue(
this._timeline,
historyReadRawModifiedDetails,
maxNumberToExtract, isReversed);
if (reverseDataValue) {
dataValues = dataValues.reverse();
}
callback(null, dataValues);
}
}
function _get_startOfOfflineArchive(node: UAVariable) {
if (!node.$historicalDataConfiguration) {
throw new Error("this variable has no HistoricalDataConfiguration");
}
return node.$historicalDataConfiguration.startOfArchive.readValue();
}
function _get_startOfArchive(node: UAVariable) {
if (!node.$historicalDataConfiguration) {
throw new Error("this variable has no HistoricalDataConfiguration");
}
return node.$historicalDataConfiguration!.startOfArchive.readValue();
}
function _update_startOfArchive(this: UAVariable, newDate: Date): void {
const node = this;
if (!node.$historicalDataConfiguration) {
throw new Error("this variable has no HistoricalDataConfiguration");
}
node.$historicalDataConfiguration.startOfArchive.setValueFromSource({
dataType: DataType.DateTime, value: newDate
});
}
function _update_startOfOnlineArchive(this: UAVariable, newDate: Date): void {
const node = this;
if (!node.$historicalDataConfiguration) {
throw new Error("this variable has no HistoricalDataConfiguration");
}
// The StartOfArchive Variable specifies the date before which there is no data
// in the archive either online or offline.
// The StartOfOnlineArchive Variable specifies the date of the earliest data
// in the online archive.
node.$historicalDataConfiguration.startOfOnlineArchive.setValueFromSource({
dataType: DataType.DateTime, value: newDate
});
const startOfArchiveDataValue = _get_startOfOfflineArchive(node);
if (startOfArchiveDataValue.statusCode !== StatusCodes.Good ||
!startOfArchiveDataValue.value || ! startOfArchiveDataValue.value.value ||
startOfArchiveDataValue.value.value.getTime() >= newDate.getTime()) {
node._update_startOfArchive(newDate);
}
}
UAVariable.prototype._update_startOfOnlineArchive = _update_startOfOnlineArchive;
UAVariable.prototype._update_startOfArchive = _update_startOfArchive;
function _historyPush(this: UAVariable, newDataValue: DataValue) {
const node = this;
if (!node.varHistorian) {
throw new Error("this variable has no HistoricalDataConfiguration");
}
assert(node.hasOwnProperty("historizing"), "expecting a historizing attribute on node");
if (!node.historizing) {
return; //
}
assert(node.historizing === true);
node.varHistorian.push(newDataValue);
}
function createContinuationPoint(): ContinuationPoint {
// todo: improve
return Buffer.from("ABCDEF");
}
function _historyReadModify(
this: UAVariable,
context: SessionContext,
historyReadRawModifiedDetails: any,
indexRange: NumericRange | null,
dataEncoding: QualifiedNameLike | null,
continuationPoint: ContinuationPoint | null,
callback: Callback<HistoryReadResult>
) {
//
// 6.4.3.3 Read modified functionality
// Release 1.03 26 OPC Unified Architecture, Part 11
//
// When this structure is used for reading Modified Values (isReadModified is set to TRUE), it
// reads the modified values, StatusCodes, timestamps, modification type, the user identifier,
// and the timestamp of the modification from the history database for the specified time domain
// for one or more HistoricalDataNodes. If there are multiple replaced values the Server shall
// return all of them. The updateType specifies what value is returned in the modification record.
// If the updateType is INSERT the value is the new value that was inserted. If the updateType
// is anything else the value is the old value that was changed. See 6.8 HistoryUpdateDetails
// parameter for details on what updateTypes are available.
// The purpose of this function is to read values from history that have been Modified. The
// returnBounds parameter shall be set to FALSE for this case, otherwise the Server returns a
// BadInvalidArgument StatusCode.
// The domain of the request is defined by startTime, endTime, and numValuesPerNode; at least
// two of these shall be specified. If endTime is less than startTime, or endTime and
// numValuesPerNode alone are specified, then the data shall be returned in reverse order with
// the later data coming first. If all three are specified then the call shall return up to
// numValuesPerNode results going from StartTime to EndTime, in either ascending or
// descending order depending on the relative values of StartTime and EndTime. If more than
// numValuesPerNode values exist within that time range for a given Node then only
// numValuesPerNode values per Node are returned along with a continuationPoint. When a
// continuationPoint is returned, a Client wanting the next numValuesPerNode values should call
// ReadRaw again with the continuationPoint set. If numValuesPerNode is 0 then all of the
// values in the range are returned. If the Server cannot return all modified values for a given
// timestamp in a single response then it shall return modified values with the same timestamp
// in subsequent calls.
// If the request takes a long time to process then the Server can return partial results with a
// ContinuationPoint. This might be done if the request is going to take more time than the Client
// timeout hint. It may take longer than the Client timeout hint to retrieve any results. In this case
// the Server may return zero results with a ContinuationPoint that allows the Server to resume
// the calculation on the next Client HistoryRead call.
// If a value has been modified multiple times then all values for the time are returned. This
// means that a timestamp can appear in the array more than once. The order of the returned
// values with the same timestamp should be from the most recent to oldest modification
// timestamp, if startTime is less than or equal to endTime. If endTime is less than startTime,
// then the order of the returned values will be from the oldest modification timestamp to the
// most recent. It is Server dependent whether multiple modifications are kept or only the most
// recent.
// A Server does not have to create a modification record for data when it is first added to the
// historical collection. If it does then it shall set the ExtraData bit and the Client can read the
// modification record using a ReadModified call. If the data is subsequently modified the Server
// shall create a second modification record which is returned along with the original
// modification record whenever a Client uses the ReadModified call if the Server supports
// multiple modification records per timestamp.
// If the requested TimestampsToReturn is not supported for a Node then the operation shall
// return the BadTimestampNotSupported StatusCode.
// todo : provide correct implementation
const result = new HistoryReadResult({
historyData: new HistoryData({ dataValues: [] }),
statusCode: StatusCodes.BadUnexpectedError
});
return callback(null, result);
}
function _historyReadRawAsync(
this: UAVariable,
historyReadRawModifiedDetails: ReadRawModifiedDetails,
maxNumberToExtract: number,
isReversed: boolean,
reverseDataValue: boolean,
callback: Callback<DataValue[]>
) {
assert(callback instanceof Function);
const node = this;
node.varHistorian!.extractDataValues(
historyReadRawModifiedDetails,
maxNumberToExtract,
isReversed,
reverseDataValue,
callback);
}
function _historyReadRaw(
this: UAVariable,
context: SessionContext,
historyReadRawModifiedDetails: ReadRawModifiedDetails,
indexRange: NumericRange | null,
dataEncoding: QualifiedNameLike | null,
continuationPoint: ContinuationPoint | null,
callback: Callback<HistoryReadResult>
): void {
const node = this;
assert(historyReadRawModifiedDetails instanceof ReadRawModifiedDetails);
// 6.4.3.2 Read raw functionality
//
// When this structure is used for reading Raw Values (isReadModified is set to FALSE), it reads
// the values, qualities, and timestamps from the history database for the specified time domain
// for one or more HistoricalDataNodes.
//
// This parameter is intended for use by a Client that wants the actual data saved within the historian.
//
// The actual data may be compressed or may be all raw data collected for the item depending on the
// historian and the storage rules invoked when the item values were saved.
//
// When returnBounds is TRUE, the Bounding Values for the time domain are returned. The optional Bounding
// Values are provided to allow the Client to interpolate values for the start and end times when trending
// the actual data on a display.
//
// The time domain of the request is defined by startTime, endTime, and numValuesPerNode;
// at least two of these shall be specified.
//
// If endTime is less than startTime, or endTime and numValuesPerNode alone are specified
// then the data will be returned in reverse order, with later data coming first as if time
// were flowing backward.
//
// If all three are specified then the call shall return up to numValuesPerNode results going from
// startTime to endTime, in either ascending or descending order depending on the relative values
// of startTime and endTime.
//
// If numValuesPerNode is 0, then all the values in the range are returned.
//
// A default value of DateTime.MinValue (see Part 6) is used to indicate when startTime or
// endTime is not specified.
//
// It is specifically allowed for the startTime and the endTime to be identical.
// This allows the Client to request just one value.
// When the startTime and endTime are identical then time is presumed to be flowing forward.
// It is specifically not allowed for the Server to return a Bad_InvalidArgument StatusCode
// if the requested time domain is outside of the Server's range. Such a case shall be treated
// as an interval in which no data exists.
//
// If a startTime, endTime and numValuesPerNode are all provided and if more than
// numValuesPerNode values exist within that time range for a given Node then only
// numValuesPerNode values per Node are returned along with a continuationPoint.
//
// When a continuationPoint is returned, a Client wanting the next numValuesPerNode values
// should call ReadRaw again with the continuationPoint set.
//
// If the request takes a long time to process then the Server can return partial results with a
// ContinuationPoint. This might be done if the request is going to take more time than the Client
// timeout hint. It may take longer than the Client timeout hint to retrieve any results.
// In this case the Server may return zero results with a ContinuationPoint that allows the
// Server to resume the calculation on the next Client HistoryRead call.
//
// If Bounding Values are requested and a non-zero numValuesPerNode was specified then any
// Bounding Values returned are included in the numValuesPerNode count.
//
// If numValuesPerNode is 1 then only the start bound is returned (the end bound if the reverse
// order is needed).
//
// If numValuesPerNode is 2 then the start bound and the first data point are
// returned (the end bound if reverse order is needed).
//
// When Bounding Values are requested and no bounding value is found then the corresponding
// StatusCode entry will be set to Bad_BoundNotFound, a timestamp equal to the start or end time
// as appropriate, and a value of null.
// How far back or forward to look in history for Bounding Values is Server dependent.
//
// For an interval in which no data exists, if Bounding Values are not requested, then the
// corresponding StatusCode shall be Good_NoData. If Bounding Values are requested and one
// or both exist, then the result code returned is Success and the bounding value(s) are
// returned.
//
// For cases where there are multiple values for a given timestamp, all but the most recent are
// considered to be Modified values and the Server shall return the most recent value. If the
// Server returns a value which hides other values at a timestamp then it shall set the ExtraData
// bit in the StatusCode associated with that value. If the Server contains additional information
// regarding a value then the ExtraData bit shall also be set. It indicates that ModifiedValues are
// available for retrieval, see 6.4.3.3.
//
// If the requested TimestampsToReturn is not supported for a Node, the operation shall return
// the Bad_TimestampNotSupported StatusCode.
if (continuationPoint) {
const cnt = context.continuationPoints
? context.continuationPoints[continuationPoint.toString("hex")] : null;
if (!cnt) {
// invalid continuation point
const result1 = new HistoryReadResult({
historyData: new HistoryData({ dataValues: [] }),
statusCode: StatusCodes.BadContinuationPointInvalid
});
return callback(null, result1);
}
const dataValues = cnt.dataValues.splice(0, historyReadRawModifiedDetails.numValuesPerNode);
if (cnt.dataValues.length > 0) {
//
} else {
context.continuationPoints[continuationPoint.toString("hex")] = null;
continuationPoint = null;
}
const result2 = new HistoryReadResult({
continuationPoint: continuationPoint || undefined,
historyData: new HistoryData({ dataValues }),
statusCode: StatusCodes.Good
});
return callback(null, result2);
}
// todo add special treatment for when startTime > endTime
// ( in this case series must be return in reverse order )
let maxNumberToExtract = 0;
let isReversed = false;
let reverseDataValue = false;
if (isMinDate(historyReadRawModifiedDetails.endTime!)) {
// end time is not specified
maxNumberToExtract = historyReadRawModifiedDetails.numValuesPerNode;
if (isMinDate(historyReadRawModifiedDetails.startTime!)) {
// end start and start time are not specified, this is invalid
const result = new HistoryReadResult({
statusCode: StatusCodes.BadHistoryOperationUnsupported // should be an error
});
return callback(null, result);
}
} else if (isMinDate(historyReadRawModifiedDetails.startTime!)) {
// start time is not specified
// end time is specified
maxNumberToExtract = historyReadRawModifiedDetails.numValuesPerNode;
isReversed = true;
reverseDataValue = false;
if (historyReadRawModifiedDetails.numValuesPerNode === 0) {
// when start time is not specified
// and end time is specified
// numValuesPerNode shall be greater than 0
const result = new HistoryReadResult({
statusCode: StatusCodes.BadHistoryOperationUnsupported // should be an error
});
return callback(null, result);
}
} else {
// start time is specified
// end time is specified
if (historyReadRawModifiedDetails.endTime!.getTime() < historyReadRawModifiedDetails.startTime!.getTime()) {
reverseDataValue = true;
const tmp = historyReadRawModifiedDetails.endTime;
historyReadRawModifiedDetails.endTime = historyReadRawModifiedDetails.startTime;
historyReadRawModifiedDetails.startTime = tmp;
}
}
node._historyReadRawAsync(
historyReadRawModifiedDetails,
maxNumberToExtract,
isReversed,
reverseDataValue,
(err: Error | null, dataValues?: DataValue[]) => {
if (err || !dataValues) {
return callback(err);
}
// now make sure that only the requested number of value is returned
if (historyReadRawModifiedDetails.numValuesPerNode >= 1) {
if (dataValues.length === 0) {
const result1 = new HistoryReadResult({
historyData: new HistoryData({ dataValues: [] }),
statusCode: StatusCodes.GoodNoData
});
return callback(null, result1);
} else {
const remaining = dataValues;
dataValues = remaining.splice(0, historyReadRawModifiedDetails.numValuesPerNode);
if (remaining.length > 0 && !isMinDate(historyReadRawModifiedDetails.endTime)) {
continuationPoint = createContinuationPoint();
context.continuationPoints = context.continuationPoints || {};
context.continuationPoints[continuationPoint.toString("hex")] = {
dataValues: remaining
};
}
}
}
const result = new HistoryReadResult({
continuationPoint: continuationPoint || undefined,
historyData: new HistoryData({ dataValues }),
statusCode: StatusCodes.Good
});
callback(null, result);
});
}
function _historyReadRawModify(
this: UAVariable,
context: SessionContext,
historyReadRawModifiedDetails: ReadRawModifiedDetails,
indexRange: NumericRange | null,
dataEncoding: QualifiedNameLike | null,
continuationPoint: ContinuationPoint | null,
callback: Callback<HistoryReadResult>
) {
const node = this;
assert(historyReadRawModifiedDetails instanceof ReadRawModifiedDetails);
if (!historyReadRawModifiedDetails.isReadModified) {
return node._historyReadRaw(
context,
historyReadRawModifiedDetails,
indexRange,
dataEncoding,
continuationPoint,
callback);
} else {
return node._historyReadModify(
context,
historyReadRawModifiedDetails,
indexRange,
dataEncoding,
continuationPoint,
callback);
}
}
function _historyRead(
this: UAVariable,
context: SessionContext,
historyReadDetails:
ReadRawModifiedDetails | ReadEventDetails | ReadProcessedDetails | ReadAtTimeDetails,
indexRange: NumericRange | null,
dataEncoding: QualifiedNameLike | null,
continuationPoint: ContinuationPoint | null,
callback: Callback<HistoryReadResult>
) {
assert(context instanceof SessionContext);
assert(callback instanceof Function);
const node = this;
if (historyReadDetails instanceof ReadRawModifiedDetails) {
// note: only ReadRawModifiedDetails supported at this time
return node._historyReadRawModify(
context,
historyReadDetails,
indexRange,
dataEncoding,
continuationPoint,
callback);
} else if (historyReadDetails instanceof ReadEventDetails) {
// The ReadEventDetails structure is used to read the Events from the history database for the
// specified time domain for one or more HistoricalEventNodes. The Events are filtered based on
// the filter structure provided. This filter includes the EventFields that are to be returned. For a
// complete description of filter refer to Part 4.
// The startTime and endTime are used to filter on the Time field for Events.
// The time domain of the request is defined by startTime, endTime, and numValuesPerNode; at
// least two of these shall be specified. If endTime is less than startTime, or endTime and
// numValuesPerNode alone are specified then the data will be returned in reverse order with
// later/newer data provided first as if time were flowing backward. If all three are specified then
// the call shall return up to numValuesPerNode results going from startTime to endTime, in
// either ascending or descending order depending on the relative values of startTime and
// endTime. If numValuesPerNode is 0 then all of the values in the range are returned. The
// default value is used to indicate when startTime, endTime or numValuesPerNode are not
// specified.
// It is specifically allowed for the startTime and the endTime to be identical. This allows the
// Client to request the Event at a single instance in time. When the startTime and endTime are
// identical then time is presumed to be flowing forward. If no data exists at the time specified
// then the Server shall return the Good_NoData StatusCode.
// If a startTime, endTime and numValuesPerNode are all provided, and if more than
// numValuesPerNode Events exist within that time range for a given Node, then only
// numValuesPerNode Events per Node are returned along with a ContinuationPoint. When a
// ContinuationPoint is returned, a Client wanting the next numValuesPerNode values should
// call HistoryRead again with the continuationPoint set.
// If the request takes a long time to process then the Server can return partial results with a
// ContinuationPoint. This might be done if the request is going to take more time than the Client
// timeout hint. It may take longer than the Client timeout hint to retrieve any results. In this case
// the Server may return zero results with a ContinuationPoint that allows the Server to resume
// the calculation on the next Client HistoryRead call.
// For an interval in which no data exists, the corresponding StatusCode shall be Good_NoData.
// The filter parameter is used to determine which historical Events and their corresponding
// fields are returned. It is possible that the fields of an EventType are available for real time
// updating, but not available from the historian. In this case a StatusCode value will be returned
// for any Event field that cannot be returned. The value of the StatusCode shall be
// Bad_NoData.
// If the requested TimestampsToReturn is not supported for a Node then the operation shall
// return the Bad_TimestampNotSupported StatusCode. When reading Events this only applies
// to Event fields that are of type DataValue.
// todo provide correct implementation
const result = new HistoryReadResult({
historyData: new HistoryData({ dataValues: [] }),
statusCode: StatusCodes.BadHistoryOperationUnsupported
});
return callback(null, result);
} else if (historyReadDetails instanceof ReadProcessedDetails) {
// OPC Unified Architecture, Part 11 27 Release 1.03
//
// This structure is used to compute Aggregate values, qualities, and timestamps from data in
// the history database for the specified time domain for one or more HistoricalDataNodes. The
// time domain is divided into intervals of duration ProcessingInterval. The specified Aggregate
// Type is calculated for each interval beginning with startTime by using the data within the next
// ProcessingInterval.
// For example, this function can provide hourly statistics such as Maximum, Minimum , and
// Average for each item during the specified time domain when ProcessingInterval is 1 hour.
// The domain of the request is defined by startTime, endTime, and ProcessingInterval. All three
// shall be specified. If endTime is less than startTime then the data shall be returned in reverse
// order with the later data coming first. If startTime and endTime are the same then the Server
// shall return Bad_InvalidArgument as there is no meaningful way to interpret such a case. If
// the ProcessingInterval is specified as 0 then Aggregates shall be calculated using one interval
// starting at startTime and ending at endTime.
// The aggregateType[] parameter allows a Client to request multiple Aggregate calculations per
// requested NodeId. If multiple Aggregates are requested then a corresponding number of
// entries are required in the NodesToRead array.
// For example, to request Min Aggregate for NodeId FIC101, FIC102, and both Min and Max
// Aggregates for NodeId FIC103 would require NodeId FIC103 to appear twice in the
// NodesToRead array request parameter.
// aggregateType[] NodesToRead[]
// Min FIC101
// Min FIC102
// Min FIC103
// Max FIC103
// If the array of Aggregates does not match the array of NodesToRead then the Server shall
// return a StatusCode of Bad_AggregateListMismatch.
// The aggregateConfiguration parameter allows a Client to override the Aggregate configuration
// settings supplied by the AggregateConfiguration Object on a per call basis. See Part 13 for
// more information on Aggregate configurations. If the Server does not support the ability to
// override the Aggregate configuration settings then it shall return a StatusCode of Bad_
// AggregateConfigurationRejected. If the Aggregate is not valid for the Node then the
// StatusCode shall be Bad_AggregateNotSupported.
// The values used in computing the Aggregate for each interval shall include any value that
// falls exactly on the timestamp at the beginning of the interval, but shall not include any value
// that falls directly on the timestamp ending the interval. Thus, each value shall be included
// only once in the calculation. If the time domain is in reverse order then we consider the later
// timestamp to be the one beginning the sub interval, and the earlier timestamp to be the one
// ending it. Note that this means that simply swapping the start and end times will not result in
// getting the same values back in reverse order as the intervals being requested in the two
// cases are not the same.
// If an Aggregate is taking a long time to calculate then the Server can return partial results
// with a continuation point. This might be done if the calculation is going to take more time th an
// the Client timeout hint. In some cases it may take longer than the Client timeout hint to
// calculate even one Aggregate result. Then the Server may return zero results with a
// continuation point that allows the Server to resume the calculation on the next Client read
// call.
// todo provide correct implementation
const result = new HistoryReadResult({
historyData: new HistoryData({ dataValues: [] }),
statusCode: StatusCodes.BadHistoryOperationUnsupported
});
return callback(null, result);
} else if (historyReadDetails instanceof ReadAtTimeDetails) {
// Release 1.03 28 OPC Unified Architecture, Part 11
// The ReadAtTimeDetails structure reads the values and qualities from the history database for
// the specified timestamps for one or more HistoricalDataNodes. This function is intended to
// provide values to correlate with other values with a known timestamp. For example, a Client
// may need to read the values of sensors when lab samples were collected.
// The order of the values and qualities returned shall match the order of the timestamps
// supplied in the request.
// When no value exists for a specified timestamp, a value shall be Interpolated from the
// surrounding values to represent the value at the specified timestamp. The interpolation will
// follow the same rules as the standard Interpolated Aggregate as outlined in Part 13.
// If the useSimpleBounds flag is True and Interpolation is required then simple bounding values
// will be used to calculate the data value. If useSimpleBounds is False and Interpolation is
// required then interpolated bounding values will be used to calculate the data value. See
// Part 13 for the definition of simple bounding values and interpolated bounding values.
// If a value is found for the specified timestamp, then the Server will set the StatusCode
// InfoBits to be Raw. If the value is Interpolated from the surrounding values, then the Server
// will set the StatusCode InfoBits to be Interpolated.
// If the read request is taking a long time to calculate then the Server may return zero results
// with a ContinuationPoint that allows the Server to resume the calculation on the next Client
// HistoryRead call.
// If the requested TimestampsToReturn is not supported for a Node, then the operation shall
// return the Bad_TimestampNotSupported StatusCode.
// todo provide correct implementation
const result = new HistoryReadResult({
historyData: new HistoryData({ dataValues: [] }),
statusCode: StatusCodes.BadHistoryOperationUnsupported
});
return callback(null, result);
} else {
const result = new HistoryReadResult({
historyData: new HistoryData({ dataValues: [] }),
statusCode: StatusCodes.BadHistoryOperationUnsupported
});
return callback(null, result);
}
}
function on_value_change(this: UAVariable, newDataValue: DataValue): void {
this._historyPush.call(this, newDataValue);
}
/**
* @method installHistoricalDataNode
* @param node UAVariable
* @param [options] {Object}
* @param [options.maxOnlineValues = 1000]
*/
export function AddressSpace_installHistoricalDataNode(
this: AddressSpace,
node: UAVariable,
options?: IVariableHistorianOptions
) {
AddressSpacePublic.historizerFactory = AddressSpacePublic.historizerFactory || {
create(node1: UAVariable, options1: IVariableHistorianOptions) {
return new VariableHistorian(node1, options1);
}
};
assert(node.nodeClass === NodeClass.Variable);
options = options || {};
const addressSpace = node.addressSpace;
// install specific history behavior
node._historyRead = _historyRead;
node._historyPush = _historyPush;
node._historyReadRawModify = _historyReadRawModify;
node._historyReadModify = _historyReadModify;
node._historyReadRaw = _historyReadRaw;
node._historyReadRawAsync = _historyReadRawAsync;
node.varHistorian = (options as any).historian || AddressSpacePublic.historizerFactory.create(node, options);
const historicalDataConfigurationType = addressSpace.findObjectType("HistoricalDataConfigurationType");
if (!historicalDataConfigurationType) {
throw new Error("cannot find HistoricalDataConfigurationType");
}
node.historizing = true;
// tslint:disable:no-bitwise
node.accessLevel = node.accessLevel | AccessLevelFlag.CurrentRead | AccessLevelFlag.HistoryRead;
node.userAccessLevel = node.userAccessLevel | AccessLevelFlag.CurrentRead | AccessLevelFlag.HistoryRead;
const optionals = [
"Stepped",
"Definition",
"MaxTimeInterval",
"MinTimeInterval",
"StartOfArchive",
"StartOfOnlineArchive"
];
// Note from spec : If a HistoricalDataNode has configuration defined then one
// instance shall have a BrowseName of ‘HA Configuration’
const historicalDataConfiguration = historicalDataConfigurationType.instantiate({
browseName: { name: "HA Configuration", namespaceIndex: 0 },
optionals
}) as HistoricalDataConfiguration;
// All Historical Configuration Objects shall be referenced using the HasHistoricalConfiguration ReferenceType.
node.addReference({
isForward: true,
nodeId: historicalDataConfiguration.nodeId,
referenceType: "HasHistoricalConfiguration"
});
// The Stepped Variable specifies whether the historical data was collected in such a manner
// that it should be displayed as SlopedInterpolation (sloped line between points) or as
// SteppedInterpolation (vertically-connected horizontal lines between points) when raw data is
// examined. This Property also effects how some Aggregates are calculated. A value of True
// indicates the stepped interpolation mode. A value of False indicates SlopedInterpolation
// mode. The default value is False.
historicalDataConfiguration.stepped.setValueFromSource({ dataType: "Boolean", value: false });
// The MaxTimeInterval Variable specifies the maximum interval between data points in the
// history repository regardless of their value change (see Part 3 for definition of Duration).
historicalDataConfiguration.maxTimeInterval.setValueFromSource({ dataType: "Duration", value: 10 * 1000 });
// The MinTimeInterval Variable specifies the minimum interval between data points in the
// history repository regardless of their value change
historicalDataConfiguration.minTimeInterval.setValueFromSource({ dataType: "Duration", value: 0.1 * 1000 });
// The StartOfArchive Variable specifies the date before which there is no data in the archive
// either online or offline.
// The StartOfOnlineArchive Variable specifies the date of the earliest data in the online archive.
const startOfOnlineArchive = new Date();
historicalDataConfiguration.startOfOnlineArchive.setValueFromSource({
dataType: DataType.DateTime,
value: startOfOnlineArchive
});
// TreatUncertainAsBad
// The TreatUncertainAsBad Variable indicates how the Server treats data returned with a
// StatusCode severity Uncertain with respect to Aggregate calculations. A value of True indicates
// the Server considers the severity equivalent to Bad, a value of False indicates the Server
// considers the severity equivalent to Good, unless the Aggregate definition says otherwise. The
// default value is True. Note that the value is still treated as Uncertain when the StatusCode for
// the result is calculated.
historicalDataConfiguration.aggregateConfiguration.treatUncertainAsBad.setValueFromSource({
dataType: "Boolean",
value: true
});
// The PercentDataBad Variable indicates the minimum percentage of Bad data in a given interval required for the
// StatusCode for the given interval for processed data request to be set to Bad.
// (Uncertain is treated as defined above.) Refer to 5.4.3 for details on using this Variable when assigning
// StatusCodes. For details on which Aggregates use the PercentDataBad Variable, see
// the definition of each Aggregate. The default value is 100.
historicalDataConfiguration.aggregateConfiguration.percentDataBad.setValueFromSource({
dataType: "Byte",
value: 100
});
// The PercentDataGood Variable indicates the minimum percentage of Good data in a given
// interval required for the StatusCode for the given interval for the processed data requests to be
// set to Good. Refer to 5.4.3 for details on using this Variable when assigning StatusCodes. For
// details on which Aggregates use the PercentDataGood Variable, see the definition of each
// Aggregate. The default value is 100.
historicalDataConfiguration.aggregateConfiguration.percentDataGood.setValueFromSource({
dataType: "Byte",
value: 100
});
//
// The PercentDataGood and PercentDataBad shall follow the following relationship
// PercentDataGood ≥ (100 – PercentDataBad). If they are equal the result of the
// PercentDataGood calculation is used. If the values entered for PercentDataGood and
//
// PercentDataBad do not result in a valid calculation (e.g. Bad = 80; Good = 0) the result will
// have a StatusCode of Bad_AggregateInvalidInputs The StatusCode
//
// Bad_AggregateInvalidInputs will be returned if the value of PercentDataGood or
// PercentDataBad exceed 100.
node.$historicalDataConfiguration = historicalDataConfiguration;
const dataValue = node.readValue();
if (
dataValue.statusCode !== StatusCodes.BadWaitingForInitialData
&& dataValue.statusCode !== StatusCodes.UncertainInitialValue) {
on_value_change.call(node, dataValue);
}
node.on("value_changed", on_value_change);
// update the index of historizing nodes in the addressSpace
node.addressSpace.historizingNodes = node.addressSpace.historizingNodes || {};
node.addressSpace.historizingNodes[node.nodeId.toString()] = node;
}