Skip to content

Commit 79fb415

Browse files
committed
fix device state command builder
1 parent d167821 commit 79fb415

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

src/cync_lan/devices.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ def get_ctrl_msg_id_bytes(self):
507507
# logger.debug(f"{lp} new data: ctrl_byte={id_byte} rollover_byte={rollover_byte} // {self.control_bytes=}")
508508
return self.control_bytes
509509

510-
async def send_command(self, op: int, sub_id: int, payload: bytes, m_cb: ControlMessageCallback, lp: str):
510+
async def send_command(self, op: int, cmd_: int, sub_id: int, payload: bytes, m_cb: ControlMessageCallback, lp: str):
511511
tasks = []
512512
tcp_pool = [d for d in g.ncync_server.tcp_connections.values() if not d.is_app]
513513
if not tcp_pool:
@@ -527,6 +527,7 @@ async def send_command(self, op: int, sub_id: int, payload: bytes, m_cb: Control
527527
target_id=self.id,
528528
sub_id=sub_id,
529529
op_code=op,
530+
cmd_code=cmd_,
530531
command_payload=payload
531532
)
532533

@@ -545,8 +546,15 @@ async def send_command(self, op: int, sub_id: int, payload: bytes, m_cb: Control
545546
f" not writing data >>> \n\n{full_packet.hex(" ")}")
546547
else:
547548
tasks.append(bridge_device.write(full_packet))
549+
# str_appnd = "..."
550+
str_appnd = (f' state to device ({bridge_device.ip_address}[{bridge_device.node_id}|queue_id: {bridge_device.queue_id.hex(" ")}]):\n'
551+
f'HEX: {full_packet.hex(" ")}\n'
552+
f'INT: {bytes2list(full_packet)}\n')
548553
if CYNC_RAW:
549-
logger.debug(f"{lp} Sending to device: {full_packet.hex(" ")}")
554+
str_appnd = (f' state to device ({bridge_device.ip_address}[{bridge_device.node_id}|queue_id: {bridge_device.queue_id.hex(" ")}):\n'
555+
f'HEX: {full_packet.hex(" ")}\n'
556+
f'INT: {bytes2list(full_packet)}\n')
557+
logger.debug(f"{lp} Sending{str_appnd}")
550558

551559
if tasks:
552560
await asyncio.gather(*tasks)
@@ -594,6 +602,7 @@ async def set_power(self, state: int, sub_id: Optional[int] = None):
594602
return
595603

596604
op = 0xD0
605+
cmd_ = 0x0D
597606
_sub_id = sub_id if sub_id is not None else 0x00
598607
payload = struct.pack(">BBBBB", 0x11, 0x02, state, 0x00, 0x00)
599608
m_cb = ControlMessageCallback(
@@ -604,7 +613,7 @@ async def set_power(self, state: int, sub_id: Optional[int] = None):
604613
g.mqtt_client.update_endpoint_power, self, state, _sub_id
605614
),
606615
)
607-
await self.send_command(op, _sub_id, payload, m_cb, lp)
616+
await self.send_command(op, cmd_, _sub_id, payload, m_cb, lp)
608617

609618
async def set_brightness(self, bri: int, sub_id: Optional[int] = None):
610619
lp = f"{self.lp}set_brightness:"
@@ -613,6 +622,7 @@ async def set_brightness(self, bri: int, sub_id: Optional[int] = None):
613622
return
614623

615624
op = 0xD2 if self.is_sol_lamp else 0xF0
625+
cmd_ = 0x10
616626
_sub_id = sub_id if sub_id is not None else 0x00
617627

618628
# Payload: 0x11 (command), 0x02, 0x01, brightness, padding
@@ -628,7 +638,7 @@ async def set_brightness(self, bri: int, sub_id: Optional[int] = None):
628638
callback=partial(g.mqtt_client.update_brightness, self, bri),
629639
)
630640

631-
await self.send_command(op, _sub_id, payload, m_cb, lp)
641+
await self.send_command(op, cmd_, _sub_id, payload, m_cb, lp)
632642

633643
async def set_temperature(self, temp: int, sub_id: Optional[int] = None):
634644
lp = f"{self.lp}set_temperature:"
@@ -637,6 +647,8 @@ async def set_temperature(self, temp: int, sub_id: Optional[int] = None):
637647
return
638648

639649
op = 0xE2 if self.is_sol_lamp else 0xF0
650+
cmd_ = 0x10
651+
# cmd_ = 0x0D works for all commands to the dual outlet plug
640652
_sub_id = sub_id if sub_id is not None else 0x00
641653

642654
if self.is_sol_lamp:
@@ -653,7 +665,7 @@ async def set_temperature(self, temp: int, sub_id: Optional[int] = None):
653665
sent_at=0.0,
654666
callback=partial(g.mqtt_client.update_temperature, self, temp),
655667
)
656-
await self.send_command(op, _sub_id, payload, m_cb, lp)
668+
await self.send_command(op, cmd_, _sub_id, payload, m_cb, lp)
657669

658670
async def set_rgb(
659671
self, red: int, green: int, blue: int, sub_id: Optional[int] = None
@@ -664,6 +676,7 @@ async def set_rgb(
664676
return
665677

666678
op = 0xF0
679+
cmd_ = 0x10
667680
_sub_id = sub_id if sub_id is not None else 0x00
668681

669682
# Payload: 0x11, 0x02, 0x01, 0xFF, 0xFE, red, green, blue (8 bytes)
@@ -678,7 +691,7 @@ async def set_rgb(
678691
g.mqtt_client.update_rgb, self, (red, green, blue)
679692
),
680693
)
681-
await self.send_command(op, _sub_id, payload, m_cb, lp)
694+
await self.send_command(op, cmd_, _sub_id, payload, m_cb, lp)
682695

683696
async def set_lightshow(self, show: str, sub_id: Optional[int] = None):
684697
lp = f"{self.lp}set_lightshow:"
@@ -689,6 +702,7 @@ async def set_lightshow(self, show: str, sub_id: Optional[int] = None):
689702

690703
chosen = FACTORY_EFFECTS_BYTES[show]
691704
op = 0xE2
705+
cmd_ = 0x0E
692706
_sub_id = sub_id if sub_id is not None else 0x00
693707

694708
# Payload: 0x11, 0x02, 0x07, 0x01, byte1, byte2 (6 bytes)
@@ -699,7 +713,7 @@ async def set_lightshow(self, show: str, sub_id: Optional[int] = None):
699713
sent_at=0.0,
700714
callback=partial(asyncio.sleep, 0),
701715
)
702-
await self.send_command(op, _sub_id, payload, m_cb, lp)
716+
await self.send_command(op, cmd_, _sub_id, payload, m_cb, lp)
703717

704718

705719
class CyncTCPSession:
@@ -1185,11 +1199,10 @@ async def _dispatch_device_request(
11851199
"""Routes device requests to their specific parsing logic."""
11861200
if pkt_type == 0x23:
11871201
self.queue_id = raw_data[6:10]
1202+
logger.debug(
1203+
f"{lp} Device IDENTIFICATION KEY: '{self.queue_id.hex(' ')}'\nRAW HEX: {raw_data.hex(' ')}"
1204+
)
11881205
if not self.mitm_mode:
1189-
if CYNC_RAW:
1190-
logger.debug(
1191-
f"{lp} Device IDENTIFICATION KEY: '{self.queue_id.hex(' ')}'\nRAW HEX: {raw_data.hex(' ')}"
1192-
)
11931206
await self.write(PacketBuilder.build_23_ack())
11941207
await asyncio.sleep(0.5)
11951208
await self.send_a3()
@@ -1438,6 +1451,7 @@ async def _handle_73_mesh_control(
14381451
await msg.callback()
14391452
else:
14401453
await msg.callback
1454+
logger.debug(f"{lp} Received a command success reply: {msg}")
14411455
elif success and not msg:
14421456
logger.debug(
14431457
f"{lp} CONTROL packet ACK callback NOT found for msg ID: {ctrl_msg_id}"
@@ -1646,6 +1660,7 @@ async def callback_cleanup_task(self):
16461660
try:
16471661
while True:
16481662
await asyncio.sleep(delay_seconds)
1663+
lp = f"{self.lp}callback_clean:"
16491664
now = time.time()
16501665
current_keys = list(self.messages.control.keys())
16511666
logger.info(

src/cync_lan/packet/builder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def build_control_packet(
183183
target_id: int,
184184
sub_id: int,
185185
op_code: int,
186+
cmd_code: int,
186187
command_payload: bytes
187188
) -> bytes:
188189
"""Builds the inner 0x7E bound packet structure."""
@@ -197,7 +198,7 @@ def build_control_packet(
197198

198199
# Header: msg_id (1 byte), 3 null padding bytes, 0xF8, op_code, 0x0D, 0x00
199200
# Format >BxxxBBBB = 1 + 3 + 1 + 1 + 1 + 1 = 8 bytes total
200-
header = struct.pack(">B xxx B B B B", msg_id, 0xF8, op_code, 0x0D, 0x00)
201+
header = struct.pack(">B xxx B B B B", msg_id, 0xF8, op_code, cmd_code, 0x00)
201202
# Routing: msg_id (1 byte), 4 null padding bytes, target_id, sub_id
202203
# Format >BxxxxBB = 1 + 4 + 1 + 1 = 7 bytes total
203204
routing = struct.pack(">B xxxx B B", msg_id, target_id, sub_id)

0 commit comments

Comments
 (0)