Feat: Implement correct response parsing and GetDevices decoder

This commit is contained in:
wes
2025-12-29 09:59:28 -05:00
parent eda93dc7fe
commit 275f08658f
+37 -3
View File
@@ -41,6 +41,7 @@ class OneControlClient:
self.encoder = CobsEncoder() self.encoder = CobsEncoder()
self.decoder = CobsDecoder() self.decoder = CobsDecoder()
self._seq = 0 self._seq = 0
self._pending_commands = {} # seq -> cmd_type
async def connect(self): async def connect(self):
"""Connect to OneControl device""" """Connect to OneControl device"""
@@ -58,8 +59,37 @@ class OneControlClient:
"""Handle notifications from device""" """Handle notifications from device"""
try: try:
decoded = self.decoder.decode(data) decoded = self.decoder.decode(data)
print(f"Received: {decoded.hex()}") # print(f"Received: {decoded.hex()}")
# Parse response here
# Response Structure: [Event(1)] [Seq(2)] [RespType(1)] [Payload...]
if len(decoded) < 4:
return
event_type = decoded[0]
seq = struct.unpack("<H", decoded[1:3])[0]
resp_type = decoded[3]
payload = decoded[4:]
# 2 = DeviceCommand response
if event_type == 2:
cmd_type = self._pending_commands.get(seq)
if cmd_type:
if cmd_type == CommandType.GET_DEVICES:
self._parse_get_devices_response(payload)
elif cmd_type == CommandType.ACTION_SWITCH:
print(f"Switch Command Response (Seq {seq}): {payload.hex()}")
else:
print(f"Response (Seq {seq}, Cmd {cmd_type}): {payload.hex()}")
# If completed, remove from pending?
# 0x81 (129) = SuccessCompleted, 0x82 (130) = FailureCompleted
if resp_type >= 128:
self._pending_commands.pop(seq, None)
else:
print(f"Received Response for unknown Seq {seq}: {payload.hex()}")
else:
print(f"Received Event {event_type}: {decoded.hex()}")
except Exception as e: except Exception as e:
print(f"Error decoding: {e}") print(f"Error decoding: {e}")
@@ -72,6 +102,10 @@ class OneControlClient:
"""Send command to device""" """Send command to device"""
# Build packet # Build packet
seq = self._next_seq() seq = self._next_seq()
# Track pending command
self._pending_commands[seq] = cmd_type
packet = struct.pack("<HBB", seq, cmd_type, table_id) + payload packet = struct.pack("<HBB", seq, cmd_type, table_id) + payload
# Encode # Encode
@@ -79,7 +113,7 @@ class OneControlClient:
# Send # Send
await self.client.write_gatt_char(self.WRITE_CHAR, encoded) await self.client.write_gatt_char(self.WRITE_CHAR, encoded)
print(f"Sent: {packet.hex()} -> {encoded.hex()}") print(f"Sent (Seq {seq}): {packet.hex()}")
async def get_devices(self, start_id: int = 0, max_count: int = 255): async def get_devices(self, start_id: int = 0, max_count: int = 255):
"""Get list of devices (Command 1)""" """Get list of devices (Command 1)"""