@@ 297-370 (lines=74) @@ | ||
294 | ||
295 | ||
296 | class AttributeWriteMask(IntEnum): |
|
297 | ''' |
|
298 | Define bits used to indicate which attributes are writable. |
|
299 | ||
300 | :ivar None_: |
|
301 | :vartype None_: 0 |
|
302 | :ivar AccessLevel: |
|
303 | :vartype AccessLevel: 1 |
|
304 | :ivar ArrayDimensions: |
|
305 | :vartype ArrayDimensions: 2 |
|
306 | :ivar BrowseName: |
|
307 | :vartype BrowseName: 4 |
|
308 | :ivar ContainsNoLoops: |
|
309 | :vartype ContainsNoLoops: 8 |
|
310 | :ivar DataType: |
|
311 | :vartype DataType: 16 |
|
312 | :ivar Description: |
|
313 | :vartype Description: 32 |
|
314 | :ivar DisplayName: |
|
315 | :vartype DisplayName: 64 |
|
316 | :ivar EventNotifier: |
|
317 | :vartype EventNotifier: 128 |
|
318 | :ivar Executable: |
|
319 | :vartype Executable: 256 |
|
320 | :ivar Historizing: |
|
321 | :vartype Historizing: 512 |
|
322 | :ivar InverseName: |
|
323 | :vartype InverseName: 1024 |
|
324 | :ivar IsAbstract: |
|
325 | :vartype IsAbstract: 2048 |
|
326 | :ivar MinimumSamplingInterval: |
|
327 | :vartype MinimumSamplingInterval: 4096 |
|
328 | :ivar NodeClass: |
|
329 | :vartype NodeClass: 8192 |
|
330 | :ivar NodeId: |
|
331 | :vartype NodeId: 16384 |
|
332 | :ivar Symmetric: |
|
333 | :vartype Symmetric: 32768 |
|
334 | :ivar UserAccessLevel: |
|
335 | :vartype UserAccessLevel: 65536 |
|
336 | :ivar UserExecutable: |
|
337 | :vartype UserExecutable: 131072 |
|
338 | :ivar UserWriteMask: |
|
339 | :vartype UserWriteMask: 262144 |
|
340 | :ivar ValueRank: |
|
341 | :vartype ValueRank: 524288 |
|
342 | :ivar WriteMask: |
|
343 | :vartype WriteMask: 1048576 |
|
344 | :ivar ValueForVariableType: |
|
345 | :vartype ValueForVariableType: 2097152 |
|
346 | ''' |
|
347 | None_ = 0 |
|
348 | AccessLevel = 1 |
|
349 | ArrayDimensions = 2 |
|
350 | BrowseName = 4 |
|
351 | ContainsNoLoops = 8 |
|
352 | DataType = 16 |
|
353 | Description = 32 |
|
354 | DisplayName = 64 |
|
355 | EventNotifier = 128 |
|
356 | Executable = 256 |
|
357 | Historizing = 512 |
|
358 | InverseName = 1024 |
|
359 | IsAbstract = 2048 |
|
360 | MinimumSamplingInterval = 4096 |
|
361 | NodeClass = 8192 |
|
362 | NodeId = 16384 |
|
363 | Symmetric = 32768 |
|
364 | UserAccessLevel = 65536 |
|
365 | UserExecutable = 131072 |
|
366 | UserWriteMask = 262144 |
|
367 | ValueRank = 524288 |
|
368 | WriteMask = 1048576 |
|
369 | ValueForVariableType = 2097152 |
|
370 | ||
371 | ||
372 | class BrowseDirection(IntEnum): |
|
373 | ''' |
@@ 805-865 (lines=61) @@ | ||
802 | raise UaError("could not guess UA type of variable {0}".format(error_val)) |
|
803 | val = val[0] |
|
804 | if val is None: |
|
805 | return VariantType.Null |
|
806 | elif isinstance(val, bool): |
|
807 | return VariantType.Boolean |
|
808 | elif isinstance(val, float): |
|
809 | return VariantType.Double |
|
810 | elif isinstance(val, IntEnum): |
|
811 | return VariantType.Int32 |
|
812 | elif isinstance(val, int): |
|
813 | return VariantType.Int64 |
|
814 | elif type(val) in (str, unicode): |
|
815 | return VariantType.String |
|
816 | elif isinstance(val, bytes): |
|
817 | return VariantType.ByteString |
|
818 | elif isinstance(val, datetime): |
|
819 | return VariantType.DateTime |
|
820 | elif isinstance(val, uuid.UUID): |
|
821 | return VariantType.Guid |
|
822 | else: |
|
823 | if isinstance(val, object): |
|
824 | try: |
|
825 | return getattr(VariantType, val.__class__.__name__) |
|
826 | except AttributeError: |
|
827 | return VariantType.ExtensionObject |
|
828 | else: |
|
829 | raise UaError("Could not guess UA type of {0} with type {1}, specify UA type".format(val, type(val))) |
|
830 | ||
831 | def __str__(self): |
|
832 | return "Variant(val:{0!s},type:{1})".format(self.Value, self.VariantType) |
|
833 | __repr__ = __str__ |
|
834 | ||
835 | def to_binary(self): |
|
836 | b = [] |
|
837 | encoding = self.VariantType.value & 0b111111 |
|
838 | if self.is_array or isinstance(self.Value, (list, tuple)): |
|
839 | self.is_array = True |
|
840 | encoding = uabin.set_bit(encoding, 7) |
|
841 | if self.Dimensions is not None: |
|
842 | encoding = uabin.set_bit(encoding, 6) |
|
843 | b.append(uabin.Primitives.UInt8.pack(encoding)) |
|
844 | b.append(uabin.pack_uatype_array(self.VariantType, flatten(self.Value))) |
|
845 | if self.Dimensions is not None: |
|
846 | b.append(uabin.pack_uatype_array(VariantType.Int32, self.Dimensions)) |
|
847 | else: |
|
848 | b.append(uabin.Primitives.UInt8.pack(encoding)) |
|
849 | b.append(uabin.pack_uatype(self.VariantType, self.Value)) |
|
850 | ||
851 | return b"".join(b) |
|
852 | ||
853 | @staticmethod |
|
854 | def from_binary(data): |
|
855 | dimensions = None |
|
856 | array = False |
|
857 | encoding = ord(data.read(1)) |
|
858 | int_type = encoding & 0b00111111 |
|
859 | vtype = datatype_to_varianttype(int_type) |
|
860 | if uabin.test_bit(encoding, 7): |
|
861 | value = uabin.unpack_uatype_array(vtype, data) |
|
862 | array = True |
|
863 | else: |
|
864 | value = uabin.unpack_uatype(vtype, data) |
|
865 | if uabin.test_bit(encoding, 6): |
|
866 | dimensions = uabin.unpack_uatype_array(VariantType.Int32, data) |
|
867 | value = reshape(value, dimensions) |
|
868 | return Variant(value, vtype, dimensions, is_array=array) |