|
| 1 | + |
| 2 | +import struct |
| 3 | +from .pdu_pytype_HakoCmdMagnetHolder import HakoCmdMagnetHolder |
| 4 | +from ..pdu_utils import * |
| 5 | +from .. import binary_io |
| 6 | + |
| 7 | +# dependencies for the generated Python class |
| 8 | +from ..hako_msgs.pdu_conv_HakoCmdHeader import * |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +def pdu_to_py_HakoCmdMagnetHolder(binary_data: bytearray) -> HakoCmdMagnetHolder: |
| 13 | + py_obj = HakoCmdMagnetHolder() |
| 14 | + meta_parser = binary_io.PduMetaDataParser() |
| 15 | + meta = meta_parser.load_pdu_meta(binary_data) |
| 16 | + if meta is None: |
| 17 | + raise ValueError("Invalid PDU binary data: MetaData not found or corrupted") |
| 18 | + binary_read_recursive_HakoCmdMagnetHolder(meta, binary_data, py_obj, binary_io.PduMetaData.PDU_META_DATA_SIZE) |
| 19 | + return py_obj |
| 20 | + |
| 21 | + |
| 22 | +def binary_read_recursive_HakoCmdMagnetHolder(meta: binary_io.PduMetaData, binary_data: bytearray, py_obj: HakoCmdMagnetHolder, base_off: int): |
| 23 | + # array_type: single |
| 24 | + # data_type: struct |
| 25 | + # member_name: header |
| 26 | + # type_name: HakoCmdHeader |
| 27 | + # offset: 0 size: 12 |
| 28 | + # array_len: 1 |
| 29 | + |
| 30 | + tmp_py_obj = HakoCmdHeader() |
| 31 | + binary_read_recursive_HakoCmdHeader(meta, binary_data, tmp_py_obj, base_off + 0) |
| 32 | + py_obj.header = tmp_py_obj |
| 33 | + |
| 34 | + # array_type: single |
| 35 | + # data_type: primitive |
| 36 | + # member_name: magnet_on |
| 37 | + # type_name: bool |
| 38 | + # offset: 12 size: 4 |
| 39 | + # array_len: 1 |
| 40 | + |
| 41 | + |
| 42 | + bin = binary_io.readBinary(binary_data, base_off + 12, 4) |
| 43 | + py_obj.magnet_on = binary_io.binTovalue("bool", bin) |
| 44 | + |
| 45 | + return py_obj |
| 46 | + |
| 47 | + |
| 48 | +def py_to_pdu_HakoCmdMagnetHolder(py_obj: HakoCmdMagnetHolder) -> bytearray: |
| 49 | + binary_data = bytearray() |
| 50 | + base_allocator = DynamicAllocator(False) |
| 51 | + bw_container = BinaryWriterContainer(binary_io.PduMetaData()) |
| 52 | + binary_write_recursive_HakoCmdMagnetHolder(0, bw_container, base_allocator, py_obj) |
| 53 | + |
| 54 | + # メタデータの設定 |
| 55 | + total_size = base_allocator.size() + bw_container.heap_allocator.size() + binary_io.PduMetaData.PDU_META_DATA_SIZE |
| 56 | + bw_container.meta.total_size = total_size |
| 57 | + bw_container.meta.heap_off = binary_io.PduMetaData.PDU_META_DATA_SIZE + base_allocator.size() |
| 58 | + |
| 59 | + # binary_data のサイズを total_size に調整 |
| 60 | + if len(binary_data) < total_size: |
| 61 | + binary_data.extend(bytearray(total_size - len(binary_data))) |
| 62 | + elif len(binary_data) > total_size: |
| 63 | + del binary_data[total_size:] |
| 64 | + |
| 65 | + # メタデータをバッファにコピー |
| 66 | + binary_io.writeBinary(binary_data, 0, bw_container.meta.to_bytes()) |
| 67 | + |
| 68 | + # 基本データをバッファにコピー |
| 69 | + binary_io.writeBinary(binary_data, bw_container.meta.base_off, base_allocator.to_array()) |
| 70 | + |
| 71 | + # ヒープデータをバッファにコピー |
| 72 | + binary_io.writeBinary(binary_data, bw_container.meta.heap_off, bw_container.heap_allocator.to_array()) |
| 73 | + |
| 74 | + return binary_data |
| 75 | + |
| 76 | +def binary_write_recursive_HakoCmdMagnetHolder(parent_off: int, bw_container: BinaryWriterContainer, allocator, py_obj: HakoCmdMagnetHolder): |
| 77 | + # array_type: single |
| 78 | + # data_type: struct |
| 79 | + # member_name: header |
| 80 | + # type_name: HakoCmdHeader |
| 81 | + # offset: 0 size: 12 |
| 82 | + # array_len: 1 |
| 83 | + type = "HakoCmdHeader" |
| 84 | + off = 0 |
| 85 | + |
| 86 | + binary_write_recursive_HakoCmdHeader(parent_off + off, bw_container, allocator, py_obj.header) |
| 87 | + |
| 88 | + # array_type: single |
| 89 | + # data_type: primitive |
| 90 | + # member_name: magnet_on |
| 91 | + # type_name: bool |
| 92 | + # offset: 12 size: 4 |
| 93 | + # array_len: 1 |
| 94 | + type = "bool" |
| 95 | + off = 12 |
| 96 | + |
| 97 | + |
| 98 | + bin = binary_io.typeTobin(type, py_obj.magnet_on) |
| 99 | + bin = get_binary(type, bin, 4) |
| 100 | + allocator.add(bin, expected_offset=parent_off + off) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + import sys |
| 105 | + import json |
| 106 | + |
| 107 | + def print_usage(): |
| 108 | + print(f"Usage: python -m pdu.python.pdu_conv_HakoCmdMagnetHolder <read|write> [args...]") |
| 109 | + print(f" read <input_binary_file> <output_json_file>") |
| 110 | + print(f" write <input_json_file> <output_binary_file>") |
| 111 | + |
| 112 | + if len(sys.argv) < 2: |
| 113 | + print_usage() |
| 114 | + sys.exit(1) |
| 115 | + |
| 116 | + command = sys.argv[1] |
| 117 | + |
| 118 | + if command == "read": |
| 119 | + if len(sys.argv) != 4: |
| 120 | + print_usage() |
| 121 | + sys.exit(1) |
| 122 | + |
| 123 | + binary_filepath = sys.argv[2] |
| 124 | + output_json_filepath = sys.argv[3] |
| 125 | + |
| 126 | + with open(binary_filepath, "rb") as f: |
| 127 | + binary_data = bytearray(f.read()) |
| 128 | + |
| 129 | + py_obj = pdu_to_py_HakoCmdMagnetHolder(binary_data) |
| 130 | + |
| 131 | + with open(output_json_filepath, "w") as f: |
| 132 | + f.write(py_obj.to_json()) |
| 133 | + |
| 134 | + elif command == "write": |
| 135 | + if len(sys.argv) != 4: |
| 136 | + print_usage() |
| 137 | + sys.exit(1) |
| 138 | + |
| 139 | + input_json_filepath = sys.argv[2] |
| 140 | + output_binary_filepath = sys.argv[3] |
| 141 | + |
| 142 | + with open(input_json_filepath, "r") as f: |
| 143 | + json_str = f.read() |
| 144 | + |
| 145 | + py_obj = HakoCmdMagnetHolder.from_json(json_str) |
| 146 | + |
| 147 | + binary_data = py_to_pdu_HakoCmdMagnetHolder(py_obj) |
| 148 | + |
| 149 | + with open(output_binary_filepath, "wb") as f: |
| 150 | + f.write(binary_data) |
| 151 | + |
| 152 | + else: |
| 153 | + print(f"Unknown command: {command}") |
| 154 | + print_usage() |
| 155 | + sys.exit(1) |
0 commit comments