|
20 | 20 | from uprotocol.uri.factory.uri_factory import UriFactory |
21 | 21 | from uprotocol.v1.uattributes_pb2 import ( |
22 | 22 | UAttributes, |
23 | | - UPayloadFormat, |
24 | 23 | UPriority, |
25 | 24 | ) |
26 | 25 | from uprotocol.v1.ucode_pb2 import UCode |
27 | 26 | from uprotocol.v1.uri_pb2 import UUri |
28 | 27 | from uprotocol.v1.ustatus_pb2 import UStatus |
29 | | -from zenoh import Encoding, Priority |
30 | | -from zenoh.value import Attachment |
| 28 | +from zenoh import Priority, ZBytes |
31 | 29 |
|
32 | 30 | UATTRIBUTE_VERSION: int = 1 |
33 | 31 |
|
@@ -72,77 +70,71 @@ def get_uauth_from_uuri(uri: UUri) -> Union[str, UStatus]: |
72 | 70 | @staticmethod |
73 | 71 | def to_zenoh_key_string(authority_name: str, src_uri: UUri, dst_uri: UUri = None) -> str: |
74 | 72 | src = ZenohUtils.uri_to_zenoh_key(authority_name, src_uri) |
75 | | - dst = ZenohUtils.uri_to_zenoh_key(authority_name, dst_uri) if dst_uri else "{}/{}/{}/{}" |
| 73 | + dst = ZenohUtils.uri_to_zenoh_key(authority_name, dst_uri) if dst_uri and dst_uri != UUri() else "{}/{}/{}/{}" |
76 | 74 | return f"up/{src}/{dst}" |
77 | 75 |
|
78 | 76 | @staticmethod |
79 | 77 | def map_zenoh_priority(upriority: UPriority) -> Priority: |
80 | 78 | mapping = { |
81 | | - UPriority.UPRIORITY_CS0: Priority.BACKGROUND(), |
82 | | - UPriority.UPRIORITY_CS1: Priority.DATA_LOW(), |
83 | | - UPriority.UPRIORITY_CS2: Priority.DATA(), |
84 | | - UPriority.UPRIORITY_CS3: Priority.DATA_HIGH(), |
85 | | - UPriority.UPRIORITY_CS4: Priority.INTERACTIVE_LOW(), |
86 | | - UPriority.UPRIORITY_CS5: Priority.INTERACTIVE_HIGH(), |
87 | | - UPriority.UPRIORITY_CS6: Priority.REAL_TIME(), |
88 | | - UPriority.UPRIORITY_UNSPECIFIED: Priority.DATA_LOW(), |
| 79 | + UPriority.UPRIORITY_CS0: Priority.BACKGROUND, |
| 80 | + UPriority.UPRIORITY_CS1: Priority.DATA_LOW, |
| 81 | + UPriority.UPRIORITY_CS2: Priority.DATA, |
| 82 | + UPriority.UPRIORITY_CS3: Priority.DATA_HIGH, |
| 83 | + UPriority.UPRIORITY_CS4: Priority.INTERACTIVE_LOW, |
| 84 | + UPriority.UPRIORITY_CS5: Priority.INTERACTIVE_HIGH, |
| 85 | + UPriority.UPRIORITY_CS6: Priority.REAL_TIME, |
| 86 | + UPriority.UPRIORITY_UNSPECIFIED: Priority.DATA_LOW, |
89 | 87 | } |
90 | 88 | return mapping[upriority] |
91 | 89 |
|
92 | | - @staticmethod |
93 | | - def to_upayload_format(encoding: Encoding) -> UPayloadFormat: |
94 | | - try: |
95 | | - value = int(encoding.suffix) |
96 | | - return value if UPayloadFormat.Name(value) else None |
97 | | - except (ValueError, AttributeError): |
98 | | - return None |
99 | | - |
100 | 90 | @staticmethod |
101 | 91 | def uattributes_to_attachment(uattributes: UAttributes): |
102 | | - attachment = [("", UATTRIBUTE_VERSION.to_bytes(1, byteorder='little')), ("", uattributes.SerializeToString())] |
103 | | - return attachment |
| 92 | + # Convert the version number to bytes (assuming 1 as in the Rust example) |
| 93 | + version_bytes = UATTRIBUTE_VERSION.to_bytes(1, byteorder='little') |
| 94 | + |
| 95 | + # Serialize the UAttributes to bytes |
| 96 | + uattributes_bytes = uattributes.SerializeToString() |
| 97 | + |
| 98 | + # Combine version bytes and uattributes bytes into one list of bytes |
| 99 | + attachment_bytes = [version_bytes, uattributes_bytes] |
| 100 | + |
| 101 | + # Convert the combined bytes to ZBytes |
| 102 | + return attachment_bytes |
104 | 103 |
|
105 | 104 | @staticmethod |
106 | | - def attachment_to_uattributes(attachment: Attachment) -> UAttributes: |
| 105 | + def attachment_to_uattributes(attachment: ZBytes) -> UAttributes: |
107 | 106 | try: |
108 | | - version = None |
109 | | - version_found = False |
110 | | - uattributes = None |
111 | | - |
112 | | - items = attachment.items() |
113 | | - for pair in items: |
114 | | - if not version_found: |
115 | | - version = pair[1] |
116 | | - version_found = True |
117 | | - else: |
118 | | - # Process UAttributes data |
119 | | - uattributes = UAttributes() |
120 | | - uattributes.ParseFromString(pair[1]) |
121 | | - break |
122 | | - |
123 | | - if version is None: |
124 | | - msg = f"UAttributes version is empty (should be {UATTRIBUTE_VERSION})" |
125 | | - logging.debug(msg) |
126 | | - raise UStatusError.from_code_message(code=UCode.INVALID_ARGUMENT, message=msg) |
| 107 | + # Convert ZBytes to a list of bytes |
| 108 | + attachment_bytes = attachment.deserialize(list) |
127 | 109 |
|
128 | | - if not version_found: |
129 | | - msg = "UAttributes version is missing in the attachment" |
| 110 | + # Ensure there is at least one byte for the version |
| 111 | + if len(attachment_bytes) < 1: |
| 112 | + msg = "Unable to get the UAttributes version" |
130 | 113 | logging.debug(msg) |
131 | 114 | raise UStatusError.from_code_message(code=UCode.INVALID_ARGUMENT, message=msg) |
132 | 115 |
|
133 | | - if version != UATTRIBUTE_VERSION.to_bytes(1, byteorder='little'): |
| 116 | + # Check the version |
| 117 | + version = int.from_bytes(bytes(attachment_bytes[0]), byteorder='big') |
| 118 | + if version != UATTRIBUTE_VERSION: |
134 | 119 | msg = f"UAttributes version is {version} (should be {UATTRIBUTE_VERSION})" |
135 | 120 | logging.debug(msg) |
136 | 121 | raise UStatusError.from_code_message(code=UCode.INVALID_ARGUMENT, message=msg) |
137 | 122 |
|
138 | | - if uattributes is None: |
| 123 | + # Get the attributes from the remaining bytes |
| 124 | + uattributes_data = bytes(attachment_bytes[1]) |
| 125 | + if not uattributes_data: |
139 | 126 | msg = "Unable to get the UAttributes" |
140 | 127 | logging.debug(msg) |
141 | 128 | raise UStatusError.from_code_message(code=UCode.INVALID_ARGUMENT, message=msg) |
142 | 129 |
|
| 130 | + # Parse the UAttributes from the bytes |
| 131 | + uattributes = UAttributes() |
| 132 | + uattributes.ParseFromString(uattributes_data) |
| 133 | + |
143 | 134 | return uattributes |
| 135 | + |
144 | 136 | except Exception as e: |
145 | | - msg = f"Failed to convert Attachment to UAttributes: {e}" |
| 137 | + msg = f"Failed to convert Attachment to UAttributes: {str(e)}" |
146 | 138 | logging.debug(msg) |
147 | 139 | raise UStatusError.from_code_message(code=UCode.INVALID_ARGUMENT, message=msg) |
148 | 140 |
|
|
0 commit comments