我在这里找到了一个示例代码,可以作为事件的订阅(下面的代码),我试图使用它的自定义服务,发送一些其他字段,但如果我添加这些字段名称到const字段,我得到空值,所以我想得到所有可用的字段在事件中了解什么服务器发送,但如果我删除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("----------------nn")
});
console.log("CTRL+C to stop");
await new Promise<void>((resolve) => process.once("SIGINT", resolve));
}
);
}
main();
我认为您还必须为您的单个事件字段添加名称空间索引。如果它们是子属性,则为完整路径。
可以是这样的
const fields = [
"EventId",
"EventType",
"3:MyProp1",
"3:MyProp2",
"3:SomeSubObject/SomeOtherProperty",
];