diff --git a/src/device/dsmr.rs b/src/device/dsmr.rs index 3568700..510c2a7 100644 --- a/src/device/dsmr.rs +++ b/src/device/dsmr.rs @@ -3,6 +3,8 @@ use std::{ time::Instant, }; +use crate::device::FormattableDevice; + pub enum DsmrMessageType { Water, Gas, @@ -34,8 +36,10 @@ impl DsmrState { last_seen: Instant::now(), } } +} - pub fn format_open_metrics(&self) -> impl Display + '_ { +impl FormattableDevice for DsmrState { + fn format_open_metrics(&self) -> impl Display + '_ { DsmrFormatter { device: self } } } diff --git a/src/device/mitemp.rs b/src/device/mitemp.rs index a7dd025..6ceb3ef 100644 --- a/src/device/mitemp.rs +++ b/src/device/mitemp.rs @@ -1,6 +1,6 @@ use jzon::JsonValue; -use crate::device::BDAddr; +use crate::device::{BDAddr, FormattableDevice}; use std::{ fmt::{self, Debug, Display}, time::Instant, @@ -48,8 +48,10 @@ impl MiTempDevice { self.dew_point = dew_point; } } +} - pub fn format_open_metrics(&self) -> impl Display + '_ { +impl FormattableDevice for MiTempDevice { + fn format_open_metrics(&self) -> impl Display + '_ { MitempFormatter { device: self } } } diff --git a/src/device/mod.rs b/src/device/mod.rs index 614f66b..a1afedb 100644 --- a/src/device/mod.rs +++ b/src/device/mod.rs @@ -32,6 +32,10 @@ pub struct DeviceStates { active_rf_temp_id: RfDeviceId, } +pub trait FormattableDevice { + fn format_open_metrics(&self) -> impl Display + '_; +} + impl DeviceStates { pub fn new( mi_temp_names: BTreeMap, @@ -44,12 +48,12 @@ impl DeviceStates { } } - pub fn devices(&self) -> impl Iterator { - self.devices.iter() + pub fn devices(&self) -> impl Iterator { + self.devices.values() } - pub fn dsmr_devices(&self) -> impl Iterator { - self.dsmr_devices.iter() + pub fn dsmr_devices(&self) -> impl Iterator { + self.dsmr_devices.values() } pub fn update(&mut self, device: Device, json: JsonValue) { @@ -147,12 +151,12 @@ impl DeviceStates { } } - pub fn mi_temp(&self) -> impl Iterator { - self.mi_temp_devices.iter() + pub fn mi_temp(&self) -> impl Iterator { + self.mi_temp_devices.values() } - pub fn rf_temp(&self) -> impl Iterator { - self.rf_temp_devices.iter() + pub fn rf_temp(&self) -> impl Iterator { + self.rf_temp_devices.values() } pub fn retain(&mut self, cleanup_time: Instant, ping_time: Instant, client: &AsyncClient) { diff --git a/src/device/rf.rs b/src/device/rf.rs index 7077ef6..3726120 100644 --- a/src/device/rf.rs +++ b/src/device/rf.rs @@ -5,6 +5,8 @@ use std::str::FromStr; use serde::{Deserialize, Deserializer, de::Error}; +use crate::device::FormattableDevice; + #[derive(Debug)] pub struct TempState { pub name: String, @@ -22,8 +24,10 @@ impl TempState { humidity: 0, } } +} - pub fn format_open_metrics(&self) -> impl Display + '_ { +impl FormattableDevice for TempState { + fn format_open_metrics(&self) -> impl Display + '_ { TempFormatter { device: self } } } diff --git a/src/device/tasmota.rs b/src/device/tasmota.rs index 3fd880e..85937f3 100644 --- a/src/device/tasmota.rs +++ b/src/device/tasmota.rs @@ -5,6 +5,8 @@ use std::{ use jzon::JsonValue; +use crate::device::FormattableDevice; + #[derive(Debug)] pub struct DeviceState { pub state: Option, @@ -100,8 +102,10 @@ impl DeviceState { pms.update(&json["PMS5003"]); } } +} - pub fn format_open_metrics(&self) -> impl Display + '_ { +impl FormattableDevice for DeviceState { + fn format_open_metrics(&self) -> impl Display + '_ { TasmotaFormatter { device: self } } } diff --git a/src/main.rs b/src/main.rs index 983ed51..d6afec3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ mod mqtt; mod topic; use crate::config::{Config, ListenConfig}; -use crate::device::{Device, DeviceStates}; +use crate::device::{Device, DeviceStates, FormattableDevice}; use crate::mqtt::mqtt_stream; use crate::topic::Topic; use clap::Parser; @@ -78,19 +78,19 @@ async fn serve(device_states: Arc>, listen: ListenConfig) { .map(move |state: Arc>| { let state = state.lock().unwrap(); let mut response = String::new(); - for (_, state) in state.devices() { + for state in state.devices() { write!(&mut response, "{}", state.format_open_metrics()) .expect("formatting failed"); } - for (_, state) in state.dsmr_devices() { + for state in state.dsmr_devices() { write!(&mut response, "{}", state.format_open_metrics()) .expect("formatting failed"); } - for (_, state) in state.mi_temp() { + for state in state.mi_temp() { write!(&mut response, "{}", state.format_open_metrics()) .expect("formatting failed"); } - for (_, state) in state.rf_temp() { + for state in state.rf_temp() { write!(&mut response, "{}", state.format_open_metrics()) .expect("formatting failed"); }