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) -> Option { match label { POWER => Some(Value::Power( String::from_utf8(value.clone()) .or_else(|x| { warn!("{:#?}: {}", value, x); Err(x) }) .ok()?, )), BRIGHTNESS => Some(Value::Brightness(vec_to_f32(value)?)), HUE => Some(Value::Hue(vec_to_f32(value)?)), SATURATION => Some(Value::Saturation(vec_to_f32(value)?)), KELVIN => Some(Value::Kelvin(vec_to_i16(value)?)), _ => unimplemented!(), } } pub fn unravel(self) -> (&'static str, Vec) { match self { Value::Power(val) => (POWER, val.into_bytes()), Value::Brightness(val) => (BRIGHTNESS, (val as u32).to_ne_bytes().to_vec()), Value::Hue(val) => (HUE, (val as u32).to_ne_bytes().to_vec()), Value::Saturation(val) => (SATURATION, (val as u32).to_ne_bytes().to_vec()), Value::Kelvin(val) => (KELVIN, (val as u32).to_ne_bytes().to_vec()), } } } fn vec_to_f32(value: Vec) -> Option { if value.len() == 4 { Some(u32::from_ne_bytes([value[0], value[1], value[2], value[3]]) as f32) } else { None } } fn vec_to_i16(value: Vec) -> Option { if value.len() == 2 { Some(i16::from_ne_bytes([value[0], value[1]])) } else { None } } 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), }