Go to the documentation of this file.00001
00002 import sys
00003 import time
00004 sys.path.append(".")
00005
00006 from IPython import embed
00007 import opcua
00008
00009 class SubHandler(opcua.SubscriptionHandler):
00010 """
00011 Client to subcsription. It will receive events from server
00012 """
00013 def __init__(self, *args):
00014 opcua.SubscriptionHandler.__init__(self, *args)
00015 self.ev = MessageSecurityMode::None
00016
00017 def data_change(self, handle, node, val, attr):
00018 print("Python: New data change event", handle, node, val, attr)
00019
00020 def event(self, handle, event):
00021 print("Python: New event", handle, event)
00022 self.ev = event
00023
00024
00025 if __name__ == "__main__":
00026
00027 client = opcua.Client(False)
00028 client.connect("opc.tcp://localhost:4841/freeopcua/server/")
00029
00030 try:
00031
00032
00033 uri = "http://examples.freeopcua.github.io"
00034 idx = client.get_namespace_index(uri)
00035
00036
00037 statenode = client.get_node(opcua.ObjectId.Server_ServerStatus_State)
00038 print("Server state is: ", statenode.get_value())
00039
00040
00041 root = client.get_root_node()
00042 print("I got root: ", root)
00043 print("Childs are: ", root.get_children())
00044
00045
00046
00047 print("Objects is: ", client.get_objects_node())
00048 objects = client.get_objects_node()
00049 print("Children of objects are: ", objects.get_children())
00050
00051
00052 myvar = objects.get_child(["{}:NewObject".format(idx), "MyVariable"])
00053 print("yvar is: ", myvar)
00054
00055
00056 sclt = SubHandler()
00057 sub = client.create_subscription(100, sclt)
00058
00059
00060
00061
00062
00063
00064 evhandle = sub.subscribe_events()
00065 print("Subscribe handle is: ", evhandle)
00066
00067
00068 embed()
00069 finally:
00070 client.disconnect()