我使用node-opcua module
和
我想通过订阅监控许多opc ua nodes
我看到结果如下:用户在html用户界面中选择要监控的节点,然后点击发送这些nodeIds
作为参数的监控按钮,然后为每个nodeid
设置订阅和.on("改变")适用于所有这些项目,例如并行。现在代码如下:
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
([code])
答案 0 :(得分: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);
});
答案 1 :(得分:0)
现在您可以使用方法the_subscription。monitorItems()