2024-02-28 21:43:18 -08:00
|
|
|
use network_tables::v4::{Client, SubscriptionOptions};
|
|
|
|
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 21:43:18 -08:00
|
|
|
pub async fn subscribe_topics(app_handle: AppHandle) {
|
|
|
|
let client = Client::try_new_w_config(
|
|
|
|
SocketAddrV4::new(Ipv4Addr::new(10, 12, 80, 2), 5810),
|
|
|
|
network_tables::v4::client_config::Config {
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.expect("Failed to create client");
|
2024-02-27 22:54:06 -08:00
|
|
|
|
2024-02-28 21:43:18 -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();
|
|
|
|
|
|
|
|
if let Some(stripped) = modified_message.topic_name.strip_prefix("/SmartDashboard/") {
|
|
|
|
modified_message.topic_name = stripped.to_string();
|
2024-02-27 22:54:06 -08:00
|
|
|
}
|
2024-02-28 21:43:18 -08:00
|
|
|
|
|
|
|
let json_message = to_string(&modified_message).expect("Failed to serialize message");
|
|
|
|
app_handle
|
|
|
|
.emit_all("telemetry", json_message.clone())
|
|
|
|
.expect("Failed to send telemetry message");
|
|
|
|
|
|
|
|
println!("{}", json_message);
|
|
|
|
}
|
2024-02-27 22:54:06 -08:00
|
|
|
}
|