Variable update within server method #1167
Answered
by
schroeder-
IevgenVovk
asked this question in
Q&A
-
Hello, Schematically (omitting non-essential parts of server-minimal.py): ...
@uamethod
def func(parent, value):
return value * 2 # <-- Want to set MyVariable = 2*value
async def main():
...
# populating our address space
# server.nodes, contains links to very common nodes like objects and root
myobj = await server.nodes.objects.add_object(idx, "MyObject")
myvar = await myobj.add_variable(idx, "MyVariable", 6.7) # <-- Want to change this from func()
# Set MyVariable to be writable by clients
await myvar.set_writable()
await server.nodes.objects.add_method(
ua.NodeId("ServerMethod", idx),
ua.QualifiedName("ServerMethod", idx),
func,
[ua.VariantType.Int64],
[ua.VariantType.Int64],
)
... |
Beta Was this translation helpful? Give feedback.
Answered by
schroeder-
Jan 5, 2023
Replies: 1 comment 1 reply
-
You can use a class for example: class MultiplyMethodCallHandler:
def __init__(self, target) -> None:
self._target = target
@uamethod
async def call(self, parent, y, x):
print(self._target)
await self._target.write_value(ua.Double(x * y))
print(x * y)
return x * y
.....
async def main():
.....
mydouble = await myobj.add_variable(idx, "MyDouble", 0.0)
callh = MultiplyMethodCallHandler(mydouble)
multiply_node = await myobj.add_method(
idx,
"multiply",
callh.call,
[ua.VariantType.Double, ua.VariantType.Double],
[ua.VariantType.Double],
) The other possiblity is to use inner functions, but in this case this not a good idea. Because they can get messy in larger applications. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
IevgenVovk
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use a class for example:
The other possiblity is to use inner …