def _guess_type(self, val): if isinstance(val, (list, tuple)): error_val = val while isinstance(val, (list, tuple)): if len(val) == 0: raise UaError( "could not guess UA type of variable {}".format(error_val)) val = val[0] if val is None: return VariantType.Null elif isinstance(val, bool): return VariantType.Boolean elif isinstance(val, float): return VariantType.Double elif isinstance(val, int): return VariantType.Int64 elif type(val) in (str, unicode): return VariantType.String elif isinstance(val, bytes): return VariantType.ByteString elif isinstance(val, datetime): return VariantType.DateTime else: if isinstance(val, object): try: return getattr(VariantType, val.__class__.__name__) except AttributeError: return VariantType.ExtensionObject else: raise UaError( "Could not guess UA type of {} with type {}, specify UA type" .format(val, type(val)))
def __init__(self, val): self.name = "Custom" self.value = val if self.value > 0b00111111: raise UaError( "Cannot create VariantType. VariantType must be {} > x > {}, received {}" .format(0b111111, 25, val))
def from_binary(data): nid = NodeId() encoding = ord(data.read(1)) nid.NodeIdType = NodeIdType(encoding & 0b00111111) if nid.NodeIdType == NodeIdType.TwoByte: nid.Identifier = ord(data.read(1)) elif nid.NodeIdType == NodeIdType.FourByte: nid.NamespaceIndex, nid.Identifier = struct.unpack( "<BH", data.read(3)) elif nid.NodeIdType == NodeIdType.Numeric: nid.NamespaceIndex, nid.Identifier = struct.unpack( "<HI", data.read(6)) elif nid.NodeIdType == NodeIdType.String: nid.NamespaceIndex = uabin.Primitives.UInt16.unpack(data) nid.Identifier = uabin.Primitives.String.unpack(data) elif nid.NodeIdType == NodeIdType.ByteString: nid.NamespaceIndex = uabin.Primitives.UInt16.unpack(data) nid.Identifier = uabin.Primitives.Bytes.unpack(data) elif nid.NodeIdType == NodeIdType.Guid: nid.NamespaceIndex = uabin.Primitives.UInt16.unpack(data) nid.Identifier = Guid.from_binary(data) else: raise UaError("Unknown NodeId encoding: " + str(nid.NodeIdType)) if uabin.test_bit(encoding, 7): nid.NamespaceUri = uabin.Primitives.String.unpack(data) if uabin.test_bit(encoding, 6): nid.ServerIndex = uabin.Primitives.UInt32.unpack(data) return nid
def __init__(self, val): self.name = "Custom" self.value = val if self.value > 0b00111111: raise UaError( "Cannot create VariantType. VariantType must be %s > x > %s", 0b111111, 25)
def _find_parent_eventtype(node): """ """ parents = node.get_referenced_nodes(refs=ua.ObjectIds.HasSubtype, direction=ua.BrowseDirection.Inverse, includesubtypes=False) if len(parents) != 1: # Something went wrong raise UaError("Parent of event type could notbe found") if parents[0].nodeid.Identifier in opcua.common.event_objects.IMPLEMENTED_EVENTS.keys(): return parents[0].nodeid.Identifier, opcua.common.event_objects.IMPLEMENTED_EVENTS[parents[0].nodeid.Identifier] else: return _find_parent_eventtype(parents[0])
def __init__(self, identifier=None, namespaceidx=0, nodeidtype=None): self.Identifier = identifier self.NamespaceIndex = namespaceidx self.NodeIdType = nodeidtype self.NamespaceUri = "" self.ServerIndex = 0 self._freeze = True if not isinstance(self.NamespaceIndex, int): raise UaError("NamespaceIndex must be an int") if self.Identifier is None: self.Identifier = 0 self.NodeIdType = NodeIdType.TwoByte return if self.NodeIdType is None: if isinstance(self.Identifier, int): self.NodeIdType = NodeIdType.Numeric elif isinstance(self.Identifier, str): self.NodeIdType = NodeIdType.String elif isinstance(self.Identifier, bytes): self.NodeIdType = NodeIdType.ByteString else: raise UaError("NodeId: Could not guess type of NodeId, set NodeIdType")
def pack_uatype(vtype, value): if hasattr(Primitives, vtype.name): return getattr(Primitives, vtype.name).pack(value) elif vtype.value > 25: return Primitives.Bytes.pack(value) elif vtype.name == "ExtensionObject": # dependency loop: classes in uaprotocol_auto use Variant defined in this file, # but Variant can contain any object from uaprotocol_auto as ExtensionObject. # Using local import to avoid import loop from opcua.ua.uaprotocol_auto import extensionobject_to_binary return extensionobject_to_binary(value) else: try: return value.to_binary() except AttributeError: raise UaError("{} could not be packed with value {}".format( vtype, value))
def __init__(self): parent_eventtype.__init__(self) self.EventType = node.nodeid curr_node = node while curr_node.nodeid.Identifier != parent_identifier: for prop in curr_node.get_properties(): name = prop.get_browse_name().Name val = prop.get_data_value() self.add_property(name, val.Value.Value, val.Value.VariantType) parents = curr_node.get_referenced_nodes(refs=ua.ObjectIds.HasSubtype, direction=ua.BrowseDirection.Inverse, includesubtypes=False) if len(parents) != 1: # Something went wrong raise UaError("Parent of event type could notbe found") curr_node = parents[0] self._freeze = True
def unpack_uatype(vtype, data): if hasattr(Primitives, vtype.name): st = getattr(Primitives, vtype.name) return st.unpack(data) elif vtype.value > 25: return Primitives.Bytes.unpack(data) elif vtype.name == "ExtensionObject": # dependency loop: classes in uaprotocol_auto use Variant defined in this file, # but Variant can contain any object from uaprotocol_auto as ExtensionObject. # Using local import to avoid import loop from opcua.ua.uaprotocol_auto import extensionobject_from_binary return extensionobject_from_binary(data) else: from opcua.ua import uatypes if hasattr(uatypes, vtype.name): klass = getattr(uatypes, vtype.name) return klass.from_binary(data) else: raise UaError("can not unpack unknown vtype %s" % vtype)
def unpack_uatype(uatype, data): if uatype in uatype2struct: st = uatype2struct[uatype] return st.unpack(data.read(st.size))[0] elif uatype == "String": return unpack_string(data) elif uatype in ("CharArray", "ByteString", "Custom"): return unpack_bytes(data) elif uatype == "DateTime": return unpack_datetime(data) elif uatype == "ExtensionObject": # dependency loop: classes in uaprotocol_auto use Variant defined in this file, # but Variant can contain any object from uaprotocol_auto as ExtensionObject. # Using local import to avoid import loop from opcua.ua.uaprotocol_auto import extensionobject_from_binary return extensionobject_from_binary(data) else: glbs = globals() if uatype in glbs: klass = glbs[uatype] if hasattr(klass, 'from_binary'): return klass.from_binary(data) raise UaError("can not unpack unknown uatype %s" % uatype)
def __init__(self, name=None, namespaceidx=0): if not isinstance(namespaceidx, int): raise UaError("namespaceidx must be an int") self.NamespaceIndex = namespaceidx self.Name = name self._freeze = True