'Can node-opcua sever return values dynamically bases on the user context?
In my use case, I have to return different values based on the user context / current session in the server.
In pseudo-node-opcua code something like following:
namespace.addVariable({
componentOf: device,
browseName: "MyVariable1",
dataType: "Double",
value: {
get: context => {
const user = context.userIdentity;
const value = user === "admin"
? service.getRootValue()
: service.getUserValueFor(user);
return new Variant({dataType: DataType.Double, value })
}
}
});
Is there a way how to achieve this with node-opcua?
Solution 1:[1]
One can override the readValue function, which takes the context as a parameter, to return a dynamic value:
myVariable1.readValue = (context, indexRange, dataEncoding) => (
new DataValue({
value: {
dataType: DataType.String,
value: `Hello, ${context.session.userIdentityToken.userName}!`,
statusCode: StatusCodes.Good
}
})
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ttulka |
