|
| 1 | +""" |
| 2 | +Convex Data Source |
| 3 | +
|
| 4 | +Receives telemetry data from Convex.dev cloud database. |
| 5 | +This is the primary data source for cloud deployments and |
| 6 | +can also be used alongside serial in local mode. |
| 7 | +""" |
| 8 | + |
| 9 | +import asyncio |
| 10 | +import threading |
| 11 | +import time |
| 12 | +from typing import Dict, Any, Optional |
| 13 | +from .base import DataSource |
| 14 | +import config |
| 15 | + |
| 16 | + |
| 17 | +class ConvexDataSource(DataSource): |
| 18 | + """ |
| 19 | + Receives telemetry data from Convex.dev. |
| 20 | + |
| 21 | + Convex provides real-time subscriptions, making it ideal for |
| 22 | + receiving live telemetry data from the solar car via LTE. |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__(self): |
| 26 | + super().__init__("convex") |
| 27 | + self.convex_url = config.CONVEX_URL |
| 28 | + self._client = None |
| 29 | + self._subscription = None |
| 30 | + self._poll_interval = 0.5 # seconds |
| 31 | + |
| 32 | + def connect(self) -> bool: |
| 33 | + """ |
| 34 | + Initialize Convex client. |
| 35 | + |
| 36 | + Note: Full Convex integration requires the convex Python client. |
| 37 | + For now, this is a placeholder that can be implemented when |
| 38 | + you're ready to integrate with Convex. |
| 39 | + """ |
| 40 | + if not self.convex_url: |
| 41 | + print("[Convex] No Convex URL configured") |
| 42 | + return False |
| 43 | + |
| 44 | + try: |
| 45 | + # TODO: Initialize actual Convex client |
| 46 | + # from convex import ConvexClient |
| 47 | + # self._client = ConvexClient(self.convex_url) |
| 48 | + |
| 49 | + print(f"[Convex] Would connect to: {self.convex_url}") |
| 50 | + print("[Convex] Note: Full Convex integration pending") |
| 51 | + |
| 52 | + # For now, mark as connected if URL is configured |
| 53 | + # Real implementation will verify connection |
| 54 | + self.is_connected = True |
| 55 | + return True |
| 56 | + |
| 57 | + except Exception as e: |
| 58 | + print(f"[Convex] Failed to connect: {e}") |
| 59 | + self.is_connected = False |
| 60 | + return False |
| 61 | + |
| 62 | + def disconnect(self): |
| 63 | + """Disconnect from Convex.""" |
| 64 | + if self._subscription: |
| 65 | + # Cancel subscription |
| 66 | + self._subscription = None |
| 67 | + |
| 68 | + if self._client: |
| 69 | + self._client = None |
| 70 | + |
| 71 | + self.is_connected = False |
| 72 | + print("[Convex] Disconnected") |
| 73 | + |
| 74 | + def start_listening(self): |
| 75 | + """Start listening for Convex updates.""" |
| 76 | + if not self.convex_url: |
| 77 | + print("[Convex] Cannot start - no URL configured") |
| 78 | + return |
| 79 | + |
| 80 | + self._running = True |
| 81 | + self._thread = threading.Thread(target=self._listen_loop, daemon=True) |
| 82 | + self._thread.start() |
| 83 | + |
| 84 | + def _listen_loop(self): |
| 85 | + """ |
| 86 | + Main listening loop for Convex data. |
| 87 | + |
| 88 | + This uses polling for now. When fully integrated, |
| 89 | + this should use Convex's real-time subscriptions. |
| 90 | + """ |
| 91 | + while self._running: |
| 92 | + try: |
| 93 | + if not self.is_connected: |
| 94 | + self.connect() |
| 95 | + if not self.is_connected: |
| 96 | + time.sleep(5) |
| 97 | + continue |
| 98 | + |
| 99 | + # TODO: Replace with actual Convex subscription |
| 100 | + # For now, this is a placeholder polling loop |
| 101 | + # |
| 102 | + # Real implementation would look like: |
| 103 | + # data = await self._client.query("telemetry:getLatest") |
| 104 | + # if data: |
| 105 | + # timestamp = data.get('tstamp_unix', 0) |
| 106 | + # self._emit_data(data, timestamp) |
| 107 | + |
| 108 | + time.sleep(self._poll_interval) |
| 109 | + |
| 110 | + except Exception as e: |
| 111 | + print(f"[Convex] Error in listen loop: {e}") |
| 112 | + self.is_connected = False |
| 113 | + time.sleep(5) |
| 114 | + |
| 115 | + async def push_data(self, data: Dict[str, Any]): |
| 116 | + """ |
| 117 | + Push data to Convex (for when we receive data from other sources). |
| 118 | + |
| 119 | + This allows the dashboard to sync data from serial/UDP to Convex |
| 120 | + for other clients to access. |
| 121 | + """ |
| 122 | + if not self._client: |
| 123 | + return |
| 124 | + |
| 125 | + try: |
| 126 | + # TODO: Implement actual Convex mutation |
| 127 | + # await self._client.mutation("telemetry:insert", data) |
| 128 | + pass |
| 129 | + except Exception as e: |
| 130 | + print(f"[Convex] Failed to push data: {e}") |
0 commit comments