Commit dee4543e authored by olivier R-D's avatar olivier R-D

fix NodeId ordering

parent 1f3680bb
......@@ -221,7 +221,7 @@ class StatusCode(FrozenClass):
return not self.__eq__(other)
class NodeIdType(Enum):
class NodeIdType(IntEnum):
TwoByte = 0
FourByte = 1
Numeric = 2
......@@ -271,28 +271,30 @@ class NodeId(FrozenClass):
self.NodeIdType = NodeIdType.String
elif isinstance(self.Identifier, bytes):
self.NodeIdType = NodeIdType.ByteString
elif isinstance(self.Identifier, uuid.UUID):
self.NodeIdType = NodeIdType.Guid
else:
raise UaError("NodeId: Could not guess type of NodeId, set NodeIdType")
def __key(self):
if self.NodeIdType in (NodeIdType.TwoByte, NodeIdType.FourByte, NodeIdType.Numeric): # twobyte, fourbyte and numeric may represent the same node
return self.NamespaceIndex, self.Identifier
else:
return self.NodeIdType, self.NamespaceIndex, self.Identifier
def _key(self):
if self.NodeIdType in (NodeIdType.TwoByte, NodeIdType.FourByte, NodeIdType.Numeric):
# twobyte, fourbyte and numeric may represent the same node
return (NodeIdType.Numeric, self.NamespaceIndex, self.Identifier)
return (self.NodeIdType, self.NamespaceIndex, self.Identifier)
def __eq__(self, node):
return isinstance(node, NodeId) and self.__key() == node.__key()
return isinstance(node, NodeId) and self._key() == node._key()
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash(self.__key())
return hash(self._key())
def __lt__(self, other):
if not isinstance(other, NodeId):
raise AttributeError("Can only compare to NodeId")
return self.__hash__() < other.__hash__()
return self._key() < other._key()
def is_null(self):
if self.NamespaceIndex != 0:
......
......@@ -24,6 +24,23 @@ class TestUnit(unittest.TestCase):
Simple unit test that do not need to setup a server or a client
'''
def test_nodeid_ordering(self):
a = ua.NodeId(2000, 1)
b = ua.NodeId(3000, 1)
c = ua.NodeId(20, 0)
d = ua.NodeId("tititu", 1)
e = ua.NodeId("aaaaa", 1)
f = ua.NodeId("aaaaa", 2)
g = ua.NodeId(uuid.uuid4(), 1)
h = ua.TwoByteNodeId(2001)
i = ua.NodeId(b"lkjkl", 1)
j = ua.NodeId(b"aaa", 5)
mylist = [a, b, c, d, e, f, g, h, i, j]
mylist.sort()
expected = [c, h, a, b, e, d, f, g, i, j]
self.assertEqual(mylist, expected)
def test_string_to_variant_int(self):
s_arr_uint = "[1, 2, 3, 4]"
arr_uint = [1, 2, 3, 4]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment