use tokios command crate instead of the std one

also removed pm3_mock as it is no longer needed
This commit is contained in:
Niklas Kapelle 2025-04-27 15:51:07 +02:00
parent 5e82036458
commit 6ad018db7a
Signed by: niklas
GPG Key ID: 4EB651B36D841D16

View File

@ -1,10 +1,10 @@
use log::{debug, error, info, trace}; use log::{info, trace};
use std::env; use std::env;
use std::error::Error; use std::error::Error;
use std::io::{self, BufRead}; use std::process::Stdio;
use std::process::{Command, Stdio}; use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio::time::{Duration, sleep};
/// Runs the pm3 binary and monitors it's output /// Runs the pm3 binary and monitors it's output
/// The pm3 binary is ether set in the env var PM3_BIN or found in the path /// The pm3 binary is ether set in the env var PM3_BIN or found in the path
@ -27,30 +27,17 @@ pub async fn run_pm3(tx: mpsc::Sender<String>) -> Result<(), Box<dyn Error>> {
.spawn()?; .spawn()?;
let stdout = cmd.stdout.take().ok_or("Failed to get stdout")?; let stdout = cmd.stdout.take().ok_or("Failed to get stdout")?;
let reader = io::BufReader::new(stdout);
for line_result in reader.lines() { let mut reader = BufReader::new(stdout).lines();
match line_result {
Ok(line) => { while let Some(line) = reader.next_line().await? {
trace!("PM3: {}", line); trace!("PM3: {}", line);
let parse_result = super::parser::parse_line(&line); if let Some(uid) = super::parser::parse_line(&line) {
if let Some(uid) = parse_result { tx.send(uid).await?;
debug!("Read ID: {}", uid);
match tx.send(uid).await {
Ok(()) => {}
Err(e) => {
error!("Failed to send to channel: {}", e);
}
}
}
}
Err(e) => {
error!("Failed to read line from PM3: {}", e);
}
} }
} }
let status = cmd.wait()?; let status = cmd.wait().await?;
if status.success() { if status.success() {
Ok(()) Ok(())
@ -58,17 +45,3 @@ pub async fn run_pm3(tx: mpsc::Sender<String>) -> Result<(), Box<dyn Error>> {
Err("PM3 exited with a non zero exit code".into()) Err("PM3 exited with a non zero exit code".into())
} }
} }
/// Mocks the `run_pm3` command. Outputs the same ID every second.
pub async fn pm3_mock(tx: mpsc::Sender<String>) -> Result<(), Box<dyn Error>> {
loop {
match tx.send("F1409618".to_owned()).await {
Ok(()) => {}
Err(e) => {
error!("Failed to send to channel: {}", e);
}
}
sleep(Duration::from_millis(1000)).await;
}
}