Go to the documentation of this file.00001 import sys
00002 sys.path.insert(0, "..")
00003 import logging
00004
00005 try:
00006 from IPython import embed
00007 except ImportError:
00008 import code
00009
00010 def embed():
00011 vars = globals()
00012 vars.update(locals())
00013 shell = code.InteractiveConsole(vars)
00014 shell.interact()
00015
00016
00017 from opcua import ua, uamethod, Server
00018
00019
00020
00021 def func(parent, variant):
00022 ret = False
00023 if variant.Value % 2 == 0:
00024 ret = True
00025 return [ua.Variant(ret, ua.VariantType.Boolean)]
00026
00027
00028
00029
00030
00031 @uamethod
00032 def multiply(parent, x, y):
00033 print("multiply method call with parameters: ", x, y)
00034 return x * y
00035
00036
00037 if __name__ == "__main__":
00038
00039 logging.basicConfig(level=logging.WARN)
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 server = Server()
00053
00054 server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
00055 server.set_server_name("FreeOpcUa Example Server")
00056
00057
00058 uri = "http://examples.freeopcua.github.io"
00059 idx = server.register_namespace(uri)
00060
00061
00062 objects = server.get_objects_node()
00063
00064
00065 myfolder = objects.add_folder(idx, "myEmptyFolder")
00066 myobj = objects.add_object(idx, "MyObject")
00067 myvar = myobj.add_variable(idx, "MyVariable", 6.7)
00068 myvar.set_writable()
00069 myarrayvar = myobj.add_variable(idx, "myarrayvar", [6.7, 7.9])
00070 myarrayvar = myobj.add_variable(idx, "myStronglytTypedVariable", ua.Variant([], ua.VariantType.UInt32))
00071 myprop = myobj.add_property(idx, "myproperty", "I am a property")
00072 mymethod = myobj.add_method(idx, "mymethod", func, [ua.VariantType.Int64], [ua.VariantType.Boolean])
00073
00074 inargx = ua.Argument()
00075 inargx.Name = "x"
00076 inargx.DataType = ua.NodeId(ua.ObjectIds.Int64)
00077 inargx.ValueRank = -1
00078 inargx.ArrayDimensions = []
00079 inargx.Description = ua.LocalizedText("First number x")
00080 inargy = ua.Argument()
00081 inargy.Name = "y"
00082 inargy.DataType = ua.NodeId(ua.ObjectIds.Int64)
00083 inargy.ValueRank = -1
00084 inargy.ArrayDimensions = []
00085 inargy.Description = ua.LocalizedText("Second number y")
00086 outarg = ua.Argument()
00087 outarg.Name = "Result"
00088 outarg.DataType = ua.NodeId(ua.ObjectIds.Int64)
00089 outarg.ValueRank = -1
00090 outarg.ArrayDimensions = []
00091 outarg.Description = ua.LocalizedText("Multiplication result")
00092
00093 multiply_node = myobj.add_method(idx, "multiply", multiply, [inargx, inargy], [outarg])
00094
00095
00096 server.start()
00097 print("Available loggers are: ", logging.Logger.manager.loggerDict.keys())
00098 try:
00099
00100 embed()
00101 finally:
00102 server.stop()