use std::time::{Duration, SystemTime};
use deimos::{
Controller, LoopMethod, Termination, ThreadChannelSocket,
controller::context::ControllerCtx,
dispatcher::CsvDispatcher,
peripheral::{DeimosDaqRev7, HootlTransport, Peripheral},
};
fn main() {
if std::env::var("RUST_LOG").is_err() {
unsafe { std::env::set_var("RUST_LOG", "info") };
}
let op_dir = std::env::temp_dir().join("deimos_hootl_lifecycle");
std::fs::create_dir_all(&op_dir).expect("Failed to create temp op_dir");
let rate_hz = 20.0;
let mut ctx = ControllerCtx::default();
ctx.op_name = "hootl_lifecycle".to_string();
ctx.op_dir = op_dir;
ctx.dt_ns = (1e9_f64 / rate_hz).ceil() as u32;
ctx.loop_method = LoopMethod::Efficient;
ctx.termination_criteria = Some(Termination::Timeout(Duration::from_secs(3)));
let mut controller = Controller::new(ctx);
controller.clear_sockets();
let peripheral = DeimosDaqRev7 { serial_number: 1 };
let peripheral_id = peripheral.id();
controller.add_socket(
&ThreadChannelSocket::socket_name(peripheral_id),
Box::new(ThreadChannelSocket::new(peripheral_id)),
);
controller
.add_peripheral("p1", Box::new(peripheral))
.unwrap();
let csv_dispatcher = CsvDispatcher::new(50, deimos::Overflow::Wrap);
controller.add_dispatcher("csv", csv_dispatcher);
let end = Some(SystemTime::now() + Duration::from_secs(5));
let mut hootl_handle = controller
.attach_hootl_driver("p1", HootlTransport::thread_channel(), end)
.expect("Failed to attach HOOTL driver");
let result = controller.run(&None, None);
hootl_handle.join().expect("HOOTL runner thread panicked");
match result {
Ok(_) => println!("Controller exited cleanly."),
Err(e) => eprintln!("Controller exited with error: {e}"),
}
}