node.jsnode-opcua

Remove variable dynamically from node-opcua server


I am trying to add and remove variables dynamically in a node-opcua server.

Using sample_server.js, I was able to simulate the procedure of dynamically adding a variable using the following code in the end of post_initialize():

setTimeout(() => {
    var variable = 
        namespace.addVariable({
            componentOf: device,

            nodeId: "s=free_memory2", // a string nodeID
            browseName: "FreeMemory2",
            dataType: "Double",    
            value: {
                get: function () {return new opcua.Variant({dataType: opcua.DataType.Double, value: available_memory() });}
            }
        });

    console.log("New variable added");
}, 10000);

My question is, how can I remove a variable?

In namespace documentation, I don't see any method to remove variables.

Updating the previous code, I want something like this:

setTimeout(() => {
    var variable = 
        namespace.addVariable({
            componentOf: device,

            nodeId: "s=free_memory2", // a string nodeID
            browseName: "FreeMemory2",
            dataType: "Double",    
            value: {
                get: function () {return new opcua.Variant({dataType: opcua.DataType.Double, value: available_memory() });}
            }
        });


        setTimeout(() => {
            // How to remove a variable?
            variable.removeReference(); // not working, throws "TypeError: Cannot read property 'hasOwnProperty' of undefined"

            console.log("variable.removeReference()");

        },10000);

    console.log("New variable added");
}, 10000);

Thanks in advance.


Solution

  • Found the solution:

    namespace.deleteNode(variable);