Compare commits

...

2 commits

Author SHA1 Message Date
Nickiel12
8924d2ab8f added restart limits and feedback 2024-05-27 11:12:06 -07:00
Nickiel12
a37f5ed266 added updating tracker status 2024-05-27 10:50:11 -07:00
5 changed files with 159 additions and 73 deletions

View file

@ -17,9 +17,9 @@ use tokio_tungstenite::{tungstenite::Message, MaybeTlsStream, WebSocketStream};
use tracing::{debug, error, info, instrument}; use tracing::{debug, error, info, instrument};
mod coord_state; mod coord_state;
mod perf_state;
mod process_box_string; mod process_box_string;
mod remote_video_processor; mod remote_video_processor;
mod perf_state;
use crate::config::AppConfig; use crate::config::AppConfig;
use crate::remote_sources::TrackerState; use crate::remote_sources::TrackerState;
@ -63,7 +63,15 @@ pub async fn start_coordinator(
let mec = pin!(mec); let mec = pin!(mec);
let mut state = CoordState::new(mec, to_mec, to_gui, runtime, tracker_state, settings, tracker_header); let mut state = CoordState::new(
mec,
to_mec,
to_gui,
runtime,
tracker_state,
settings,
tracker_header,
);
state state
.pipeline .pipeline

View file

@ -1,4 +1,4 @@
use std::{sync, time::Duration, collections::VecDeque}; use std::{collections::VecDeque, sync, time::Duration};
const MAX_RECORDED_TIMES: usize = 10; const MAX_RECORDED_TIMES: usize = 10;
const DEGRADED_TRACKER_TIME: u128 = 100; const DEGRADED_TRACKER_TIME: u128 = 100;
@ -7,15 +7,28 @@ const DEGRADED_TRACKER_TIME: u128 = 100;
pub struct TrackerMetrics { pub struct TrackerMetrics {
pub header_text: sync::Arc<sync::RwLock<String>>, pub header_text: sync::Arc<sync::RwLock<String>>,
tracker_times: VecDeque<u128>, tracker_times: VecDeque<u128>,
} }
impl TrackerMetrics { impl TrackerMetrics {
pub fn new(text_reference: sync::Arc<sync::RwLock<String>>) -> Self { pub fn new(text_reference: sync::Arc<sync::RwLock<String>>) -> Self {
TrackerMetrics { let mut ret = TrackerMetrics {
header_text: text_reference, header_text: text_reference,
tracker_times: VecDeque::with_capacity(MAX_RECORDED_TIMES), tracker_times: VecDeque::with_capacity(MAX_RECORDED_TIMES),
};
ret.clear_times();
ret
}
pub fn starting_connection(&mut self, fail_count: Option<usize>) {
self.clear_times();
if let Ok(mut writer) = self.header_text.write() {
writer.clear();
match fail_count {
None => writer.push_str("Status: Connecting ..."),
Some(v) => writer.push_str(&format!("Status: Attempt {}/5", v)),
}
} }
} }
@ -24,6 +37,10 @@ impl TrackerMetrics {
self.tracker_times.pop_front(); self.tracker_times.pop_front();
} }
self.insert_time(Duration::new(0, 0)); self.insert_time(Duration::new(0, 0));
if let Ok(mut writer) = self.header_text.write() {
writer.clear();
writer.push_str("Status: Disconnected");
}
} }
pub fn insert_time(&mut self, new_measurement: Duration) { pub fn insert_time(&mut self, new_measurement: Duration) {
@ -37,21 +54,29 @@ impl TrackerMetrics {
if avg_time == 0 { if avg_time == 0 {
if let Ok(mut writer) = self.header_text.write() { if let Ok(mut writer) = self.header_text.write() {
writer.clear(); writer.clear();
writer.push_str(&format!("Status: Failed Avg Response: {} ms", avg_time.to_string())); writer.push_str(&format!(
"Status: Failed Avg Response: {} ms",
avg_time.to_string()
));
} }
} }
if avg_time > DEGRADED_TRACKER_TIME { if avg_time > DEGRADED_TRACKER_TIME {
if let Ok(mut writer) = self.header_text.write() { if let Ok(mut writer) = self.header_text.write() {
writer.clear(); writer.clear();
writer.push_str(&format!("Status: Degraded Avg Response: {} ms", avg_time.to_string())); writer.push_str(&format!(
"Status: Degraded Avg Response: {} ms",
avg_time.to_string()
));
} }
} else { } else {
if let Ok(mut writer) = self.header_text.write() { if let Ok(mut writer) = self.header_text.write() {
writer.clear(); writer.clear();
writer.push_str(&format!("Status: Nominal Avg Response: {} ms", avg_time.to_string())); writer.push_str(&format!(
"Status: Nominal Avg Response: {} ms",
avg_time.to_string()
));
} }
} }
} }
} }

View file

@ -18,7 +18,10 @@ use tracing::{error, info, instrument};
use crate::remote_sources::TrackerState; use crate::remote_sources::TrackerState;
use super::{perf_state::TrackerMetrics, process_box_string::process_incoming_string, ApplicationEvent, SocketState}; use super::{
perf_state::TrackerMetrics, process_box_string::process_incoming_string, ApplicationEvent,
SocketState,
};
#[instrument] #[instrument]
pub async fn remote_video_loop( pub async fn remote_video_loop(
@ -40,10 +43,27 @@ pub async fn remote_video_loop(
.build() .build()
.expect("Could not build video info!"); .expect("Could not build video info!");
let mut fail_count = 0;
{
let mut tm = tracker_metrics.lock().await;
tm.starting_connection(None);
}
loop { loop {
socket_state.is_connected.store(true, Ordering::SeqCst); socket_state.is_connected.store(true, Ordering::SeqCst);
match connect_async(&conn_string).await { match connect_async(&conn_string).await {
Err(e) => { Err(e) => {
fail_count += 1;
{
let mut tm = tracker_metrics.lock().await;
tm.starting_connection(Some(fail_count));
}
if fail_count >= 5 {
break;
}
error!("Could not connect to remote video loop! Trying again in 1 seconds: {e}"); error!("Could not connect to remote video loop! Trying again in 1 seconds: {e}");
sleep_until(Instant::now() + Duration::from_secs(1)).await; sleep_until(Instant::now() + Duration::from_secs(1)).await;
} }
@ -117,10 +137,15 @@ pub async fn remote_video_loop(
} }
} }
if !socket_state.stay_connected.load(Ordering::SeqCst) { if !socket_state.stay_connected.load(Ordering::SeqCst) {
info!("Shutting down remote video loop");
break; break;
} }
} }
info!("Shutting down remote video loop");
{
let mut tm = tracker_metrics.lock().await;
tm.clear_times();
}
socket_state.is_connected.store(false, Ordering::SeqCst); socket_state.is_connected.store(false, Ordering::SeqCst);
} }
@ -166,7 +191,7 @@ async fn listen_to_messages(
} }
} }
info!( info!(
"Stopping tracker connection listen with keep alive: {}", "Stopping tracker listen connection with keep alive: {}",
socket_state.stay_connected.load(Ordering::SeqCst) socket_state.stay_connected.load(Ordering::SeqCst)
); );
} }

View file

@ -1,5 +1,6 @@
use gtk::{gdk::Paintable, prelude::BoxExt, AspectFrame, Box, DrawingArea, Label, Overlay, Picture}; use gtk::{
gdk::Paintable, prelude::BoxExt, AspectFrame, Box, DrawingArea, Label, Overlay, Picture,
};
pub struct LiveViewPanel { pub struct LiveViewPanel {
top_level: gtk::Box, top_level: gtk::Box,
@ -13,7 +14,6 @@ pub struct LiveViewPanel {
impl LiveViewPanel { impl LiveViewPanel {
pub fn new() -> Self { pub fn new() -> Self {
let right_box = gtk::Box::builder() let right_box = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical) .orientation(gtk::Orientation::Vertical)
.hexpand(true) .hexpand(true)
@ -23,12 +23,13 @@ impl LiveViewPanel {
let tracker_status_label = Label::builder() let tracker_status_label = Label::builder()
.label("No Status Yet".to_string()) .label("No Status Yet".to_string())
.can_focus(true) .can_focus(true)
.css_classes(vec!["large-label"]) .css_classes(vec!["large-label", "NoConnection"])
.build(); .build();
// let conn_status_label = Label::new(Some(&"No Connection".to_string())); // let conn_status_label = Label::new(Some(&"No Connection".to_string()));
let cam_status_label = Label::builder() let cam_status_label = Label::builder()
.label("No Connection".to_string()) .label("No Connection".to_string())
.css_classes(vec!["NoConnection"])
.can_focus(true) .can_focus(true)
.build(); .build();
@ -69,7 +70,7 @@ impl LiveViewPanel {
} }
pub fn set_drawable(&self, new_drawable: &DrawingArea) { pub fn set_drawable(&self, new_drawable: &DrawingArea) {
self.overlay.add_overlay(new_drawable); self.overlay.add_overlay(new_drawable);
} }
pub fn set_paintable(&self, new_paintable: &Paintable) { pub fn set_paintable(&self, new_paintable: &Paintable) {

View file

@ -16,8 +16,8 @@ use crate::coordinator::{start_coordinator, ApplicationEvent, MoveEvent};
use crate::remote_sources::TrackerState; use crate::remote_sources::TrackerState;
mod control_panel; mod control_panel;
mod settings_modal;
mod liveview_panel; mod liveview_panel;
mod settings_modal;
use control_panel::ControlPanel; use control_panel::ControlPanel;
use liveview_panel::LiveViewPanel; use liveview_panel::LiveViewPanel;
@ -165,7 +165,7 @@ pub fn build_ui(app: &Application, config: Arc<RwLock<AppConfig>>, runtime: Hand
tracker_header.clone(), tracker_header.clone(),
)); ));
let control_panel = Arc::new(ControlPanel::new(tracker_state.clone())); let control_panel = ControlPanel::new(tracker_state.clone());
control_panel.connect_button_callbacks(to_mec.clone()); control_panel.connect_button_callbacks(to_mec.clone());
// left_box.append(&conn_status_label); // left_box.append(&conn_status_label);
@ -221,52 +221,79 @@ pub fn build_ui(app: &Application, config: Arc<RwLock<AppConfig>>, runtime: Hand
}), }),
); );
glib::spawn_future_local( let tracker_status_label = liveview_panel.tracker_status_label.clone();
glib::clone!(@weak drawable => async move { let tracker_enable_toggle = control_panel
while let Ok(d) = gui_recv.recv().await { .connection_buttons
drawable.queue_draw(); .tracker_enable_toggle
if let Ok(reader) = tracker_header.read() { .clone();
liveview_panel.tracker_status_label.set_text(reader.as_str());
glib::timeout_add_seconds_local(1, move || {
if let Ok(reader) = tracker_header.read() {
tracker_status_label.set_text(reader.as_str());
if reader.contains("Failed") || reader.contains("Disconnected") {
tracker_status_label.set_css_classes(&["NoConnection"]);
tracker_enable_toggle.set_label("Press to Connect Tracker");
tracker_enable_toggle.set_active(false);
} else if reader.contains("Degraded") || reader.contains("Connecting") {
tracker_status_label.set_css_classes(&["LoadingConnection"]);
tracker_enable_toggle.set_label("Press to Connect");
tracker_enable_toggle.set_active(false);
} else if reader.contains("Nominal") {
tracker_status_label.set_css_classes(&["YesConnection"]);
tracker_enable_toggle.set_label("Press to Disconnect");
tracker_enable_toggle.set_active(true);
}
glib::ControlFlow::Continue
} else {
error!("Couldn't get rwlock on metrics");
glib::ControlFlow::Break
}
});
glib::spawn_future_local(glib::clone!(@weak drawable => async move {
while let Ok(d) = gui_recv.recv().await {
drawable.queue_draw();
match d {
GuiUpdate::MoveEvent(msg) => {
liveview_panel.adjustment_label.set_text(
format!("X: {:>4} Y: {:>4}", msg.x, msg.y).as_str()
);
} }
match d { GuiUpdate::SocketConnected => {
GuiUpdate::MoveEvent(msg) => { control_panel.connection_buttons.camera_connection.set_sensitive(true);
liveview_panel.adjustment_label.set_text( control_panel.connection_buttons.camera_connection.set_label("Press to Disconnect");
format!("X: {:>4} Y: {:>4}", msg.x, msg.y).as_str() liveview_panel.cam_status_label.set_label("Connected");
);
}
GuiUpdate::SocketConnected => {
control_panel.connection_buttons.camera_connection.set_sensitive(true);
control_panel.connection_buttons.camera_connection.set_label("Press to Disconnect");
liveview_panel.cam_status_label.set_label("Connected");
liveview_panel.cam_status_label.set_css_classes(&["YesConnection"]); liveview_panel.cam_status_label.set_css_classes(&["YesConnection"]);
}, },
GuiUpdate::SocketConnecting => { GuiUpdate::SocketConnecting => {
control_panel.connection_buttons.camera_connection.set_sensitive(false); control_panel.connection_buttons.camera_connection.set_sensitive(false);
control_panel.connection_buttons.camera_connection.set_label("Please wait"); control_panel.connection_buttons.camera_connection.set_label("Please wait");
liveview_panel.cam_status_label.set_label("Connecting"); liveview_panel.cam_status_label.set_label("Connecting");
liveview_panel.cam_status_label.set_css_classes(&["LoadingConnection"]); liveview_panel.cam_status_label.set_css_classes(&["LoadingConnection"]);
}, },
GuiUpdate::SocketDisconnected => { GuiUpdate::SocketDisconnected => {
control_panel.connection_buttons.camera_connection.set_sensitive(true); control_panel.connection_buttons.camera_connection.set_sensitive(true);
control_panel.connection_buttons.camera_connection.set_label("Press to Connect to Camera"); control_panel.connection_buttons.camera_connection.set_label("Press to Connect to Camera");
liveview_panel.cam_status_label.set_label("Not Connected to Camera"); liveview_panel.cam_status_label.set_label("Not Connected to Camera");
liveview_panel.cam_status_label.set_css_classes(&["NoConnection"]); liveview_panel.cam_status_label.set_css_classes(&["NoConnection"]);
} }
GuiUpdate::UpdatePaintable(sink) => { GuiUpdate::UpdatePaintable(sink) => {
let paintable = sink.property::<Paintable>("paintable"); let paintable = sink.property::<Paintable>("paintable");
liveview_panel.set_paintable(&paintable); liveview_panel.set_paintable(&paintable);
}
} }
} }
info!("Closing update loop"); }
}), info!("Closing update loop");
); }));
window.connect_close_request(move |_| glib::Propagation::Proceed); window.connect_close_request(move |_| glib::Propagation::Proceed);