diff --git a/receiver/Cargo.lock b/receiver/Cargo.lock index 6bd6822..f1b4890 100644 --- a/receiver/Cargo.lock +++ b/receiver/Cargo.lock @@ -446,6 +446,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.139" @@ -611,6 +617,7 @@ dependencies = [ "pbkdf2", "serde", "serde_json", + "simulate", "uuid", ] @@ -685,6 +692,16 @@ dependencies = [ "sha2 0.9.9", ] +[[package]] +name = "simulate" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbee16431539949a38f46684d2cd31deeb822c12c5ef33fd6eb2e418987f5691" +dependencies = [ + "lazy_static", + "winapi", +] + [[package]] name = "slab" version = "0.4.7" diff --git a/receiver/Cargo.toml b/receiver/Cargo.toml index f4a34bc..f67edd9 100644 --- a/receiver/Cargo.toml +++ b/receiver/Cargo.toml @@ -15,3 +15,4 @@ uuid = { version = "1.3.0", features = ["serde", "v4"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" enigo = "0.0.14" +simulate = "0.3.0" diff --git a/receiver/src/main.rs b/receiver/src/main.rs index 5c1ed69..00138f6 100644 --- a/receiver/src/main.rs +++ b/receiver/src/main.rs @@ -1,12 +1,8 @@ -use std::{env, process, time::Duration, fmt::format}; -use enigo::KeyboardControllable; use serde::{Deserialize, Serialize}; -use serde_json::Result; +use std::time::Duration; extern crate paho_mqtt as mqtt; -const BROKER: &str = "tcp://broker.emqx.io:1883"; - #[derive(Serialize, Deserialize, PartialEq)] enum MqttMessageKind { Time, @@ -20,10 +16,11 @@ struct MqttMessage { time: u32, } -fn init_mqtt(uri: &str, topic: &str) -> mqtt::Result<(mqtt::Client, mqtt::Receiver>)> { - let create_opts = mqtt::CreateOptionsBuilder::new() - .server_uri(uri) - .finalize(); +fn init_mqtt( + uri: &str, + topic: &str, +) -> mqtt::Result<(mqtt::Client, mqtt::Receiver>)> { + let create_opts = mqtt::CreateOptionsBuilder::new().server_uri(uri).finalize(); // Create a client. let cli = mqtt::Client::new(create_opts)?; @@ -44,13 +41,18 @@ fn init_mqtt(uri: &str, topic: &str) -> mqtt::Result<(mqtt::Client, mqtt::Receiv Ok((cli, rx)) } +fn millis_to_string(millis: u32) -> String { + let seconds = (millis as f64) / 1000.0; + let formatted = format!("{:.2}", seconds); + formatted.replace(".", ",") +} + fn main() { let password = "test"; let broker_domain = "broker.emqx.io"; let broker_url = format!("tcp://{}:1883", broker_domain); - let mut en = enigo::Enigo::new(); - let c = crypto_helper::crypto::Crypto::new("test", "broker.emqx.io"); + let c = crypto_helper::crypto::Crypto::new(password, broker_domain); let topic = format!("org.speedclimbing.ok-ready-go.{password}"); let topic = crypto_helper::crypto::Crypto::sha256(&c.encrypt(&topic)); @@ -70,10 +72,12 @@ fn main() { } println!("Got time: {} with id {}", msg.time, msg.id); - let time_with_comma = format!("{},{}", msg.time / 1000, (msg.time/10) % 100); - en.key_sequence(&time_with_comma); - en.key_click(enigo::Key::Tab); - en.key_click(enigo::Key::Tab); + let time_with_comma = millis_to_string(msg.time); + + println!("Trying to type {time_with_comma}"); + simulate::type_str(&time_with_comma); + simulate::send(simulate::Key::Tab); + simulate::send(simulate::Key::Tab); let reply = MqttMessage { id: msg.id, @@ -83,13 +87,11 @@ fn main() { let reply = serde_json::to_string(&reply).unwrap(); let reply = c.encrypt(&reply); - println!("Sending reply: {reply}"); + //println!("Sending reply: {reply}"); let reply = mqtt::Message::new(&topic, reply, 1); cli.publish(reply).expect("publish"); - } - else if !cli.is_connected() { + } else if !cli.is_connected() { break; } } - }