import * as async from "async" ; import { AggregateFunction } from "node-opcua-constants" ; import { ISessionContext , ContinuationPoint , UAVariable , ContinuationData } from "node-opcua-address-space" ; import { NumericRange } from "node-opcua-numeric-range" ; import { QualifiedNameLike } from "node-opcua-data-model" ; import { CallbackT , StatusCodes } from "node-opcua-status-code" ; import { DataValue } from "node-opcua-data-value" ; import { ObjectIds } from "node-opcua-constants" ; import { NodeId } from "node-opcua-nodeid" ; import { HistoryData , HistoryReadResult , ReadAtTimeDetails , ReadEventDetails , ReadProcessedDetails , ReadRawModifiedDetails } from "node-opcua-service-history" ; import { getMinData , getMaxData } from "./minmax" ; import { getInterpolatedData } from "./interpolate" ; import { getAverageData } from "./average" ; function _buildResult ( err , dataValues , callback2 ) { if ( err ) { return callback2 ( null , new HistoryReadResult ( { statusCode : StatusCodes . BadInternalError } ) ) ; } const result = new HistoryReadResult ( { historyData : new HistoryData ( { dataValues } ) , statusCode : StatusCodes . Good } ) ; return callback2 ( null , result ) ; } function applyAggregate ( variable , processingInterval , startTime , endTime , aggregateType , callback2 ) { if ( ! startTime || ! endTime ) { return _buildResult ( new Error ( "Invalid date time" ) , undefined , callback2 ) ; } const buildResult = ( err , dataValues ) => { _buildResult ( err , dataValues , callback2 ) ; } ; switch ( aggregateType . value ) { case AggregateFunction . Minimum : getMinData ( variable , processingInterval , startTime , endTime , buildResult ) ; break ; case AggregateFunction . Maximum : getMaxData ( variable , processingInterval , startTime , endTime , buildResult ) ; break ; case AggregateFunction . Interpolative : getInterpolatedData ( variable , processingInterval , startTime , endTime , buildResult ) ; break ; case AggregateFunction . Average : getAverageData ( variable , processingInterval , startTime , endTime , buildResult ) ; break ; case AggregateFunction . Count : default : return callback2 ( null , new HistoryReadResult ( { statusCode : StatusCodes . BadAggregateNotSupported } ) ) ; } } export function readProcessedDetails ( variable , context , historyReadDetails , indexRange , dataEncoding , continuationData , callback ) { const startTime = historyReadDetails . startTime ; const endTime = historyReadDetails . endTime ; if ( ! startTime || ! endTime ) { return callback ( null , new HistoryReadResult ( { statusCode : StatusCodes . BadInvalidArgument } ) ) ; } if ( startTime . getTime ( ) === endTime . getTime ( ) ) { return callback ( null , new HistoryReadResult ( { statusCode : StatusCodes . BadInvalidArgument } ) ) ; } const aggregateTypes = historyReadDetails . aggregateType || [ ] ; const processingInterval = historyReadDetails . processingInterval || endTime . getTime ( ) - startTime . getTime ( ) ; if ( historyReadDetails . aggregateType ?. length !== 1 ) { return callback ( null , new HistoryReadResult ( { statusCode : StatusCodes . BadInternalError } ) ) ; } return applyAggregate ( variable , processingInterval , startTime , endTime , aggregateTypes [ 0 ] , callback ) ; }