diff --git a/src/main.rs b/src/main.rs index b9492c9..843c460 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,8 @@ use std::path::PathBuf; use std::sync::{Arc,Mutex as StdMutex}; use gstreamer_rtsp_server::prelude::SettingsExt; use tokio::sync::{Mutex, MutexGuard}; +use tokio::signal; +use futures::future::join_all; pub use config::Config; use crate::overlay::Watermark; @@ -46,13 +48,6 @@ async fn main() -> Result<()> { builder.init(); info!("Meteor detection system starting up"); - - // // Initialize logger with default settings - // env_logger::init_from_env( - // env_logger::Env::default().filter_or("RUST_LOG", "info") - // ); - // - // info!("Meteor detection system starting up"); // Check if we're running on a platform that supports GPIO if PLATFORM_SUPPORTS_GPIO { @@ -61,14 +56,6 @@ async fn main() -> Result<()> { info!("Running on a platform without GPIO support (macOS/Windows/etc). Using simulated sensors."); } - // // Load configuration - // let config = config::load_config() - // .context("Failed to load configuration")?; - // - // // Re-initialize logger with configured log level - // std::env::set_var("RUST_LOG", &config.log_level); - // env_logger::builder().init(); - // info!("Loaded configuration with device ID: {}", config.device.id); // Initialize camera subsystem @@ -169,69 +156,70 @@ async fn main() -> Result<()> { "Adds timestamp, GPS, and sensor data overlay to frames", config.watermark.enabled, move |frame, timestamp| { - // Using try_lock to avoid the async issue in a sync context + // Using block_in_place to properly handle async operations in sync context tokio::task::block_in_place(|| { let mut guard = futures::executor::block_on(watermark_clone.lock()); guard.apply(frame, timestamp) }) - // tokio::task::block_in_place(|| { - // match watermark_clone.try_lock() { - // Ok(mut guard) => guard.apply(frame, timestamp), - // Err(e) => Err(anyhow::anyhow!("Failed to lock watermark mutex: {}", e)) - // } - // }) - // tokio::task::block_in_place(|| { - // match futures::executor::block_on(watermark_clone.lock()) { - // Ok(mut guard) => guard.apply(frame, timestamp), - // Err(e) => Err(anyhow::anyhow!("Failed to lock watermark mutex: {}", e)) - // } - // }) - // tokio::task::block_in_place(|| { - // let mutex_guard = futures::executor::block_on(watermark_clone.lock()); - // match mutex_guard { - // Ok(mut guard) => guard.apply(frame, timestamp), - // Err(e) => Err(anyhow::anyhow!("Failed to lock watermark mutex: {}", e)) - // } - // }) }, ))); } + // Spawn detection pipeline task tasks.push(tokio::spawn(async move { if let Err(e) = detection_pipeline.run().await { error!("Detection pipeline error: {}", e); } })); + // Spawn communication manager task tasks.push(tokio::spawn(async move { if let Err(e) = comms.run().await { error!("Communication manager error: {}", e); } })); + // Spawn system monitor task tasks.push(tokio::spawn(async move { if let Err(e) = monitor.run().await { error!("System monitor error: {}", e); } })); - // Add RTSP streaming task - let rtsp_server_clone = rtsp_server.clone(); + // Add RTSP streaming task with proper loop to keep it alive + let rtsp_config_enabled = config.rtsp.enabled; tasks.push(tokio::spawn(async move { - if config.rtsp.enabled { + if rtsp_config_enabled { info!("Starting RTSP streaming task"); - // This task would feed frames to the RTSP server from the frame buffer - // Implementation placeholder for now + + // Implement a proper continuous loop to keep the task alive + loop { + // This would normally feed frames to the RTSP server + // For now, just sleep to keep the task alive + tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + + // Add some actual implementation here later + } } })); - // Wait for all tasks to complete (they generally shouldn't unless there's an error) - for task in tasks { - if let Err(e) = task.await { - error!("Task panicked: {}", e); + // Wait for the Ctrl+C signal + info!("Press Ctrl+C to stop the application"); + match signal::ctrl_c().await { + Ok(()) => { + info!("Shutdown signal received, preparing to exit"); + // Here you would add cleanup code + } + Err(err) => { + error!("Error waiting for Ctrl+C: {}", err); } } - error!("Main loop exited, this should not happen under normal circumstances"); + // Now that we've received a shutdown signal, cancel all tasks + for task in tasks { + task.abort(); + } + + info!("Meteor detection system shutting down"); Ok(()) }