From 13399fcd9300e2909d88b80e69ee18c929347702 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Fri, 8 Mar 2024 08:37:32 -0800 Subject: [PATCH] fix: detect connectivity properly --- .../src-tauri/src/telemetry/create_client.rs | 2 +- client/src-tauri/src/telemetry/mod.rs | 32 +++++++++++++++---- client/src/lib/utils/initializeTelemetry.ts | 15 +++++++-- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/client/src-tauri/src/telemetry/create_client.rs b/client/src-tauri/src/telemetry/create_client.rs index f1987fc..b1b1c90 100644 --- a/client/src-tauri/src/telemetry/create_client.rs +++ b/client/src-tauri/src/telemetry/create_client.rs @@ -32,7 +32,7 @@ pub async fn create_client( Ok(client) => { println!("Client created"); app_handle - .emit_all("telemetry_connected", "connected") + .emit_all("telemetry_status", "connected") .expect("Failed to emit telemetry_status connected event"); break client; // Exit the loop if the client is successfully created } diff --git a/client/src-tauri/src/telemetry/mod.rs b/client/src-tauri/src/telemetry/mod.rs index c65a87e..a10ad42 100644 --- a/client/src-tauri/src/telemetry/mod.rs +++ b/client/src-tauri/src/telemetry/mod.rs @@ -24,7 +24,12 @@ pub async fn subscribe_topics( let client = create_client(&app_handle, &ntable_ip, &ntable_port).await; let mut subscription: Subscription = match create_subscription(&client).await { - Ok(subscription) => subscription, + Ok(subscription) => { + app_handle + .emit_all("telemetry_status", "connected") + .expect("Failed to emit telemetry_connected event"); + subscription + } Err(_) => { app_handle .emit_all("telemetry_status", "disconnected") @@ -41,13 +46,28 @@ pub async fn subscribe_topics( .emit_all("telemetry_data", json_message.clone()) .expect("Failed to send telemetry message"); - if cfg!(debug_assertions) { - println!("{}", json_message); + app_handle + .emit_all("telemetry_status", "connected") + .expect("Failed to emit telemetry_connected event"); + + check_triggers( + &app_handle, + &message.topic_name, + &message.data, + &previous_gpws, + ); + + if message.topic_name == "gpws" { + previous_gpws = match message.data { + network_tables::Value::Boolean(b) => b, + _ => previous_gpws, + }; } + + tracing::debug!("{}", json_message); } - if cfg!(debug_assertions) { - println!("disconnected"); - } + + tracing::debug!("disconnected"); app_handle .emit_all("telemetry_status", "disconnected") .expect("Failed to emit telemetry_disconnected event"); diff --git a/client/src/lib/utils/initializeTelemetry.ts b/client/src/lib/utils/initializeTelemetry.ts index d98ed08..35fc723 100644 --- a/client/src/lib/utils/initializeTelemetry.ts +++ b/client/src/lib/utils/initializeTelemetry.ts @@ -10,7 +10,8 @@ import { listen } from '@tauri-apps/api/event' */ export const initializeTelemetry = async () => { - const unlistenStatus = await listen('telemetry_status', (event) => { + const unlistenStatus = await listen('telemetry_status', event => { + console.log(event) if (event.payload === 'connected') { telemetryStore.set('connected', true) } else if (event.payload === 'disconnected') { @@ -18,11 +19,21 @@ export const initializeTelemetry = async () => { } }) - const unlistenTelemetry = await listen('telemetry_data', (event) => { + const unlistenTelemetry = await listen('telemetry_data', event => { const data = JSON.parse(event.payload as string) telemetryStore.set(data['topic_name'], data['data']) }) +<<<<<<< HEAD +======= + const unlistenGPWS = await listen('telemetry_gpws', event => { + const data = JSON.parse(event.payload as string) as boolean + if (data) { + gpwsTriggeredSequence() + } + }) + +>>>>>>> cffa594 (fix: detect connectivity properly) const unlistenAll = () => { unlistenStatus() unlistenTelemetry()