New struct to simplify tray handling and the device state is now updated more efficiently.
This commit is contained in:
76
src/main.rs
76
src/main.rs
@@ -1,7 +1,8 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use hyper_x_cloud_ii_wireless::{Device, DeviceError};
|
||||
use ksni::TrayService;
|
||||
mod battery_tray;
|
||||
use battery_tray::BatteryTray;
|
||||
use battery_tray::TrayHandler;
|
||||
|
||||
fn pair_device() -> Device {
|
||||
loop {
|
||||
@@ -15,41 +16,58 @@ fn pair_device() -> Device {
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let service = TrayService::new(BatteryTray::new());
|
||||
let handle = service.handle();
|
||||
service.spawn();
|
||||
//TODO: status messages
|
||||
//TODO: use mute state
|
||||
//TODO: make trayHandler dynamic
|
||||
|
||||
fn handle_error(error: DeviceError, device: &mut Device, tray_handler: &TrayHandler) {
|
||||
match error {
|
||||
DeviceError::HidError(hidapi::HidError::HidApiError { message }) => {
|
||||
if message == "No such device" {
|
||||
eprintln!("No device found.");
|
||||
// handle.update(|tray: &mut BatteryTray| { tray.set_status_message("No device found"); });
|
||||
*device = pair_device();
|
||||
} else {
|
||||
eprintln!("{message}");
|
||||
}
|
||||
}
|
||||
DeviceError::NoDeviceFound() => {
|
||||
eprintln!("{}", DeviceError::NoDeviceFound());
|
||||
// handle.update(|tray: &mut BatteryTray| { tray.set_status_message("No device found"); });
|
||||
}
|
||||
DeviceError::HeadSetOff() => {
|
||||
eprintln!("{}", DeviceError::HeadSetOff());
|
||||
// handle.update(|tray: &mut BatteryTray| { tray.set_status_message(&DeviceError::HeadSetOff().to_string()); });
|
||||
}
|
||||
error => {
|
||||
eprintln!("{error}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let tray_handler = TrayHandler::new();
|
||||
let mut device = pair_device();
|
||||
tray_handler.update(&device);
|
||||
|
||||
// Run loop
|
||||
loop {
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
let (battery_level, charging) = match device.get_battery_level() {
|
||||
Ok(t) => t,
|
||||
Err(DeviceError::HidError(hidapi::HidError::HidApiError { message })) => {
|
||||
eprintln!("Error: {message}");
|
||||
if message == "No such device" {
|
||||
handle.update(|tray: &mut BatteryTray| { tray.no_device_found(); });
|
||||
device = pair_device();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
Err(DeviceError::NoDeviceFound()) => {
|
||||
eprintln!("{}", DeviceError::NoDeviceFound());
|
||||
handle.update(|tray: &mut BatteryTray| { tray.no_device_found(); });
|
||||
continue;
|
||||
}
|
||||
Err(DeviceError::HeadSetOff()) => {
|
||||
eprintln!("{}", DeviceError::HeadSetOff());
|
||||
handle.update(|tray: &mut BatteryTray| { tray.no_device_found(); });
|
||||
continue;
|
||||
}
|
||||
match device.update_battery_level() {
|
||||
Ok(_) => {
|
||||
tray_handler.update(&device);
|
||||
},
|
||||
Err(error) => {
|
||||
eprintln!("{error}");
|
||||
handle_error(error, &mut device, &tray_handler);
|
||||
continue;
|
||||
},
|
||||
};
|
||||
match device.wait_for_updates(Duration::from_secs(30)) {
|
||||
Ok(_) => tray_handler.update(&device),
|
||||
Err(error) => {
|
||||
handle_error(error, &mut device, &tray_handler);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
handle.update(|tray: &mut BatteryTray| { tray.update(battery_level, charging); });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user