feat: use HACI_SERIAL_PORT to pick serial port and support mock PTY

Co-authored-by: aider (openai/gpt-5) <aider@aider.chat>
This commit is contained in:
Chaos Rogers 2025-10-22 00:12:36 +01:00
parent d63a019584
commit ee4a5a3630

View file

@ -18,6 +18,7 @@ use daemonize::Daemonize;
use serialport; use serialport;
use std::{ use std::{
env,
fs, fs,
path::Path, path::Path,
sync::{mpsc, Arc, Mutex}, sync::{mpsc, Arc, Mutex},
@ -74,6 +75,28 @@ fn ensure_config() {
} }
fn find_serial_port() -> Option<String> { fn find_serial_port() -> Option<String> {
// Prefer environment variable if provided (e.g., by entrypoint for mock PTY)
if let Ok(path) = env::var("HACI_SERIAL_PORT") {
let p = Path::new(&path);
if !p.exists() {
// Wait up to ~5 seconds for the PTY to appear
for _ in 0..50 {
if p.exists() {
break;
}
thread::sleep(std::time::Duration::from_millis(100));
}
}
if p.exists() {
return Some(path);
} else {
log::warn!(
"HACI_SERIAL_PORT='{}' does not exist. Falling back to port scan.",
path
);
}
}
if let Ok(ports) = serialport::available_ports() { if let Ok(ports) = serialport::available_ports() {
for p in ports { for p in ports {
if p.port_name.starts_with("/dev/ttyACM") if p.port_name.starts_with("/dev/ttyACM")
@ -172,7 +195,7 @@ async fn main() {
let serial_port_path = match find_serial_port() { let serial_port_path = match find_serial_port() {
Some(port) => port, Some(port) => port,
None => { None => {
log::error!("❌ No serial port found. Please connect the Teensy device."); log::error!("❌ No serial port found. Set HACI_SERIAL_PORT to a device path (e.g., /dev/ttyACM0) or connect the Teensy device.");
return; return;
} }
}; };