From 4b56e1bc5d35b02c01a0985773f93877f62a253f Mon Sep 17 00:00:00 2001 From: Nickiel12 <35903114+Nickiel12@users.noreply.github.com> Date: Sat, 18 May 2024 21:13:45 -0700 Subject: [PATCH] small improvements, but tracker state do_send swapped? --- settings.toml | 7 +++++-- src/config.rs | 2 +- src/coordinator/mod.rs | 19 ++++++++++++++----- src/coordinator/remote_video_processor.rs | 21 ++++++++++++++++++--- src/ui/settings_modal.rs | 3 ++- src/ui/tracker_panel.rs | 5 ++++- 6 files changed, 44 insertions(+), 13 deletions(-) diff --git a/settings.toml b/settings.toml index 5cebaaa..38593c3 100644 --- a/settings.toml +++ b/settings.toml @@ -1,2 +1,5 @@ -ip = "localhost" -port = 8765 +camera_ip = "10.0.0.33" +camera_port = 8765 +tracker_ip = "localhost" +tracker_port = 6543 +tracker_refresh_rate_millis = 100 diff --git a/src/config.rs b/src/config.rs index 93e05aa..c0b29ef 100644 --- a/src/config.rs +++ b/src/config.rs @@ -22,7 +22,7 @@ impl Default for AppConfig { camera_port: 8765, tracker_ip: "10.0.0.210".to_string(), - tracker_port: 8765, + tracker_port: 6543, tracker_refresh_rate_millis: 100, } } diff --git a/src/coordinator/mod.rs b/src/coordinator/mod.rs index 2bbd0f1..e45403b 100644 --- a/src/coordinator/mod.rs +++ b/src/coordinator/mod.rs @@ -73,7 +73,8 @@ struct CoordState<'a> { pub rt: Handle, pub pipeline: gstreamer_pipeline::WebcamPipeline, - pub keep_windows_pipe_alive: Arc, + pub tracker_keep_alive: Arc, + pub tracker_is_alive: Arc, pub tracker_state: Arc>, } @@ -104,7 +105,8 @@ impl<'a> CoordState<'a> { rt, pipeline: gstreamer_pipeline::WebcamPipeline::new(), - keep_windows_pipe_alive: Arc::new(AtomicBool::new(true)), + tracker_keep_alive: Arc::new(AtomicBool::new(false)), + tracker_is_alive: Arc::new(AtomicBool::new(false)), tracker_state, }; @@ -180,7 +182,8 @@ impl<'a> CoordState<'a> { conn_string, self.pipeline.sink_frame.clone(), self.to_mec.clone(), - self.keep_windows_pipe_alive.clone(), + self.tracker_keep_alive.clone(), + self.tracker_is_alive.clone(), self.tracker_state.clone(), self.rt.clone(), )); @@ -195,7 +198,11 @@ impl<'a> CoordState<'a> { )); } - if !self.keep_windows_pipe_alive.load(Ordering::SeqCst) {} + if !self.tracker_is_alive.load(Ordering::SeqCst) { + if self.tracker_keep_alive.load(Ordering::SeqCst) { + self.start_video_loop().await; + } + } if !self.sck_alive_server.load(Ordering::SeqCst) { info!("Restarting socket server"); @@ -222,7 +229,7 @@ impl<'a> CoordState<'a> { pub async fn close(&mut self) { info!("closing coord state"); - self.keep_windows_pipe_alive.store(false, Ordering::SeqCst); + self.tracker_keep_alive.store(false, Ordering::SeqCst); self.socket_close().await; self.joystick_loop_alive.store(false, Ordering::SeqCst); @@ -289,7 +296,9 @@ pub async fn start_coordinator( debug!("Trying to get lock on tracker_state for enable automatic"); if let Ok(mut ts) = state.tracker_state.lock() { ts.enabled = do_enable; + state.tracker_keep_alive.store(do_enable, Ordering::SeqCst); } + state.check_states().await; } ApplicationEvent::MoveEvent(coord, priority) => { // If Automatic control, but local event happens, override the automatice events for 2 seconds diff --git a/src/coordinator/remote_video_processor.rs b/src/coordinator/remote_video_processor.rs index 2763018..4739d02 100644 --- a/src/coordinator/remote_video_processor.rs +++ b/src/coordinator/remote_video_processor.rs @@ -28,19 +28,22 @@ pub async fn remote_video_loop( appsink: Arc>, to_mec: Sender, keep_alive: Arc, + is_alive: Arc, tracker_state: Arc>, runtime: Handle, ) { + info!("Starting remote tracker processing connection to: {}", conn_string); let video_info = gstreamer_video::VideoInfo::builder(gstreamer_video::VideoFormat::Rgb, 640, 480) .build() .expect("Could not build video info!"); loop { + is_alive.store(true, Ordering::SeqCst); match connect_async(&conn_string).await { Err(e) => { - error!("Could not connect to remote video loop! Trying again in 5 seconds: {e}"); - sleep_until(Instant::now() + Duration::from_secs(5)).await; + error!("Could not connect to remote video loop! Trying again in 1 seconds: {e}"); + sleep_until(Instant::now() + Duration::from_secs(1)).await; } Ok((connection, _)) => { let (mut sender, recvr) = connection.split(); @@ -92,6 +95,11 @@ pub async fn remote_video_loop( return; } + + if !keep_alive.load(Ordering::SeqCst) { + info!("Shutting down remote video loop"); + break; + } // rate limit updates sleep_until(Instant::now() + Duration::from_millis(50)).await; } @@ -102,6 +110,7 @@ pub async fn remote_video_loop( break; } } + is_alive.store(false, Ordering::SeqCst); } async fn listen_to_messages( @@ -110,14 +119,19 @@ async fn listen_to_messages( tracker_state: Arc>, keep_alive: Arc, ) { + info!("Starting tracker connection listen"); while keep_alive.load(Ordering::SeqCst) { match recvr.try_next().await { Ok(Some(message)) => { - let (x_off, y_off, do_send) = + let (x_off, y_off, _do_send) = process_incoming_string(message.to_string(), &tracker_state) .and_then(|_| calculate_tracking(&tracker_state)) .unwrap_or((0, 0, false)); + let do_send = true; + + // For some reason, this do_send is inverted from what it should be + // info!("Do Send is: {}", do_send.to_string()); if do_send { if let Err(e) = to_mec .send(ApplicationEvent::MoveEvent( @@ -138,6 +152,7 @@ async fn listen_to_messages( } } } + info!("Stopping tracker connection listen with keep alive: {}", keep_alive.load(Ordering::SeqCst)); } fn get_video_frame( diff --git a/src/ui/settings_modal.rs b/src/ui/settings_modal.rs index c43a2b7..2ee6dc2 100644 --- a/src/ui/settings_modal.rs +++ b/src/ui/settings_modal.rs @@ -120,7 +120,7 @@ impl ConnectionsModal { main_box.append(&quit_button); let new_ref = app_config.clone(); - quit_button.connect_activate(clone!( + quit_button.connect_clicked(clone!( @strong rt, @weak camera_ip_entry, @weak camera_port_entry, @weak tracker_ip_entry, @weak tracker_port_entry, @@ -132,6 +132,7 @@ impl ConnectionsModal { let new_tracker_ip = tracker_ip_entry.text().to_string(); let new_tracker_port = tracker_port_entry.text().parse::().unwrap(); let new_tracker_millis = tracker_refresh_millis.text().parse::().unwrap(); + info!("Starting config save"); { // maybe just send the police. diff --git a/src/ui/tracker_panel.rs b/src/ui/tracker_panel.rs index 65abf81..dfd7372 100644 --- a/src/ui/tracker_panel.rs +++ b/src/ui/tracker_panel.rs @@ -85,7 +85,10 @@ impl TrackerPanel { .margin_bottom(12) .build(); - let enable_disable = ToggleButton::with_label("Enable Automatic Tracking"); + let enable_disable = ToggleButton::builder() + .label("Enable Automatic Tracking") + .active(false) + .build(); let current_id = Label::builder() .label("Not Tracking")