修复启动后立即关闭的问题

This commit is contained in:
grabbit 2025-04-05 13:47:56 +08:00
parent 2ee09e36cc
commit f97a49452a

View File

@ -24,6 +24,8 @@ use std::path::PathBuf;
use std::sync::{Arc,Mutex as StdMutex}; use std::sync::{Arc,Mutex as StdMutex};
use gstreamer_rtsp_server::prelude::SettingsExt; use gstreamer_rtsp_server::prelude::SettingsExt;
use tokio::sync::{Mutex, MutexGuard}; use tokio::sync::{Mutex, MutexGuard};
use tokio::signal;
use futures::future::join_all;
pub use config::Config; pub use config::Config;
use crate::overlay::Watermark; use crate::overlay::Watermark;
@ -47,13 +49,6 @@ async fn main() -> Result<()> {
info!("Meteor detection system starting up"); 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 // Check if we're running on a platform that supports GPIO
if PLATFORM_SUPPORTS_GPIO { if PLATFORM_SUPPORTS_GPIO {
info!("Running on a Linux platform with GPIO support"); info!("Running on a Linux platform with GPIO support");
@ -61,14 +56,6 @@ async fn main() -> Result<()> {
info!("Running on a platform without GPIO support (macOS/Windows/etc). Using simulated sensors."); 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); info!("Loaded configuration with device ID: {}", config.device.id);
// Initialize camera subsystem // Initialize camera subsystem
@ -169,69 +156,70 @@ async fn main() -> Result<()> {
"Adds timestamp, GPS, and sensor data overlay to frames", "Adds timestamp, GPS, and sensor data overlay to frames",
config.watermark.enabled, config.watermark.enabled,
move |frame, timestamp| { 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(|| { tokio::task::block_in_place(|| {
let mut guard = futures::executor::block_on(watermark_clone.lock()); let mut guard = futures::executor::block_on(watermark_clone.lock());
guard.apply(frame, timestamp) 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 { tasks.push(tokio::spawn(async move {
if let Err(e) = detection_pipeline.run().await { if let Err(e) = detection_pipeline.run().await {
error!("Detection pipeline error: {}", e); error!("Detection pipeline error: {}", e);
} }
})); }));
// Spawn communication manager task
tasks.push(tokio::spawn(async move { tasks.push(tokio::spawn(async move {
if let Err(e) = comms.run().await { if let Err(e) = comms.run().await {
error!("Communication manager error: {}", e); error!("Communication manager error: {}", e);
} }
})); }));
// Spawn system monitor task
tasks.push(tokio::spawn(async move { tasks.push(tokio::spawn(async move {
if let Err(e) = monitor.run().await { if let Err(e) = monitor.run().await {
error!("System monitor error: {}", e); error!("System monitor error: {}", e);
} }
})); }));
// Add RTSP streaming task // Add RTSP streaming task with proper loop to keep it alive
let rtsp_server_clone = rtsp_server.clone(); let rtsp_config_enabled = config.rtsp.enabled;
tasks.push(tokio::spawn(async move { tasks.push(tokio::spawn(async move {
if config.rtsp.enabled { if rtsp_config_enabled {
info!("Starting RTSP streaming task"); 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) // Wait for the Ctrl+C signal
for task in tasks { info!("Press Ctrl+C to stop the application");
if let Err(e) = task.await { match signal::ctrl_c().await {
error!("Task panicked: {}", e); 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(()) Ok(())
} }