From f16ac48d0de1efa3fd71e07d847912e7aefc77e2 Mon Sep 17 00:00:00 2001 From: David Rapan Date: Sun, 22 Dec 2024 17:10:49 +0100 Subject: [PATCH] refactor: _handle_protocol_frame through _received_frame_response --- pysolarmanv5/pysolarmanv5.py | 16 +++++++++++++--- pysolarmanv5/pysolarmanv5_async.py | 25 ++++--------------------- 2 files changed, 17 insertions(+), 24 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..4e1fe3e 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() @@ -192,10 +175,10 @@ async def _handle_protocol_frame(self, frame): if isinstance(e, OSError) and e.errno == errno.EHOSTUNREACH: e = TimeoutError self.log.debug( # pylint: disable=logging-fstring-interpolation - f"[{self.serial}] V5_HEARTBEAT error: {type(e).__name__}{f': {e}' if f'{e}' else ''}" + f"[{self.serial}] V5_PROTOCOL error: {type(e).__name__}{f': {e}' if f'{e}' else ''}" ) except Exception as e: - self.log.exception("[%s] V5_HEARTBEAT error: %s", self.serial, e) + self.log.exception("[%s] V5_PROTOCOL error: %s", self.serial, e) return False return True