I am currently developing a system with multiple communicating entites. It models the communication in a production plant. I am using the freeopcua Python implementation for the communication. To break it down:
There is a script that is running a simulation and hosts the OPC UA server. The other script has the client and connects to the server of the first script. It reads some of the variables, while it writes others. In essence, it controls the simulation.
The structure of the script is:
# - connect to the server
# - map the information model variables onto Python to make them accessible
try:
while True:
# - read some of the variables using node.get_value()
# - perform some logic
# - write some variables node.set_value(new_value)
except:
# - shut down the server
I want to implement PubSub, but I seem to be missing something. The following is a slightly changed version of the example of the freeopcua Github page:
class SubHandler(object):
"""
Client to subscription. It will receive events from server
"""
def datachange_notification(self, node, val, data):
print("Python: New data change event", node, val)
# pass
def event_notification(self, event):
print("Python: New event", event)
if __name__ == "__main__":
# configure client
client = Client("opc.tcp://127.0.0.1:10000")
try:
# connect client
client.connect()
# map the nodes of the informaiton model to Pyhton
# e.g. node1, node2
handler = SubHandler()
sub = client.create_subscription(500, handler)
handle1 = sub.subscribe_data_change(node1)
handle2 = sub.subscribe_data_change(node2)
while True:
# use the data of the read-nodes ?
except:
sub.unsubscribe(handle1)
sub.unsubscribe(handle2)
sub.delete()
client.disconnect()
This subscription accurately prints changes of the nodes. However, I want to use the data in Python to perform some logic and need the values in Python variables. Do I still need to call
node1_py = node1.get_value()
? If I do call get_value(), am I circumventing the subscription and making it obsolete? Or am I missing some other way to access the values in the loop?
I really appreciate some help. Thanks in advance