/********************************************************************************
 * Copyright (c) 2021 Contributors to the Eclipse Foundation
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
 * Document License (2015-05-13) which is available at
 * https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
 *
 * SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
 ********************************************************************************/ import { OPCUAServer , Variant , DataType , StatusCodes , MethodFunctorCallback , SessionContext } from "node-opcua" ; export class OpcuaServer { server : OPCUAServer ; private testingVariable : number ; constructor ( ) { this . server = new OPCUAServer ( { port : 5050 , resourcePath : "/opcua/server" , allowAnonymous : true , } ) ; this . testingVariable = 1 ; } forceValueChange ( ) { this . testingVariable ++ ; } async start ( ) { try { await this . server . initialize ( ) ; this . constructMyAddressSpace ( this . server ) ; await this . server . start ( ) ; const endpointUrl = this . server . getEndpointUrl ( ) ; console . log ( "OPCUA server started at" , endpointUrl ) ; } catch ( err ) { throw new Error ( err ) ; } } async stop ( ) { await this . server . shutdown ( 1000 ) ; } constructMyAddressSpace ( server ) { const addressSpace = server . engine . addressSpace ; const namespace = addressSpace . getOwnNamespace ( ) ; const device = namespace . addObject ( { nodeId : "ns=1;s=device" , organizedBy : addressSpace . rootFolder . objects , browseName : "WotDevice" , } ) ; namespace . addVariable ( { componentOf : device , nodeId : "ns=1;s=9998FFAA" , browseName : "Increment" , dataType : "Double" , value : { get : ( ) => { return new Variant ( { dataType : DataType . Double , value : this . testingVariable } ) ; } , } , } ) ; let str = "" ; namespace . addVariable ( { componentOf : device , nodeId : "ns=1;s=Case_Lamp_Variable" , browseName : "TestString" , dataType : "String" , value : { get : function ( ) { return new Variant ( { dataType : DataType . String , value : str } ) ; } , set : function ( variant ) { str = variant . value ; return StatusCodes . Good ; } , } , } ) ; namespace . addVariable ( { componentOf : device , nodeId : 'ns=1;s="Case_Lamp_Variable"' , browseName : "TestString" , dataType : "String" , value : { get : function ( ) { return new Variant ( { dataType : DataType . String , value : str } ) ; } , set : function ( variant ) { str = variant . value ; return StatusCodes . Good ; } , } , } ) ; let testVariable = Math . random ( ) ; const v = namespace . addVariable ( { browseName : "RandomValue" , nodeId : "ns=1;s=9998FF00" , dataType : "Double" , value : { get : function ( ) { return new Variant ( { dataType : DataType . Double , value : testVariable } ) ; } , set : function ( variant ) { testVariable = variant . value ; return StatusCodes . Good ; } , } , } ) ; v . setValueFromSource ( { dataType : DataType . Double , value : testVariable } ) ; const method = namespace . addMethod ( device , { nodeId : "ns=1;s=method" , browseName : "DivideFunction" , inputArguments : [ { name : "a" , description : { text : "specifies the first number" } , dataType : DataType . Double , } , { name : "b" , description : { text : "specifies the second number" } , dataType : DataType . Double , } , ] , outputArguments : [ { name : "division" , description : { text : "the generated barks" } , dataType : DataType . Double , valueRank : 1 , } , ] , } ) ; method . bindMethod ( ( inputArguments , context , callback ) => { const a = inputArguments [ 0 ] . value ; const b = inputArguments [ 1 ] . value ; const res = a / b ; const callMethodResult = { statusCode : StatusCodes . Good , outputArguments : [ { dataType : DataType . Double , value : res , } , ] , } ; callback ( null , callMethodResult ) ; } ) ; } }