diff --git a/main/proto/client.proto b/main/proto/client.proto index 4bd51c4..5470b0a 100644 --- a/main/proto/client.proto +++ b/main/proto/client.proto @@ -24,7 +24,7 @@ message BatchCollectCommand { // CollectTask // 定义了单个采集任务的“意图”。 message CollectTask { - Raw485Command command = 2; // 平台生成的原始485指令 + Raw485Command command = 1; // 平台生成的原始485指令 } // CollectResult diff --git a/main/proto/client_pb.py b/main/proto/client_pb.py index 09874ab..3cb7b00 100644 --- a/main/proto/client_pb.py +++ b/main/proto/client_pb.py @@ -8,6 +8,7 @@ import struct + # --- Protobuf基础类型辅助函数 --- def encode_varint(value): @@ -19,6 +20,7 @@ def encode_varint(value): buf.append(value & 0x7F) return buf + def decode_varint(buf, pos=0): """解码varint整数""" result = 0 @@ -32,19 +34,22 @@ def decode_varint(buf, pos=0): shift += 7 return result, pos + def encode_string(value): """编码字符串""" value_bytes = value.encode('utf-8') length = encode_varint(len(value_bytes)) return length + value_bytes + def decode_string(buf, pos=0): """解码字符串""" length, pos = decode_varint(buf, pos) - value = buf[pos:pos+length].decode('utf-8') + value = buf[pos:pos + length].decode('utf-8') pos += length return value, pos + # --- 消息编码/解码函数 --- def encode_raw_485_command(bus_number, command_bytes): @@ -66,6 +71,7 @@ def encode_raw_485_command(bus_number, command_bytes): result.extend(command_bytes) return result + def decode_raw_485_command(buf): """ 解码Raw485Command消息 @@ -88,16 +94,21 @@ def decode_raw_485_command(buf): elif field_number == 2: # command_bytes if wire_type == 2: length, pos = decode_varint(buf, pos) - value = buf[pos:pos+length] + value = buf[pos:pos + length] pos += length result['command_bytes'] = value else: # 跳过未知字段 - if wire_type == 0: _, pos = decode_varint(buf, pos) - elif wire_type == 2: length, pos = decode_varint(buf, pos); pos += length - else: pos += 1 + if wire_type == 0: + _, pos = decode_varint(buf, pos) + elif wire_type == 2: + length, pos = decode_varint(buf, pos); + pos += length + else: + pos += 1 return result + def encode_collect_task(command_msg): """ 编码CollectTask消息 @@ -107,13 +118,14 @@ def encode_collect_task(command_msg): bytearray: 编码后的数据 """ result = bytearray() - # command (field 2, wire type 2) + # command (field 1, wire type 2) encoded_command = encode_raw_485_command(command_msg['bus_number'], command_msg['command_bytes']) - result.extend(encode_varint((2 << 3) | 2)) + result.extend(encode_varint((1 << 3) | 2)) # 字段编号已改为1 result.extend(encode_varint(len(encoded_command))) result.extend(encoded_command) return result + def decode_collect_task(buf): """ 解码CollectTask消息 @@ -129,18 +141,23 @@ def decode_collect_task(buf): field_number = tag >> 3 wire_type = tag & 0x07 - if field_number == 2: # command + if field_number == 1: # command if wire_type == 2: length, pos = decode_varint(buf, pos) - value_buf = buf[pos:pos+length] + value_buf = buf[pos:pos + length] pos += length result['command'] = decode_raw_485_command(value_buf) else: - if wire_type == 0: _, pos = decode_varint(buf, pos) - elif wire_type == 2: length, pos = decode_varint(buf, pos); pos += length - else: pos += 1 + if wire_type == 0: + _, pos = decode_varint(buf, pos) + elif wire_type == 2: + length, pos = decode_varint(buf, pos); + pos += length + else: + pos += 1 return result + def encode_batch_collect_command(correlation_id, tasks): """ 编码BatchCollectCommand消息 @@ -162,6 +179,7 @@ def encode_batch_collect_command(correlation_id, tasks): result.extend(encoded_task) return result + def decode_batch_collect_command(buf): """ 解码BatchCollectCommand消息 @@ -184,15 +202,20 @@ def decode_batch_collect_command(buf): elif field_number == 2: # tasks (repeated) if wire_type == 2: length, pos = decode_varint(buf, pos) - value_buf = buf[pos:pos+length] + value_buf = buf[pos:pos + length] pos += length result['tasks'].append(decode_collect_task(value_buf)) else: - if wire_type == 0: _, pos = decode_varint(buf, pos) - elif wire_type == 2: length, pos = decode_varint(buf, pos); pos += length - else: pos += 1 + if wire_type == 0: + _, pos = decode_varint(buf, pos) + elif wire_type == 2: + length, pos = decode_varint(buf, pos); + pos += length + else: + pos += 1 return result + def encode_collect_result(correlation_id, values): """ 编码CollectResult消息 @@ -209,9 +232,10 @@ def encode_collect_result(correlation_id, values): # values (field 2, wire type 5) - repeated fixed32 for value in values: result.extend(encode_varint((2 << 3) | 5)) # Tag for fixed32 - result.extend(struct.pack('