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
00016 def data_change(self, handle, node, val, attr):
00017 print("Python: New data change event", handle, node, val, attr)
00018
00019 def event(self, handle, event):
00020 print("Python: New event", handle, event)
00021
00022
00023 if __name__ == "__main__":
00024
00025 client = opcua.Client(False)
00026 client.connect("opc.tcp://localhost:4841/freeopcua/server/")
00027
00028 try:
00029
00030
00031 uri = "http://examples.freeopcua.github.io"
00032 idx = client.get_namespace_index(uri)
00033
00034
00035 statenode = client.get_node(opcua.ObjectId.Server_ServerStatus_State)
00036 print("Server state is: ", statenode.get_value())
00037
00038
00039 root = client.get_root_node()
00040 print("I got root: ", root)
00041 print("Childs are: ", root.get_children())
00042
00043
00044
00045 print("Objects is: ", client.get_objects_node())
00046 objects = client.get_objects_node()
00047 print("Children of objects are: ", objects.get_children())
00048
00049
00050 myvar = objects.get_child(["{}:NewObject".format(idx), "MyVariable"])
00051 print("yvar is: ", myvar)
00052
00053
00054 sclt = SubHandler()
00055 sub = client.create_subscription(100, sclt)
00056
00057
00058 handle = sub.subscribe_data_change(myvar)
00059 print("Subscribe handle is: ", handle)
00060
00061
00062 evhandle = sub.subscribe_events()
00063 print("Subscribe handle is: ", evhandle)
00064
00065
00066 embed()
00067 finally:
00068 client.disconnect()