Completed
Pull Request — master (#296)
by Olivier
04:13
created

pack_datetime()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
"""
2
implement ua datatypes
3
"""
4 1
import logging
5 1
import struct
6 1
from enum import Enum, IntEnum, EnumMeta
7 1
from datetime import datetime
8 1
import sys
9 1
import os
10 1
import uuid
11 1
import re
12 1
import itertools
13 1
if sys.version_info.major > 2:
14 1
    unicode = str
15
16 1
from opcua.ua import ua_binary as uabin
17 1
from opcua.ua import status_codes
18 1
from opcua.ua import ObjectIds
19 1
from opcua.common.uaerrors import UaError
20
from opcua.common.uaerrors import UaStatusCodeError
21
from opcua.common.uaerrors import UaStringParsingError
22 1
23
24
logger = logging.getLogger('opcua.uaprotocol')
25
26 1
27
def get_win_epoch():
28
    return uabin.win_epoch_to_datetime(0)
29 1
30 1
31 1
class _FrozenClass(object):
32
33
    """
34 1
    make it impossible to add members to a class.
35
    This is a hack since I found out that most bugs are due to misspelling a variable in protocol
36
    """
37
    _freeze = False
38 1
39
    def __setattr__(self, key, value):
40
        if self._freeze and not hasattr(self, key):
41 1
            raise TypeError("Error adding member '{}' to class '{}', class is frozen, members are {}".format(
42
                key, self.__class__.__name__, self.__dict__.keys()))
43
        object.__setattr__(self, key, value)
44 1
45 1
if "PYOPCUA_NO_TYPO_CHECK" in os.environ:
46
    # typo check is cpu consuming, but it will make debug easy.
47
    # if typo check is not need (in production), please set env PYOPCUA_NO_TYPO_CHECK.
48
    # this will make all uatype class inherit from object intead of _FrozenClass
49 1
    # and skip the typo check.
50 1
    FrozenClass = object
51 1
else:
52 1
    FrozenClass = _FrozenClass
53 1
54
55
class ValueRank(IntEnum):
56 1
    """
57 1
    Defines dimensions of a variable.
58 1
    This enum does not support all cases since ValueRank support any n>0
59
    but since it is an IntEnum it can be replace by a normal int
60
    """
61
    ScalarOrOneDimension = -3
62
    Any = -2
63
    Scalar = -1
64
    OneOrMoreDimensions = 0
65
    OneDimension = 1
66 1
    # the next names are not in spec but so common we express them here
67
    TwoDimensions = 2
68
    ThreeDimensions = 3
69 1
    FourDimensions = 4
70
71
72
class _MaskEnum(IntEnum):
73 1
74 1
    @classmethod
75
    def parse_bitfield(cls, the_int):
76
        """ Take an integer and interpret it as a set of enum values. """
77 1
        assert isinstance(the_int, int)
78
79
        return {cls(b) for b in cls._bits(the_int)}
80 1
81
    @classmethod
82
    def to_bitfield(cls, collection):
83 1
        """ Takes some enum values and creates an integer from them. """
84 1
        # make sure all elements are of the correct type (use itertools.tee in case we get passed an
85 1
        # iterator)
86
        iter1, iter2 = itertools.tee(iter(collection))
87 1
        assert all(isinstance(x, cls) for x in iter1)
88
89
        return sum(x.mask for x in iter2)
90 1
91 1
    @property
92
    def mask(self):
93 1
        return 1 << self.value
94 1
95 1
    @staticmethod
96 1
    def _bits(n):
97 1
        """ Iterate over the bits in n.
98 1
99 1
            e.g. bits(44) yields at 2, 3, 5
100 1
        """
101 1
        assert n >= 0  # avoid infinite recursion
102 1
103
        pos = 0
104
        while n:
105 1
            if n & 0x1:
106 1
                yield pos
107 1
            n = n // 2
108
            pos += 1
109 1
110 1
111 1
class AccessLevel(_MaskEnum):
112 1
    """
113 1
    Bit index to indicate what the access level is.
114 1
115 1
    Spec Part 3, appears multiple times, e.g. paragraph 5.6.2 Variable NodeClass
116 1
    """
117 1
    CurrentRead = 0
118 1
    CurrentWrite = 1
119
    HistoryRead = 2
120
    HistoryWrite = 3
121
    SemanticChange = 4
122 1
    StatusWrite = 5
123 1
    TimestampWrite = 6
124
125 1
126
class WriteMask(_MaskEnum):
127 1
    """
128 1
    Bit index to indicate which attribute of a node is writable
129 1
130 1
    Spec Part 3, Paragraph 5.2.7 WriteMask
131 1
    """
132 1
    AccessLevel = 0
133 1
    ArrayDimensions = 1
134 1
    BrowseName = 2
135 1
    ContainsNoLoops = 3
136 1
    DataType = 4
137 1
    Description = 5
138 1
    DisplayName = 6
139 1
    EventNotifier = 7
140 1
    Executable = 8
141
    Historizing = 9
142 1
    InverseName = 10
143
    IsAbstract = 11
144
    MinimumSamplingInterval = 12
145
    NodeClass = 13
146
    NodeId = 14
147
    Symmetric = 15
148
    UserAccessLevel = 16
149
    UserExecutable = 17
150
    UserWriteMask = 18
151
    ValueRank = 19
152
    WriteMask = 20
153
    ValueForVariableType = 21
154
155
156
class EventNotifier(_MaskEnum):
157
    """
158
    Bit index to indicate how a node can be used for events.
159
160 1
    Spec Part 3, appears multiple times, e.g. Paragraph 5.4 View NodeClass
161 1
    """
162 1
    SubscribeToEvents = 0
163 1
    # Reserved        = 1
164 1
    HistoryRead = 2
165 1
    HistoryWrite = 3
166 1
167 1
168 1
class Guid(FrozenClass):
169 1
170 1
    def __init__(self):
171
        self.uuid = uuid.uuid4()
172
        self._freeze = True
173
174 1
    def to_binary(self):
175 1
        return self.uuid.bytes
176
177 1
    def __hash__(self):
178 1
        return hash(self.uuid.bytes)
179 1
180 1
    @staticmethod
181 1
    def from_binary(data):
182
        g = Guid()
183
        g.uuid = uuid.UUID(bytes=data.read(16))
184
        return g
185 1
186 1
    def __eq__(self, other):
187 1
        return isinstance(other, Guid) and self.uuid == other.uuid
188
189 1
190 1
class StatusCode(FrozenClass):
191 1
192 1
    """
193 1
    :ivar value:
194 1
    :vartype value: int
195
    :ivar name:
196 1
    :vartype name: string
197 1
    :ivar doc:
198
    :vartype doc: string
199 1
    """
200 1
201 1
    def __init__(self, value=0):
202 1
        if isinstance(value, str):
203
            self.name = value
204
            self.value = getattr(status_codes.StatusCodes, value)
205 1
        else:
206 1
            self.value = value
207 1
            self.name, self.doc = status_codes.get_name_and_doc(value)
208
        self._freeze = True
209
210 1
    def to_binary(self):
211 1
        return uabin.Primitives.UInt32.pack(self.value)
212 1
213
    @staticmethod
214
    def from_binary(data):
215 1
        val = uabin.Primitives.UInt32.unpack(data)
216 1
        sc = StatusCode(val)
217 1
        return sc
218 1
219 1
    def check(self):
220 1
        """
221 1
        Raises an exception if the status code is anything else than 0 (good).
222
223 1
        Use the is_good() method if you do not want an exception.
224
        """
225
        if self.value != 0:
226 1
            raise UaStatusCodeError(self.value)
227 1
228 1
    def is_good(self):
229 1
        """
230 1
        return True if status is Good.
231
        """
232
        if self.value == 0:
233 1
            return True
234 1
        return False
235 1
236 1
    def __str__(self):
237 1
        return 'StatusCode({})'.format(self.name)
238
    __repr__ = __str__
239
240 1
    def __eq__(self, other):
241
        return self.value == other.value
242
243 1
    def __ne__(self, other):
244
        return not self.__eq__(other)
245
246 1
247 1
class NodeIdType(Enum):
248 1
    TwoByte = 0
249
    FourByte = 1
250
    Numeric = 2
251 1
    String = 3
252 1
    Guid = 4
253 1
    ByteString = 5
254
255
256 1
class NodeId(FrozenClass):
257 1
258 1
    """
259
    NodeId Object
260
261 1
    Args:
262
        identifier: The identifier might be an int, a string, bytes or a Guid
263
        namespaceidx(int): The index of the namespace
264
        nodeidtype(NodeIdType): The type of the nodeid if it cannor be guess or you want something special like twobyte nodeid or fourbytenodeid
265
266
267 1
    :ivar Identifier:
268
    :vartype Identifier: NodeId
269 1
    :ivar NamespaceIndex:
270 1
    :vartype NamespaceIndex: Int
271
    :ivar NamespaceUri:
272 1
    :vartype NamespaceUri: String
273
    :ivar ServerIndex:
274 1
    :vartype ServerIndex: Int
275
    """
276
277
    def __init__(self, identifier=None, namespaceidx=0, nodeidtype=None):
278
        self.Identifier = identifier
279
        self.NamespaceIndex = namespaceidx
280
        self.NodeIdType = nodeidtype
281 1
        self.NamespaceUri = ""
282
        self.ServerIndex = 0
283
        self._freeze = True
284 1
        if not isinstance(self.NamespaceIndex, int):
285
            raise UaError("NamespaceIndex must be an int")
286
        if self.Identifier is None:
287
            self.Identifier = 0
288
            self.NodeIdType = NodeIdType.TwoByte
289
            return
290 1
        if self.NodeIdType is None:
291 1
            if isinstance(self.Identifier, int):
292 1
                self.NodeIdType = NodeIdType.Numeric
293 1
            elif isinstance(self.Identifier, str):
294 1
                self.NodeIdType = NodeIdType.String
295
            elif isinstance(self.Identifier, bytes):
296 1
                self.NodeIdType = NodeIdType.ByteString
297 1
            else:
298 1
                raise UaError("NodeId: Could not guess type of NodeId, set NodeIdType")
299
300
    def __key(self):
301 1
        if self.NodeIdType in (NodeIdType.TwoByte, NodeIdType.FourByte, NodeIdType.Numeric):  # twobyte, fourbyte and numeric may represent the same node
302
            return self.NamespaceIndex, self.Identifier
303
        else:
304 1
            return self.NodeIdType, self.NamespaceIndex, self.Identifier
305 1
306 1
    def __eq__(self, node):
307 1
        return isinstance(node, NodeId) and self.__key() == node.__key()
308 1
309
    def __ne__(self, other):
310
        return not self.__eq__(other)
311 1
312
    def __hash__(self):
313
        return hash(self.__key())
314
315 1
    def is_null(self):
316 1
        if self.NamespaceIndex != 0:
317 1
            return False
318 1
        return self.has_null_identifier()
319 1
320
    def has_null_identifier(self):
321
        if not self.Identifier:
322 1
            return True
323
        if self.NodeIdType == NodeIdType.Guid and re.match(b'0.', self.Identifier):
324 1
            return True
325 1
        return False
326 1
327
    @staticmethod
328 1
    def from_string(string):
329 1
        try:
330
            return NodeId._from_string(string)
331 1
        except ValueError as ex:
332
            raise UaStringParsingError("Error parsing string {}".format(string), ex)
333
334 1
    @staticmethod
335
    def _from_string(string):
336 1
        l = string.split(";")
337 1
        identifier = None
338 1
        namespace = 0
339
        ntype = None
340 1
        srv = None
341 1
        nsu = None
342
        for el in l:
343
            if not el:
344 1
                continue
345
            k, v = el.split("=", 1)
346
            k = k.strip()
347
            v = v.strip()
348
            if k == "ns":
349
                namespace = int(v)
350
            elif k == "i":
351
                ntype = NodeIdType.Numeric
352
                identifier = int(v)
353
            elif k == "s":
354
                ntype = NodeIdType.String
355 1
                identifier = v
356 1
            elif k == "g":
357 1
                ntype = NodeIdType.Guid
358 1
                identifier = v
359
            elif k == "b":
360 1
                ntype = NodeIdType.ByteString
361 1
                identifier = v
362
            elif k == "srv":
363 1
                srv = v
364
            elif k == "nsu":
365 1
                nsu = v
366 1
        if identifier is None:
367 1
            raise UaStringParsingError("Could not find identifier in string: " + string)
368
        nodeid = NodeId(identifier, namespace, ntype)
369 1
        nodeid.NamespaceUri = nsu
370
        nodeid.ServerIndex = srv
371
        return nodeid
372
373
    def to_string(self):
374 1
        string = ""
375 1
        if self.NamespaceIndex != 0:
376
            string += "ns={};".format(self.NamespaceIndex)
377 1
        ntype = None
378
        if self.NodeIdType == NodeIdType.Numeric:
379
            ntype = "i"
380
        elif self.NodeIdType == NodeIdType.String:
381 1
            ntype = "s"
382 1
        elif self.NodeIdType == NodeIdType.TwoByte:
383 1
            ntype = "i"
384
        elif self.NodeIdType == NodeIdType.FourByte:
385 1
            ntype = "i"
386
        elif self.NodeIdType == NodeIdType.Guid:
387 1
            ntype = "g"
388
        elif self.NodeIdType == NodeIdType.ByteString:
389 1
            ntype = "b"
390
        string += "{}={}".format(ntype, self.Identifier)
391
        if self.ServerIndex:
392 1
            string = "srv=" + str(self.ServerIndex) + string
393
        if self.NamespaceUri:
394
            string += "nsu={}".format(self.NamespaceUri)
395
        return string
396 1
397 1
    def __str__(self):
398 1
        return "{}NodeId({})".format(self.NodeIdType.name, self.to_string())
399 1
    __repr__ = __str__
400 1
401 1
    def to_binary(self):
402 1
        if self.NodeIdType == NodeIdType.TwoByte:
403
            return struct.pack("<BB", self.NodeIdType.value, self.Identifier)
404
        elif self.NodeIdType == NodeIdType.FourByte:
405 1
            return struct.pack("<BBH", self.NodeIdType.value, self.NamespaceIndex, self.Identifier)
406
        elif self.NodeIdType == NodeIdType.Numeric:
407
            return struct.pack("<BHI", self.NodeIdType.value, self.NamespaceIndex, self.Identifier)
408
        elif self.NodeIdType == NodeIdType.String:
409
            return struct.pack("<BH", self.NodeIdType.value, self.NamespaceIndex) + \
410
                uabin.Primitives.String.pack(self.Identifier)
411
        elif self.NodeIdType == NodeIdType.ByteString:
412
            return struct.pack("<BH", self.NodeIdType.value, self.NamespaceIndex) + \
413
                uabin.Primitives.Bytes.pack(self.Identifier)
414
        else:
415
            return struct.pack("<BH", self.NodeIdType.value, self.NamespaceIndex) + \
416
                self.Identifier.to_binary()
417
        # FIXME: Missing NNamespaceURI and ServerIndex
418
419
    @staticmethod
420
    def from_binary(data):
421
        nid = NodeId()
422
        encoding = ord(data.read(1))
423
        nid.NodeIdType = NodeIdType(encoding & 0b00111111)
424
425
        if nid.NodeIdType == NodeIdType.TwoByte:
426 1
            nid.Identifier = ord(data.read(1))
427 1
        elif nid.NodeIdType == NodeIdType.FourByte:
428 1
            nid.NamespaceIndex, nid.Identifier = struct.unpack("<BH", data.read(3))
429 1
        elif nid.NodeIdType == NodeIdType.Numeric:
430 1
            nid.NamespaceIndex, nid.Identifier = struct.unpack("<HI", data.read(6))
431 1
        elif nid.NodeIdType == NodeIdType.String:
432 1
            nid.NamespaceIndex = uabin.Primitives.UInt16.unpack(data)
433 1
            nid.Identifier = uabin.Primitives.String.unpack(data)
434 1
        elif nid.NodeIdType == NodeIdType.ByteString:
435 1
            nid.NamespaceIndex = uabin.Primitives.UInt16.unpack(data)
436 1
            nid.Identifier = uabin.Primitives.Bytes.unpack(data)
437 1
        elif nid.NodeIdType == NodeIdType.Guid:
438 1
            nid.NamespaceIndex = uabin.Primitives.UInt16.unpack(data)
439 1
            nid.Identifier = Guid.from_binary(data)
440 1
        else:
441 1
            raise UaError("Unknown NodeId encoding: " + str(nid.NodeIdType))
442 1
443 1
        if uabin.test_bit(encoding, 7):
444
            nid.NamespaceUri = uabin.Primitives.String.unpack(data)
445
        if uabin.test_bit(encoding, 6):
446
            nid.ServerIndex = uabin.Primitives.UInt32.unpack(data)
447
448
        return nid
449 1
450 1
451 1
class TwoByteNodeId(NodeId):
452
453 1
    def __init__(self, identifier):
454
        NodeId.__init__(self, identifier, 0, NodeIdType.TwoByte)
455 1
456 1
457
class FourByteNodeId(NodeId):
458 1
459 1
    def __init__(self, identifier, namespace=0):
460
        NodeId.__init__(self, identifier, namespace, NodeIdType.FourByte)
461 1
462 1
463
class NumericNodeId(NodeId):
464 1
465 1
    def __init__(self, identifier, namespace=0):
466 1
        NodeId.__init__(self, identifier, namespace, NodeIdType.Numeric)
467 1
468
469 1
class ByteStringNodeId(NodeId):
470 1
471 1
    def __init__(self, identifier, namespace=0):
472 1
        NodeId.__init__(self, identifier, namespace, NodeIdType.ByteString)
473 1
474 1
475
class GuidNodeId(NodeId):
476 1
477
    def __init__(self, identifier, namespace=0):
478 1
        NodeId.__init__(self, identifier, namespace, NodeIdType.Guid)
479 1
480 1
481 1
class StringNodeId(NodeId):
482
483 1
    def __init__(self, identifier, namespace=0):
484
        NodeId.__init__(self, identifier, namespace, NodeIdType.String)
485 1
486 1
487 1
ExpandedNodeId = NodeId
488 1
489 1
490 1
class QualifiedName(FrozenClass):
491 1
492 1
    '''
493 1
    A string qualified with a namespace index.
494 1
    '''
495 1
496 1
    def __init__(self, name=None, namespaceidx=0):
497 1
        if not isinstance(namespaceidx, int):
498 1
            raise UaError("namespaceidx must be an int")
499 1
        self.NamespaceIndex = namespaceidx
500 1
        self.Name = name
501 1
        self._freeze = True
502 1
503 1
    def to_string(self):
504 1
        return "{}:{}".format(self.NamespaceIndex, self.Name)
505 1
506
    @staticmethod
507
    def from_string(string):
508 1
        if ":" in string:
509
            try:
510
                idx, name = string.split(":", 1)
511 1
                idx = int(idx)
512 1
            except (TypeError, ValueError) as ex:
513
                raise UaStringParsingError("Error parsing string {}".format(string), ex)
514
        else:
515 1
            idx = 0
516 1
            name = string
517 1
        return QualifiedName(name, idx)
518 1
519 1
    def to_binary(self):
520 1
        packet = []
521
        packet.append(uabin.Primitives.UInt16.pack(self.NamespaceIndex))
522 1
        packet.append(uabin.Primitives.String.pack(self.Name))
523 1
        return b''.join(packet)
524 1
525 1
    @staticmethod
526 1
    def from_binary(data):
527 1
        obj = QualifiedName()
528 1
        obj.NamespaceIndex = uabin.Primitives.UInt16.unpack(data)
529 1
        obj.Name = uabin.Primitives.String.unpack(data)
530 1
        return obj
531 1
532 1
    def __eq__(self, bname):
533 1
        return isinstance(bname, QualifiedName) and self.Name == bname.Name and self.NamespaceIndex == bname.NamespaceIndex
534 1
535
    def __ne__(self, other):
536
        return not self.__eq__(other)
537
538
    def __lt__(self, other):
539 1
        if not isinstance(other, QualifiedName):
540 1
            raise TypeError("Cannot compare QualifiedName and {}".format(other))
541
        if self.NamespaceIndex == other.NamespaceIndex:
542 1
            return self.Name < other.Name
543
        else:
544 1
            return self.NamespaceIndex < other.NamespaceIndex
545
546 1
    def __str__(self):
547 1
        return 'QualifiedName({}:{})'.format(self.NamespaceIndex, self.Name)
548 1
549
    __repr__ = __str__
550 1
551 1
552 1
class LocalizedText(FrozenClass):
553 1
554 1
    '''
555 1
    A string qualified with a namespace index.
556 1
    '''
557 1
558 1
    def __init__(self, text=None):
559
        self.Encoding = 0
560 1
        self.Text = text
561 1
        if isinstance(self.Text, unicode):
562
            self.Text = self.Text.encode('utf-8')
563
        if self.Text:
564 1
            self.Encoding |= (1 << 1)
565
        self.Locale = None
566
        self._freeze = True
567
568 1
    def to_binary(self):
569
        packet = []
570 1
        if self.Locale:
571 1
            self.Encoding |= (1 << 0)
572 1
        if self.Text:
573
            self.Encoding |= (1 << 1)
574 1
        packet.append(uabin.Primitives.UInt8.pack(self.Encoding))
575 1
        if self.Locale:
576 1
            packet.append(uabin.Primitives.Bytes.pack(self.Locale))
577 1
        if self.Text:
578 1
            packet.append(uabin.Primitives.Bytes.pack(self.Text))
579 1
        return b''.join(packet)
580 1
581 1
    @staticmethod
582 1
    def from_binary(data):
583 1
        obj = LocalizedText()
584 1
        obj.Encoding = ord(data.read(1))
585 1
        if obj.Encoding & (1 << 0):
586 1
            obj.Locale = uabin.Primitives.Bytes.unpack(data)
587 1
        if obj.Encoding & (1 << 1):
588 1
            obj.Text = uabin.Primitives.Bytes.unpack(data)
589
        return obj
590
591
    def to_string(self):
592 1
        # FIXME: use local
593
        if self.Text is None:
594 1
            return ""
595
        return self.Text.decode('utf-8')
596
597 1
    def __str__(self):
598
        return 'LocalizedText(' + 'Encoding:' + str(self.Encoding) + ', ' + \
599
            'Locale:' + str(self.Locale) + ', ' + \
600 1
            'Text:' + str(self.Text) + ')'
601
    __repr__ = __str__
602 1
603 1
    def __eq__(self, other):
604
        if isinstance(other, LocalizedText) and self.Locale == other.Locale and self.Text == other.Text:
605
            return True
606 1
        return False
607
608 1
    def __ne__(self, other):
609 1
        return not self.__eq__(other)
610
611
612 1
class ExtensionObject(FrozenClass):
613
614 1
    '''
615 1
616
    Any UA object packed as an ExtensionObject
617
618 1
619
    :ivar TypeId:
620 1
    :vartype TypeId: NodeId
621 1
    :ivar Body:
622
    :vartype Body: bytes
623
624 1
    '''
625
626 1
    def __init__(self):
627 1
        self.TypeId = NodeId()
628
        self.Encoding = 0
629
        self.Body = b''
630 1
        self._freeze = True
631
632 1
    def to_binary(self):
633 1
        packet = []
634
        if self.Body:
635
            self.Encoding |= (1 << 0)
636 1
        packet.append(self.TypeId.to_binary())
637
        packet.append(uabin.Primitives.UInt8.pack(self.Encoding))
638
        if self.Body:
639 1
            packet.append(uabin.Primitives.ByteString.pack(self.Body))
640
        return b''.join(packet)
641
642
    @staticmethod
643
    def from_binary(data):
644
        obj = ExtensionObject()
645 1
        obj.TypeId = NodeId.from_binary(data)
646 1
        obj.Encoding = uabin.Primitives.UInt8.unpack(data)
647
        if obj.Encoding & (1 << 0):
648 1
            obj.Body = uabin.Primitives.ByteString.unpack(data)
649 1
        return obj
650 1
651
    @staticmethod
652 1
    def from_object(obj):
653 1
        ext = ExtensionObject()
654
        oid = getattr(ObjectIds, "{}_Encoding_DefaultBinary".format(obj.__class__.__name__))
655 1
        ext.TypeId = FourByteNodeId(oid)
656
        ext.Body = obj.to_binary()
657 1
        return ext
658 1
659 1
    def __str__(self):
660 1
        return 'ExtensionObject(' + 'TypeId:' + str(self.TypeId) + ', ' + \
661 1
            'Encoding:' + str(self.Encoding) + ', ' + str(len(self.Body)) + ' bytes)'
662 1
663
    __repr__ = __str__
664 1
665 1
666 1
class VariantType(Enum):
667
668 1
    '''
669 1
    The possible types of a variant.
670 1
671 1
    :ivar Null:
672 1
    :ivar Boolean:
673
    :ivar SByte:
674 1
    :ivar Byte:
675
    :ivar Int16:
676 1
    :ivar UInt16:
677 1
    :ivar Int32:
678 1
    :ivar UInt32:
679 1
    :ivar Int64:
680
    :ivar UInt64:
681 1
    :ivar Float:
682 1
    :ivar Double:
683
    :ivar String:
684 1
    :ivar DateTime:
685
    :ivar Guid:
686
    :ivar ByteString:
687 1
    :ivar XmlElement:
688
    :ivar NodeId:
689
    :ivar ExpandedNodeId:
690 1
    :ivar StatusCode:
691
    :ivar QualifiedName:
692
    :ivar LocalizedText:
693 1
    :ivar ExtensionObject:
694
    :ivar DataValue:
695
    :ivar Variant:
696
    :ivar DiagnosticInfo:
697
698
699 1
700 1
    '''
701 1
    Null = 0
702 1
    Boolean = 1
703 1
    SByte = 2
704 1
    Byte = 3
705 1
    Int16 = 4
706 1
    UInt16 = 5
707 1
    Int32 = 6
708
    UInt32 = 7
709 1
    Int64 = 8
710 1
    UInt64 = 9
711 1
    Float = 10
712
    Double = 11
713 1
    String = 12
714 1
    DateTime = 13
715 1
    Guid = 14
716 1
    ByteString = 15
717
    XmlElement = 16
718 1
    NodeId = 17
719 1
    ExpandedNodeId = 18
720 1
    StatusCode = 19
721
    QualifiedName = 20
722 1
    LocalizedText = 21
723
    ExtensionObject = 22
724 1
    DataValue = 23
725 1
    Variant = 24
726 1
    DiagnosticInfo = 25
727
728 1
729 1
class VariantTypeCustom(object):
730 1
    """
731
    Looks like sometime we get variant with other values than those
732 1
    defined in VariantType.
733
    FIXME: We should not need this class, as far as I iunderstand the spec
734
    variants can only be of VariantType
735
    """
736 1
737
    def __init__(self, val):
738
        self.name = "Custom"
739
        self.value = val
740 1
        if self.value > 0b00111111:
741
            raise UaError("Cannot create VariantType. VariantType must be {} > x > {}, received {}".format(0b111111, 25, val))
742 1
743 1
    def __str__(self):
744 1
        return "VariantType.Custom:{}".format(self.value)
745 1
    __repr__ = __str__
746
747 1
    def __eq__(self, other):
748 1
        return self.value == other.value
749
750
751 1
class Variant(FrozenClass):
752
753
    """
754
    Create an OPC-UA Variant object.
755
    if no argument a Null Variant is created.
756
    if not variant type is given, attemps to guess type from python type
757
    if a variant is given as value, the new objects becomes a copy of the argument
758
759
    :ivar Value:
760
    :vartype Value: Any supported type
761
    :ivar VariantType:
762
    :vartype VariantType: VariantType
763
    """
764
765 1
    def __init__(self, value=None, varianttype=None, dimensions=None):
766 1
        self.Value = value
767 1
        self.VariantType = varianttype
768 1
        self.Dimensions = dimensions
769 1
        self._freeze = True
770
        if isinstance(value, Variant):
771 1
            self.Value = value.Value
772 1
            self.VariantType = value.VariantType
773 1
        if self.VariantType is None:
774 1
            self.VariantType = self._guess_type(self.Value)
775 1
        if self.Dimensions is None and type(self.Value) in (list, tuple):
776 1
            dims = get_shape(self.Value)
777 1
            if len(dims) > 1:
778 1
                self.Dimensions = dims
779 1
780
    def __eq__(self, other):
781 1
        if isinstance(other, Variant) and self.VariantType == other.VariantType and self.Value == other.Value:
782
            return True
783
        return False
784
785
    def __ne__(self, other):
786
        return not self.__eq__(other)
787
788
    def _guess_type(self, val):
789
        if isinstance(val, (list, tuple)):
790 1
            error_val = val
791
        while isinstance(val, (list, tuple)):
792
            if len(val) == 0:
793
                raise UaError("could not guess UA type of variable {}".format(error_val))
794
            val = val[0]
795
        if val is None:
796
            return VariantType.Null
797
        elif isinstance(val, bool):
798 1
            return VariantType.Boolean
799
        elif isinstance(val, float):
800
            return VariantType.Double
801
        elif isinstance(val, int):
802 1
            return VariantType.Int64
803
        elif type(val) in (str, unicode):
804
            return VariantType.String
805 1 View Code Duplication
        elif isinstance(val, bytes):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
806
            return VariantType.ByteString
807
        elif isinstance(val, datetime):
808
            return VariantType.DateTime
809
        else:
810
            if isinstance(val, object):
811
                try:
812
                    return getattr(VariantType, val.__class__.__name__)
813
                except AttributeError:
814
                    return VariantType.ExtensionObject
815
            else:
816
                raise UaError("Could not guess UA type of {} with type {}, specify UA type".format(val, type(val)))
817
818
    def __str__(self):
819
        return "Variant(val:{!s},type:{})".format(self.Value, self.VariantType)
820
    __repr__ = __str__
821
822
    def to_binary(self):
823
        b = []
824
        encoding = self.VariantType.value & 0b111111
825
        if type(self.Value) in (list, tuple):
826
            if self.Dimensions is not None:
827
                encoding = uabin.set_bit(encoding, 6)
828
            encoding = uabin.set_bit(encoding, 7)
829
            b.append(uabin.Primitives.UInt8.pack(encoding))
830
            b.append(uabin.pack_uatype_array(self.VariantType, flatten(self.Value)))
831
            if self.Dimensions is not None:
832
                b.append(uabin.pack_uatype_array(VariantType.Int32, self.Dimensions))
833
        else:
834
            b.append(uabin.Primitives.UInt8.pack(encoding))
835
            b.append(uabin.pack_uatype(self.VariantType, self.Value))
836
837
        return b"".join(b)
838
839
    @staticmethod
840 1
    def from_binary(data):
841 1
        dimensions = None
842 1
        encoding = ord(data.read(1))
843 1
        int_type = encoding & 0b00111111
844 1
        vtype = DataType_to_VariantType(int_type)
845 1
        if vtype == VariantType.Null:
846 1
            return Variant(None, vtype, encoding)
847 1
        if uabin.test_bit(encoding, 7):
848 1
            value = uabin.unpack_uatype_array(vtype, data)
849 1
        else:
850 1
            value = uabin.unpack_uatype(vtype, data)
851 1
        if uabin.test_bit(encoding, 6):
852 1
            dimensions = uabin.unpack_uatype_array(VariantType.Int32, data)
853 1
            value = reshape(value, dimensions)
854 1
855 1
        return Variant(value, vtype, dimensions)
856 1
857 1
858 1
def reshape(flat, dims):
859 1
    subdims = dims[1:]
860 1
    subsize = 1
861 1
    for i in subdims:
862 1
        if i == 0:
863 1
            i = 1
864 1
        subsize *= i
865 1
    while dims[0] * subsize > len(flat):
866
        flat.append([])
867
    if not subdims or subdims == [0]:
868 1
        return flat
869 1
    return [reshape(flat[i: i + subsize], subdims) for i in range(0, len(flat), subsize)]
870 1
871 1
872 1
def _split_list(l, n):
873 1
    n = max(1, n)
874
    return [l[i:i + n] for i in range(0, len(l), n)]
875 1
876
877 1
def flatten_and_get_shape(mylist):
878
    dims = []
879 1
    dims.append(len(mylist))
880 1
    while isinstance(mylist[0], (list, tuple)):
881
        dims.append(len(mylist[0]))
882
        mylist = [item for sublist in mylist for item in sublist]
883 1
        if len(mylist) == 0:
884
            break
885
    return mylist, dims
886
887
888
def flatten(mylist):
889
    if len(mylist) == 0:
890
        return mylist
891
    while isinstance(mylist[0], (list, tuple)):
892
        mylist = [item for sublist in mylist for item in sublist]
893
        if len(mylist) == 0:
894
            break
895
    return mylist
896
897 1
898 1
def get_shape(mylist):
899 1
    dims = []
900 1
    while isinstance(mylist, (list, tuple)):
901 1
        dims.append(len(mylist))
902 1
        if len(mylist) == 0:
903 1
            break
904 1
        mylist = mylist[0]
905 1
    return dims
906 1
907 1
908 1
class XmlElement(FrozenClass):
909 1
    '''
910 1
    An XML element encoded as an UTF-8 string.
911
    '''
912 1
913 1
    def __init__(self, binary=None):
914 1
        if binary is not None:
915 1
            self._binary_init(binary)
916
            self._freeze = True
917 1
            return
918 1
        self.Value = []
919
        self._freeze = True
920 1
921 1
    def to_binary(self):
922 1
        return uabin.Primitives.String.pack(self.Value)
923 1
924 1
    @staticmethod
925
    def from_binary(data):
926 1
        return XmlElement(data)
927 1
928 1
    def _binary_init(self, data):
929 1
        self.Value = uabin.Primitives.String.unpack(data)
930 1
931 1
    def __str__(self):
932 1
        return 'XmlElement(Value:' + str(self.Value) + ')'
933 1
934 1
    __repr__ = __str__
935 1
936 1
937 1
class DataValue(FrozenClass):
938 1
939 1
    '''
940 1
    A value with an associated timestamp, and quality.
941
    Automatically generated from xml , copied and modified here to fix errors in xml spec
942 1
943 1
    :ivar Value:
944 1
    :vartype Value: Variant
945 1
    :ivar StatusCode:
946 1
    :vartype StatusCode: StatusCode
947
    :ivar SourceTimestamp:
948
    :vartype SourceTimestamp: datetime
949
    :ivar SourcePicoSeconds:
950 1
    :vartype SourcePicoSeconds: int
951 1
    :ivar ServerTimestamp:
952 1
    :vartype ServerTimestamp: datetime
953
    :ivar ServerPicoseconds:
954 1
    :vartype ServerPicoseconds: int
955 1
956 1
    '''
957 1
958 1
    def __init__(self, variant=None, status=None):
959 1
        self.Encoding = 0
960 1
        if not isinstance(variant, Variant):
961 1
            variant = Variant(variant)
962 1
        self.Value = variant
963 1
        if status is None:
964 1
            self.StatusCode = StatusCode()
965
        else:
966 1
            self.StatusCode = status
967 1
        self.SourceTimestamp = None  # DateTime()
968
        self.SourcePicoseconds = None
969 1
        self.ServerTimestamp = None  # DateTime()
970
        self.ServerPicoseconds = None
971 1
        self._freeze = True
972
973 1
    def to_binary(self):
974 1
        packet = []
975 1
        if self.Value:
976 1
            self.Encoding |= (1 << 0)
977 1
        if self.StatusCode:
978
            self.Encoding |= (1 << 1)
979 1
        if self.SourceTimestamp:
980 1
            self.Encoding |= (1 << 2)
981 1
        if self.ServerTimestamp:
982 1
            self.Encoding |= (1 << 3)
983 1
        if self.SourcePicoseconds:
984
            self.Encoding |= (1 << 4)
985 1
        if self.ServerPicoseconds:
986 1
            self.Encoding |= (1 << 5)
987 1
        packet.append(uabin.Primitives.UInt8.pack(self.Encoding))
988 1
        if self.Value:
989
            packet.append(self.Value.to_binary())
990 1
        if self.StatusCode:
991
            packet.append(self.StatusCode.to_binary())
992
        if self.SourceTimestamp:
993 1
            packet.append(uabin.Primitives.DateTime.pack(self.SourceTimestamp))  # self.SourceTimestamp.to_binary())
994 1
        if self.ServerTimestamp:
995 1
            packet.append(uabin.Primitives.DateTime.pack(self.ServerTimestamp))  # self.ServerTimestamp.to_binary())
996 1
        if self.SourcePicoseconds:
997 1
            packet.append(uabin.Primitives.UInt16.pack(self.SourcePicoseconds))
998 1
        if self.ServerPicoseconds:
999 1
            packet.append(uabin.Primitives.UInt16.pack(self.ServerPicoseconds))
1000 1
        return b''.join(packet)
1001 1
1002 1
    @staticmethod
1003 1
    def from_binary(data):
1004 1
        encoding = ord(data.read(1))
1005
        if encoding & (1 << 0):
1006
            value = Variant.from_binary(data)
1007 1
        else:
1008
            value = None
1009
        if encoding & (1 << 1):
1010
            status = StatusCode.from_binary(data)
1011
        else:
1012 1
            status = None
1013
        obj = DataValue(value, status)
1014
        obj.Encoding = encoding
1015
        if obj.Encoding & (1 << 2):
1016
            obj.SourceTimestamp = uabin.Primitives.DateTime.unpack(data)  # DateTime.from_binary(data)
1017
        if obj.Encoding & (1 << 3):
1018
            obj.ServerTimestamp = uabin.Primitives.DateTime.unpack(data)  # DateTime.from_binary(data)
1019
        if obj.Encoding & (1 << 4):
1020
            obj.SourcePicoseconds = uabin.Primitives.UInt16.unpack(data)
1021
        if obj.Encoding & (1 << 5):
1022
            obj.ServerPicoseconds = uabin.Primitives.UInt16.unpack(data)
1023 1
        return obj
1024 1
1025 1
    def __str__(self):
1026 1
        s = 'DataValue(Value:{}'.format(self.Value)
1027 1
        if self.StatusCode is not None:
1028 1
            s += ', StatusCode:{}'.format(self.StatusCode)
1029 1
        if self.SourceTimestamp is not None:
1030 1
            s += ', SourceTimestamp:{}'.format(self.SourceTimestamp)
1031
        if self.ServerTimestamp is not None:
1032
            s += ', ServerTimestamp:{}'.format(self.ServerTimestamp)
1033 1
        if self.SourcePicoseconds is not None:
1034 1
            s += ', SourcePicoseconds:{}'.format(self.SourcePicoseconds)
1035 1
        if self.ServerPicoseconds is not None:
1036 1
            s += ', ServerPicoseconds:{}'.format(self.ServerPicoseconds)
1037 1
        s += ')'
1038 1
        return s
1039 1
1040 1
    __repr__ = __str__
1041
1042
1043 1
def DataType_to_VariantType(int_type):
1044
    """
1045
    Takes a NodeId or int and return a VariantType
1046
    This is only supported if int_type < 63 due to VariantType encoding
1047 1
    At low level we do not have access to address space thus decodig is limited
1048
    a better version of this method can be find in ua_utils.py
1049
    """
1050
    if isinstance(int_type, NodeId):
1051
        int_type = int_type.Identifier
1052
1053
    if int_type <= 25:
1054
        return VariantType(int_type)
1055 1
    else:
1056
        return VariantTypeCustom(int_type)
1057