feat: update frontend to prepare for full telemetry uplink

This commit is contained in:
Youwen Wu 2024-02-28 22:15:04 -08:00
parent 7278b978d9
commit 2f72f594a5
5 changed files with 78 additions and 26 deletions

View file

@ -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 serde_json::to_string;
use std::net::{Ipv4Addr, SocketAddrV4}; use std::net::{Ipv4Addr, SocketAddrV4};
use tauri::{AppHandle, Manager}; 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) { pub async fn subscribe_topics(app_handle: AppHandle) {
let client = Client::try_new_w_config( let client = loop {
SocketAddrV4::new(Ipv4Addr::new(10, 12, 80, 2), 5810), match Client::try_new_w_config(
network_tables::v4::client_config::Config { SocketAddrV4::new(
Ipv4Addr::new(NTABLE_IP.0, NTABLE_IP.1, NTABLE_IP.2, NTABLE_IP.3),
NTABLE_PORT,
),
Config {
..Default::default() ..Default::default()
}, },
) )
.await .await
.expect("Failed to create client"); {
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 let mut subscription = client
.subscribe_w_options( .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"); let json_message = to_string(&modified_message).expect("Failed to serialize message");
app_handle app_handle
.emit_all("telemetry", json_message.clone()) .emit_all("telemetry_data", json_message.clone())
.expect("Failed to send telemetry message"); .expect("Failed to send telemetry message");
println!("{}", json_message); println!("{}", json_message);

View file

@ -6,7 +6,7 @@
import AppBar from './lib/Apps/AppBar.svelte' import AppBar from './lib/Apps/AppBar.svelte'
import { appList } from './lib/Apps/appList' import { appList } from './lib/Apps/appList'
import { initializeTelemetry } from './lib/utils/initializeTelemetry' import { initializeTelemetry } from './lib/utils/initializeTelemetry'
import { onMount } from 'svelte' import { onDestroy, onMount } from 'svelte'
import { Toaster } from 'svelte-french-toast' import { Toaster } from 'svelte-french-toast'
import { initializationSequence } from './lib/Sequences/sequences' import { initializationSequence } from './lib/Sequences/sequences'
import Loading from './lib/Loading/Loading.svelte' import Loading from './lib/Loading/Loading.svelte'
@ -33,6 +33,7 @@
} }
let loading = $settingsStore.fastStartup ? false : true let loading = $settingsStore.fastStartup ? false : true
let unlistenAll: () => void
onMount(() => { onMount(() => {
let savedSettings = getSettings() let savedSettings = getSettings()
@ -41,11 +42,21 @@
} }
window.ResizeObserver = ResizeObserver window.ResizeObserver = ResizeObserver
// disabled while migrating away from python // disabled while migrating away from python
initializeTelemetry(topics, 200) initializeTelemetry(topics, 200).then((unsubFunction: () => void) => {
unlistenAll = unsubFunction
})
setTimeout(() => { setTimeout(() => {
loading = false loading = false
initializationSequence() initializationSequence()
}, 3000) }, 3000)
settingsStore.subscribe(value => {
localStorage.setItem('settings', JSON.stringify(value))
})
})
onDestroy(() => {
unlistenAll && unlistenAll()
}) })
</script> </script>

View file

@ -54,6 +54,7 @@ interface TelemetryData {
'ebrake': boolean 'ebrake': boolean
'reorient': boolean 'reorient': boolean
'gpws': boolean 'gpws': boolean
'connected': boolean
} }
type CardinalDirection = type CardinalDirection =

View file

@ -1,20 +1,21 @@
import { writable, readonly } from 'svelte/store' import { writable, readonly } from 'svelte/store'
let defaults: TelemetryData = { let defaults: TelemetryData = {
'orientation': -999, 'orientation': 0,
'chassis-x-speed': -999, 'chassis-x-speed': 0,
'chassis-y-speed': -999, 'chassis-y-speed': 0,
'accx': -999, 'accx': 0,
'accy': -999, 'accy': 0,
'accz': -999, 'accz': 0,
'jerk-x': -999, 'jerk-x': 0,
'jerk-y': -999, 'jerk-y': 0,
'voltage': -999, 'voltage': 0,
'acc-profile': '-999', 'acc-profile': 'chill',
'gear': '-999', 'gear': 'park',
'ebrake': false, 'ebrake': false,
'reorient': false, 'reorient': false,
'gpws': false, 'gpws': false,
'connected': false,
} }
const createTelemetryStore = () => { const createTelemetryStore = () => {

View file

@ -1,3 +1,4 @@
import { get } from 'svelte/store'
import { telemetryStore } from '../stores/telemetryStore' import { telemetryStore } from '../stores/telemetryStore'
import { emit, listen } from '@tauri-apps/api/event' import { emit, listen } from '@tauri-apps/api/event'
@ -25,14 +26,27 @@ export const initializeTelemetry = async (
) )
} }
const unlisten = await listen('hello_from_rust', event => { const unlistenDisconnected = await listen('telemetry_disconnected', event => {
console.log(event.event) telemetryStore.update({
...get(telemetryStore),
connected: false,
})
})
const unlistenTelemetry = await listen('telemetry_data', event => {
console.log(event.payload) console.log(event.payload)
}) })
emit('subscribe', topics) const unlistenAll = () => {
unlistenDisconnected()
unlistenTelemetry()
} }
return unlistenAll
}
// emit('subscribe', topics)
// export const initializeTelemetry = ( // export const initializeTelemetry = (
// topics: TelemetryTopics, // topics: TelemetryTopics,
// refreshRate: number // refreshRate: number