2019-11-24 14:54:36 +00:00
|
|
|
mod service_specific;
|
2022-07-18 10:11:17 +00:00
|
|
|
mod shared;
|
|
|
|
|
|
|
|
use std::process::Command;
|
2019-11-22 10:24:12 +00:00
|
|
|
|
2022-07-15 17:08:22 +00:00
|
|
|
use anyhow::Result;
|
2019-11-24 14:54:36 +00:00
|
|
|
use service_specific::*;
|
2022-07-18 10:11:17 +00:00
|
|
|
use shared::config::Config;
|
2019-11-22 10:24:12 +00:00
|
|
|
|
2022-07-15 17:08:22 +00:00
|
|
|
fn main() -> Result<()> {
|
2019-11-24 14:54:36 +00:00
|
|
|
let mut port = Port::open(SerialPortSettings {
|
2022-07-16 15:05:31 +00:00
|
|
|
baud_rate: 57600,
|
2019-11-24 07:10:54 +00:00
|
|
|
data_bits: DataBits::Eight,
|
|
|
|
parity: Parity::None,
|
|
|
|
stop_bits: StopBits::One,
|
|
|
|
flow_control: FlowControl::None,
|
|
|
|
timeout: Duration::from_millis(2500),
|
2019-11-24 14:54:36 +00:00
|
|
|
})?;
|
2019-11-23 22:31:21 +00:00
|
|
|
|
2019-11-24 14:54:36 +00:00
|
|
|
let mut message_builder = MessageBuilder::default();
|
2019-11-23 22:31:21 +00:00
|
|
|
|
|
|
|
loop {
|
2022-07-18 10:11:17 +00:00
|
|
|
if let Ok(r) = port.read() {
|
|
|
|
match r {
|
|
|
|
SerialReadResult::Message(msg) => {
|
|
|
|
if let Some(_remaining) = message_builder.check(msg) {
|
|
|
|
debug_assert!(message_builder.is_complete());
|
|
|
|
|
|
|
|
println!("{}", message_builder.message());
|
2019-11-24 14:54:36 +00:00
|
|
|
|
2022-07-18 10:11:17 +00:00
|
|
|
if let Ok(index) = message_builder.message().parse() {
|
|
|
|
execute_button_press(index)?;
|
|
|
|
}
|
2019-11-24 14:54:36 +00:00
|
|
|
|
2022-07-18 10:11:17 +00:00
|
|
|
message_builder = MessageBuilder::default();
|
|
|
|
}
|
2019-11-24 14:54:36 +00:00
|
|
|
}
|
2022-07-18 10:11:17 +00:00
|
|
|
SerialReadResult::Timeout => (),
|
2019-11-24 14:54:36 +00:00
|
|
|
}
|
2019-11-23 22:31:21 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-22 10:24:12 +00:00
|
|
|
}
|
2022-07-18 10:11:17 +00:00
|
|
|
|
|
|
|
fn execute_button_press(button_id: usize) -> Result<()> {
|
|
|
|
let config = Config::load_config()?;
|
|
|
|
|
|
|
|
if button_id < 1 && button_id > 8 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(cmd) = &config.commands[button_id - 1] {
|
|
|
|
if !cmd.is_empty() {
|
|
|
|
println!("cmd: {}", cmd);
|
|
|
|
|
|
|
|
Command::new(cmd).spawn()?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|