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};
mod coord_state;
mod perf_state;
mod process_box_string;
mod remote_video_processor;
mod perf_state;
use crate::config::AppConfig;
use crate::remote_sources::TrackerState;
@ -63,7 +63,15 @@ pub async fn start_coordinator(
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
.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 DEGRADED_TRACKER_TIME: u128 = 100;
@ -7,15 +7,28 @@ const DEGRADED_TRACKER_TIME: u128 = 100;
pub struct TrackerMetrics {
pub header_text: sync::Arc<sync::RwLock<String>>,
tracker_times: VecDeque<u128>,
}
impl TrackerMetrics {
pub fn new(text_reference: sync::Arc<sync::RwLock<String>>) -> Self {
TrackerMetrics {
let mut ret = TrackerMetrics {
header_text: text_reference,
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.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) {
@ -37,21 +54,29 @@ impl TrackerMetrics {
if avg_time == 0 {
if let Ok(mut writer) = self.header_text.write() {
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 let Ok(mut writer) = self.header_text.write() {
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 {
if let Ok(mut writer) = self.header_text.write() {
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 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]
pub async fn remote_video_loop(
@ -40,10 +43,27 @@ pub async fn remote_video_loop(
.build()
.expect("Could not build video info!");
let mut fail_count = 0;
{
let mut tm = tracker_metrics.lock().await;
tm.starting_connection(None);
}
loop {
socket_state.is_connected.store(true, Ordering::SeqCst);
match connect_async(&conn_string).await {
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}");
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) {
info!("Shutting down remote video loop");
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);
}
@ -166,7 +191,7 @@ async fn listen_to_messages(
}
}
info!(
"Stopping tracker connection listen with keep alive: {}",
"Stopping tracker listen connection with keep alive: {}",
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 {
top_level: gtk::Box,
@ -13,7 +14,6 @@ pub struct LiveViewPanel {
impl LiveViewPanel {
pub fn new() -> Self {
let right_box = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.hexpand(true)
@ -23,12 +23,13 @@ impl LiveViewPanel {
let tracker_status_label = Label::builder()
.label("No Status Yet".to_string())
.can_focus(true)
.css_classes(vec!["large-label"])
.css_classes(vec!["large-label", "NoConnection"])
.build();
// let conn_status_label = Label::new(Some(&"No Connection".to_string()));
let cam_status_label = Label::builder()
.label("No Connection".to_string())
.css_classes(vec!["NoConnection"])
.can_focus(true)
.build();

View file

@ -16,8 +16,8 @@ use crate::coordinator::{start_coordinator, ApplicationEvent, MoveEvent};
use crate::remote_sources::TrackerState;
mod control_panel;
mod settings_modal;
mod liveview_panel;
mod settings_modal;
use control_panel::ControlPanel;
use liveview_panel::LiveViewPanel;
@ -165,7 +165,7 @@ pub fn build_ui(app: &Application, config: Arc<RwLock<AppConfig>>, runtime: Hand
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());
// left_box.append(&conn_status_label);
@ -221,13 +221,41 @@ pub fn build_ui(app: &Application, config: Arc<RwLock<AppConfig>>, runtime: Hand
}),
);
glib::spawn_future_local(
glib::clone!(@weak drawable => async move {
let tracker_status_label = liveview_panel.tracker_status_label.clone();
let tracker_enable_toggle = control_panel
.connection_buttons
.tracker_enable_toggle
.clone();
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();
if let Ok(reader) = tracker_header.read() {
liveview_panel.tracker_status_label.set_text(reader.as_str());
}
match d {
GuiUpdate::MoveEvent(msg) => {
liveview_panel.adjustment_label.set_text(
@ -265,8 +293,7 @@ pub fn build_ui(app: &Application, config: Arc<RwLock<AppConfig>>, runtime: Hand
}
}
info!("Closing update loop");
}),
);
}));
window.connect_close_request(move |_| glib::Propagation::Proceed);