improvement: connectivity detectivity improvement

This commit is contained in:
Youwen Wu 2024-02-29 20:44:35 -08:00
parent 5791179aec
commit eb41340ee0
2 changed files with 16 additions and 41 deletions

View file

@ -23,13 +23,16 @@ pub async fn subscribe_topics(app_handle: AppHandle) {
{ {
Ok(client) => { Ok(client) => {
println!("Client created"); println!("Client created");
app_handle
.emit_all("telemetry_connected", "connected")
.expect("Failed to emit telemetry_status connected event");
break client; // Exit the loop if the client is successfully created break client; // Exit the loop if the client is successfully created
} }
Err(e) => { Err(e) => {
println!("Failed to create client: {}. Retrying in 3 seconds...", e); println!("Failed to create client: {}. Retrying in 3 seconds...", e);
app_handle app_handle
.emit_all("telemetry_disconnected", true) .emit_all("telemetry_status", "disconnected")
.expect("Failed to emit telemetry_disconnected event"); .expect("Failed to emit telemetry_status disconnected event");
sleep(Duration::from_secs(3)).await; // Wait for 3 seconds before retrying sleep(Duration::from_secs(3)).await; // Wait for 3 seconds before retrying
continue; // Continue the loop to retry continue; // Continue the loop to retry
@ -62,4 +65,8 @@ pub async fn subscribe_topics(app_handle: AppHandle) {
println!("{}", json_message); println!("{}", json_message);
} }
println!("disconnected");
app_handle
.emit_all("telemetry_status", "disconnected")
.expect("Failed to emit telemetry_disconnected event");
} }

View file

@ -26,56 +26,24 @@ export const initializeTelemetry = async (
) )
} }
const unlistenDisconnected = await listen('telemetry_disconnected', event => { const unlistenStatus = await listen('telemetry_status', event => {
telemetryStore.update({ if (event.payload === 'connected') {
...get(telemetryStore), telemetryStore.set('connected', false)
connected: false, } else if (event.payload === 'disconnected') {
}) telemetryStore.set('connected', false)
}
}) })
const unlistenTelemetry = await listen('telemetry_data', event => { const unlistenTelemetry = await listen('telemetry_data', event => {
const data = JSON.parse(event.payload as string) const data = JSON.parse(event.payload as string)
// console.log(JSON.parse) // console.log(JSON.parse)
telemetryStore.set('connected', true)
telemetryStore.set(data['topic_name'], data['data']) telemetryStore.set(data['topic_name'], data['data'])
}) })
const unlistenAll = () => { const unlistenAll = () => {
unlistenDisconnected() unlistenStatus()
unlistenTelemetry() unlistenTelemetry()
} }
return unlistenAll return unlistenAll
} }
// emit('subscribe', topics)
// export const initializeTelemetry = (
// topics: TelemetryTopics,
// refreshRate: number
// ) => {
// // Make sure refreshRate is valid
// if (!Number.isInteger(refreshRate) || refreshRate < 1) {
// throw new Error(
// 'refreshRate must be an integer greater than or equal to 1.'
// )
// }
// const socket = io('localhost:1280')
// socket.on('connect', () => {
// console.log('Socket-IO connected!')
// socket.emit('subscribe', topics)
// console.log(`Subscribing to topics: ${JSON.stringify(topics)}`)
// })
// socket.on('subscribed', () => {
// console.log('Successfully subscribed to requested topics!')
// socket.emit('request_data', { refresh_rate: refreshRate })
// console.log(`Refreshing at ${refreshRate} Hz`)
// })
// socket.on('telemetry_data', (data: string) => {
// onUpdate(JSON.parse(data))
// })
// }