Better error handling

This commit is contained in:
Lennard Kittner
2025-03-22 20:19:16 +01:00
parent 08366a63c8
commit 257f109695
2 changed files with 35 additions and 40 deletions

View File

@@ -1,8 +1,8 @@
use std::time::Duration; use std::time::Duration;
mod status_tray; mod status_tray;
use status_tray::{StatusTray, TrayHandler};
use hyper_x_cloud_ii_wireless::devices::{connect_compatible_device, DeviceError}; use hyper_x_cloud_ii_wireless::devices::{connect_compatible_device, DeviceError};
use status_tray::{StatusTray, TrayHandler};
fn handle_error(error: DeviceError) -> String { fn handle_error(error: DeviceError) -> String {
match error { match error {
@@ -22,27 +22,30 @@ fn handle_error(error: DeviceError) -> String {
} }
} }
//TODO: error handling e.g. reconnect on "no such device" error
fn main() { fn main() {
let mut device = loop {
match connect_compatible_device() {
Ok(d) => break d,
Err(e) => println!("Connecting failed with error: {e}"),
}
std::thread::sleep(Duration::from_secs(1));
};
let tray_handler = TrayHandler::new(StatusTray::new()); let tray_handler = TrayHandler::new(StatusTray::new());
// Run loop
loop { loop {
std::thread::sleep(Duration::from_secs(1)); let mut device = loop {
match device.refresh_state() { match connect_compatible_device() {
Ok(()) => (), Ok(d) => break d,
Err(error) => { Err(e) => println!("Connecting failed with error: {e}"),
eprintln!("{}", error);
} }
std::thread::sleep(Duration::from_secs(1));
}; };
tray_handler.update(device.get_device_state())
// Run loop
loop {
std::thread::sleep(Duration::from_secs(1));
match device.refresh_state() {
Ok(()) => (),
Err(error) => {
eprintln!("{error}");
device.get_device_state_mut().connected = None;
tray_handler.update(device.get_device_state());
break; // try to reconnect
}
};
tray_handler.update(device.get_device_state())
}
} }
} }

View File

@@ -5,6 +5,8 @@ pub struct TrayHandler {
handle: Handle<StatusTray>, handle: Handle<StatusTray>,
} }
const NO_COMPATIBLE_DEVICE: &str = "No compatible device found.\nIs the dongle plugged in?\nIf you are using Linux did you add the Udev rules?";
impl TrayHandler { impl TrayHandler {
pub fn new(tray: StatusTray) -> Self { pub fn new(tray: StatusTray) -> Self {
let tray_service = TrayService::new(tray); let tray_service = TrayService::new(tray);
@@ -14,12 +16,14 @@ impl TrayHandler {
} }
pub fn update(&self, device_state: &DeviceState) { pub fn update(&self, device_state: &DeviceState) {
let message = if device_state.connected.unwrap_or(false) { let (message, name) = match device_state.connected {
Some(device_state.to_string()) None => (NO_COMPATIBLE_DEVICE.to_string(), None),
} else { Some(false) => (
None "Headset is not connected".to_string(),
device_state.device_name.clone(),
),
Some(true) => (device_state.to_string(), device_state.device_name.clone()),
}; };
let name = device_state.device_name.clone();
self.handle.update(|tray| { self.handle.update(|tray| {
tray.message = message; tray.message = message;
tray.device_name = name; tray.device_name = name;
@@ -29,14 +33,14 @@ impl TrayHandler {
pub struct StatusTray { pub struct StatusTray {
device_name: Option<String>, device_name: Option<String>,
message: Option<String>, message: String,
} }
impl StatusTray { impl StatusTray {
pub fn new() -> Self { pub fn new() -> Self {
StatusTray { StatusTray {
device_name: None, device_name: None,
message: None, message: NO_COMPATIBLE_DEVICE.to_string(),
} }
} }
} }
@@ -49,28 +53,16 @@ impl Tray for StatusTray {
"audio-headset".into() "audio-headset".into()
} }
fn tool_tip(&self) -> ToolTip { fn tool_tip(&self) -> ToolTip {
println!("tool_tip");
let description = if let Some(message) = self.message.clone() {
message
} else {
"Headset is not connected".to_string()
};
ToolTip { ToolTip {
title: self.device_name.clone().unwrap_or("Unknown".to_string()), title: self.device_name.clone().unwrap_or("Unknown".to_string()),
description, description: self.message.clone(),
icon_name: "audio-headset".into(), icon_name: "audio-headset".into(),
icon_pixmap: Vec::new(), icon_pixmap: Vec::new(),
} }
} }
fn menu(&self) -> Vec<MenuItem<Self>> { fn menu(&self) -> Vec<MenuItem<Self>> {
println!("menu"); let mut state_items: Vec<MenuItem<Self>> = self
let message = if let Some(message) = &self.message { .message
message
} else {
&"Headset is not connected".to_string()
};
let mut state_items: Vec<MenuItem<Self>> = message
.lines() .lines()
.map(|line| { .map(|line| {
StandardItem { StandardItem {