mirror of
https://codeberg.org/icewind/taspromto.git
synced 2026-08-02 12:14:45 +02:00
cleanup mitemp formatting
This commit is contained in:
parent
7335d35c68
commit
a9a2f5270e
3 changed files with 37 additions and 44 deletions
|
|
@ -2,8 +2,7 @@ use jzon::JsonValue;
|
|||
|
||||
use crate::device::BDAddr;
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
fmt::{self, Debug, Write},
|
||||
fmt::{self, Debug, Display},
|
||||
time::Instant,
|
||||
};
|
||||
|
||||
|
|
@ -49,46 +48,41 @@ impl MiTempDevice {
|
|||
self.dew_point = dew_point;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_prometheus(&self) -> impl Display + '_ {
|
||||
MitempFormatter { device: &self }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_mi_temp_state<W: Write>(
|
||||
mut writer: W,
|
||||
addr: BDAddr,
|
||||
names: &BTreeMap<BDAddr, String>,
|
||||
state: &MiTempDevice,
|
||||
) -> fmt::Result {
|
||||
// sensor_battery{name="Living Room", mac="58:2D:34:39:1D:5B"} 100
|
||||
// sensor_temperature{name="Living Room", mac="58:2D:34:39:1D:5B"} 16.2
|
||||
// sensor_humidity{name="Living Room", mac="58:2D:34:39:1D:5B"} 61.
|
||||
struct MitempFormatter<'a> {
|
||||
device: &'a MiTempDevice,
|
||||
}
|
||||
|
||||
let name = if let Some(name) = names.get(&addr) {
|
||||
name
|
||||
} else {
|
||||
return Ok(());
|
||||
};
|
||||
impl Display for MitempFormatter<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if self.device.battery > 0 {
|
||||
writeln!(
|
||||
f,
|
||||
"sensor_battery{{mac=\"{}\", name=\"{}\"}} {}",
|
||||
self.device.addr, self.device.name, self.device.battery
|
||||
)?;
|
||||
}
|
||||
|
||||
if state.battery > 0 {
|
||||
writeln!(
|
||||
writer,
|
||||
"sensor_battery{{mac=\"{}\", name=\"{}\"}} {}",
|
||||
addr, name, state.battery
|
||||
)?;
|
||||
}
|
||||
|
||||
if state.temperature > 0.0 {
|
||||
writeln!(
|
||||
writer,
|
||||
"sensor_temperature{{mac=\"{}\", name=\"{}\"}} {}",
|
||||
addr, name, state.temperature
|
||||
)?;
|
||||
}
|
||||
if self.device.temperature > 0.0 {
|
||||
writeln!(
|
||||
f,
|
||||
"sensor_temperature{{mac=\"{}\", name=\"{}\"}} {}",
|
||||
self.device.addr, self.device.name, self.device.temperature
|
||||
)?;
|
||||
}
|
||||
|
||||
if state.humidity > 0.0 {
|
||||
writeln!(
|
||||
writer,
|
||||
"sensor_humidity{{mac=\"{}\", name=\"{}\"}} {}",
|
||||
addr, name, state.humidity
|
||||
)?;
|
||||
if self.device.humidity > 0.0 {
|
||||
writeln!(
|
||||
f,
|
||||
"sensor_humidity{{mac=\"{}\", name=\"{}\"}} {}",
|
||||
self.device.addr, self.device.name, self.device.humidity
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
mod mitemp;
|
||||
|
||||
pub use crate::device::mitemp::{MiTempDevice, format_mi_temp_state};
|
||||
pub use crate::device::mitemp::MiTempDevice;
|
||||
use color_eyre::{Report, Result, eyre::WrapErr};
|
||||
use jzon::JsonValue;
|
||||
use rumqttc::{AsyncClient, QoS};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue