use channels in pm3 function

This commit is contained in:
Niklas Kapelle 2025-04-16 15:31:23 +02:00
parent a9bbc61300
commit 9a26dad304
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
2 changed files with 40 additions and 8 deletions

View File

@ -1,9 +1,18 @@
use pm3::run_pm3;
use pm3::{pm3_mock, run_pm3};
use std::{sync::mpsc::channel, thread};
mod id_store;
mod parser;
mod pm3;
mod id_store;
fn main() {
run_pm3().unwrap();
let (sender, receiver) = channel();
thread::spawn(move || {
// run_pm3(sender);
pm3_mock(sender);
});
while true {
println!("{}", receiver.recv().unwrap());
}
}

View File

@ -1,8 +1,10 @@
use std::error::Error;
use std::process::{Command, Stdio};
use std::io::{self, BufRead};
use std::process::{Command, Stdio};
use std::sync::mpsc::Sender;
use std::{thread, time};
pub fn run_pm3() -> Result<(), Box<dyn Error>> {
pub fn run_pm3(sender: Sender<String>) -> Result<(), Box<dyn Error>> {
let mut cmd = Command::new("stdbuf")
.arg("-oL")
.arg("pm3")
@ -19,11 +21,16 @@ pub fn run_pm3() -> Result<(), Box<dyn Error>> {
Ok(line) => {
let parse_result = super::parser::parse_line(&line);
if let Some(uid) = parse_result {
println!("UID: {}",uid);
match sender.send(uid) {
Ok(_) => {}
Err(e) => {
eprintln!("Failed to send to channel: {}", e);
}
}
}
}
Err(e) => {
eprintln!("{}",e);
eprintln!("{}", e);
}
}
}
@ -32,7 +39,23 @@ pub fn run_pm3() -> Result<(), Box<dyn Error>> {
if status.success() {
Ok(())
}else {
} else {
Err("pm3 had non zero exit code".into())
}
}
pub fn pm3_mock(sender: Sender<String>) -> Result<(), Box<dyn Error>> {
#![allow(while_true)]
while true {
match sender.send("F1409618".to_owned()) {
Ok(()) => {}
Err(e) => {
eprintln!("Failed to send to channel: {}", e);
}
}
thread::sleep(time::Duration::from_secs(2));
}
Ok(())
}