import * as chalk from "chalk" ; import { AttributeIds , BrowseResult , ClientMonitoredItemBase , ClientMonitoredItemGroup , ConnectionStrategyOptions , DataValue , MessageSecurityMode , OPCUAClient , OPCUAClientOptions , resolveNodeId , SecurityPolicy , TimestampsToReturn , UserTokenType } from "node-opcua-client" ; const endpointUri = "opc.tcp://localhost:48010" ; ( async ( ) => { try { const connectionStrategy = { initialDelay : 1000 , maxDelay : 20000 , maxRetry : 2 } ; const options = { applicationName : "ClientBrowseNextDemo" , connectionStrategy , endpointMustExist : false , keepSessionAlive : false , requestedSessionTimeout : 60000 , securityMode : MessageSecurityMode . None , securityPolicy : SecurityPolicy . None } ; const client = OPCUAClient . create ( options ) ; client . on ( "backoff" , ( retry , delay ) => { console . log ( "Backoff " , retry , " next attempt in " , delay , "ms" ) ; } ) ; client . on ( "connection_lost" , ( ) => { console . log ( "Connection lost" ) ; } ) ; client . on ( "connection_reestablished" , ( ) => { console . log ( "Connection re-established" ) ; } ) ; client . on ( "connection_failed" , ( ) => { console . log ( "Connection failed" ) ; } ) ; client . on ( "start_reconnection" , ( ) => { console . log ( "Starting reconnection" ) ; } ) ; client . on ( "after_reconnection" , ( err ) => { console . log ( "After Reconnection event =>" , err ) ; } ) ; await client . connect ( endpointUri ) ; const session = await client . createSession ( ) ; const subscription = await session . createSubscription2 ( { maxNotificationsPerPublish : 1000 , publishingEnabled : true , requestedLifetimeCount : 100 , requestedMaxKeepAliveCount : 10 , requestedPublishingInterval : 1000 } ) ; subscription . on ( "raw_notification" , ( n ) => { console . log ( n . toString ( ) ) ; } ) ; const itemsToMonitor = [ { attributeId : AttributeIds . Value , nodeId : resolveNodeId ( "ns=3;s=AirConditioner_1.Temperature" ) } , { attributeId : AttributeIds . Value , nodeId : resolveNodeId ( "ns=3;s=AirConditioner_2.Temperature" ) } , { attributeId : AttributeIds . Value , nodeId : resolveNodeId ( "ns=3;s=AirConditioner_3.Temperature" ) } , { attributeId : AttributeIds . Value , nodeId : resolveNodeId ( "ns=3;s=AirConditioner_4.Temperature" ) } ] ; const optionsGroup = { discardOldest : true , queueSize : 1 , samplingInterval : 10 } ; const monitoredItemGroup = ClientMonitoredItemGroup . create ( subscription , itemsToMonitor , optionsGroup , TimestampsToReturn . Both ) ; monitoredItemGroup . on ( "initialized" , async ( ) => { console . log ( " Initialized !" ) ; } ) ; monitoredItemGroup . on ( "changed" , ( monitoredItem , dataValue , index ) => { console . log ( "Changed on " , index , dataValue . value . toString ( ) ) ; } ) ; await new Promise ( ( resolve ) => setTimeout ( resolve , 1000000 ) ) ; await monitoredItemGroup . terminate ( ) ; await session . close ( ) ; await client . disconnect ( ) ; console . log ( chalk . green ( "Done !" ) ) ; } catch ( err ) { console . log ( chalk . red ( "Error" ) , err ) ; } } ) ( ) ;