async def _lsprint_1(node, depth, indent=""): if not indent: print("{0:30} {1:25} {2:25} {3:25}".format("DisplayName", "NodeId", "BrowseName", "Value")) print("") for desc in await node.get_children_descriptions(): if desc.NodeClass == ua.NodeClass.Variable: try: val = await Node(node.server, desc.NodeId).read_value() except UaStatusCodeError as err: val = "Bad (0x{0:x})".format(err.code) print( "{0}{1:30} {2!s:25} {3!s:25}, {4!s:3}".format( indent, desc.DisplayName.to_string(), desc.NodeId.to_string(), desc.BrowseName.to_string(), val, ) ) else: print( "{0}{1:30} {2!s:25} {3!s:25}".format( indent, desc.DisplayName.to_string(), desc.NodeId.to_string(), desc.BrowseName.to_string(), ) ) if depth: await _lsprint_1(Node(node.server, desc.NodeId), depth - 1, indent + " ")
async def test_guid_node_id(): """ Test that a Node can be instantiated with a GUID string and that the NodeId ca be converted to binary. """ node = Node(None, "ns=4;g=35d5f86f-2777-4550-9d48-b098f5ee285c") binary_node_id = ua.ua_binary.nodeid_to_binary(node.nodeid) assert type(binary_node_id) is bytes
async def _lsprint_0(node, depth, indent=""): if not indent: print("{0:30} {1:25}".format("DisplayName", "NodeId")) print("") for desc in await node.get_children_descriptions(): print("{0}{1:30} {2:25}".format(indent, desc.DisplayName.to_string(), desc.NodeId.to_string())) if depth: await _lsprint_0(Node(node.server, desc.NodeId), depth - 1, indent + " ")
async def init(self, etype=None, emitting_node=ua.ObjectIds.Server): node = None if isinstance(etype, event_objects.BaseEvent): self.event = etype elif isinstance(etype, Node): node = etype elif isinstance(etype, ua.NodeId): node = Node(self.isession, etype) else: node = Node(self.isession, ua.NodeId(etype)) if node: self.event = await events.get_event_obj_from_type_node(node) if isinstance(emitting_node, Node): pass elif isinstance(emitting_node, ua.NodeId): emitting_node = Node(self.isession, emitting_node) else: emitting_node = Node(self.isession, ua.NodeId(emitting_node)) self.event.emitting_node = emitting_node.nodeid if not self.event.SourceNode: self.event.SourceNode = emitting_node.nodeid if not self.event.SourceName: self.event.SourceName = (await Node(self.isession, self.event.SourceNode).get_browse_name()).Name await emitting_node.set_event_notifier([ua.EventNotifier.SubscribeToEvents]) refs = [] ref = ua.AddReferencesItem() ref.IsForward = True ref.ReferenceTypeId = ua.NodeId(ua.ObjectIds.GeneratesEvent) ref.SourceNodeId = emitting_node.nodeid ref.TargetNodeClass = ua.NodeClass.ObjectType ref.TargetNodeId = self.event.EventType refs.append(ref) results = await self.isession.add_references(refs) # result.StatusCode.check() self.emitting_node = emitting_node
async def test_xml_ns(opc, tmpdir): """ This test is far too complicated but catches a lot of things... """ ns_array = await opc.opc.get_namespace_array() if len(ns_array) < 3: await opc.opc.register_namespace("dummy_ns") ref_ns = await opc.opc.register_namespace("ref_namespace") new_ns = await opc.opc.register_namespace("my_new_namespace") bname_ns = await opc.opc.register_namespace("bname_namespace") o = await opc.opc.nodes.objects.add_object(0, "xmlns0") o50 = await opc.opc.nodes.objects.add_object(50, "xmlns20") o200 = await opc.opc.nodes.objects.add_object(200, "xmlns200") onew = await opc.opc.nodes.objects.add_object(new_ns, "xmlns_new") vnew = await onew.add_variable(new_ns, "xmlns_new_var", 9.99) o_no_export = await opc.opc.nodes.objects.add_object( ref_ns, "xmlns_parent") v_no_parent = await o_no_export.add_variable(new_ns, "xmlns_new_var_no_parent", 9.99) o_bname = await onew.add_object(f"ns={new_ns};i=4000", f"{bname_ns}:BNAME") nodes = [o, o50, o200, onew, vnew, v_no_parent, o_bname] tmp_path = tmpdir.join("tmp_test_export-ns.xml").strpath await opc.opc.export_xml(nodes, tmp_path) # delete node and change index og new_ns before re-importing await opc.opc.delete_nodes(nodes) ns_node = opc.opc.get_node(ua.NodeId(ua.ObjectIds.Server_NamespaceArray)) nss = await ns_node.read_value() nss.remove("my_new_namespace") # nss.remove("ref_namespace") nss.remove("bname_namespace") await ns_node.write_value(nss) new_ns = await opc.opc.register_namespace("my_new_namespace_offsett") new_ns = await opc.opc.register_namespace("my_new_namespace") new_nodes = await opc.opc.import_xml(tmp_path) for i in [o, o50, o200]: await i.read_browse_name() with pytest.raises(uaerrors.BadNodeIdUnknown): await onew.read_browse_name() # since my_new_namesspace2 is referenced byt a node it should have been reimported nss = await opc.opc.get_namespace_array() assert "bname_namespace" in nss # get index of namespaces after import new_ns = await opc.opc.register_namespace("my_new_namespace") bname_ns = await opc.opc.register_namespace("bname_namespace") nnode = Node( onew.server, ua.NodeId(Identifier=onew.nodeid.Identifier, NamespaceIndex=new_ns)) await nnode.read_browse_name() vnew2 = (await nnode.get_children())[0] assert vnew2.nodeid.NamespaceIndex == new_ns
def trigger(self, time=None, message=None): """ Trigger the event. This will send a notification to all subscribed clients """ self.event.EventId = ua.Variant(uuid.uuid4().hex.encode('utf-8'), ua.VariantType.ByteString) if time: self.event.Time = time else: self.event.Time = datetime.utcnow() self.event.ReceiveTime = datetime.utcnow() # FIXME: LocalTime is wrong but currently know better. For description s. Part 5 page 18 self.event.LocalTime = datetime.utcnow() if message: self.event.Message = ua.LocalizedText(message) elif not self.event.Message: self.event.Message = ua.LocalizedText(Node(self.isession, self.event.SourceNode).get_browse_name().Text) self.isession.subscription_service.trigger_event(self.event)
def nodes(self): return [Node(self._opc_client.uaclient, x) for x in self.node_ids]