5

我使用node-opcua module并且我想opc ua nodes通过订阅监视许多我看到的结果如下:html UI 中的用户选择要监视的节点,然后单击将这些nodeIds作为参数发送的监视按钮,然后为每个nodeid设置订阅和 .on("changed" ) 适用于这些项目中的每一个,就像并行一样。现在代码看起来像:

 var monitoredItem  = the_subscription.monitor({
       nodeId: opcua.resolveNodeId("ns=6;s=S71500ET200MP station_1.Master.111"),
       attributeId: 13
   },
   {
       samplingInterval: 100,
       discardOldest: true,
       queueSize: 10
   },
   opcua.read_service.TimestampsToReturn.Both
   );
   console.log("-------------------------------------");
   var nodes = [];

   monitoredItem.on("changed",function(dataValue){
      //console.log(" value = ",dataValue.value.value);
      //console.log(" sourceTimestamp = ",dataValue.sourceTimestamp.toISOString());
      //console.log(JSON.stringify(dataValue));
      var Node = {nodeId: "ns=6;s=S71500ET200MP station_1.Master.111", nodeName: "111" , nodeValue: dataValue.value.value , nodeTimestamp: dataValue.sourceTimestamp.toISOString()};
      var arrayNode = Object.keys(Node).map(function(k) { return Node[k] });
      //console.log(JSON.stringify(Node));
      nodes.push(arrayNode);

    //  console.log(nodes);
   });
},

现在,如果我想设置新项目来监控它,只需添加许多变量 MonitorItem1 、 ..2 、 ..3 等。

如何以更多agile/dynamic方式做到这一点?如果我有数组(字符串)nodeIds并且我希望在订阅中监控其中的每一个。代码是async.series([ 代码 ])的一部分

4

2 回答 2

2

使用 async.each 方法解决

async.each(nodeIdArr, function(nodeid, callback) {

     var monitoredItem  = the_subscription.monitor({
           nodeId: opcua.resolveNodeId(nodeid),
           attributeId: 13
       },
       {
           samplingInterval: 100,
           discardOldest: true,
           queueSize: 10
       },
       opcua.read_service.TimestampsToReturn.Both
       );
       console.log("-------------------------------------");


       monitoredItem.on("changed",function(dataValue){
          //console.log(" value = ",dataValue.value.value);
          //console.log(" sourceTimestamp = ",dataValue.sourceTimestamp.toISOString());
          //console.log(JSON.stringify(dataValue));
          var Node = {nodeId: nodeid, nodeName: "111" , nodeValue: dataValue.value.value , nodeTimestamp: dataValue.sourceTimestamp.toISOString()};
          var arrayNode = Object.keys(Node).map(function(k) { return Node[k] });
          //console.log(JSON.stringify(Node));
          nodes.push(arrayNode);

        //  console.log(nodes);
      });
于 2015-12-20T09:55:35.803 回答
0

现在您可以使用the_subscription 方法。监控项()

于 2021-05-14T18:09:21.873 回答