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;
mod status_tray;
use status_tray::{StatusTray, TrayHandler};
use hyper_x_cloud_ii_wireless::devices::{connect_compatible_device, DeviceError};
use status_tray::{StatusTray, TrayHandler};
fn handle_error(error: DeviceError) -> String {
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() {
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());
// Run loop
loop {
std::thread::sleep(Duration::from_secs(1));
match device.refresh_state() {
Ok(()) => (),
Err(error) => {
eprintln!("{}", error);
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));
};
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>,
}
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 {
pub fn new(tray: StatusTray) -> Self {
let tray_service = TrayService::new(tray);
@@ -14,12 +16,14 @@ impl TrayHandler {
}
pub fn update(&self, device_state: &DeviceState) {
let message = if device_state.connected.unwrap_or(false) {
Some(device_state.to_string())
} else {
None
let (message, name) = match device_state.connected {
None => (NO_COMPATIBLE_DEVICE.to_string(), None),
Some(false) => (
"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| {
tray.message = message;
tray.device_name = name;
@@ -29,14 +33,14 @@ impl TrayHandler {
pub struct StatusTray {
device_name: Option<String>,
message: Option<String>,
message: String,
}
impl StatusTray {
pub fn new() -> Self {
StatusTray {
device_name: None,
message: None,
message: NO_COMPATIBLE_DEVICE.to_string(),
}
}
}
@@ -49,28 +53,16 @@ impl Tray for StatusTray {
"audio-headset".into()
}
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 {
title: self.device_name.clone().unwrap_or("Unknown".to_string()),
description,
description: self.message.clone(),
icon_name: "audio-headset".into(),
icon_pixmap: Vec::new(),
}
}
fn menu(&self) -> Vec<MenuItem<Self>> {
println!("menu");
let message = if let Some(message) = &self.message {
message
} else {
&"Headset is not connected".to_string()
};
let mut state_items: Vec<MenuItem<Self>> = message
let mut state_items: Vec<MenuItem<Self>> = self
.message
.lines()
.map(|line| {
StandardItem {