test/helpers/build_server_with_temperature_device.js
/**
 * create and start a fake OPCUA Server that exposes a temperature set point variable.
 *
 *    the SetPoint temperature can be set on the server side by accessing the
 *    'set_point_temperature'  of the return server object.
 *
 *    the SetPoint temperature can be accessed as a Read/Write variable by a opcua client
 *    as "Root/MyDevices/SetPointTemperature". The node id of this variable is stored into
 *    the 'temperatureVariableId' of the server object.
 *
 * @param options {options}
 * @param [options.add_simulation = false] {Boolean} add the simulation nodes in the server
 * @param done {callback}
 * @return {OPCUAServer}
 */
create and start a fake OPCUA Server that exposes a temperature set point variable.
the SetPoint temperature can be set on the server side by accessing the
'set_point_temperature'  of the return server object.

the SetPoint temperature can be accessed as a Read/Write variable by a opcua client
as "Root/MyDevices/SetPointTemperature". The node id of this variable is stored into
the 'temperatureVariableId' of the server object.

@param options {options}
@param [options.add_simulation = false] {Boolean} add the simulation nodes in the server
@param done {callback}
@return {OPCUAServer}
create and start a fake OPCUA Server that exposes a temperature set point variable . the SetPoint temperature can be set on the server side by accessing the ' set_point_temperature ' of the return server object . the SetPoint temperature can be accessed as a Read / Write variable by a opcua client as " Root / MyDevices / SetPointTemperature " . The node id of this variable is stored into the ' temperatureVariableId ' of the server object . @param options { options } @param [ options . add_simulation = false ] { Boolean } add the simulation nodes in the server @param done { callback } @return { OPCUAServer }
function build_server_with_temperature_device(options, done) {
    assert(_.isFunction(done, "expecting a callback function"));
    assert(typeof opcua.part8_nodeset_filename === "string");
    options.nodeset_filename = options.nodeset_filename ||
        [
            opcua.mini_nodeset_filename,
            opcua.part8_nodeset_filename
        ];
    options.userManager = userManager;
    var server = new OPCUAServer(options);
    server.on("session_closed", function (session, reason) {
    });
    server.on("post_initialize", function () {
        var addressSpace = server.engine.addressSpace;
        var addressSpace = server.engine.addressSpace;
        var myDevices = addressSpace.addFolder("ObjectsFolder", {browseName: "MyDevices"});
        assert(myDevices.browseName.toString() === "MyDevices");
        var variable0 = addressSpace.addVariable({
            componentOf: myDevices,
            browseName: "FanSpeed",
            nodeId: "ns=2;s=FanSpeed",
            dataType: "Double",
            value: new Variant({dataType: DataType.Double, value: 1000.0})
        });
        var setPointTemperatureId = "ns=4;s=SetPointTemperature";
        server.temperatureVariableId = addressSpace.addVariable({
            componentOf: myDevices,
            browseName: "SetPointTemperature",
            nodeId: setPointTemperatureId,
            dataType: "Double",
            value: {
                get: function () {
                    return new Variant({dataType: DataType.Double, value: server.set_point_temperature});
                },
                set: function (variant) {
                    server.set_point_temperature = parseFloat(variant.value);
                    return StatusCodes.Good;
                }
            }
        });
        var pumpSpeedId = "ns=4;b=0102030405060708090a0b0c0d0e0f10";
        server.pumpSpeed = addressSpace.addVariable({
            componentOf: myDevices,
            browseName: "PumpSpeed",
            nodeId: pumpSpeedId,
            dataType: "Double",
            value: {
                get: function () {
                    var pump_speed = 200 + Math.random();
                    return new Variant({dataType: DataType.Double, value: pump_speed});
                },
                set: function (variant) {
                    return StatusCodes.BadNotWritable;
                }
            }
        });
        assert(server.pumpSpeed.nodeId.toString() === pumpSpeedId);
        var endpointUrl = server.endpoints[0].endpointDescriptions()[0].endpointUrl;
        debugLog("endpointUrl", endpointUrl);
        opcua.is_valid_endpointUrl(endpointUrl).should.equal(true);
        if (options.add_simulation) {
            build_address_space_for_conformance_testing(server.engine);
        }
        addTestUAAnalogItem(myDevices);
        var asyncWriteNodeId = "ns=4;s=AsynchronousVariable";
        var asyncValue = 46;
        server.asyncWriteNode = addressSpace.addVariable({
            componentOf: myDevices,
            browseName: "AsynchronousVariable",
            nodeId: asyncWriteNodeId,
            dataType: "Double",
            value: {
                refreshFunc: function (callback) {
                    var dataValue = new DataValue({
                        value: {
                            dataType: DataType.Double,
                            value: asyncValue
                        },
                        sourceTimestamp: new Date()
                    });
                    setTimeout(function () {
                        callback(null, dataValue);
                    }, 100);
                },
                set: function (variant) {
                    setTimeout(function () {
                        asyncValue = variant.value;
                    }, 1000);
                    return StatusCodes.GoodCompletesAsynchronously;
                }
            }
        });
    });
    server.set_point_temperature = 20.0;
    function start(done) {
        server.start(function (err) {
            if (err) {
                return done(err);
            }
            assert(server.engine.status === "initialized");
            done();
        });
    }
    start(done);
    return server;
}
function build_server_with_temperature_device ( options , done ) { assert ( _ . isFunction ( done , "expecting a callback function" ) ) ; assert ( typeof opcua . part8_nodeset_filename === "string" ) ; options . nodeset_filename = options . nodeset_filename || [ opcua . mini_nodeset_filename , opcua . part8_nodeset_filename ] ; options . userManager = userManager ; var server = new OPCUAServer ( options ) ; server . on ( "session_closed" , function ( session , reason ) { } ) ; server . on ( "post_initialize" , function ( ) { var addressSpace = server . engine . addressSpace ; var addressSpace = server . engine . addressSpace ; var myDevices = addressSpace . addFolder ( "ObjectsFolder" , { browseName : "MyDevices" } ) ; assert ( myDevices . browseName . toString ( ) === "MyDevices" ) ; var variable0 = addressSpace . addVariable ( { componentOf : myDevices , browseName : "FanSpeed" , nodeId : "ns=2;s=FanSpeed" , dataType : "Double" , value : new Variant ( { dataType : DataType . Double , value : 1000.0 } ) } ) ; var setPointTemperatureId = "ns=4;s=SetPointTemperature" ; server . temperatureVariableId = addressSpace . addVariable ( { componentOf : myDevices , browseName : "SetPointTemperature" , nodeId : setPointTemperatureId , dataType : "Double" , value : { get : function ( ) { return new Variant ( { dataType : DataType . Double , value : server . set_point_temperature } ) ; } , set : function ( variant ) { server . set_point_temperature = parseFloat ( variant . value ) ; return StatusCodes . Good ; } } } ) ; var pumpSpeedId = "ns=4;b=0102030405060708090a0b0c0d0e0f10" ; server . pumpSpeed = addressSpace . addVariable ( { componentOf : myDevices , browseName : "PumpSpeed" , nodeId : pumpSpeedId , dataType : "Double" , value : { get : function ( ) { var pump_speed = 200 + Math . random ( ) ; return new Variant ( { dataType : DataType . Double , value : pump_speed } ) ; } , set : function ( variant ) { return StatusCodes . BadNotWritable ; } } } ) ; assert ( server . pumpSpeed . nodeId . toString ( ) === pumpSpeedId ) ; var endpointUrl = server . endpoints [ 0 ] . endpointDescriptions ( ) [ 0 ] . endpointUrl ; debugLog ( "endpointUrl" , endpointUrl ) ; opcua . is_valid_endpointUrl ( endpointUrl ) . should . equal ( true ) ; if ( options . add_simulation ) { build_address_space_for_conformance_testing ( server . engine ) ; } addTestUAAnalogItem ( myDevices ) ; var asyncWriteNodeId = "ns=4;s=AsynchronousVariable" ; var asyncValue = 46 ; server . asyncWriteNode = addressSpace . addVariable ( { componentOf : myDevices , browseName : "AsynchronousVariable" , nodeId : asyncWriteNodeId , dataType : "Double" , value : { refreshFunc : function ( callback ) { var dataValue = new DataValue ( { value : { dataType : DataType . Double , value : asyncValue } , sourceTimestamp : new Date ( ) } ) ; setTimeout ( function ( ) { callback ( null , dataValue ) ; } , 100 ) ; } , set : function ( variant ) { setTimeout ( function ( ) { asyncValue = variant . value ; } , 1000 ) ; return StatusCodes . GoodCompletesAsynchronously ; } } } ) ; } ) ; server . set_point_temperature = 20.0 ; function start ( done ) { server . start ( function ( err ) { if ( err ) { return done ( err ) ; } assert ( server . engine . status === "initialized" ) ; done ( ) ; } ) ; } start ( done ) ; return server ; }
create and start a fake OPCUA Server that exposes a temperature set point variable.
create and start a fake OPCUA Server that exposes a temperature set point variable .
//xx console.log("xxx building server with temperature device"); // use mini_nodeset_filename for speed up if not otherwise specified // we will connect to first server end point //xx console.log(" server_with_temperature_device has closed a session :",reason); //xx console.log("              session name: ".cyan,session.sessionName.toString()); // install a Read/Write variable representing a temperature set point of a temperature controller. // to do : test if variant can be coerce to Float or Double // install a Read-Only variable defined with a fancy Opaque nodeid // add a Analog Data Item // add a variable that can be written asynchronously // asynchronous read // simulate a asynchronous behaviour