【问题标题】:node-opcua subscribe to event and get all fieldsnode-opcua 订阅事件并获取所有字段
【发布时间】:2023-02-19 11:34:56
【问题描述】:

我在这里找到了一个示例代码,它可以作为事件订阅正常工作(下面的代码), 我正在尝试在发送一些其他字段的自定义服务上使用它,但是如果我将这些文件名称添加到常量字段,我会得到空值,所以我想获得事件中可用的所有字段以了解什么服务器发送,但如果我删除 eventFilter 选项,我只会得到空事件

关于如何从事件中获取所有字段的任何建议?

import {
  AttributeIds,
  constructEventFilter,
  ObjectIds,
  OPCUAClient,
  TimestampsToReturn,
  Variant,
} from "node-opcua-client";

async function main(): Promise<void> {

  const client = OPCUAClient.create({});

  const endpointUrl = "opc.tcp://opcua.demo-this.com:62544/Quickstarts/AlarmConditionServer";

  const subscriptionParamters = {
    requestedPublishingInterval: 1000,
    maxNotificationsPerPublish: 100,
    publishingEnabled: true,
    priority: 10,
  };

  await client.withSubscriptionAsync(endpointUrl, subscriptionParamters, async (session, subscription) => {
      const fields = [
        "EventId",
        "EventType",
        "SourceNode",
        "SourceName",
        "Time",
        "ReceiveTime",
        "Message",
        "Severity",
      ];
      const eventFilter = constructEventFilter(fields);
      const event_monitoringItem = await subscription.monitor(
        {
          nodeId: ObjectIds.Server,
          attributeId: AttributeIds.EventNotifier,
        },
        {
          queueSize: 10,
          filter: eventFilter,
          discardOldest: true,
        },
        TimestampsToReturn.Both
      );

      event_monitoringItem.on("changed", (events: Variant[]) => {
        for(let i=0;i<events.length;i++) {
            console.log(fields[i],"=", events[i].toString());
        }
        console.log("----------------\n\n")
      });

      console.log("CTRL+C to stop");
      await new Promise<void>((resolve) => process.once("SIGINT", resolve));

    }
  );
}
main();

【问题讨论】:

  • 您能否尝试完全删除 eventFilter 选项和字段数组。这可能允许您接收事件中可用的所有字段:而不是这一行:const eventFilter = constructEventFilter(fields); 您可以使用:const eventFilter = null;

标签: opc-ua node-opcua


【解决方案1】:

我认为您还必须为单个事件字段添加名称空间索引。如果它们是子属性,则完整路径。

所以它看起来像这样:

   const fields = [
    "EventId",
    "EventType",
    "3:MyProp1",
    "3:MyProp2",
    "3:SomeSubObject/SomeOtherProperty",
  ];

【讨论】:

    猜你喜欢
    • 2018-03-17
    • 1970-01-01
    • 2015-11-06
    • 2022-09-27
    • 2021-11-21
    • 1970-01-01
    • 2019-10-04
    • 2017-07-25
    • 2020-03-29
    相关资源
    最近更新 更多