2 sys.path.insert(0,
"..")
4 from datetime
import datetime
7 from IPython
import embed
14 shell = code.InteractiveConsole(vars)
18 from opcua
import ua, uamethod, Server
24 Subscription Handler. To receive events from server for a subscription 28 print(
"Python: New data change event", node, val)
31 print(
"Python: New event", event)
38 if variant.Value % 2 == 0:
40 return [ua.Variant(ret, ua.VariantType.Boolean)]
48 print(
"multiply method call with parameters: ", x, y)
52 if __name__ ==
"__main__":
54 logging.basicConfig(level=logging.WARN)
68 server.set_endpoint(
"opc.tcp://0.0.0.0:4840/freeopcua/server/")
69 server.set_server_name(
"FreeOpcUa Example Server")
72 uri =
"http://examples.freeopcua.github.io" 73 idx = server.register_namespace(uri)
76 dev = server.nodes.base_object_type.add_object_type(0,
"MyDevice")
77 dev.add_variable(0,
"sensor1", 1.0)
78 dev.add_property(0,
"device_id",
"0340")
79 ctrl = dev.add_object(0,
"controller")
80 ctrl.add_property(0,
"state",
"Idle")
85 myfolder = server.nodes.objects.add_folder(idx,
"myEmptyFolder")
87 mydevice = server.nodes.objects.add_object(idx,
"Device0001", dev)
88 mydevice_var = mydevice.get_child([
"0:controller",
"0:state"])
90 myobj = server.nodes.objects.add_object(idx,
"MyObject")
91 myvar = myobj.add_variable(idx,
"MyVariable", 6.7)
93 mystringvar = myobj.add_variable(idx,
"MyStringVariable",
"Really nice string")
94 mystringvar.set_writable()
95 mydtvar = myobj.add_variable(idx,
"MyDateTimeVar", datetime.now())
96 mydtvar.set_writable()
97 myarrayvar = myobj.add_variable(idx,
"myarrayvar", [6.7, 7.9])
98 myarrayvar = myobj.add_variable(idx,
"myStronglytTypedVariable", ua.Variant([], ua.VariantType.UInt32))
99 myprop = myobj.add_property(idx,
"myproperty",
"I am a property")
100 mymethod = myobj.add_method(idx,
"mymethod", func, [ua.VariantType.Int64], [ua.VariantType.Boolean])
101 multiply_node = myobj.add_method(idx,
"multiply", multiply, [ua.VariantType.Int64, ua.VariantType.Int64], [ua.VariantType.Int64])
104 server.import_xml(
"custom_nodes.xml")
109 myevgen = server.get_event_generator()
110 myevgen.event.Severity = 300
114 print(
"Available loggers are: ", logging.Logger.manager.loggerDict.keys())
121 mydevice_var.set_value(
"Running")
122 myevgen.trigger(message=
"This is BaseEvent")
def datachange_notification(self, node, val, data)
def multiply(parent, x, y)
def func(parent, variant)
def event_notification(self, event)