implemented priority overrides; moved ui code for refactor

This commit is contained in:
Nickiel12 2024-04-20 09:04:25 -07:00
parent ef96409082
commit 94bc81071b
4 changed files with 36 additions and 17 deletions

View file

@ -4,7 +4,7 @@ use log::{error, info};
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use crate::ui_code::AppState; use crate::ui::AppState;
pub fn load_config() -> AppState { pub fn load_config() -> AppState {
let settings = Config::builder() let settings = Config::builder()

View file

@ -3,6 +3,7 @@ use std::sync::{
atomic::{AtomicBool, Ordering}, atomic::{AtomicBool, Ordering},
Arc, Arc,
}; };
use std::time::{Duration, Instant};
use async_channel::{Receiver, Sender}; use async_channel::{Receiver, Sender};
use futures_util::{ use futures_util::{
@ -15,7 +16,10 @@ use tokio::runtime::Handle;
use tokio_tungstenite::{connect_async, tungstenite::Message, MaybeTlsStream, WebSocketStream}; use tokio_tungstenite::{connect_async, tungstenite::Message, MaybeTlsStream, WebSocketStream};
use crate::remote_sources; use crate::remote_sources;
use crate::{joystick_source::joystick_loop, ui_code::GuiUpdate}; use crate::{joystick_source::joystick_loop, ui::GuiUpdate};
const PRIORITY_TIMEOUT: Duration = Duration::from_secs(2);
#[derive(Clone)] #[derive(Clone)]
pub struct MoveEvent { pub struct MoveEvent {
@ -23,6 +27,7 @@ pub struct MoveEvent {
pub y: i32, pub y: i32,
} }
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
pub enum ConnectionType { pub enum ConnectionType {
Local, Local,
Remote, Remote,
@ -41,6 +46,9 @@ struct CoordState<'a> {
pub sck_alive_recvr: Arc<AtomicBool>, pub sck_alive_recvr: Arc<AtomicBool>,
pub joystick_loop_alive: Arc<AtomicBool>, pub joystick_loop_alive: Arc<AtomicBool>,
pub current_priority: ConnectionType,
pub last_update_of_priority: Instant,
pub mec: Pin<&'a mut Receiver<ApplicationEvent>>, pub mec: Pin<&'a mut Receiver<ApplicationEvent>>,
pub to_mec: Sender<ApplicationEvent>, pub to_mec: Sender<ApplicationEvent>,
pub to_gui: Sender<GuiUpdate>, pub to_gui: Sender<GuiUpdate>,
@ -60,6 +68,9 @@ impl<'a> CoordState<'a> {
sck_alive_server: Arc::new(AtomicBool::new(false)), sck_alive_server: Arc::new(AtomicBool::new(false)),
joystick_loop_alive: Arc::new(AtomicBool::new(false)), joystick_loop_alive: Arc::new(AtomicBool::new(false)),
current_priority: ConnectionType::Local,
last_update_of_priority: Instant::now(),
mec, mec,
to_mec, to_mec,
to_gui, to_gui,
@ -185,20 +196,27 @@ pub async fn start_coordinator(
state.socket_send(socket_message).await; state.socket_send(socket_message).await;
} }
ApplicationEvent::MoveEvent(coord, priority) => { ApplicationEvent::MoveEvent(coord, priority) => {
if let Err(e) = state.to_gui.send(GuiUpdate::MoveEvent(coord.clone())).await { // If Automatic control, but local event happens, override the automatice events for 2 seconds
panic!("Could not set message to gui channel; Unrecoverable: {e}"); if priority <= state.current_priority || Instant::now() > state.last_update_of_priority + PRIORITY_TIMEOUT {
} state.last_update_of_priority = Instant::now();
state.current_priority = priority;
if state.socket_connected() { if let Err(e) = state.to_gui.send(GuiUpdate::MoveEvent(coord.clone())).await {
let message = format!( panic!("Could not set message to gui channel; Unrecoverable: {e}");
"{}{}:{}{}", }
if coord.y > 0 { "D" } else { "U" },
coord.y.abs(), if state.socket_connected() {
if coord.x > 0 { "R" } else { "L" }, let message = format!(
coord.x.abs() "{}{}:{}{}",
); if coord.y > 0 { "D" } else { "U" },
coord.y.abs(),
if coord.x > 0 { "R" } else { "L" },
coord.x.abs()
);
state.socket_send(Message::Text(message)).await;
}
state.socket_send(Message::Text(message)).await;
} }
} }
} }

View file

@ -10,7 +10,7 @@ mod config;
mod coordinator; mod coordinator;
mod joystick_source; mod joystick_source;
mod remote_sources; mod remote_sources;
mod ui_code; mod ui;
const APP_ID: &str = "net.nickiel.joystick-controller-client"; const APP_ID: &str = "net.nickiel.joystick-controller-client";
fn main() -> glib::ExitCode { fn main() -> glib::ExitCode {
@ -30,7 +30,7 @@ fn main() -> glib::ExitCode {
app.connect_startup(|_| load_css()); app.connect_startup(|_| load_css());
app.connect_activate(move |app| { app.connect_activate(move |app| {
ui_code::build_ui(app, handle.clone()); ui::build_ui(app, handle.clone());
}); });
let exit_code = app.run(); let exit_code = app.run();

View file

@ -1,3 +1,4 @@
use gtk::{glib, prelude::*, Box, Entry, Label, ListBox}; use gtk::{glib, prelude::*, Box, Entry, Label, ListBox};
use gtk::{Application, ApplicationWindow, Button}; use gtk::{Application, ApplicationWindow, Button};
use log::error; use log::error;
@ -167,4 +168,4 @@ pub fn build_ui(app: &Application, runtime: Handle) {
// Present window // Present window
window.present(); window.present();
} }