| 240 | |
| 241 | |
| 242 | def struct_to_binary(obj): |
| 243 | packet = [] |
| 244 | has_switch = hasattr(obj, "ua_switches") |
| 245 | if has_switch: |
| 246 | for name, switch in obj.ua_switches.items(): |
| 247 | member = getattr(obj, name) |
| 248 | container_name, idx = switch |
| 249 | if member is not None: |
| 250 | container_val = getattr(obj, container_name) |
| 251 | container_val = container_val | 1 << idx |
| 252 | setattr(obj, container_name, container_val) |
| 253 | for name, uatype in obj.ua_types: |
| 254 | val = getattr(obj, name) |
| 255 | if uatype.startswith("ListOf"): |
| 256 | packet.append(list_to_binary(uatype[6:], val)) |
| 257 | else: |
| 258 | if has_switch and val is None and name in obj.ua_switches: |
| 259 | pass |
| 260 | else: |
| 261 | packet.append(to_binary(uatype, val)) |
| 262 | return b''.join(packet) |
| 263 | |
| 264 | |
| 265 | def to_binary(uatype, val): |