Skip to content

Commit

Permalink
refactor: _handle_protocol_frame through _received_frame_response
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Dec 22, 2024
1 parent 1424502 commit f16ac48
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
16 changes: 13 additions & 3 deletions pysolarmanv5/pysolarmanv5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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() <pysolarmanv5.PySolarmanV5._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
Expand Down
25 changes: 4 additions & 21 deletions pysolarmanv5/pysolarmanv5_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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() <pysolarmanv5.PySolarmanV5._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()
Expand All @@ -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

Expand Down

0 comments on commit f16ac48

Please sign in to comment.