2024-02-28 22:15:04 -08:00
|
|
|
use network_tables::v4::client_config::Config;
|
2024-02-29 01:21:53 -08:00
|
|
|
use network_tables::v4::{Client, SubscriptionOptions};
|
2024-02-28 21:43:18 -08:00
|
|
|
use serde_json::to_string;
|
|
|
|
use std::net::{Ipv4Addr, SocketAddrV4};
|
2024-02-27 22:54:06 -08:00
|
|
|
use tauri::{AppHandle, Manager};
|
2024-02-28 22:15:04 -08:00
|
|
|
use tokio::time::{sleep, Duration};
|
|
|
|
|
|
|
|
const NTABLE_IP: (u8, u8, u8, u8) = (10, 12, 80, 2);
|
|
|
|
const NTABLE_PORT: u16 = 5810;
|
2024-02-27 22:54:06 -08:00
|
|
|
|
2024-02-28 21:43:18 -08:00
|
|
|
pub async fn subscribe_topics(app_handle: AppHandle) {
|
2024-02-29 23:49:20 -08:00
|
|
|
loop {
|
|
|
|
// I hope this doesn't lead to a catastrophic infinite loop failure
|
|
|
|
let client = loop {
|
|
|
|
match Client::try_new_w_config(
|
|
|
|
SocketAddrV4::new(
|
|
|
|
Ipv4Addr::new(NTABLE_IP.0, NTABLE_IP.1, NTABLE_IP.2, NTABLE_IP.3),
|
|
|
|
NTABLE_PORT,
|
|
|
|
),
|
|
|
|
Config {
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
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_status", "disconnected")
|
|
|
|
.expect("Failed to emit telemetry_status disconnected event");
|
2024-02-28 22:15:04 -08:00
|
|
|
|
2024-02-29 23:49:20 -08:00
|
|
|
sleep(Duration::from_secs(3)).await; // Wait for 3 seconds before retrying
|
|
|
|
continue; // Continue the loop to retry
|
|
|
|
}
|
|
|
|
};
|
2024-02-28 22:15:04 -08:00
|
|
|
};
|
2024-02-27 22:54:06 -08:00
|
|
|
|
2024-02-29 23:49:20 -08:00
|
|
|
let mut subscription = client
|
|
|
|
.subscribe_w_options(
|
|
|
|
&["/SmartDashboard"],
|
|
|
|
Some(SubscriptionOptions {
|
|
|
|
all: Some(true),
|
|
|
|
prefix: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.expect("Failed to subscribe");
|
|
|
|
while let Some(message) = subscription.next().await {
|
|
|
|
let mut modified_message = message.clone();
|
2024-02-28 21:43:18 -08:00
|
|
|
|
2024-02-29 23:49:20 -08:00
|
|
|
if let Some(stripped) = modified_message.topic_name.strip_prefix("/SmartDashboard/") {
|
|
|
|
modified_message.topic_name = stripped.to_string();
|
|
|
|
}
|
2024-02-28 21:43:18 -08:00
|
|
|
|
2024-02-29 23:49:20 -08:00
|
|
|
let json_message = to_string(&modified_message).expect("Failed to serialize message");
|
|
|
|
app_handle
|
|
|
|
.emit_all("telemetry_data", json_message.clone())
|
|
|
|
.expect("Failed to send telemetry message");
|
2024-02-28 21:43:18 -08:00
|
|
|
|
2024-02-29 23:49:20 -08:00
|
|
|
println!("{}", json_message);
|
|
|
|
}
|
|
|
|
println!("disconnected");
|
|
|
|
app_handle
|
|
|
|
.emit_all("telemetry_status", "disconnected")
|
|
|
|
.expect("Failed to emit telemetry_disconnected event");
|
2024-02-28 21:43:18 -08:00
|
|
|
}
|
2024-02-27 22:54:06 -08:00
|
|
|
}
|