From 10f66420339c9ab5ba0cd53d326e0ff7f3ca903b Mon Sep 17 00:00:00 2001 From: David Rapan Date: Sun, 22 Dec 2024 17:10:18 +0100 Subject: [PATCH] refactor: _handle_protocol_frame through _received_frame_response --- pysolarmanv5/pysolarmanv5.py | 16 +++++++++++++--- pysolarmanv5/pysolarmanv5_async.py | 21 ++------------------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/pysolarmanv5/pysolarmanv5.py b/pysolarmanv5/pysolarmanv5.py index 697dfaa..7f7e798 100644 --- a/pysolarmanv5/pysolarmanv5.py +++ b/pysolarmanv5/pysolarmanv5.py @@ -332,9 +332,9 @@ def _received_frame_is_valid(self, frame): return False return True - def _handle_protocol_frame(self, frame): + def _received_frame_response(self, frame): """ - Handles frames with control code 0x41 (handshake), 0x42 (data), 0x43 (wifi) and 0x47 (heartbeat). + Return response to frames with control codes 0x41 (handshake), 0x42 (data), 0x43 (wifi) and 0x47 (heartbeat). """ response_frame = None if frame[4] == 0x41: @@ -353,7 +353,17 @@ def _handle_protocol_frame(self, frame): self.log.debug("[%s] V5_HEARTBEAT: %s", self.serial, frame.hex(" ")) response_frame = self._v5_time_response_frame(frame) self.log.debug("[%s] V5_HEARTBEAT RESP: %s", self.serial, response_frame.hex(" ")) - if response_frame: + if frame[4] == 0x48: + self.log.debug("[%s] V5_REPORT: %s", self.serial, frame.hex(" ")) + response_frame = self._v5_time_response_frame(frame) + self.log.debug("[%s] V5_REPORT RESP: %s", self.serial, response_frame.hex(" ")) + return response_frame + + def _handle_protocol_frame(self, frame): + """ + Handles frames with known control codes :func:`_received_frame_response() ` + """ + if (response_frame := self._received_frame_response(frame)) is not None: if self._reader_thr.is_alive(): self.sock.sendall(response_frame) return False diff --git a/pysolarmanv5/pysolarmanv5_async.py b/pysolarmanv5/pysolarmanv5_async.py index 214e688..bf91da8 100644 --- a/pysolarmanv5/pysolarmanv5_async.py +++ b/pysolarmanv5/pysolarmanv5_async.py @@ -163,26 +163,9 @@ def _send_data(self, data: bytes): async def _handle_protocol_frame(self, frame): """ - Handles frames with control code 0x41 (handshake), 0x42 (data), 0x43 (wifi) and 0x47 (heartbeat). + Handles frames with known control codes :func:`_received_frame_response() ` """ - response_frame = None - if frame[4] == 0x41: - self.log.debug("[%s] V5_HANDSHAKE: %s", self.serial, frame.hex(" ")) - response_frame = self._v5_time_response_frame(frame) - self.log.debug("[%s] V5_HANDSHAKE RESP: %s", self.serial, response_frame.hex(" ")) - if frame[4] == 0x42: - self.log.debug("[%s] V5_DATA: %s", self.serial, frame.hex(" ")) - response_frame = self._v5_time_response_frame(frame) - self.log.debug("[%s] V5_DATA RESP: %s", self.serial, response_frame.hex(" ")) - if frame[4] == 0x43: - self.log.debug("[%s] V5_WIFI: %s", self.serial, frame.hex(" ")) - response_frame = self._v5_time_response_frame(frame) - self.log.debug("[%s] V5_WIFI RESP: %s", self.serial, response_frame.hex(" ")) - if frame[4] == 0x47: - self.log.debug("[%s] V5_HEARTBEAT: %s", self.serial, frame.hex(" ")) - response_frame = self._v5_time_response_frame(frame) - self.log.debug("[%s] V5_HEARTBEAT RESP: %s", self.serial, response_frame.hex(" ")) - if response_frame: + if (response_frame := self._received_frame_response(frame)) is not None: try: self.writer.write(response_frame) await self.writer.drain()