5

我正在学习节点 opc-ua,并遵循了 GitHub 页面中为 sample_server.js 和 simple_client.js 提供的示例。

在 sample_server 中,我在构造服务器的地址空间时添加了一个变量,例如:

//this code is in the server    
addressSpace.addVariable({
        componentOf: device,
        nodeId: "ns=1;s=variable_1",
        browseName: "MyVariable1",
        dataType: "Double",
        value: {
            get: function () {
                return new opcua.Variant({ dataType: opcua.DataType.Double, value: variable1 });
            }
        }
    });

这是整个服务器代码供参考:

var opcua = require("node-opcua");

var server = new opcua.OPCUAServer({
    port: 4334, // the port of the listening socket of the server
    resourcePath: "UA/MyLittleServer", // this path will be added to the endpoint resource name
    buildInfo: {
        productName: "MySampleServer1",
        buildNumber: "7658",
        buildDate: new Date(2014, 5, 2)
    }
});

server.initialize(post_initialize);

function post_initialize() {
    console.log("initialized");
    construct_my_address_space(server, function () {
        server.start(function () {
            console.log("Server is now listening ... ( press CTRL+C to stop)");
            console.log("port ", server.endpoints[0].port);
            var endpointUrl = server.endpoints[0].endpointDescriptions()[0].endpointUrl;
            console.log(" the primary server endpoint url is ", endpointUrl);
        });
    });

}

function construct_my_address_space(server, callback) {

    var addressSpace = server.engine.addressSpace;

    // declare a new object
    var device = addressSpace.addObject({
        organizedBy: addressSpace.rootFolder.objects,
        browseName: "MyDevice"
    });

    // add a variable named MyVariable1 to the newly created folder "MyDevice"
    var variable1 = 1;

    // emulate variable1 changing every 500 ms
    setInterval(function () { variable1 += 1; }, 500);

    addressSpace.addVariable({
        componentOf: device,
        nodeId: "ns=1;s=variable_1",
        browseName: "MyVariable1",
        dataType: "Double",
        value: {
            get: function () {
                return new opcua.Variant({ dataType: opcua.DataType.Double, value: variable1 });
            }
        }
    });

    callback();
}

现在,在客户端中,我想通过名称或完整的 nodeId 来发现这个变量。我正在使用提供的示例来浏览与服务器的会话,但在返回时我只看到 FolderType、Objects、Types 和 Views,并且无法在任何地方找到此变量。

这是客户端代码:

var opcua = require("node-opcua");
var async = require("async");

var client = new opcua.OPCUAClient();
var endpointUrl = "opc.tcp://" + require("os").hostname() + ":4334/UA/MyLittleServer";

var the_session, the_subscription;

async.series([

    // step 1 : connect to
    function (callback) {
        client.connect(endpointUrl, function (err) {
            if (err) {
                console.log(" cannot connect to endpoint :", endpointUrl);
            } else {
                console.log("connected !");
            }
            callback(err);
        });
    },

    // step 2 : createSession
    function (callback) {
        client.createSession(function (err, session) {
            if (!err) {
                the_session = session;
            }
            callback(err);
        });
    },

    // step 3 : browse
    function (callback) {
        the_session.browse("RootFolder", function (err, browse_result) {
            if (!err) {
                console.log('Result of browsing: ', browse_result);
                browse_result[0].references.forEach(function (reference) {
                    console.log('browsing: ', reference.browseName);
                    console.log(reference);
                    console.log("**************************************");
                });
            }
            callback(err);
        });
    },

    /* step 4 : read a variable with readVariableValue
    function (callback) {
        the_session.readVariableValue("ns=1;s=variable_1", function (err, dataValue) {
            if (!err) {
                console.log(" var 1 = ", dataValue.toString());
            }
            callback(err);
        });
    },

    // step 4' : read a variable with read
    function (callback) {
        var max_age = 0;
        var nodes_to_read = [
            { nodeId: "ns=1;s=variable_1", attributeId: opcua.AttributeIds.Value }
        ];
        the_session.read(nodes_to_read, max_age, function (err, nodes_to_read, dataValues) {
            if (!err) {
                console.log(" variable 1 = ", dataValues[0]);
            }
            callback(err);
        });
    },
    */
    // step 5: install a subscription and install a monitored item for 10 seconds
    function (callback) {
        the_subscription = new opcua.ClientSubscription(the_session, {
            requestedPublishingInterval: 1000,
            requestedLifetimeCount: 10,
            requestedMaxKeepAliveCount: 2,
            maxNotificationsPerPublish: 10,
            publishingEnabled: true,
            priority: 10
        });

        the_subscription.on("started", function () {
            console.log("subscription started for 2 seconds - subscriptionId=", the_subscription.subscriptionId);
        }).on("keepalive", function () {
            console.log("keepalive");
        }).on("terminated", function () {
            callback();
        });

        setTimeout(function () {
            the_subscription.terminate();
        }, 10000);

        // install monitored item
        var monitoredItem = the_subscription.monitor({
            nodeId: opcua.resolveNodeId("ns=1;s=variable_1"),
            attributeId: opcua.AttributeIds.Value
        },
            {
                samplingInterval: 100,
                discardOldest: true,
                queueSize: 10
            },
            opcua.read_service.TimestampsToReturn.Both
        );
        console.log("-------------------------------------");

        monitoredItem.on("changed", function (dataValue) {
            console.log("variable_1 = ", dataValue.value.value);
        });
    },

    // close session
    function (callback) {
        the_session.close(function (err) {
            if (err) {
                console.log("session closed failed ?");
            } else{
                console.log("session closed!");
            }
            callback();
        });
    }

],
    function (err) {
        if (err) {
            console.log(" failure ", err);
        } else {
            console.log("done!");
        }
        client.disconnect(function () { });
    });

提前致谢

4

1 回答 1

10

您需要从 rootFolder 向下钻取并调查各种对象,直到找到要监视的变量,以及一系列 browseNode。最好的选择是使用商业供应商提供的免费 OPCUA 客户端,例如,UAExpertProsys OPC UA 客户端等等。

您还可以使用 opcua-commander:opcua-指挥官这个小实用程序将允许您探索服务器的地址空间并找到您正在寻找的节点。(源代码可在github上找到)

于 2016-05-19T21:32:28.647 回答