feat: able to fetch data through network tables

This commit is contained in:
Youwen Wu 2024-02-28 21:43:18 -08:00
parent 4fc84c7dba
commit 7278b978d9
4 changed files with 180 additions and 36 deletions

View file

@ -71,6 +71,9 @@ dependencies = [
"serde_json", "serde_json",
"tauri", "tauri",
"tauri-build", "tauri-build",
"tokio",
"tracing",
"tracing-subscriber",
] ]
[[package]] [[package]]
@ -789,6 +792,12 @@ dependencies = [
"syn 2.0.50", "syn 2.0.50",
] ]
[[package]]
name = "futures-sink"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5"
[[package]] [[package]]
name = "futures-task" name = "futures-task"
version = "0.3.30" version = "0.3.30"
@ -803,6 +812,7 @@ checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-macro", "futures-macro",
"futures-sink",
"futures-task", "futures-task",
"pin-project-lite", "pin-project-lite",
"pin-utils", "pin-utils",
@ -1185,6 +1195,12 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573" checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573"
[[package]]
name = "httparse"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904"
[[package]] [[package]]
name = "iana-time-zone" name = "iana-time-zone"
version = "0.1.60" version = "0.1.60"
@ -1577,8 +1593,14 @@ dependencies = [
"futures-util", "futures-util",
"parking_lot", "parking_lot",
"rand 0.8.5", "rand 0.8.5",
"rmp",
"rmp-serde",
"rmpv",
"serde",
"serde_json",
"thiserror", "thiserror",
"tokio", "tokio",
"tokio-tungstenite",
"tracing", "tracing",
] ]
@ -1747,6 +1769,12 @@ dependencies = [
"windows-targets 0.48.5", "windows-targets 0.48.5",
] ]
[[package]]
name = "paste"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c"
[[package]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.3.1" version = "2.3.1"
@ -2168,6 +2196,40 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "rmp"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f9860a6cc38ed1da53456442089b4dfa35e7cedaa326df63017af88385e6b20"
dependencies = [
"byteorder",
"num-traits",
"paste",
]
[[package]]
name = "rmp-serde"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bffea85eea980d8a74453e5d02a8d93028f3c34725de143085a844ebe953258a"
dependencies = [
"byteorder",
"rmp",
"serde",
]
[[package]]
name = "rmpv"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e0e0214a4a2b444ecce41a4025792fc31f77c7bb89c46d253953ea8c65701ec"
dependencies = [
"num-traits",
"rmp",
"serde",
"serde_bytes",
]
[[package]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.23" version = "0.1.23"
@ -2273,6 +2335,15 @@ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]]
name = "serde_bytes"
version = "0.11.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b8497c313fd43ab992087548117643f6fcd935cbf36f176ffda0aacf9591734"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.197" version = "1.0.197"
@ -2378,6 +2449,17 @@ dependencies = [
"stable_deref_trait", "stable_deref_trait",
] ]
[[package]]
name = "sha1"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]] [[package]]
name = "sha2" name = "sha2"
version = "0.10.8" version = "0.10.8"
@ -2398,6 +2480,15 @@ dependencies = [
"lazy_static", "lazy_static",
] ]
[[package]]
name = "signal-hook-registry"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "simd-adler32" name = "simd-adler32"
version = "0.3.7" version = "0.3.7"
@ -2936,6 +3027,7 @@ dependencies = [
"num_cpus", "num_cpus",
"parking_lot", "parking_lot",
"pin-project-lite", "pin-project-lite",
"signal-hook-registry",
"socket2", "socket2",
"tokio-macros", "tokio-macros",
"windows-sys 0.48.0", "windows-sys 0.48.0",
@ -2952,6 +3044,18 @@ dependencies = [
"syn 2.0.50", "syn 2.0.50",
] ]
[[package]]
name = "tokio-tungstenite"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54319c93411147bced34cb5609a80e0a8e44c5999c93903a81cd866630ec0bfd"
dependencies = [
"futures-util",
"log",
"tokio",
"tungstenite",
]
[[package]] [[package]]
name = "toml" name = "toml"
version = "0.5.11" version = "0.5.11"
@ -3090,6 +3194,25 @@ dependencies = [
"serde_json", "serde_json",
] ]
[[package]]
name = "tungstenite"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30ee6ab729cd4cf0fd55218530c4522ed30b7b6081752839b68fcec8d0960788"
dependencies = [
"base64 0.13.1",
"byteorder",
"bytes",
"http",
"httparse",
"log",
"rand 0.8.5",
"sha1",
"thiserror",
"url",
"utf-8",
]
[[package]] [[package]]
name = "typenum" name = "typenum"
version = "1.17.0" version = "1.17.0"

View file

@ -18,7 +18,10 @@ tauri-build = { version = "1.5.1", features = [] }
serde_json = "1.0" serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.6.0", features = [] } tauri = { version = "1.6.0", features = [] }
network-tables = "0.1.3" tokio = { version = "1.23.0", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
network-tables = { version = "=0.1.3", features = ["client-v4"] }
[features] [features]
# this feature is used for production builds or when `devPath` points to the filesystem and the built-in dev server is disabled. # this feature is used for production builds or when `devPath` points to the filesystem and the built-in dev server is disabled.

View file

@ -2,7 +2,6 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use tauri::Manager; use tauri::Manager;
mod telemetry; mod telemetry;
#[derive(Clone, serde::Serialize)] #[derive(Clone, serde::Serialize)]
@ -11,14 +10,21 @@ struct Payload {
} }
fn main() { fn main() {
let rt = tokio::runtime::Runtime::new().expect("Failed to create Tokio runtime");
rt.block_on(async {
tauri::Builder::default() tauri::Builder::default()
.setup(|app| { .setup(|app| {
// create app handle and send it to our event listeners // create app handle and send it to our event listeners
let app_handle = app.app_handle(); let app_handle = app.app_handle();
telemetry::subscribe_topics(&app_handle);
tokio::spawn(async move {
crate::telemetry::subscribe_topics(app_handle.clone()).await;
});
Ok(()) Ok(())
}) })
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("failed to run app") .expect("failed to run app")
})
} }

View file

@ -1,30 +1,42 @@
use network_tables::v4::{Client, SubscriptionOptions};
use serde_json::to_string;
use std::net::{Ipv4Addr, SocketAddrV4};
use tauri::{AppHandle, Manager}; use tauri::{AppHandle, Manager};
pub fn subscribe_topics(app: &AppHandle) { pub async fn subscribe_topics(app_handle: AppHandle) {
let app_handle = app.clone(); 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");
app_handle.listen_global("subscribe", |event| { let mut subscription = client
let parsed: Result<serde_json::Value, serde_json::Error> = .subscribe_w_options(
serde_json::from_str(event.payload().unwrap()); &["/SmartDashboard"],
match parsed { Some(SubscriptionOptions {
Ok(value) => { all: Some(true),
// handle the successfully parsed value prefix: Some(true),
let topics = value.as_object(); ..Default::default()
match topics { }),
Some(topics) => { )
for (key, value) in topics { .await
println!("{}: {}", key, value); .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();
}
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);
} }
} }
None => {
println!("No topics requested!");
}
}
}
Err(err) => {
// handle the error
println!("{:?}", err);
}
}
});
}