[sunnylink] Integrate debug logging and revise retry mechanism in sunnylinkd.py

This commit is contained in:
DevTekVE
2024-04-15 19:24:40 +00:00
parent fe023d946f
commit 39bcc41f9f
+11 -6
View File
@@ -68,7 +68,9 @@ def ws_recv(ws: WebSocket, end_event: threading.Event) -> None:
if opcode == ABNF.OPCODE_TEXT: if opcode == ABNF.OPCODE_TEXT:
data = data.decode("utf-8") data = data.decode("utf-8")
recv_queue.put_nowait(data) recv_queue.put_nowait(data)
cloudlog.debug(f"sunnylinkd.ws_recv.recv {data}")
elif opcode in (ABNF.OPCODE_PING, ABNF.OPCODE_PONG): elif opcode in (ABNF.OPCODE_PING, ABNF.OPCODE_PONG):
cloudlog.debug(f"sunnylinkd.ws_recv.pong {opcode}")
last_ping = int(time.monotonic() * 1e9) last_ping = int(time.monotonic() * 1e9)
Params().put("LastSunnylinkPingTime", str(last_ping)) Params().put("LastSunnylinkPingTime", str(last_ping))
except WebSocketTimeoutException: except WebSocketTimeoutException:
@@ -86,6 +88,7 @@ def ws_ping(ws: WebSocket, end_event: threading.Event) -> None:
while not end_event.is_set(): while not end_event.is_set():
try: try:
ws.ping() ws.ping()
cloudlog.debug(f"sunnylinkd.ws_recv.ws_ping: Pinging")
except Exception: except Exception:
cloudlog.exception("sunnylinkd.ws_ping.exception") cloudlog.exception("sunnylinkd.ws_ping.exception")
end_event.set() end_event.set()
@@ -93,20 +96,22 @@ def ws_ping(ws: WebSocket, end_event: threading.Event) -> None:
def ws_queue(end_event: threading.Event) -> None: def ws_queue(end_event: threading.Event) -> None:
resume_requested = False resume_requested = False
backoff_time = 1 # Start with a delay of 1 second tries = 0
max_backoff_time = 60 # Maximum delay of 60 seconds
while not end_event.is_set(): while not end_event.is_set() and not resume_requested:
try: try:
if not resume_requested: if not resume_requested:
cloudlog.debug(f"sunnylinkd.ws_queue.resume_queued")
sunnylink_api.resume_queued(timeout=29) sunnylink_api.resume_queued(timeout=29)
resume_requested = True resume_requested = True
backoff_time = 1 # Reset backoff time after a successful request tries = 0
except Exception: except Exception:
cloudlog.exception("sunnylinkd.ws_queue.resume_queued.exception") cloudlog.exception("sunnylinkd.ws_queue.resume_queued.exception")
resume_requested = False resume_requested = False
time.sleep(backoff_time) # Wait for the backoff time before the next attempt tries += 1
backoff_time = min(backoff_time * 2, max_backoff_time) # Double the backoff time for the next attempt, up to a maximum time.sleep(backoff(tries)) # Wait for the backoff time before the next attempt
cloudlog.debug("Resume requested or end_event is set, exiting ws_queue thread")
@dispatcher.add_method @dispatcher.add_method
def getParamsAllKeys() -> list[str]: def getParamsAllKeys() -> list[str]: