lifx-mqtt-bridge/src/light.rs

69 lines
1.5 KiB
Rust
Raw Normal View History

2019-01-15 20:41:43 +00:00
#[derive(Deserialize, Debug)]
pub struct Color {
pub hue: f32,
pub saturation: f32,
pub kelvin: f32,
}
#[derive(Deserialize, Debug)]
pub struct Light {
pub id: String,
pub label: String,
pub connected: bool,
pub power: String,
pub color: Color,
pub brightness: f32,
}
2019-01-16 21:46:12 +00:00
const POWER: &str = "power";
const BRIGHTNESS: &str = "brightness";
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()),
BRIGHTNESS => Value::Brightness(Self::vec_to_f32(value)),
_ => unimplemented!(),
}
}
fn vec_to_f32(vec: Vec<u8>) -> f32 {
assert!(vec.len() == 4);
let mut value_u32: u32 = 0;
for val in vec.clone() {
value_u32 = value_u32 << 8;
value_u32 = value_u32 | val as u32;
}
println!("{:?} -> {}", vec, value_u32);
f32::from_bits(value_u32)
}
pub fn unravel(self) -> (&'static str, Vec<u8>) {
match self {
Value::Power(val) => (POWER, val.into_bytes()),
Value::Brightness(val) => (BRIGHTNESS, Vec::new()),
}
}
}
pub struct Command {
pub lampname: String,
pub command: Value,
}
pub struct Update {
pub lampname: String,
pub status: Value,
}
pub enum Status {
Update(Update),
New(Light),
Remove(String),
}