Go to the documentation of this file.00001 import sys
00002 sys.path.insert(0, "..")
00003 import logging
00004 import time
00005
00006 try:
00007 from IPython import embed
00008 except ImportError:
00009 import code
00010
00011 def embed():
00012 vars = globals()
00013 vars.update(locals())
00014 shell = code.InteractiveConsole(vars)
00015 shell.interact()
00016
00017
00018 from opcua import Client
00019 from opcua import ua
00020
00021
00022 class SubHandler(object):
00023
00024 """
00025 Subscription Handler. To receive events from server for a subscription
00026 data_change and event methods are called directly from receiving thread.
00027 Do not do expensive, slow or network operation there. Create another
00028 thread if you need to do such a thing
00029 """
00030
00031 def datachange_notification(self, node, val, data):
00032 print("Python: New data change event", node, val)
00033
00034 def event_notification(self, event):
00035 print("Python: New event", event)
00036
00037
00038 if __name__ == "__main__":
00039 logging.basicConfig(level=logging.WARN)
00040
00041
00042
00043 client = Client("opc.tcp://localhost:4840/freeopcua/server/")
00044
00045 try:
00046 client.connect()
00047
00048
00049 root = client.get_root_node()
00050 print("Root node is: ", root)
00051 objects = client.get_objects_node()
00052 print("Objects node is: ", objects)
00053
00054
00055 print("Children of root are: ", root.get_children())
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 myvar = root.get_child(["0:Objects", "2:MyObject", "2:MyVariable"])
00068 obj = root.get_child(["0:Objects", "2:MyObject"])
00069 print("myvar is: ", myvar)
00070
00071
00072 handler = SubHandler()
00073 sub = client.create_subscription(500, handler)
00074 handle = sub.subscribe_data_change(myvar)
00075 time.sleep(0.1)
00076
00077
00078 sub.subscribe_events()
00079
00080
00081
00082
00083 res = obj.call_method("2:multiply", 3, "klk")
00084 print("method result is: ", res)
00085
00086 embed()
00087 finally:
00088 client.disconnect()