diff --git a/Cargo.lock b/Cargo.lock index 975eed9..45b7d32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -126,6 +126,7 @@ version = "0.1.0" dependencies = [ "config", "home", + "quick-xml", "reqwest", "serde", "serde_derive", @@ -793,6 +794,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "quick-xml" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1004a344b30a54e2ee58d66a71b32d2db2feb0a31f9a2d302bf0536f15de2a33" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "quote" version = "1.0.33" @@ -835,7 +846,6 @@ dependencies = [ "once_cell", "percent-encoding", "pin-project-lite", - "rustls", "serde", "serde_json", "serde_urlencoded", @@ -850,20 +860,6 @@ dependencies = [ "winreg", ] -[[package]] -name = "ring" -version = "0.17.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "684d5e6e18f669ccebf64a92236bb7db9a34f07be010e3627368182027180866" -dependencies = [ - "cc", - "getrandom", - "libc", - "spin", - "untrusted", - "windows-sys 0.48.0", -] - [[package]] name = "ron" version = "0.7.1" @@ -904,28 +900,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "rustls" -version = "0.21.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "629648aced5775d558af50b2b4c7b02983a04b312126d45eeead26e7caa498b9" -dependencies = [ - "log", - "ring", - "rustls-webpki", - "sct", -] - -[[package]] -name = "rustls-webpki" -version = "0.101.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b6275d1ee7a1cd780b64aca7726599a1dbc893b1e64144529e55c3c2f745765" -dependencies = [ - "ring", - "untrusted", -] - [[package]] name = "ryu" version = "1.0.15" @@ -947,16 +921,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" -[[package]] -name = "sct" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da046153aa2352493d6cb7da4b6e5c0c057d8a1d0a9aa8560baffdd945acd414" -dependencies = [ - "ring", - "untrusted", -] - [[package]] name = "security-framework" version = "2.9.2" @@ -1078,12 +1042,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "spin" -version = "0.9.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" - [[package]] name = "syn" version = "2.0.39" @@ -1291,12 +1249,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "untrusted" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" - [[package]] name = "url" version = "2.5.0" diff --git a/Cargo.toml b/Cargo.toml index 551c627..4695037 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,8 @@ authors = ["Martin Brodbeck "] [dependencies] config = { version = "0.13.4", features = ["yaml"] } home = "0.5.5" -reqwest = { version = "0.11.22", features = ["json","rustls"] } +quick-xml = { version = "0.31.0", features = ["serde", "serialize"] } +reqwest = { version = "0.11.22", features = ["json"] } serde = { version = "1.0.193", features = ["serde_derive"] } serde_derive = "1.0.193" tokio = { version = "1.34.0", features = ["full"] } diff --git a/src/flrig.rs b/src/flrig.rs new file mode 100644 index 0000000..1f9b73b --- /dev/null +++ b/src/flrig.rs @@ -0,0 +1,88 @@ +use crate::cloudlog::RadioData; + +use quick_xml::events::Event; +use quick_xml::reader::Reader; +use reqwest::{self, header::CONTENT_TYPE, Error}; +use std::result::Result; + +pub fn get_radio_data(fl_host: &str, fl_port: &str) -> Result { + let cmd_freq = "rig.get_vfo"; + let cmd_mode = "rig.get_mode"; + let cmd_power = "rig.get_power"; + + let client = reqwest::Client::new(); + + let freq = get_value_from_flrig(fl_host, fl_port, &client, &cmd_freq).unwrap(); + let mode = get_value_from_flrig(fl_host, fl_port, &client, &cmd_mode).unwrap(); + let power = get_value_from_flrig(fl_host, fl_port, &client, &cmd_power).unwrap(); + + let freq = parse_xml(&freq); + let mode = parse_xml(&mode); + let power = parse_xml(&power); + + println!("{freq} --- {mode} --- {power}"); + + let radio_data = RadioData { + key: String::from(""), + radio: String::from(""), + frequency: freq, + mode: mode, + power: power, + }; + + Ok(radio_data) +} + +#[tokio::main] +async fn get_value_from_flrig( + fl_host: &str, + fl_port: &str, + client: &reqwest::Client, + cmd: &str, +) -> Result { + let xml_cmd = create_xml_cmd(&cmd); + + let res = client + .post(fl_host.to_owned() + ":" + fl_port) + .header(CONTENT_TYPE, "application/x-www-form-urlencoded") + .body(xml_cmd) + .send() + .await?; + + let body = res.text().await?; + + Ok(body) +} + +fn create_xml_cmd(cmd: &str) -> String { + format!("{cmd}") +} + +fn parse_xml(xml: &str) -> String { + let mut reader = Reader::from_str(&xml); + reader.trim_text(true); + + let mut value = String::new(); + + loop { + let mut found = false; + + match reader.read_event() { + Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e), + // exits the loop when reaching end of file + Ok(Event::Eof) => break, + + Ok(Event::Start(e)) => match e.name().as_ref() { + b"value" => found = true, + _ => (), + }, + Ok(Event::Text(e)) => value = e.unescape().unwrap().to_owned().to_string(), + + _ => (), + } + // if we don't keep a borrow elsewhere, we can clear the buffer to keep memory usage low + //buf.clear(); + } + + value +} diff --git a/src/main.rs b/src/main.rs index a3e9558..6c0fd6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod cloudlog; mod settings; +mod flrig; use settings::Settings; use crate::cloudlog::RadioData; @@ -7,7 +8,7 @@ use crate::cloudlog::RadioData; fn main() { let settings = Settings::new().expect("Could not read settings."); - let rd = RadioData { + let mut radio_data_current = RadioData { key: settings.cloudlog.key, radio: settings.cloudlog.identifier, frequency: String::from("14017000"), @@ -15,5 +16,11 @@ fn main() { power: String::from("5"), }; - cloudlog::upload(&settings.cloudlog.url, &rd); + let radio_data_new = flrig::get_radio_data(&settings.flrig.host, &settings.flrig.port).unwrap(); + + radio_data_current.frequency = radio_data_new.frequency; + radio_data_current.mode = radio_data_new.mode; + radio_data_current.power = radio_data_new.power; + + cloudlog::upload(&settings.cloudlog.url, &radio_data_current); } diff --git a/src/settings.rs b/src/settings.rs index 5267526..503cee7 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -21,7 +21,7 @@ pub struct Flrig { #[allow(unused)] pub struct Settings { pub cloudlog: Cloudlog, - flrig: Flrig, + pub flrig: Flrig, } impl Settings {