2019-01-20 09:02:44 +00:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
2019-01-15 20:41:43 +00:00
|
|
|
pub struct Color {
|
|
|
|
pub hue: f32,
|
|
|
|
pub saturation: f32,
|
|
|
|
pub kelvin: f32,
|
|
|
|
}
|
|
|
|
|
2019-01-20 09:02:44 +00:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
2019-01-15 20:41:43 +00:00
|
|
|
pub struct Light {
|
|
|
|
pub id: String,
|
|
|
|
pub label: String,
|
|
|
|
pub connected: bool,
|
|
|
|
pub power: String,
|
|
|
|
pub color: Color,
|
|
|
|
pub brightness: f32,
|
|
|
|
}
|
|
|
|
|
2019-01-20 09:02:44 +00:00
|
|
|
pub const POWER: &str = "power";
|
|
|
|
pub const BRIGHTNESS: &str = "brightness";
|
2019-01-16 21:46:12 +00:00
|
|
|
|
|
|
|
pub enum Value {
|
|
|
|
Power(String),
|
|
|
|
Brightness(f32),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
pub fn new(label: &str, value: Vec<u8>) -> Self {
|
|
|
|
match label {
|
|
|
|
POWER => Value::Power(String::from_utf8(value).unwrap()),
|
2019-01-20 09:02:44 +00:00
|
|
|
BRIGHTNESS => Value::Brightness(vec_to_f32(value)),
|
2019-01-16 21:46:12 +00:00
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unravel(self) -> (&'static str, Vec<u8>) {
|
|
|
|
match self {
|
|
|
|
Value::Power(val) => (POWER, val.into_bytes()),
|
2019-01-20 09:02:44 +00:00
|
|
|
Value::Brightness(val) => (BRIGHTNESS, (val as u32).to_ne_bytes().to_vec()),
|
2019-01-16 21:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 09:02:44 +00:00
|
|
|
fn vec_to_f32(value: Vec<u8>) -> f32 {
|
|
|
|
assert!(value.len() == 4);
|
|
|
|
u32::from_ne_bytes([value[0], value[1], value[2], value[3]]) as f32
|
|
|
|
}
|
|
|
|
|
2019-01-16 21:46:12 +00:00
|
|
|
pub struct Command {
|
|
|
|
pub lampname: String,
|
|
|
|
pub command: Value,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Update {
|
|
|
|
pub lampname: String,
|
|
|
|
pub status: Value,
|
|
|
|
}
|
|
|
|
|
2019-01-20 09:02:44 +00:00
|
|
|
impl Update {
|
|
|
|
pub fn new(lampname: &str, status: Value) -> Self {
|
|
|
|
Update {
|
|
|
|
lampname: lampname.to_owned(),
|
|
|
|
status,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 21:46:12 +00:00
|
|
|
pub enum Status {
|
|
|
|
Update(Update),
|
|
|
|
New(Light),
|
|
|
|
Remove(String),
|
|
|
|
}
|