111 lines
2.7 KiB
Rust
111 lines
2.7 KiB
Rust
use log::warn;
|
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
|
pub struct Color {
|
|
pub hue: f32,
|
|
pub saturation: f32,
|
|
pub kelvin: i16,
|
|
}
|
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
|
pub struct Light {
|
|
pub id: String,
|
|
pub label: String,
|
|
pub connected: bool,
|
|
pub power: String,
|
|
pub color: Color,
|
|
pub brightness: f32,
|
|
}
|
|
|
|
pub const POWER: &str = "power";
|
|
pub const BRIGHTNESS: &str = "brightness";
|
|
pub const HUE: &str = "hue";
|
|
pub const SATURATION: &str = "saturation";
|
|
pub const KELVIN: &str = "kelvin";
|
|
|
|
pub enum Value {
|
|
Power(String),
|
|
Brightness(f32),
|
|
Hue(f32),
|
|
Saturation(f32),
|
|
Kelvin(i16),
|
|
}
|
|
|
|
impl Value {
|
|
pub fn new(label: &str, value_vec: Vec<u8>) -> Option<Self> {
|
|
match String::from_utf8(value_vec) {
|
|
Ok(value) => match label {
|
|
POWER => Some(Value::Power(value)),
|
|
BRIGHTNESS => {
|
|
if let Ok(val) = value.parse() {
|
|
Some(Value::Brightness(val))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
HUE => {
|
|
if let Ok(val) = value.parse() {
|
|
Some(Value::Hue(val))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
SATURATION => {
|
|
if let Ok(val) = value.parse() {
|
|
Some(Value::Saturation(val))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
KELVIN => {
|
|
if let Ok(val) = value.parse() {
|
|
Some(Value::Kelvin(val))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
_ => unimplemented!(),
|
|
},
|
|
Err(x) => {
|
|
warn!("{}", x);
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn unravel(self) -> (&'static str, String) {
|
|
match self {
|
|
Value::Power(val) => (POWER, val),
|
|
Value::Brightness(val) => (BRIGHTNESS, format!("{}", val)),
|
|
Value::Hue(val) => (HUE, format!("{}", val)),
|
|
Value::Saturation(val) => (SATURATION, format!("{}", val)),
|
|
Value::Kelvin(val) => (KELVIN, format!("{}", val)),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct Command {
|
|
pub lightname: String,
|
|
pub command: Value,
|
|
}
|
|
|
|
pub struct Update {
|
|
pub lightname: String,
|
|
pub status: Value,
|
|
}
|
|
|
|
impl Update {
|
|
pub fn new(lightname: &str, status: Value) -> Self {
|
|
Update {
|
|
lightname: lightname.to_owned(),
|
|
status,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub enum Status {
|
|
Update(Update),
|
|
New(Light),
|
|
Remove(String),
|
|
}
|