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) => {
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
}
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");
.emit_all("telemetry_status", "disconnected")
.expect("Failed to emit telemetry_status disconnected event");
sleep(Duration::from_secs(3)).await; // Wait for 3 seconds before retrying
continue; // Continue the loop to retry
@ -62,4 +65,8 @@ pub async fn subscribe_topics(app_handle: AppHandle) {
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 => {
telemetryStore.update({
...get(telemetryStore),
connected: false,
})
const unlistenStatus = await listen('telemetry_status', event => {
if (event.payload === 'connected') {
telemetryStore.set('connected', false)
} else if (event.payload === 'disconnected') {
telemetryStore.set('connected', false)
}
})
const unlistenTelemetry = await listen('telemetry_data', event => {
const data = JSON.parse(event.payload as string)
// console.log(JSON.parse)
telemetryStore.set('connected', true)
telemetryStore.set(data['topic_name'], data['data'])
})
const unlistenAll = () => {
unlistenDisconnected()
unlistenStatus()
unlistenTelemetry()
}
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))
// })
// }