Convert Python object to binary-coded ExtensionObject. If obj is None, convert to empty ExtensionObject (TypeId=0, no Body). Returns a binary string
(obj)
| 443 | |
| 444 | |
| 445 | def extensionobject_to_binary(obj): |
| 446 | """ |
| 447 | Convert Python object to binary-coded ExtensionObject. |
| 448 | If obj is None, convert to empty ExtensionObject (TypeId=0, no Body). |
| 449 | Returns a binary string |
| 450 | """ |
| 451 | if isinstance(obj, ua.ExtensionObject): |
| 452 | return struct_to_binary(obj) |
| 453 | if obj is None: |
| 454 | TypeId = ua.NodeId() |
| 455 | Encoding = 0 |
| 456 | Body = None |
| 457 | else: |
| 458 | TypeId = ua.extension_object_ids[obj.__class__.__name__] |
| 459 | Encoding = 0x01 |
| 460 | Body = struct_to_binary(obj) |
| 461 | packet = [] |
| 462 | packet.append(nodeid_to_binary(TypeId)) |
| 463 | packet.append(Primitives.Byte.pack(Encoding)) |
| 464 | if Body: |
| 465 | packet.append(Primitives.Bytes.pack(Body)) |
| 466 | return b''.join(packet) |
| 467 | |
| 468 | |
| 469 | def from_binary(uatype, data): |