2 Usefull method and classes not belonging anywhere and depending on opcua library 5 from dateutil
import parser
6 from datetime
import datetime
7 from enum
import Enum, IntEnum
16 convert a python object or python-opcua object to a string 17 which should be easy to understand for human 18 easy to modify, and not too hard to parse back ....not easy 19 meant for UI or command lines 22 if isinstance(val, (list, tuple)):
26 return "[" +
", ".join(res) +
"]" 28 if hasattr(val,
"to_string"):
32 elif isinstance(val, (Enum, IntEnum)):
36 elif isinstance(val, str):
38 elif isinstance(val, bytes):
40 elif isinstance(val, datetime):
42 elif isinstance(val, (int, float)):
52 convert a variant to a string which should be easy to understand for human 53 easy to modify, and not too hard to parse back ....not easy 54 meant for UI or command lines 61 Convert back a string to a python or python-opcua object 62 Note: no error checking is done here, supplying null strings could raise exceptions (datetime and guid) 64 string = string.strip()
65 if string.startswith(
"["):
68 for s
in string.split(
","):
74 if vtype == ua.VariantType.Null:
76 elif vtype == ua.VariantType.Boolean:
77 if string
in (
"True",
"true",
"on",
"On",
"1"):
81 elif vtype
in (ua.VariantType.Int16, ua.VariantType.Int32, ua.VariantType.Int64):
83 elif vtype
in (ua.VariantType.UInt16, ua.VariantType.UInt32, ua.VariantType.UInt64):
85 elif vtype
in (ua.VariantType.Float, ua.VariantType.Double):
87 elif vtype
in (ua.VariantType.String, ua.VariantType.XmlElement):
89 elif vtype
in (ua.VariantType.SByte, ua.VariantType.ByteString):
90 val = string.encode(
"utf-8")
91 elif vtype
in (ua.VariantType.NodeId, ua.VariantType.ExpandedNodeId):
92 val = ua.NodeId.from_string(string)
93 elif vtype == ua.VariantType.QualifiedName:
94 val = ua.QualifiedName.from_string(string)
95 elif vtype == ua.VariantType.DateTime:
96 val = parser.parse(string)
97 elif vtype == ua.VariantType.LocalizedText:
99 elif vtype == ua.VariantType.StatusCode:
101 elif vtype == ua.VariantType.Guid:
102 val = uuid.UUID(string)
105 raise NotImplementedError
111 convert back a string to an ua.Variant 118 Get recursively all children of a node 122 for child
in node.get_children():
131 for child
in node.get_children(refs=ua.ObjectIds.HasSubtype):
139 return get all subtype parents of node recursive 140 :param node: can be a ua.Node or ua.NodeId 141 :param includeitself: include also node to the list 142 :param skipbase don't include the toplevel one 143 :returns list of ua.Node, top parent first 149 if skipbase
and len(parents) > 1:
150 parents = parents[:-1]
157 recursive implementation of get_node_derived_from_types 162 basetypes.append(parent)
170 return node supertype or None 172 supertypes = node.get_referenced_nodes(refs=ua.ObjectIds.HasSubtype,
173 direction=ua.BrowseDirection.Inverse,
174 includesubtypes=
True)
183 return if a browsename is present a child from the provide node 184 :param node: node wherein to find the browsename 185 :param browsename: browsename to search 186 :returns returne True if the browsename is present else False 188 child_descs = node.get_children_descriptions()
189 for child_desc
in child_descs:
190 if child_desc.BrowseName == browsename:
198 Given a Node datatype, find out the variant type to encode 199 data. This is not exactly straightforward... 203 if base.nodeid.Identifier != 29:
207 descs = dtype_node.get_children_descriptions()
208 bnames = [d.BrowseName.Name
for d
in descs]
209 if "EnumStrings" in bnames:
210 return ua.VariantType.LocalizedText
211 elif "EnumValues" in bnames:
212 return ua.VariantType.ExtensionObject
214 raise ua.UaError(
"Enumeration must have a child node describing its type and values")
219 Looks up the base datatype of the provided datatype Node 220 The base datatype is either: 221 A primitive type (ns=0, i<=21) or a complex one (ns=0 i>21 and i<=30) like Enum and Struct. 224 datatype: NodeId of a datype of a variable 226 NodeId of datatype base or None in case base datype can not be determined 230 if base.nodeid.NamespaceIndex == 0
and isinstance(base.nodeid.Identifier, int)
and base.nodeid.Identifier <= 30:
233 raise ua.UaError(
"Datatype must be a subtype of builtin types {0!s}".format(datatype))
238 Get the nodes of one or more namespaces . 240 server: opc ua server to use 241 namespaces: list of string uri or int indexes of the namespace to export 243 List of nodes that are part of the provided namespaces 245 if namespaces
is None:
247 ns_available = server.get_namespace_array()
250 namespaces = ns_available[1:]
251 elif isinstance(namespaces, (str, int)):
252 namespaces = [namespaces]
255 namespace_indexes = [n
if isinstance(n, int)
else ns_available.index(n)
for n
in namespaces]
258 nodes = [server.get_node(nodeid)
for nodeid
in server.iserver.aspace.keys()
259 if nodeid.NamespaceIndex != 0
and nodeid.NamespaceIndex
in namespace_indexes]
def is_child_present(node, browsename)
def get_nodes_of_namespace(server, namespaces=None)
def _get_node_supertypes(node)
def string_to_val(string, vtype)
def string_to_variant(string, vtype)
def variant_to_string(var)
def data_type_to_variant_type(dtype_node)
def get_node_supertypes(node, includeitself=False, skipbase=True)
def get_base_data_type(datatype)
def get_node_supertype(node)
def get_node_children(node, nodes=None)
def get_node_subtypes(node, nodes=None)