use serde_json::{Value, json};
use super::Report;
use crate::Bulb;
use crate::protocol::{BulbType, Scene};
pub async fn info(bulb: &Bulb) -> anyhow::Result<Report> {
let bulb_type = bulb.bulb_type().await?;
let scenes = bulb_type.scenes().count();
Ok(Report::new(
json(&bulb_type, scenes),
human(&bulb_type, scenes),
))
}
pub async fn scenes(bulb: &Bulb) -> anyhow::Result<Report> {
let scenes = bulb.scenes().await?;
let listing = scenes
.iter()
.map(|scene| format!("{:>3} {}", scene.id().get(), scene.name()))
.collect::<Vec<_>>()
.join("\n");
Ok(Report::new(
Value::Array(scenes.iter().map(scene_json).collect()),
listing,
))
}
fn scene_json(scene: &Scene) -> Value {
json!({
"id": scene.id().get(),
"name": scene.name(),
"category": scene.category(),
"animates": scene.animates(),
"adjustable": scene.adjustable(),
})
}
fn json(bulb_type: &BulbType, scenes: usize) -> Value {
let mut value = serde_json::to_value(bulb_type).unwrap_or_else(|_| json!({}));
value["description"] = json!(bulb_type.class.description());
value["scenes"] = json!(scenes);
value
}
fn human(bulb_type: &BulbType, scenes: usize) -> String {
let mut lines = vec![
row(
"model",
bulb_type
.module_name
.as_ref()
.map_or_else(|| "unknown".to_owned(), ToString::to_string),
),
row(
"firmware",
bulb_type
.fw_version
.clone()
.unwrap_or_else(|| "-".to_owned()),
),
row(
"class",
format!(
"{} ({})",
bulb_type.class.description(),
bulb_type.class.as_str()
),
),
];
if let Some(range) = bulb_type.kelvin_range {
lines.push(row("kelvin", format!("{}-{} K", range.min(), range.max())));
}
let features = bulb_type.features;
let can: Vec<&str> = [
("colour", features.color),
("tunable white", features.color_tmp),
("scenes", features.effect),
("dimming", features.brightness),
("dual head", features.dual_head),
("fan", features.fan),
]
.into_iter()
.filter_map(|(name, yes)| yes.then_some(name))
.collect();
lines.push(row(
"can",
if can.is_empty() {
"on and off only".to_owned()
} else {
can.join(", ")
},
));
lines.push(row("scenes", scenes.to_string()));
lines.push(row("derived from", format!("{:?}", bulb_type.derivation)));
lines.join("\n")
}
fn row(label: &str, value: String) -> String {
format!("{label:<13}{value}")
}