Removed the DeviceIdentifier trait, and Device now returns a String.

This commit is contained in:
Simon Johnston 2020-08-18 12:32:02 -07:00
parent 4c93786f1c
commit 4b95734faf
5 changed files with 35 additions and 71 deletions

View File

@ -1,7 +1,7 @@
[package] [package]
name = "luxafor" name = "luxafor"
description = "Library, and CLI, for Luxafor lights via either USB or webhooks." description = "Library, and CLI, for Luxafor lights via either USB or webhooks."
version = "0.2.0" version = "0.2.1"
authors = ["Simon Johnston <johnstonskj@gmail.com>"] authors = ["Simon Johnston <johnstonskj@gmail.com>"]
repository = "https://github.com/johnstonskj/rust-luxafor" repository = "https://github.com/johnstonskj/rust-luxafor"
documentation = "https://docs.rs/luxafor/0.1.0/luxafor/" documentation = "https://docs.rs/luxafor/0.1.0/luxafor/"

View File

@ -9,8 +9,12 @@ Library, and CLI, for [Luxafor](https://luxafor.com/products/) lights via either
![MIT License](https://img.shields.io/badge/license-mit-118811.svg) ![MIT License](https://img.shields.io/badge/license-mit-118811.svg)
[![GitHub stars](https://img.shields.io/github/stars/johnstonskj/rust-luxafor.svg)](https://github.com/johnstonskj/rust-luxafor/stargazers) [![GitHub stars](https://img.shields.io/github/stars/johnstonskj/rust-luxafor.svg)](https://github.com/johnstonskj/rust-luxafor/stargazers)
This has been tested with the USB connected [flag](https://luxafor.com/flag-usb-busylight-availability-indicator/) The main entry point for clients is the trait `Device` that has implementations for USB connected devices such as the
as well as the [Bluetooth](https://luxafor.com/bluetooth-busy-light-availability-indicator/) lights. [flag](https://luxafor.com/flag-usb-busylight-availability-indicator/) as well as webhooks for both the flag and
[bluetooth](https://luxafor.com/bluetooth-busy-light-availability-indicator/) lights.
Each connection has its own discovery or connection methods but will provide a `Device` implementation
for the manipulation of the light state.
## API Examples ## API Examples
@ -76,6 +80,10 @@ The following shows the command line tool turning the light off.
## Changes ## Changes
**Version 0.2.1**
* Removed the `DeviceIdentifier` trait, and `Device` now returns a String.
**Version 0.2.0** **Version 0.2.0**
* Refactored to provide a new `Device` trait * Refactored to provide a new `Device` trait

View File

@ -1,10 +1,13 @@
/*! /*!
Library, and CLI, for [Luxafor](https://luxafor.com/products/) lights via either USB or webhooks. Library, and CLI, for [Luxafor](https://luxafor.com/products/) lights via either USB or webhooks.
This has been The main entry point for clients is the trait [Device](trait.Device.html) that has implementations
tested with the USB connected [flag](https://luxafor.com/flag-usb-busylight-availability-indicator/) for USB connected devices such as the [flag](https://luxafor.com/flag-usb-busylight-availability-indicator/)
as well as the [Bluetooth](https://luxafor.com/bluetooth-busy-light-availability-indicator/) lights. as well as webhooks for both the flag and [bluetooth](https://luxafor.com/bluetooth-busy-light-availability-indicator/)
lights.
Each connection has its own discovery or connection methods but will provide a `Device` implementation
for the manipulation of the light state.
# API Examples # API Examples
@ -160,11 +163,6 @@ pub enum Pattern {
Synthetic, Synthetic,
} }
///
/// Trait describing a device identifier, basically you just need to be able to `to_string()` it.
///
pub trait DeviceIdentifier: Display {}
/// ///
/// A trait implemented by different access methods to control a light. /// A trait implemented by different access methods to control a light.
/// ///
@ -172,7 +170,7 @@ pub trait Device {
/// ///
/// Return the identifier for the device. /// Return the identifier for the device.
/// ///
fn id(&self) -> &dyn DeviceIdentifier; fn id(&self) -> String;
/// ///
/// Turn the light off. /// Turn the light off.

View File

@ -2,9 +2,8 @@
Implementation of the Device trait for USB connected lights. Implementation of the Device trait for USB connected lights.
*/ */
use crate::{Device, DeviceIdentifier, Pattern, SolidColor}; use crate::{Device, Pattern, SolidColor};
use hidapi::{HidApi, HidDevice}; use hidapi::{HidApi, HidDevice};
use std::fmt::{Display, Formatter};
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Public Types // Public Types
@ -18,19 +17,13 @@ pub struct USBDeviceDiscovery {
hid_api: HidApi, hid_api: HidApi,
} }
///
/// The device identifier for a USB connected light.
///
#[derive(Clone, Debug)]
pub struct USBDeviceID(String);
/// ///
/// The device implementation for a USB connected light. /// The device implementation for a USB connected light.
/// ///
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct USBDevice<'a> { pub struct USBDevice<'a> {
hid_device: HidDevice<'a>, hid_device: HidDevice<'a>,
id: USBDeviceID, id: String,
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -83,16 +76,6 @@ const PATTERN_RAINBOW_WAVE: u8 = 8;
// Implementations // Implementations
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
impl Display for USBDeviceID {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl DeviceIdentifier for USBDeviceID {}
// ------------------------------------------------------------------------------------------------
impl USBDeviceDiscovery { impl USBDeviceDiscovery {
/// ///
/// Construct a new discovery object, this initializes the USB HID interface and thus can fail. /// Construct a new discovery object, this initializes the USB HID interface and thus can fail.
@ -120,8 +103,8 @@ impl USBDeviceDiscovery {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
impl<'a> Device for USBDevice<'a> { impl<'a> Device for USBDevice<'a> {
fn id(&self) -> &dyn DeviceIdentifier { fn id(&self) -> String {
&self.id self.id.clone()
} }
fn turn_off(&self) -> crate::error::Result<()> { fn turn_off(&self) -> crate::error::Result<()> {
@ -189,7 +172,7 @@ impl<'a> Device for USBDevice<'a> {
impl<'a> USBDevice<'a> { impl<'a> USBDevice<'a> {
fn new(hid_device: HidDevice<'a>) -> crate::error::Result<USBDevice<'a>> { fn new(hid_device: HidDevice<'a>) -> crate::error::Result<USBDevice<'a>> {
let id = USBDeviceID(format!( let id = format!(
"{}::{}::{}", "{}::{}::{}",
hid_device hid_device
.get_manufacturer_string() .get_manufacturer_string()
@ -200,7 +183,7 @@ impl<'a> USBDevice<'a> {
hid_device hid_device
.get_serial_number_string() .get_serial_number_string()
.unwrap_or("<unknown>".to_string()) .unwrap_or("<unknown>".to_string())
)); );
Ok(Self { hid_device, id }) Ok(Self { hid_device, id })
} }
} }

View File

@ -3,27 +3,19 @@ Implementation of the Device trait for webhook connected lights.
*/ */
use crate::{Device, DeviceIdentifier, Pattern, SolidColor}; use crate::{Device, Pattern, SolidColor};
use reqwest::blocking::Client; use reqwest::blocking::Client;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Public Types // Public Types
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
///
/// The device identifier for a webhook connected light.
///
#[derive(Clone, Debug)]
pub struct WebhookDeviceID(String);
/// ///
/// The device implementation for a webhook connected light. /// The device implementation for a webhook connected light.
/// ///
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct WebhookDevice { pub struct WebhookDevice {
id: WebhookDeviceID, id: String,
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -34,8 +26,13 @@ pub struct WebhookDevice {
/// Return a device implementation for a webhook connected light. /// Return a device implementation for a webhook connected light.
/// ///
pub fn new_device_for(device_id: &str) -> crate::error::Result<impl Device> { pub fn new_device_for(device_id: &str) -> crate::error::Result<impl Device> {
let device_id = WebhookDeviceID::from_str(device_id)?; if !device_id.is_empty() && device_id.chars().all(|c| c.is_ascii_hexdigit()) {
Ok(WebhookDevice { id: device_id }) Ok(WebhookDevice {
id: device_id.to_string(),
})
} else {
Err(crate::error::ErrorKind::InvalidDeviceID.into())
}
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -48,31 +45,9 @@ const API_V1: &str = "https://api.luxafor.com/webhook/v1/actions";
// Implementations // Implementations
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
impl Display for WebhookDeviceID {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl DeviceIdentifier for WebhookDeviceID {}
impl FromStr for WebhookDeviceID {
type Err = crate::error::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s.is_empty() && s.chars().all(|c| c.is_ascii_hexdigit()) {
Ok(Self(s.to_string()))
} else {
Err(crate::error::ErrorKind::InvalidDeviceID.into())
}
}
}
// ------------------------------------------------------------------------------------------------
impl Device for WebhookDevice { impl Device for WebhookDevice {
fn id(&self) -> &dyn DeviceIdentifier { fn id(&self) -> String {
&self.id self.id.clone()
} }
fn turn_off(&self) -> crate::error::Result<()> { fn turn_off(&self) -> crate::error::Result<()> {