proof of concept

This commit is contained in:
Martin Brodbeck 2023-11-30 12:50:26 +01:00
parent d2b4092cd7
commit a617025d5c
5 changed files with 111 additions and 63 deletions

70
Cargo.lock generated
View file

@ -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"

View file

@ -7,7 +7,8 @@ authors = ["Martin Brodbeck <dg2smb@darc.de>"]
[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"] }

88
src/flrig.rs Normal file
View file

@ -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<RadioData, Error> {
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<String, Error> {
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!("<?xml version=\"1.0\"?><methodCall><methodName>{cmd}</methodName><params></params></methodCall>")
}
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
}

View file

@ -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);
}

View file

@ -21,7 +21,7 @@ pub struct Flrig {
#[allow(unused)]
pub struct Settings {
pub cloudlog: Cloudlog,
flrig: Flrig,
pub flrig: Flrig,
}
impl Settings {