1

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

LocalHorst
  • 11
  • 2

1 Answers1

0

isn't the sense behind the handler, that he handles the subscribed datas? Just add some logic into your handler. It provides you the node and the value, for me there is no reason, to request the data again.

When you need to compare datas as example, then you will need to request the other datas inside the handler. To prevent further requests to the server you could also store the values in variables inside the handler. Not only the last one, you can also store as example the last 10 values inside the variable.

Depending on your structure, it's maybe better to provide different handlers for different subscription. So the handler won't be a big fat class and just cares about his own nodes.

I feel like I am missing something, but I hope after 3 months this could help you.