@@ -306,48 +306,49 @@ async def _monitorSingleDevice(device: ESPDevice) -> None:
306306 packet_data = buffer [: packet_len ]
307307 packet = decode_packet_server (packet_data )
308308
309- ml .plog (f"Decoded { packet . __class__ .__name__ } from { device .name } " )
309+ ml .plog (f"Decoded { type ( packet ) .__name__ } from { device .name } " )
310310
311- if isinstance (packet , DataPacket ):
312- ml .elog (f"Unexpected DATA packet received over TCP from { device .name } . This should be sent over UDP. Ignoring." )
311+ match packet :
312+ case DataPacket ():
313+ ml .elog (f"Unexpected DATA packet received over TCP from { device .name } . This should be sent over UDP. Ignoring." )
313314
314- elif isinstance (packet , StatusPacket ):
315- # If SensorMonitor, log control states
316- if isinstance (device , SensorMonitor ) and packet .control_states :
317- # Read control states from payload (if any) and update internal state
318- for control_state in packet .control_states :
315+ case StatusPacket (control_states = control_states ) if isinstance (device , SensorMonitor ) and control_states :
316+ for control_state in control_states :
319317 control_names = list (device .controls .keys ())
320318 if control_state .id < len (control_names ):
321319 control_name = control_names [control_state .id ]
322320 state_str = "OPEN" if control_state .state == ControlState .OPEN else "CLOSED" if control_state .state == ControlState .CLOSED else "UNKNOWN"
323321 device .controls [control_name ].state = state_str
324322 ml .log (f"{ device .name } STATUS { control_name } { state_str } " )
325323
326- elif isinstance (packet , AckPacket ):
327- if packet .ack_packet_type == PacketType .TIMESYNC :
328- device .last_sync_time = time .monotonic ()
329- device ._resync_pending = False
330- ml .plog (f"{ device .name } TIMESYNC completed" )
331- elif packet .ack_packet_type == PacketType .HEARTBEAT :
332- device .handleHeartbeatAck (packet .ack_sequence )
333- ml .plog (f"{ device .name } HEARTBEAT ACK seq={ packet .ack_sequence } " )
334- elif packet .ack_packet_type == PacketType .CONTROL :
335- # Check for pending control command
336- if packet .ack_sequence in device ._pending_controls :
337- control_name , state = device ._pending_controls .pop (packet .ack_sequence )
338-
339- # Send status log for control ACK
340- state_str = "OPEN" if state == "OPEN" else "CLOSED" if state == "CLOSE" else "UNKNOWN"
341- if isinstance (device , SensorMonitor ) and control_name in device .controls :
342- device .controls [control_name ].state = state
343- ml .log (f"{ device .name } STATUS { control_name } { state_str } " )
324+ case AckPacket ():
325+ if packet .ack_packet_type == PacketType .TIMESYNC :
326+ device .last_sync_time = time .monotonic ()
327+ device ._resync_pending = False
328+ ml .plog (f"{ device .name } TIMESYNC completed" )
329+ elif packet .ack_packet_type == PacketType .HEARTBEAT :
330+ device .handleHeartbeatAck (packet .ack_sequence )
331+ ml .plog (f"{ device .name } HEARTBEAT ACK seq={ packet .ack_sequence } " )
332+ elif packet .ack_packet_type == PacketType .CONTROL :
333+ # Check for pending control command
334+ if packet .ack_sequence in device ._pending_controls :
335+ control_name , state = device ._pending_controls .pop (packet .ack_sequence )
336+
337+ # Send status log for control ACK
338+ state_str = "OPEN" if state == "OPEN" else "CLOSED" if state == "CLOSE" else "UNKNOWN"
339+ if isinstance (device , SensorMonitor ) and control_name in device .controls :
340+ device .controls [control_name ].state = state
341+ ml .log (f"{ device .name } STATUS { control_name } { state_str } " )
342+ else :
343+ ml .plog (f"{ device .name } ACK for CONTROL seq={ packet .ack_sequence } " )
344344 else :
345- ml .plog (f"{ device .name } ACK for CONTROL seq={ packet .ack_sequence } " )
346- else :
347- ml .plog (f"{ device .name } ACK for { packet .ack_packet_type .name } seq={ packet .ack_sequence } " )
345+ ml .plog (f"{ device .name } ACK for { packet .ack_packet_type .name } seq={ packet .ack_sequence } " )
346+
347+ case NackPacket ():
348+ ml .plog (f"{ device .name } NACK for { packet .nack_packet_type .name } error={ packet .error_code .name } " )
348349
349- elif isinstance ( packet , NackPacket ) :
350- ml .plog (f"{ device . name } NACK for { packet . nack_packet_type . name } error= { packet . error_code . name } " )
350+ case _ :
351+ ml .elog (f"Received unexpected packet type { type ( packet ). __name__ } from { device . name } over TCP " )
351352
352353 buffer = buffer [packet_len :]
353354
0 commit comments