#[derive(Deserialize, Debug, Clone)] pub struct Color { pub hue: f32, pub saturation: f32, pub kelvin: f32, } #[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 enum Value { Power(String), Brightness(f32), } impl Value { pub fn new(label: &str, value: Vec) -> Self { match label { POWER => Value::Power(String::from_utf8(value).unwrap()), BRIGHTNESS => Value::Brightness(vec_to_f32(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()), } } } fn vec_to_f32(value: Vec) -> f32 { assert!(value.len() == 4); u32::from_ne_bytes([value[0], value[1], value[2], value[3]]) as f32 } pub struct Command { pub lampname: String, pub command: Value, } pub struct Update { pub lampname: String, pub status: Value, } impl Update { pub fn new(lampname: &str, status: Value) -> Self { Update { lampname: lampname.to_owned(), status, } } } pub enum Status { Update(Update), New(Light), Remove(String), }