This commit is contained in:
Alex Janka 2024-02-23 15:08:37 +11:00
parent c3cd4e7bdb
commit 7ffea13691

View file

@ -1,6 +1,6 @@
use homekit_controller::{ConnectedDevice, Data}; use homekit_controller::{ConnectedDevice, Data};
use rocket::State; use rocket::State;
use std::collections::HashMap; use std::{collections::HashMap, ops::DerefMut};
use tokio::sync::Mutex; use tokio::sync::Mutex;
use crate::SENSORS; use crate::SENSORS;
@ -15,11 +15,13 @@ pub fn launch(paired: HashMap<String, ConnectedDevice>) -> rocket::Rocket<rocket
pub async fn index(state: &State<Mutex<HashMap<String, ConnectedDevice>>>) -> Option<String> { pub async fn index(state: &State<Mutex<HashMap<String, ConnectedDevice>>>) -> Option<String> {
let mut s = String::new(); let mut s = String::new();
let mut state = state.lock().await; let mut state = state.lock().await;
for connected in state.values_mut() { for (name, connected) in state.deref_mut() {
connected.update_characteristics().await.ok()?; connected.update_characteristics().await.ok()?;
s.push_str(format!("{name}: [\n").as_str());
for (aid, accessory) in &connected.accessories { for (aid, accessory) in &connected.accessories {
s.push_str(format!("{} ({}): [\n", accessory.name, aid).as_str()); s.push_str(format!("\t{} ({}): [\n", accessory.name, aid).as_str());
for (sid, service) in &accessory.services { for (sid, service) in &accessory.services {
if !SENSORS.contains(&service.service_type) { if !SENSORS.contains(&service.service_type) {
continue; continue;
@ -27,7 +29,7 @@ pub async fn index(state: &State<Mutex<HashMap<String, ConnectedDevice>>>) -> Op
s.push_str( s.push_str(
format!( format!(
"\t{} ({:?}) ({}): [\n", "\t\t{} ({:?}) ({}): [\n",
service.name.as_ref().unwrap_or(&String::from("Unknown")), service.name.as_ref().unwrap_or(&String::from("Unknown")),
service.service_type, service.service_type,
sid, sid,
@ -37,7 +39,7 @@ pub async fn index(state: &State<Mutex<HashMap<String, ConnectedDevice>>>) -> Op
for (cid, characteristic) in &service.characteristics { for (cid, characteristic) in &service.characteristics {
s.push_str( s.push_str(
format!( format!(
"\t\t{:?} ({}): {}{}\n", "\t\t\t{:?} ({}): {}{}\n",
characteristic.characteristic_type, characteristic.characteristic_type,
cid, cid,
match &characteristic.value { match &characteristic.value {
@ -53,10 +55,11 @@ pub async fn index(state: &State<Mutex<HashMap<String, ConnectedDevice>>>) -> Op
.as_str(), .as_str(),
); );
} }
s.push_str("\t]\n"); s.push_str("\t\t]\n");
} }
s.push_str("]\n"); s.push_str("\t]\n");
} }
s.push_str("]\n");
} }
Some(s) Some(s)
} }