def server(): s = Server() s.set_endpoint('opc.tcp://0.0.0.0:8840/freeopcua/server/') uri = "http://examples.freeopcua.github.io" idx = s.register_namespace(uri) myobj = s.nodes.objects.add_object(idx, "MyObject") myvar = myobj.add_variable(idx, "MyVariable", 6.7) mysin = myobj.add_variable(idx, "MySin", 0, ua.VariantType.Float) s.start() yield s s.stop()
def server(tloop): s = Server(tloop=tloop) s.disable_clock(True) s.set_endpoint('opc.tcp://0.0.0.0:8840/freeopcua/server/') uri = "http://examples.freeopcua.github.io" ns_idx = s.register_namespace(uri) myobj = s.nodes.objects.add_object(ns_idx, "MyObject") myobj.add_variable(ns_idx, "MyVariable", 6.7) myobj.add_variable(ns_idx, "MySin", 0, ua.VariantType.Float) s.nodes.objects.add_method(ns_idx, "Divide", divide, [ua.VariantType.Float, ua.VariantType.Float], [ua.VariantType.Float]) with s: yield s
class ServerPython(object): def __init__(self): self._server = None self.nodes = None self.get_node = None self.get_namespace_array = None def get_server(self): return self._server def start_server(self, endpoint): logger.info("Starting python-opcua server") self._server = Server() self._server.set_endpoint(endpoint) self._server.set_server_name("OpcUa Modeler Server") self.nodes = self._server.nodes self.get_node = self._server.get_node self.get_namespace_array = self._server.get_namespace_array self.load_type_definitions = self._server.load_type_definitions self.load_enums = self._server.load_enums # now remove freeopcua namespace, not necessary when modeling and # ensures correct idx for exported nodesets ns_node = self._server.get_node( ua.NodeId(ua.ObjectIds.Server_NamespaceArray)) nss = ns_node.read_value() ns_node.write_value(nss[:1]) self._server.start() def stop_server(self): if self._server is not None: self._server.stop() self._server = None self.get_node = None self.get_namespace_array = None def import_xml(self, path): return self._server.import_xml(path) def export_xml(self, nodes, uris, path): exp = XmlExporter(self._server) exp.build_etree(nodes, uris=uris) exp.write_xml(path)
# optional: setup logging logging.basicConfig(level=logging.WARN) #logger = logging.getLogger("opcua.address_space") # logger.setLevel(logging.DEBUG) #logger = logging.getLogger("opcua.internal_server") # logger.setLevel(logging.DEBUG) #logger = logging.getLogger("opcua.binary_server_asyncio") # logger.setLevel(logging.DEBUG) #logger = logging.getLogger("opcua.uaprocessor") # logger.setLevel(logging.DEBUG) with ThreadLoop() as tloop: # now setup our server server = Server(tloop=tloop) #server.disable_clock() #server.set_endpoint("opc.tcp://localhost:4840/freeopcua/server/") server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/") server.set_server_name("FreeOpcUa Example Server") # set all possible endpoint policies for clients to connect through server.set_security_policy([ ua.SecurityPolicyType.NoSecurity, ua.SecurityPolicyType.Basic256Sha256_SignAndEncrypt, ua.SecurityPolicyType.Basic256Sha256_Sign ]) # setup our own namespace uri = "http://examples.freeopcua.github.io" idx = server.register_namespace(uri) print("IDX", idx) # create a new node type we can instantiate in our address space dev = server.nodes.base_object_type.add_object_type(idx, "MyDevice")
def server(): s = Server() s.set_endpoint('opc.tcp://*:8840/freeopcua/server/') s.start() yield s s.stop()