diff --git a/client/src-tauri/src/telemetry.rs b/client/src-tauri/src/telemetry.rs index 1f1a434..1607a8f 100644 --- a/client/src-tauri/src/telemetry.rs +++ b/client/src-tauri/src/telemetry.rs @@ -1,17 +1,42 @@ -use network_tables::v4::{Client, SubscriptionOptions}; +use network_tables::v4::client_config::Config; +use network_tables::v4::Client; +use network_tables::v4::SubscriptionOptions; use serde_json::to_string; use std::net::{Ipv4Addr, SocketAddrV4}; use tauri::{AppHandle, Manager}; +use tokio::time::{sleep, Duration}; + +const NTABLE_IP: (u8, u8, u8, u8) = (10, 12, 80, 2); +const NTABLE_PORT: u16 = 5810; pub async fn subscribe_topics(app_handle: AppHandle) { - let client = Client::try_new_w_config( - SocketAddrV4::new(Ipv4Addr::new(10, 12, 80, 2), 5810), - network_tables::v4::client_config::Config { - ..Default::default() - }, - ) - .await - .expect("Failed to create client"); + let client = loop { + match Client::try_new_w_config( + SocketAddrV4::new( + Ipv4Addr::new(NTABLE_IP.0, NTABLE_IP.1, NTABLE_IP.2, NTABLE_IP.3), + NTABLE_PORT, + ), + Config { + ..Default::default() + }, + ) + .await + { + Ok(client) => { + println!("Client created"); + break client; // Exit the loop if the client is successfully created + } + Err(e) => { + println!("Failed to create client: {}. Retrying in 3 seconds...", e); + app_handle + .emit_all("telemetry_disconnected", true) + .expect("Failed to emit telemetry_disconnected event"); + + sleep(Duration::from_secs(3)).await; // Wait for 3 seconds before retrying + continue; // Continue the loop to retry + } + }; + }; let mut subscription = client .subscribe_w_options( @@ -34,7 +59,7 @@ pub async fn subscribe_topics(app_handle: AppHandle) { let json_message = to_string(&modified_message).expect("Failed to serialize message"); app_handle - .emit_all("telemetry", json_message.clone()) + .emit_all("telemetry_data", json_message.clone()) .expect("Failed to send telemetry message"); println!("{}", json_message); diff --git a/client/src/App.svelte b/client/src/App.svelte index ed0de7e..8c9aa48 100644 --- a/client/src/App.svelte +++ b/client/src/App.svelte @@ -6,7 +6,7 @@ import AppBar from './lib/Apps/AppBar.svelte' import { appList } from './lib/Apps/appList' import { initializeTelemetry } from './lib/utils/initializeTelemetry' - import { onMount } from 'svelte' + import { onDestroy, onMount } from 'svelte' import { Toaster } from 'svelte-french-toast' import { initializationSequence } from './lib/Sequences/sequences' import Loading from './lib/Loading/Loading.svelte' @@ -33,6 +33,7 @@ } let loading = $settingsStore.fastStartup ? false : true + let unlistenAll: () => void onMount(() => { let savedSettings = getSettings() @@ -41,11 +42,21 @@ } window.ResizeObserver = ResizeObserver // disabled while migrating away from python - initializeTelemetry(topics, 200) + initializeTelemetry(topics, 200).then((unsubFunction: () => void) => { + unlistenAll = unsubFunction + }) setTimeout(() => { loading = false initializationSequence() }, 3000) + + settingsStore.subscribe(value => { + localStorage.setItem('settings', JSON.stringify(value)) + }) + }) + + onDestroy(() => { + unlistenAll && unlistenAll() }) diff --git a/client/src/globals.d.ts b/client/src/globals.d.ts index 2a3f57e..533d9a9 100644 --- a/client/src/globals.d.ts +++ b/client/src/globals.d.ts @@ -54,6 +54,7 @@ interface TelemetryData { 'ebrake': boolean 'reorient': boolean 'gpws': boolean + 'connected': boolean } type CardinalDirection = diff --git a/client/src/lib/stores/telemetryStore.ts b/client/src/lib/stores/telemetryStore.ts index 2abc510..4663c97 100644 --- a/client/src/lib/stores/telemetryStore.ts +++ b/client/src/lib/stores/telemetryStore.ts @@ -1,20 +1,21 @@ import { writable, readonly } from 'svelte/store' let defaults: TelemetryData = { - 'orientation': -999, - 'chassis-x-speed': -999, - 'chassis-y-speed': -999, - 'accx': -999, - 'accy': -999, - 'accz': -999, - 'jerk-x': -999, - 'jerk-y': -999, - 'voltage': -999, - 'acc-profile': '-999', - 'gear': '-999', + 'orientation': 0, + 'chassis-x-speed': 0, + 'chassis-y-speed': 0, + 'accx': 0, + 'accy': 0, + 'accz': 0, + 'jerk-x': 0, + 'jerk-y': 0, + 'voltage': 0, + 'acc-profile': 'chill', + 'gear': 'park', 'ebrake': false, 'reorient': false, 'gpws': false, + 'connected': false, } const createTelemetryStore = () => { diff --git a/client/src/lib/utils/initializeTelemetry.ts b/client/src/lib/utils/initializeTelemetry.ts index 97f9f4a..3f2d847 100644 --- a/client/src/lib/utils/initializeTelemetry.ts +++ b/client/src/lib/utils/initializeTelemetry.ts @@ -1,3 +1,4 @@ +import { get } from 'svelte/store' import { telemetryStore } from '../stores/telemetryStore' import { emit, listen } from '@tauri-apps/api/event' @@ -25,14 +26,27 @@ export const initializeTelemetry = async ( ) } - const unlisten = await listen('hello_from_rust', event => { - console.log(event.event) + const unlistenDisconnected = await listen('telemetry_disconnected', event => { + telemetryStore.update({ + ...get(telemetryStore), + connected: false, + }) + }) + + const unlistenTelemetry = await listen('telemetry_data', event => { console.log(event.payload) }) - emit('subscribe', topics) + const unlistenAll = () => { + unlistenDisconnected() + unlistenTelemetry() + } + + return unlistenAll } +// emit('subscribe', topics) + // export const initializeTelemetry = ( // topics: TelemetryTopics, // refreshRate: number