formattable trait

This commit is contained in:
Robin Appelman 2026-06-27 00:13:40 +02:00
commit e13243f9a5
6 changed files with 36 additions and 18 deletions

View file

@ -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 }
}
}

View file

@ -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 }
}
}

View file

@ -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<BDAddr, String>,
@ -44,12 +48,12 @@ impl DeviceStates {
}
}
pub fn devices(&self) -> impl Iterator<Item = (&Device, &DeviceState)> {
self.devices.iter()
pub fn devices(&self) -> impl Iterator<Item = &impl FormattableDevice> {
self.devices.values()
}
pub fn dsmr_devices(&self) -> impl Iterator<Item = (&Device, &DsmrState)> {
self.dsmr_devices.iter()
pub fn dsmr_devices(&self) -> impl Iterator<Item = &impl FormattableDevice> {
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<Item = (&BDAddr, &MiTempDevice)> {
self.mi_temp_devices.iter()
pub fn mi_temp(&self) -> impl Iterator<Item = &impl FormattableDevice> {
self.mi_temp_devices.values()
}
pub fn rf_temp(&self) -> impl Iterator<Item = (&RfDeviceId, &TempState)> {
self.rf_temp_devices.iter()
pub fn rf_temp(&self) -> impl Iterator<Item = &impl FormattableDevice> {
self.rf_temp_devices.values()
}
pub fn retain(&mut self, cleanup_time: Instant, ping_time: Instant, client: &AsyncClient) {

View file

@ -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 }
}
}

View file

@ -5,6 +5,8 @@ use std::{
use jzon::JsonValue;
use crate::device::FormattableDevice;
#[derive(Debug)]
pub struct DeviceState {
pub state: Option<bool>,
@ -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 }
}
}