更新proto
This commit is contained in:
@@ -24,7 +24,7 @@ message BatchCollectCommand {
|
|||||||
// CollectTask
|
// CollectTask
|
||||||
// 定义了单个采集任务的“意图”。
|
// 定义了单个采集任务的“意图”。
|
||||||
message CollectTask {
|
message CollectTask {
|
||||||
Raw485Command command = 2; // 平台生成的原始485指令
|
Raw485Command command = 1; // 平台生成的原始485指令
|
||||||
}
|
}
|
||||||
|
|
||||||
// CollectResult
|
// CollectResult
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
import struct
|
import struct
|
||||||
|
|
||||||
|
|
||||||
# --- Protobuf基础类型辅助函数 ---
|
# --- Protobuf基础类型辅助函数 ---
|
||||||
|
|
||||||
def encode_varint(value):
|
def encode_varint(value):
|
||||||
@@ -19,6 +20,7 @@ def encode_varint(value):
|
|||||||
buf.append(value & 0x7F)
|
buf.append(value & 0x7F)
|
||||||
return buf
|
return buf
|
||||||
|
|
||||||
|
|
||||||
def decode_varint(buf, pos=0):
|
def decode_varint(buf, pos=0):
|
||||||
"""解码varint整数"""
|
"""解码varint整数"""
|
||||||
result = 0
|
result = 0
|
||||||
@@ -32,12 +34,14 @@ def decode_varint(buf, pos=0):
|
|||||||
shift += 7
|
shift += 7
|
||||||
return result, pos
|
return result, pos
|
||||||
|
|
||||||
|
|
||||||
def encode_string(value):
|
def encode_string(value):
|
||||||
"""编码字符串"""
|
"""编码字符串"""
|
||||||
value_bytes = value.encode('utf-8')
|
value_bytes = value.encode('utf-8')
|
||||||
length = encode_varint(len(value_bytes))
|
length = encode_varint(len(value_bytes))
|
||||||
return length + value_bytes
|
return length + value_bytes
|
||||||
|
|
||||||
|
|
||||||
def decode_string(buf, pos=0):
|
def decode_string(buf, pos=0):
|
||||||
"""解码字符串"""
|
"""解码字符串"""
|
||||||
length, pos = decode_varint(buf, pos)
|
length, pos = decode_varint(buf, pos)
|
||||||
@@ -45,6 +49,7 @@ def decode_string(buf, pos=0):
|
|||||||
pos += length
|
pos += length
|
||||||
return value, pos
|
return value, pos
|
||||||
|
|
||||||
|
|
||||||
# --- 消息编码/解码函数 ---
|
# --- 消息编码/解码函数 ---
|
||||||
|
|
||||||
def encode_raw_485_command(bus_number, command_bytes):
|
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)
|
result.extend(command_bytes)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def decode_raw_485_command(buf):
|
def decode_raw_485_command(buf):
|
||||||
"""
|
"""
|
||||||
解码Raw485Command消息
|
解码Raw485Command消息
|
||||||
@@ -93,11 +99,16 @@ def decode_raw_485_command(buf):
|
|||||||
result['command_bytes'] = value
|
result['command_bytes'] = value
|
||||||
else:
|
else:
|
||||||
# 跳过未知字段
|
# 跳过未知字段
|
||||||
if wire_type == 0: _, pos = decode_varint(buf, pos)
|
if wire_type == 0:
|
||||||
elif wire_type == 2: length, pos = decode_varint(buf, pos); pos += length
|
_, pos = decode_varint(buf, pos)
|
||||||
else: pos += 1
|
elif wire_type == 2:
|
||||||
|
length, pos = decode_varint(buf, pos);
|
||||||
|
pos += length
|
||||||
|
else:
|
||||||
|
pos += 1
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def encode_collect_task(command_msg):
|
def encode_collect_task(command_msg):
|
||||||
"""
|
"""
|
||||||
编码CollectTask消息
|
编码CollectTask消息
|
||||||
@@ -107,13 +118,14 @@ def encode_collect_task(command_msg):
|
|||||||
bytearray: 编码后的数据
|
bytearray: 编码后的数据
|
||||||
"""
|
"""
|
||||||
result = 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'])
|
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(encode_varint(len(encoded_command)))
|
||||||
result.extend(encoded_command)
|
result.extend(encoded_command)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def decode_collect_task(buf):
|
def decode_collect_task(buf):
|
||||||
"""
|
"""
|
||||||
解码CollectTask消息
|
解码CollectTask消息
|
||||||
@@ -129,18 +141,23 @@ def decode_collect_task(buf):
|
|||||||
field_number = tag >> 3
|
field_number = tag >> 3
|
||||||
wire_type = tag & 0x07
|
wire_type = tag & 0x07
|
||||||
|
|
||||||
if field_number == 2: # command
|
if field_number == 1: # command
|
||||||
if wire_type == 2:
|
if wire_type == 2:
|
||||||
length, pos = decode_varint(buf, pos)
|
length, pos = decode_varint(buf, pos)
|
||||||
value_buf = buf[pos:pos + length]
|
value_buf = buf[pos:pos + length]
|
||||||
pos += length
|
pos += length
|
||||||
result['command'] = decode_raw_485_command(value_buf)
|
result['command'] = decode_raw_485_command(value_buf)
|
||||||
else:
|
else:
|
||||||
if wire_type == 0: _, pos = decode_varint(buf, pos)
|
if wire_type == 0:
|
||||||
elif wire_type == 2: length, pos = decode_varint(buf, pos); pos += length
|
_, pos = decode_varint(buf, pos)
|
||||||
else: pos += 1
|
elif wire_type == 2:
|
||||||
|
length, pos = decode_varint(buf, pos);
|
||||||
|
pos += length
|
||||||
|
else:
|
||||||
|
pos += 1
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def encode_batch_collect_command(correlation_id, tasks):
|
def encode_batch_collect_command(correlation_id, tasks):
|
||||||
"""
|
"""
|
||||||
编码BatchCollectCommand消息
|
编码BatchCollectCommand消息
|
||||||
@@ -162,6 +179,7 @@ def encode_batch_collect_command(correlation_id, tasks):
|
|||||||
result.extend(encoded_task)
|
result.extend(encoded_task)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def decode_batch_collect_command(buf):
|
def decode_batch_collect_command(buf):
|
||||||
"""
|
"""
|
||||||
解码BatchCollectCommand消息
|
解码BatchCollectCommand消息
|
||||||
@@ -188,11 +206,16 @@ def decode_batch_collect_command(buf):
|
|||||||
pos += length
|
pos += length
|
||||||
result['tasks'].append(decode_collect_task(value_buf))
|
result['tasks'].append(decode_collect_task(value_buf))
|
||||||
else:
|
else:
|
||||||
if wire_type == 0: _, pos = decode_varint(buf, pos)
|
if wire_type == 0:
|
||||||
elif wire_type == 2: length, pos = decode_varint(buf, pos); pos += length
|
_, pos = decode_varint(buf, pos)
|
||||||
else: pos += 1
|
elif wire_type == 2:
|
||||||
|
length, pos = decode_varint(buf, pos);
|
||||||
|
pos += length
|
||||||
|
else:
|
||||||
|
pos += 1
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def encode_collect_result(correlation_id, values):
|
def encode_collect_result(correlation_id, values):
|
||||||
"""
|
"""
|
||||||
编码CollectResult消息
|
编码CollectResult消息
|
||||||
@@ -212,6 +235,7 @@ def encode_collect_result(correlation_id, values):
|
|||||||
result.extend(struct.pack('<f', value)) # 小端序浮点数
|
result.extend(struct.pack('<f', value)) # 小端序浮点数
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def decode_collect_result(buf):
|
def decode_collect_result(buf):
|
||||||
"""
|
"""
|
||||||
解码CollectResult消息
|
解码CollectResult消息
|
||||||
@@ -237,12 +261,18 @@ def decode_collect_result(buf):
|
|||||||
pos += 4
|
pos += 4
|
||||||
result['values'].append(value)
|
result['values'].append(value)
|
||||||
else:
|
else:
|
||||||
if wire_type == 0: _, pos = decode_varint(buf, pos)
|
if wire_type == 0:
|
||||||
elif wire_type == 5: pos += 4 # fixed32
|
_, pos = decode_varint(buf, pos)
|
||||||
elif wire_type == 2: length, pos = decode_varint(buf, pos); pos += length
|
elif wire_type == 5:
|
||||||
else: pos += 1
|
pos += 4 # fixed32
|
||||||
|
elif wire_type == 2:
|
||||||
|
length, pos = decode_varint(buf, pos);
|
||||||
|
pos += length
|
||||||
|
else:
|
||||||
|
pos += 1
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def encode_instruction(payload_type, payload_data):
|
def encode_instruction(payload_type, payload_data):
|
||||||
"""
|
"""
|
||||||
编码Instruction消息 (包含oneof字段)
|
编码Instruction消息 (包含oneof字段)
|
||||||
@@ -271,6 +301,7 @@ def encode_instruction(payload_type, payload_data):
|
|||||||
result.extend(encoded_payload)
|
result.extend(encoded_payload)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def decode_instruction(buf):
|
def decode_instruction(buf):
|
||||||
"""
|
"""
|
||||||
解码Instruction消息
|
解码Instruction消息
|
||||||
@@ -299,12 +330,18 @@ def decode_instruction(buf):
|
|||||||
result['collect_result'] = decode_collect_result(value_buf)
|
result['collect_result'] = decode_collect_result(value_buf)
|
||||||
else:
|
else:
|
||||||
# 跳过未知字段
|
# 跳过未知字段
|
||||||
if wire_type == 0: _, pos = decode_varint(buf, pos)
|
if wire_type == 0:
|
||||||
elif wire_type == 5: pos += 4
|
_, pos = decode_varint(buf, pos)
|
||||||
elif wire_type == 2: length, pos = decode_varint(buf, pos); pos += length
|
elif wire_type == 5:
|
||||||
else: pos += 1
|
pos += 4
|
||||||
|
elif wire_type == 2:
|
||||||
|
length, pos = decode_varint(buf, pos);
|
||||||
|
pos += length
|
||||||
|
else:
|
||||||
|
pos += 1
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
# --- 单元测试与使用范例 ---
|
# --- 单元测试与使用范例 ---
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("--- 测试 Raw485Command ---")
|
print("--- 测试 Raw485Command ---")
|
||||||
@@ -331,7 +368,8 @@ if __name__ == "__main__":
|
|||||||
{'command': {'bus_number': 2, 'command_bytes': b'\x02\x03\x00\x01\x00\x01\xd5\xfa'}}
|
{'command': {'bus_number': 2, 'command_bytes': b'\x02\x03\x00\x01\x00\x01\xd5\xfa'}}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
encoded_batch_collect = encode_batch_collect_command(batch_collect_data['correlation_id'], batch_collect_data['tasks'])
|
encoded_batch_collect = encode_batch_collect_command(batch_collect_data['correlation_id'],
|
||||||
|
batch_collect_data['tasks'])
|
||||||
print(f"编码后 BatchCollectCommand: {encoded_batch_collect.hex()}")
|
print(f"编码后 BatchCollectCommand: {encoded_batch_collect.hex()}")
|
||||||
decoded_batch_collect = decode_batch_collect_command(encoded_batch_collect)
|
decoded_batch_collect = decode_batch_collect_command(encoded_batch_collect)
|
||||||
print(f"解码后 BatchCollectCommand: {decoded_batch_collect}")
|
print(f"解码后 BatchCollectCommand: {decoded_batch_collect}")
|
||||||
@@ -363,7 +401,8 @@ if __name__ == "__main__":
|
|||||||
print(f"编码后 Instruction (BatchCollectCommand): {instruction_batch_collect.hex()}")
|
print(f"编码后 Instruction (BatchCollectCommand): {instruction_batch_collect.hex()}")
|
||||||
decoded_instruction_batch_collect = decode_instruction(instruction_batch_collect)
|
decoded_instruction_batch_collect = decode_instruction(instruction_batch_collect)
|
||||||
print(f"解码后 Instruction (BatchCollectCommand): {decoded_instruction_batch_collect}")
|
print(f"解码后 Instruction (BatchCollectCommand): {decoded_instruction_batch_collect}")
|
||||||
assert decoded_instruction_batch_collect['batch_collect_command']['correlation_id'] == batch_collect_data['correlation_id']
|
assert decoded_instruction_batch_collect['batch_collect_command']['correlation_id'] == batch_collect_data[
|
||||||
|
'correlation_id']
|
||||||
assert len(decoded_instruction_batch_collect['batch_collect_command']['tasks']) == len(batch_collect_data['tasks'])
|
assert len(decoded_instruction_batch_collect['batch_collect_command']['tasks']) == len(batch_collect_data['tasks'])
|
||||||
|
|
||||||
print("\n--- 测试 Instruction (内含CollectResult) ---")
|
print("\n--- 测试 Instruction (内含CollectResult) ---")
|
||||||
@@ -371,8 +410,10 @@ if __name__ == "__main__":
|
|||||||
print(f"编码后 Instruction (CollectResult): {instruction_collect_result.hex()}")
|
print(f"编码后 Instruction (CollectResult): {instruction_collect_result.hex()}")
|
||||||
decoded_instruction_collect_result = decode_instruction(instruction_collect_result)
|
decoded_instruction_collect_result = decode_instruction(instruction_collect_result)
|
||||||
print(f"解码后 Instruction (CollectResult): {decoded_instruction_collect_result}")
|
print(f"解码后 Instruction (CollectResult): {decoded_instruction_collect_result}")
|
||||||
assert decoded_instruction_collect_result['collect_result']['correlation_id'] == collect_result_data['correlation_id']
|
assert decoded_instruction_collect_result['collect_result']['correlation_id'] == collect_result_data[
|
||||||
|
'correlation_id']
|
||||||
for i in range(len(collect_result_data['values'])):
|
for i in range(len(collect_result_data['values'])):
|
||||||
assert abs(decoded_instruction_collect_result['collect_result']['values'][i] - collect_result_data['values'][i]) < 1e-5
|
assert abs(
|
||||||
|
decoded_instruction_collect_result['collect_result']['values'][i] - collect_result_data['values'][i]) < 1e-5
|
||||||
|
|
||||||
print("\n所有测试均已通过!")
|
print("\n所有测试均已通过!")
|
||||||
|
|||||||
Reference in New Issue
Block a user