我有一个 OPC-UA 服务器启动并运行一些预配置的标签,现在我想在我的某些标签更改时从我的 NodeJS OPC-UA 客户端添加一个新变量。例如
import {
OPCUAClient,
MessageSecurityMode, SecurityPolicy,
AttributeIds,
} from "node-opcua-client";
const connectionStrategy = {
initialDelay: 1000,
maxRetry: 1
}
const options = {
applicationName: "MyClient",
connectionStrategy: connectionStrategy,
securityMode: MessageSecurityMode.SignAndEncrypt,
securityPolicy: SecurityPolicy.Basic256Sha256,
endpointMustExist: false,
};
const client = OPCUAClient.create(options);
const endpointUrl = "{opc_url}";
try {
// step 1 : connect to
await client.connect(endpointUrl).then(res => console.log('connected!'))
// console.log("connected !");
// step 2 : createSession
await client.createSession({userName: "user_name", password: "password"}, async (err, session) => {
if(err){
console.log(err)
}
if(!err){
// do something
}
}
}
do something
在我试过的部分上面:
var nodeId = "nodeId";
var nodesToWrite = [{
nodeId: nodeId,
attributeId: AttributeIds.Value,
value: /*new DataValue(*/{
value: {/* Variant */
dataType: 1,
value: false
}
}
}];
session.write(nodesToWrite, (err, statusCodes) => {
if(!err){
console.log("success", statusCodes);
} else {
console.log(err, statusCodes)
}
}
);
但是由于nodeId
不存在所以它会抛出它不存在的错误。我找到了一个从服务器端添加变量的片段示例,但是是否可以从客户端执行它,因为我们想根据我从客户端监控的其他变量添加一些变量。