nodejs-如何使用node-opcua访问远程opc服务器?

nodejs-How use node-opcua access a remote opc server?

我在github找到了一个比较好的项目node-opcua,使用这个项目的简单例子可以创建一个opc server和client,并且在本地可以访问,我需要访问一个远程的opc server现在,提供的几个参数是:IP网络节点(比如10.195.156.150)、opc服务(比如ArchestrA.FSGateway)、opc组(比如ArchestrA)、opc TagName(比如HXi1_P201_00.ia) ,我可以使用 node-opcua 访问它吗? 谢谢!

sample_server代码:

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

// Let's create an instance of OPCUAServer
var server = new opcua.OPCUAServer({port: 4334});
var siteName = 'site01';

var var1 = {nodeId: 'ns=4;s=hook_weight', browseName: 'hook_weight', dataType: 'Double'};
var var2 = {nodeId: 'ns=4;s=rpm', browseName: 'rpm', dataType: 'Double'};
var var3 = {nodeId: 'ns=1;s=free_memory', browseName: 'free_memory', dataType: 'Double'};

function post_initialize(){
    console.log("server initialized");
    function construct_my_address_space(server) {
      server.engine.createFolder("RootFolder",{ browseName: siteName});
        // emulate variable1 changing every 500 ms
        var variable1 = 1;
        setInterval(function(){ variable1 += 1; }, 1000);

        var1.value = {
          get: function(){
            return new opcua.Variant({dataType: opcua.DataType.Double, value: variable1 });
          }
        };
        server.var1 = server.engine.addVariableInFolder(siteName, var1);

        var2.value = {
          get: function(){
            return new opcua.Variant({dataType: opcua.DataType.Double, value: 10});
          }
        };
        server.var2 = server.engine.addVariableInFolder(siteName, var2);

        /**
        /**
         * returns the percentage of free memory on the running machine
         * @return {double}
         */
        function available_memory() {
            // var value = process.memoryUsage().heapUsed / 1000000;
            var percentageMemUsed = os.freemem() / os.totalmem() * 100.0;
            return percentageMemUsed;
        }
        var3.value = {
          get: function(){
            return new opcua.Variant({dataType: opcua.DataType.Double, value: available_memory()});
          }
        };
        server.var3 = server.engine.addVariableInFolder(siteName, var3);

    }

    construct_my_address_space(server);

    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].endpointDescription().endpointUrl;
        console.log(" the primary server endpoint url is ", endpointUrl );
    });
}
server.initialize(post_initialize);

sample_client代码:

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

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

var session, 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) { session = _session; }
          callback(err);
      });
    },

    // step 5: install a subscription and install a monitored item for 10 seconds
    function(callback) {
      subscription=new opcua.ClientSubscription(session,{
          requestedPublishingInterval: 1000,
          requestedLifetimeCount: 10,
          requestedMaxKeepAliveCount: 2,
          maxNotificationsPerPublish: 10,
          publishingEnabled: true,
          priority: 10
      });

      subscription.on("started",function(){
        console.log("subscriptionId=", subscription.subscriptionId);
      }).on("keepalive",function(){ console.log(); })
      .on("terminated",function(){ callback(); });

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

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

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

    // close session
    function (callback) {
        // _"closing session"

        session.close(function (err) {
            if (err) {
                console.log("session closed failed ?");
            }
            callback();
        });
    }
],
function(err) {
  if (err) { console.log(" failure ",err); }
  else { console.log('done!'); }
  client.disconnect(function(){});
});

恐怕 Archestra 没有 OPC UA 服务器,所以要从 node-opcua 访问它,您需要使用 OPC UA 网关,例如 https://www.prosysopc.com/products/opc-ua-gateway/,它可以转换 OPC DA到 OPC UA。