small improvements for readability

This commit is contained in:
Fl1tzi 2023-04-26 18:00:48 +02:00
parent 73b981fabe
commit 6b5038261a
2 changed files with 24 additions and 19 deletions

View file

@ -33,6 +33,8 @@ macro_rules! skip_if_none {
}; };
} }
pub type ModuleController = (Button, JoinHandle<()>, mpsc::Sender<HostEvent>);
pub enum DeviceError { pub enum DeviceError {
DriverError(StreamDeckError), DriverError(StreamDeckError),
Config(ConfigError), Config(ConfigError),
@ -49,8 +51,7 @@ impl Display for DeviceError {
/// Handles everything related to a single device /// Handles everything related to a single device
pub struct Device { pub struct Device {
// use this in favor of DeviceConfig modules: HashMap<u8, ModuleController>,
modules: HashMap<u8, (Button, JoinHandle<()>, mpsc::Sender<HostEvent>)>,
device: Arc<AsyncStreamDeck>, device: Arc<AsyncStreamDeck>,
is_dropped: bool, is_dropped: bool,
serial: String, serial: String,
@ -93,7 +94,7 @@ impl Device {
}) })
} }
pub fn start_button(&mut self, btn: &Button) -> Result<(), DeviceError> { pub fn create_module(&mut self, btn: &Button) -> Result<(), DeviceError> {
let (button_sender, button_receiver) = mpsc::channel(4); let (button_sender, button_receiver) = mpsc::channel(4);
if let Some(module) = retrieve_module_from_name(btn.module.clone()) { if let Some(module) = retrieve_module_from_name(btn.module.clone()) {
let b = btn.clone(); let b = btn.clone();
@ -134,6 +135,10 @@ impl Device {
self.is_dropped self.is_dropped
} }
pub fn has_modules(&self) -> bool {
!self.modules.is_empty()
}
/// listener for button press changes on the device /// listener for button press changes on the device
#[tracing::instrument(skip_all, fields(serial = self.serial))] #[tracing::instrument(skip_all, fields(serial = self.serial))]
pub async fn key_listener(&mut self) { pub async fn key_listener(&mut self) {

View file

@ -1,5 +1,5 @@
use deck_driver as streamdeck; use deck_driver as streamdeck;
use device::Device; use device::{Device, DeviceError};
use hidapi::HidApi; use hidapi::HidApi;
use serde::Deserialize; use serde::Deserialize;
use std::{ use std::{
@ -36,8 +36,6 @@ pub struct Config {
struct GlobalConfig; struct GlobalConfig;
fn main() { fn main() {
// ------ LOAD CONFIG
let fmt_layer = tracing_subscriber::fmt::layer().with_target(false); let fmt_layer = tracing_subscriber::fmt::layer().with_target(false);
let filter_layer = EnvFilter::try_from_default_env() let filter_layer = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new("info")) .or_else(|_| EnvFilter::try_new("info"))
@ -48,6 +46,8 @@ fn main() {
.with(fmt_layer) .with(fmt_layer)
.init(); .init();
// ------ LOAD CONFIG
let config_file: PathBuf = match env::var_os("DACH_DECKER_CONFIG") { let config_file: PathBuf = match env::var_os("DACH_DECKER_CONFIG") {
Some(path) => PathBuf::from(path), Some(path) => PathBuf::from(path),
None => { None => {
@ -130,9 +130,8 @@ pub async fn start(config: Config, mut hid: HidApi) {
config.device.iter().find(|d| d.serial == hw_device.1) config.device.iter().find(|d| d.serial == hw_device.1)
{ {
// start the device and its listener // start the device and its listener
if let Some(mut device) = start_device(hw_device, &hid, device_config).await if let Some(device) = start_device(hw_device, &hid, device_config).await
{ {
device.key_listener().await;
devices.insert(device.serial(), device); devices.insert(device.serial(), device);
} }
} else { } else {
@ -147,6 +146,7 @@ pub async fn start(config: Config, mut hid: HidApi) {
} }
} }
/// Start a device by initially creating the [Device] and then starting all modules and listeners for that device
#[tracing::instrument(name = "device", skip_all, fields(serial = device.1))] #[tracing::instrument(name = "device", skip_all, fields(serial = device.1))]
pub async fn start_device( pub async fn start_device(
device: (streamdeck::info::Kind, String), device: (streamdeck::info::Kind, String),
@ -154,15 +154,18 @@ pub async fn start_device(
device_config: &DeviceConfig, device_config: &DeviceConfig,
) -> Option<Device> { ) -> Option<Device> {
match Device::new(device.1, device.0, device_config, &hid).await { match Device::new(device.1, device.0, device_config, &hid).await {
Ok(mut d) => { Ok(mut device) => {
info!("Connected"); info!("Connected");
// start all modules // start all modules or print out the error
for button in device_config.buttons.iter() { device_config.buttons.iter().for_each(|button| {
if let Err(e) = d.start_button(&button) { device
error!("{}", e) .create_module(&button)
} .unwrap_or_else(|e| error!("{}", e))
});
if !device.has_modules() {
warn!("All modules have failed to start");
} }
Some(d) Some(device)
} }
Err(e) => { Err(e) => {
error!("Unable to connect: {}", e); error!("Unable to connect: {}", e);
@ -209,10 +212,7 @@ pub struct Button {
module: String, module: String,
/// options which get passed to the module /// options which get passed to the module
options: Option<HashMap<String, String>>, options: Option<HashMap<String, String>>,
/// allows to overwrite what it will do on a click /// allows to overwrite what it will do on a click (executes in /bin/sh)
/// available options:
/// - \"sh:date\" - executes in sh
/// - \"bash:date\" - executes in bash
pub on_click: Option<String>, pub on_click: Option<String>,
/// allows to overwrite what it will do on a release; Same options as [on_click] /// allows to overwrite what it will do on a release; Same options as [on_click]
pub on_release: Option<String>, pub on_release: Option<String>,