From 9c4cf32b73812356d97b1b83b348377018c989f6 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Wed, 6 Mar 2024 23:29:28 -0800 Subject: [PATCH] feat: add gpws trigger code --- .../src-tauri/src/telemetry/check_triggers.rs | 24 +++++++++++++++++++ client/src-tauri/src/telemetry/mod.rs | 20 +++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 client/src-tauri/src/telemetry/check_triggers.rs diff --git a/client/src-tauri/src/telemetry/check_triggers.rs b/client/src-tauri/src/telemetry/check_triggers.rs new file mode 100644 index 0000000..540442b --- /dev/null +++ b/client/src-tauri/src/telemetry/check_triggers.rs @@ -0,0 +1,24 @@ +use tauri::{AppHandle, Manager}; + +pub fn check_triggers( + app_handle: &AppHandle, + topic_name: &str, + data: &network_tables::Value, + previous_gpws: &bool, +) { + if topic_name == "gpws" { + match data { + network_tables::Value::Boolean(b) => { + if *b != *previous_gpws { + if cfg!(debug_assertions) { + println!("gpws changed from {} to {}", previous_gpws, b); + } + app_handle + .emit_all("telemetry_gpws", b) + .expect("Failed to emit telemetry_gpws event"); + } + } + _ => {} + } + } +} diff --git a/client/src-tauri/src/telemetry/mod.rs b/client/src-tauri/src/telemetry/mod.rs index 0c6b588..0105000 100644 --- a/client/src-tauri/src/telemetry/mod.rs +++ b/client/src-tauri/src/telemetry/mod.rs @@ -1,10 +1,12 @@ use network_tables::v4::MessageData; use serde_json::to_string; use tauri::{AppHandle, Manager}; +mod check_triggers; mod create_client; mod create_subscription; -use crate::telemetry::create_client::create_client; +use check_triggers::check_triggers; +use create_client::create_client; use create_subscription::create_subscription; /// Attempts to subscribe to NetworkTables topics and send the data to the frontend. @@ -19,6 +21,8 @@ pub async fn subscribe_topics( ntable_ip: (u8, u8, u8, u8), ntable_port: u16, ) { + let mut previous_gpws: bool = false; + loop { // I hope this doesn't lead to a catastrophic infinite loop failure let client = create_client(&app_handle, &ntable_ip, &ntable_port).await; @@ -33,6 +37,20 @@ pub async fn subscribe_topics( .emit_all("telemetry_data", json_message.clone()) .expect("Failed to send telemetry message"); + 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, + }; + } + if cfg!(debug_assertions) { println!("{}", json_message); }