From 900e312687cf48db91a263da6767f17eaf4a7e6b Mon Sep 17 00:00:00 2001 From: DevTekVE Date: Sat, 13 Apr 2024 17:21:07 +0000 Subject: [PATCH] Add request_queued method to SunnylinkApi --- CHANGELOGS.md | 16 ++++++++-------- common/api/sunnylink.py | 4 ++++ selfdrive/athena/sunnylinkd.py | 17 ++++++++++++----- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/CHANGELOGS.md b/CHANGELOGS.md index 83d7da1c71..18c0a9df45 100644 --- a/CHANGELOGS.md +++ b/CHANGELOGS.md @@ -5,17 +5,17 @@ sunnypilot - 0.9.7.0 (2024-xx-xx) ************************ * UPDATED: Synced with commaai's openpilot * master commit 56e343b (February 27, 2024) -* NEW❗: Config Backup (Alpha early access) - * Remotely back up and restore sunnypilot settings easily - * Device registration with sunnylink ensures a secure, integrated experience across services - * AES encryption derived from the device's RSA private key is used for utmost security - * Settings are encrypted on-device, transmitted securely via HTTPS, and stored encrypted on sunnylink - * Prevents loss of settings after device resets, offering peace of mind through end-to-end encryption - * Early alpha access to all current and previous GitHub Sponsors and Patreon supporters +* NEW❗: sunnylink (Alpha early access) + * NEW❗: Config Backup + * Remotely back up and restore sunnypilot settings easily + * Device registration with sunnylink ensures a secure, integrated experience across services + * AES encryption derived from the device's RSA private key is used for utmost security + * Settings are encrypted on-device, transmitted securely via HTTPS, and stored encrypted on sunnylink + * Prevents loss of settings after device resets, offering peace of mind through end-to-end encryption + * Early alpha access to all current and previous GitHub Sponsors and Patreon supporters * GitHub account pairing from device settings scanning QR code * Pairing your account will allow you to access features via our API (still WIP but accessible if you dig a little on our code 😉) * Allow inheritance of your sponsorship status, allowing you to get extra features and early access whenever applicable - * Immediate sponsor recognition works only for new sponsors. If you're an earlier sponsor, we've got you — just reach out to a moderator on Discord (https://discord.gg/sunnypilot) to get sorted! * RE-ENABLED: Map-based Turn Speed Control (M-TSC) for supported platforms * openpilot Longitudianl Control available cars * Custom Stock Longitudinal Control available cars diff --git a/common/api/sunnylink.py b/common/api/sunnylink.py index c2ac89d6dd..46f5715c88 100644 --- a/common/api/sunnylink.py +++ b/common/api/sunnylink.py @@ -28,6 +28,10 @@ class SunnylinkApi(BaseApi): return super().api_get(endpoint, method, timeout, **kwargs) + def resume_queued(self, timeout=10, **kwargs): + sunnylinkId, commaId = self._resolve_dongle_ids() + return self.api_get(f"ws/{sunnylinkId}/resume_queued", "POST", timeout, access_token=self.get_token(), **kwargs) + def get_token(self, expiry_hours=1): # Add your additional data here additional_data = {} diff --git a/selfdrive/athena/sunnylinkd.py b/selfdrive/athena/sunnylinkd.py index f08cf2797b..479b9fb311 100755 --- a/selfdrive/athena/sunnylinkd.py +++ b/selfdrive/athena/sunnylinkd.py @@ -21,7 +21,8 @@ SUNNYLINK_ATHENA_HOST = os.getenv('SUNNYLINK_ATHENA_HOST', 'wss://ws.stg.api.sun HANDLER_THREADS = int(os.getenv('HANDLER_THREADS', "4")) LOCAL_PORT_WHITELIST = {8022} - +params = Params() +sunnylink_api = SunnylinkApi(params.get("SunnylinkDongleId", encoding='utf-8')) def handle_long_poll(ws: WebSocket, exit_event: threading.Event | None) -> None: end_event = threading.Event() @@ -55,7 +56,16 @@ def handle_long_poll(ws: WebSocket, exit_event: threading.Event | None) -> None: def ws_recv(ws: WebSocket, end_event: threading.Event) -> None: last_ping = int(time.monotonic() * 1e9) + resume_requested = False while not end_event.is_set(): + try: + if(not resume_requested): + sunnylink_api.resume_queued() + resume_requested = True + except Exception: + cloudlog.exception("sunnylinkd.resume_queued.exception") + resume_requested = False + try: opcode, data = ws.recv_data(control_frame=True) if opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY): @@ -92,12 +102,9 @@ def main(exit_event: threading.Event = None): except Exception: cloudlog.exception("failed to set core affinity") - params = Params() - dongle_id = params.get("SunnylinkDongleId", encoding='utf-8') UploadQueueCache.initialize(upload_queue) ws_uri = SUNNYLINK_ATHENA_HOST - api = SunnylinkApi(dongle_id) conn_start = None conn_retries = 0 while exit_event is None or not exit_event.is_set(): @@ -107,7 +114,7 @@ def main(exit_event: threading.Event = None): cloudlog.event("sunnylinkd.main.connecting_ws", ws_uri=ws_uri, retries=conn_retries) ws = create_connection(ws_uri, - cookie="jwt=" + api.get_token(), + cookie="jwt=" + sunnylink_api.get_token(), enable_multithread=True, timeout=30.0) cloudlog.event("sunnylinkd.main.connected_ws", ws_uri=ws_uri, retries=conn_retries,