Go to the documentation of this file.00001 import sys
00002 sys.path.insert(0, "..")
00003 import time
00004 import logging
00005
00006 from opcua import Client
00007 from opcua import ua
00008
00009
00010 class SubHandler(object):
00011
00012 """
00013 Client to subscription. It will receive events from server
00014 """
00015
00016 def datachange_notification(self, node, val, data):
00017 print("Python: New data change event", node, val)
00018
00019 def event_notification(self, event):
00020 print("Python: New event", event)
00021
00022
00023 if __name__ == "__main__":
00024
00025 logging.basicConfig(level=logging.DEBUG)
00026 client = Client("opc.tcp://localhost:53530/OPCUA/SimulationServer/")
00027
00028
00029 try:
00030 client.connect()
00031 root = client.get_root_node()
00032 print("Root is", root)
00033 print("childs of root are: ", root.get_children())
00034 print("name of root is", root.get_browse_name())
00035 objects = client.get_objects_node()
00036 print("childs og objects are: ", objects.get_children())
00037 myfloat = client.get_node("ns=4;s=Float")
00038 mydouble = client.get_node("ns=4;s=Double")
00039 myint64 = client.get_node("ns=4;s=Int64")
00040 myuint64 = client.get_node("ns=4;s=UInt64")
00041 myint32 = client.get_node("ns=4;s=Int32")
00042 myuint32 = client.get_node("ns=4;s=UInt32")
00043
00044 var = client.get_node(ua.NodeId("Random1", 5))
00045 print("var is: ", var)
00046 print("value of var is: ", var.get_value())
00047 var.set_value(ua.Variant([23], ua.VariantType.Double))
00048 print("setting float value")
00049 myfloat.set_value(ua.Variant(1.234, ua.VariantType.Float))
00050 print("reading float value: ", myfloat.get_value())
00051
00052 handler = SubHandler()
00053 sub = client.create_subscription(500, handler)
00054 handle = sub.subscribe_data_change(var)
00055
00056 device = objects.get_child(["2:MyObjects", "2:MyDevice"])
00057 method = device.get_child("2:MyMethod")
00058 result = device.call_method(method, ua.Variant("sin"), ua.Variant(180, ua.VariantType.Double))
00059 print("Mehtod result is: ", result)
00060
00061
00062 time.sleep(3)
00063 sub.unsubscribe(handle)
00064 sub.delete()
00065
00066 finally:
00067 client.disconnect()