maybe im stupid...

This commit is contained in:
Alex Janka 2024-04-10 07:06:05 +10:00
parent 62a934fc20
commit c780932300
4 changed files with 54 additions and 51 deletions

2
Cargo.lock generated
View file

@ -1143,7 +1143,7 @@ dependencies = [
[[package]] [[package]]
name = "homekit-exporter" name = "homekit-exporter"
version = "0.5.5" version = "0.5.6"
dependencies = [ dependencies = [
"clap", "clap",
"env_logger", "env_logger",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "homekit-exporter" name = "homekit-exporter"
version = "0.5.5" version = "0.5.6"
edition = "2021" edition = "2021"
license = "Apache-2.0" license = "Apache-2.0"
description = "Prometheus exporter for HomeKit sensors" description = "Prometheus exporter for HomeKit sensors"

View file

@ -1,7 +1,7 @@
# Maintainer: Alex Janka <alex@alexjanka.com> # Maintainer: Alex Janka <alex@alexjanka.com>
pkgname=homekit-logger pkgname=homekit-logger
pkgver=0.5.5 pkgver=0.5.6
pkgrel=1 pkgrel=1
pkgdesc="Prometheus exporter for HomeKit sensors" pkgdesc="Prometheus exporter for HomeKit sensors"
arch=('x86_64' 'aarch64') arch=('x86_64' 'aarch64')

View file

@ -20,70 +20,73 @@ pub async fn metrics(state: &State<Mutex<HashMap<String, DeviceConnection>>>) ->
let mut state = state.lock().await; let mut state = state.lock().await;
let mut set = JoinSet::new(); let mut set = JoinSet::new();
for (name, mut connected) in state.drain() { for (name, mut connected) in state.drain() {
set.spawn( set.spawn(async move {
async move { let mut return_string = String::new();
let mut return_string = String::new(); let mut types_seen = Vec::new();
let mut types_seen = Vec::new();
connected.update_characteristics().await.ok()?; if let Err(e) = connected.update_characteristics().await {
log::error!("updating characteristics for {name}: error {e:?}");
return (name, connected, None);
}
for (aid, accessory) in &connected.accessories { for (aid, accessory) in &connected.accessories {
for service in accessory.services.values() { for service in accessory.services.values() {
for (cid, characteristic) in &service.characteristics { for (cid, characteristic) in &service.characteristics {
if !MONITORED_CHARACTERISTICS.contains(&characteristic.characteristic_type) { if !MONITORED_CHARACTERISTICS.contains(&characteristic.characteristic_type)
continue; {
continue;
}
if let Some(value) = &characteristic.value {
if !types_seen.contains(&characteristic.characteristic_type) {
types_seen.push(characteristic.characteristic_type);
} }
if let Some(value) = &characteristic.value { return_string.push_str(format!(
if !types_seen.contains(&characteristic.characteristic_type) { "{}{{hub=\"{}\",service_type=\"{}\",accessory_name=\"{}\",accessory=\"{}.{}\",service=\"{}.{}.{}\"}} {}\n",
types_seen.push(characteristic.characteristic_type); characteristic.characteristic_type,
name,
service.service_type,
accessory.name,
name,
aid,
name,
aid,
cid,
match value {
Data::Bool(v) => format!("{}", if *v { 1 } else { 0 }),
Data::Uint8(v) => format!("{v}"),
Data::Uint16(v) => format!("{v}"),
Data::Uint32(v) => format!("{v}"),
Data::Uint64(v) => format!("{v}"),
Data::Int(v) => format!("{v}"),
Data::Float(v) => format!("{v}"),
Data::String(v) => v.to_string(),
Data::Tlv8(v) => v.to_string(),
Data::Data(v) => v.to_string(),
} }
).as_str());
return_string.push_str(format!(
"{}{{hub=\"{}\",service_type=\"{}\",accessory_name=\"{}\",accessory=\"{}.{}\",service=\"{}.{}.{}\"}} {}\n",
characteristic.characteristic_type,
name,
service.service_type,
accessory.name,
name,
aid,
name,
aid,
cid,
match value {
Data::Bool(v) => format!("{}", if *v { 1 } else { 0 }),
Data::Uint8(v) => format!("{v}"),
Data::Uint16(v) => format!("{v}"),
Data::Uint32(v) => format!("{v}"),
Data::Uint64(v) => format!("{v}"),
Data::Int(v) => format!("{v}"),
Data::Float(v) => format!("{v}"),
Data::String(v) => v.to_string(),
Data::Tlv8(v) => v.to_string(),
Data::Data(v) => v.to_string(),
}
).as_str());
}
} }
} }
} }
Some((name,connected,return_string,types_seen))
} }
);
(name, connected, Some((return_string, types_seen)))
});
} }
let mut types_string = String::new(); let mut types_string = String::new();
let mut values_string = String::new(); let mut values_string = String::new();
let mut shown_types = Vec::new(); let mut shown_types = Vec::new();
while let Some(Ok(Some((k, v, metrics, types)))) = set.join_next().await { while let Some(Ok((k, v, val))) = set.join_next().await {
state.insert(k, v); state.insert(k, v);
for c in types { if let Some((metrics, types)) = val {
if !shown_types.contains(&c) { for c in types {
types_string.push_str(format!("# TYPE {} gauge\n", c).as_str()); if !shown_types.contains(&c) {
shown_types.push(c); types_string.push_str(format!("# TYPE {} gauge\n", c).as_str());
shown_types.push(c);
}
} }
values_string.push_str(&metrics);
} }
values_string.push_str(&metrics);
} }
Some(format!("{types_string}{values_string}")) Some(format!("{types_string}{values_string}"))