Can node-opcua sever return values dynamically bases on the user context?

43 Views Asked by At

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?

1

There are 1 best solutions below

0
On

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
        }
    })
);