Add command pause time
This commit is contained in:
parent
6231114e0d
commit
dc30979dad
2 changed files with 34 additions and 5 deletions
|
@ -1,13 +1,14 @@
|
||||||
mod service_specific;
|
mod service_specific;
|
||||||
mod shared;
|
mod shared;
|
||||||
|
|
||||||
use std::process::Command;
|
use std::{process::Command, time::Instant};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use service_specific::*;
|
use service_specific::*;
|
||||||
use shared::config::Config;
|
use shared::config::{Config, COMMAND_COUNT, COMMAND_PAUSE};
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
// create serial port
|
||||||
let mut port = Port::open(SerialPortSettings {
|
let mut port = Port::open(SerialPortSettings {
|
||||||
baud_rate: 57600,
|
baud_rate: 57600,
|
||||||
data_bits: DataBits::Eight,
|
data_bits: DataBits::Eight,
|
||||||
|
@ -18,18 +19,26 @@ fn main() -> Result<()> {
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let mut message_builder = MessageBuilder::default();
|
let mut message_builder = MessageBuilder::default();
|
||||||
|
let mut button_infos = [Duration::default(); COMMAND_COUNT];
|
||||||
|
let start_time = Instant::now();
|
||||||
|
|
||||||
|
// loop forever
|
||||||
loop {
|
loop {
|
||||||
|
// check if port could be read
|
||||||
if let Ok(r) = port.read() {
|
if let Ok(r) = port.read() {
|
||||||
|
// handle incoming message
|
||||||
match r {
|
match r {
|
||||||
SerialReadResult::Message(msg) => {
|
SerialReadResult::Message(msg) => {
|
||||||
|
// create message out of stream
|
||||||
|
// discard remaining for now
|
||||||
if let Some(_remaining) = message_builder.check(msg) {
|
if let Some(_remaining) = message_builder.check(msg) {
|
||||||
debug_assert!(message_builder.is_complete());
|
debug_assert!(message_builder.is_complete());
|
||||||
|
|
||||||
println!("{}", message_builder.message());
|
println!("{}", message_builder.message());
|
||||||
|
|
||||||
|
// if message can be parsed to a number, try to execute a command
|
||||||
if let Ok(index) = message_builder.message().parse() {
|
if let Ok(index) = message_builder.message().parse() {
|
||||||
execute_button_press(index)?;
|
execute_button_press(index, start_time.elapsed(), &mut button_infos)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
message_builder = MessageBuilder::default();
|
message_builder = MessageBuilder::default();
|
||||||
|
@ -41,13 +50,29 @@ fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute_button_press(button_id: usize) -> Result<()> {
|
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
|
||||||
let config = Config::load_config()?;
|
let config = Config::load_config()?;
|
||||||
|
|
||||||
if button_id < 1 && button_id > 8 {
|
// check if ID is in a valid range
|
||||||
|
if button_id < 1 && button_id > COMMAND_COUNT {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
if let Some(cmd) = &config.commands[button_id - 1] {
|
if let Some(cmd) = &config.commands[button_id - 1] {
|
||||||
if !cmd.is_empty() {
|
if !cmd.is_empty() {
|
||||||
println!("cmd: {}", cmd);
|
println!("cmd: {}", cmd);
|
||||||
|
|
|
@ -5,9 +5,12 @@ use std::{
|
||||||
fs::{self, File},
|
fs::{self, File},
|
||||||
io::Write,
|
io::Write,
|
||||||
path::Path,
|
path::Path,
|
||||||
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const COMMAND_COUNT: usize = 8;
|
pub const COMMAND_COUNT: usize = 8;
|
||||||
|
#[allow(unused)]
|
||||||
|
pub const COMMAND_PAUSE: Duration = Duration::from_millis(1500);
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
@ -37,6 +40,7 @@ impl Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
pub fn save(&self, path: &str) -> Result<()> {
|
pub fn save(&self, path: &str) -> Result<()> {
|
||||||
let mut file = File::create(path)?;
|
let mut file = File::create(path)?;
|
||||||
let s = serde_json::to_string(self)?;
|
let s = serde_json::to_string(self)?;
|
||||||
|
|
Loading…
Reference in a new issue