MacroBoard-rs/src/service.rs

86 lines
2.6 KiB
Rust
Raw Normal View History

2019-11-24 14:54:36 +00:00
mod service_specific;
2022-07-18 10:11:17 +00:00
mod shared;
2022-07-20 06:52:29 +00:00
use std::{process::Command, time::Instant};
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-20 06:52:29 +00:00
use shared::config::{Config, COMMAND_COUNT, COMMAND_PAUSE};
2019-11-22 10:24:12 +00:00
2022-07-15 17:08:22 +00:00
fn main() -> Result<()> {
2022-07-20 06:52:29 +00:00
// create serial port
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,
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();
2022-07-20 06:52:29 +00:00
let mut button_infos = [Duration::default(); COMMAND_COUNT];
let start_time = Instant::now();
2019-11-23 22:31:21 +00:00
2022-07-20 06:52:29 +00:00
// loop forever
2019-11-23 22:31:21 +00:00
loop {
2022-07-20 06:52:29 +00:00
// check if port could be read
2022-07-18 10:11:17 +00:00
if let Ok(r) = port.read() {
2022-07-20 06:52:29 +00:00
// handle incoming message
2022-07-18 10:11:17 +00:00
match r {
SerialReadResult::Message(msg) => {
2022-07-20 06:52:29 +00:00
// create message out of stream
// discard remaining for now
2022-07-18 10:11:17 +00:00
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-20 06:52:29 +00:00
// if message can be parsed to a number, try to execute a command
2022-07-18 10:11:17 +00:00
if let Ok(index) = message_builder.message().parse() {
2022-07-20 06:52:29 +00:00
execute_button_press(index, start_time.elapsed(), &mut button_infos)?;
2022-07-18 10:11:17 +00:00
}
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
2022-07-20 06:52:29 +00:00
fn execute_button_press(
button_id: usize,
time: Duration,
button_infos: &mut [Duration; COMMAND_COUNT],
) -> Result<()> {
// load config every button
// since it could be modified from the gui application
2022-07-18 10:11:17 +00:00
let config = Config::load_config()?;
2022-07-20 06:52:29 +00:00
// check if ID is in a valid range
if button_id < 1 && button_id > COMMAND_COUNT {
2022-07-18 10:11:17 +00:00
return Ok(());
}
2022-07-20 06:52:29 +00:00
// check that this commands last execution is longer than COMMAND_PAUSE ago
if (button_infos[button_id] + COMMAND_PAUSE) < time {
button_infos[button_id] = time;
} else {
return Ok(());
}
// check command at ID position if a command is set
// if so, simply execute this command as child process
2022-07-18 10:11:17 +00:00
if let Some(cmd) = &config.commands[button_id - 1] {
if !cmd.is_empty() {
println!("cmd: {}", cmd);
Command::new(cmd).spawn()?;
}
}
Ok(())
}