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(());
|
||||
};
|
||||
|
||||
if state.battery > 0 {
|
||||
impl Display for MitempFormatter<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if self.device.battery > 0 {
|
||||
writeln!(
|
||||
writer,
|
||||
f,
|
||||
"sensor_battery{{mac=\"{}\", name=\"{}\"}} {}",
|
||||
addr, name, state.battery
|
||||
self.device.addr, self.device.name, self.device.battery
|
||||
)?;
|
||||
}
|
||||
|
||||
if state.temperature > 0.0 {
|
||||
if self.device.temperature > 0.0 {
|
||||
writeln!(
|
||||
writer,
|
||||
f,
|
||||
"sensor_temperature{{mac=\"{}\", name=\"{}\"}} {}",
|
||||
addr, name, state.temperature
|
||||
self.device.addr, self.device.name, self.device.temperature
|
||||
)?;
|
||||
}
|
||||
|
||||
if state.humidity > 0.0 {
|
||||
if self.device.humidity > 0.0 {
|
||||
writeln!(
|
||||
writer,
|
||||
f,
|
||||
"sensor_humidity{{mac=\"{}\", name=\"{}\"}} {}",
|
||||
addr, name, state.humidity
|
||||
self.device.addr, self.device.name, self.device.humidity
|
||||
)?;
|
||||
}
|
||||
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};
|
||||
|
|
|
|||
|
|
@ -5,13 +5,13 @@ mod topic;
|
|||
|
||||
use crate::config::{Config, ListenConfig};
|
||||
use crate::device::{
|
||||
Device, DeviceStates, format_device_state, format_dsmr_state, format_mi_temp_state,
|
||||
format_rf_temp_state,
|
||||
Device, DeviceStates, format_device_state, format_dsmr_state, format_rf_temp_state,
|
||||
};
|
||||
use crate::mqtt::mqtt_stream;
|
||||
use crate::topic::Topic;
|
||||
use clap::Parser;
|
||||
use color_eyre::{Result, eyre::WrapErr};
|
||||
use std::fmt::Write;
|
||||
|
||||
use pin_utils::pin_mut;
|
||||
use rumqttc::{AsyncClient, Publish, QoS};
|
||||
|
|
@ -74,7 +74,6 @@ async fn main() -> Result<()> {
|
|||
}
|
||||
|
||||
async fn serve(device_states: Arc<Mutex<DeviceStates>>, config: Config) {
|
||||
let mi_temp_names = config.names.mi_temp;
|
||||
let rf_temp_names = config.names.rf_temp;
|
||||
|
||||
let state = warp::any().map(move || device_states.clone());
|
||||
|
|
@ -90,8 +89,8 @@ async fn serve(device_states: Arc<Mutex<DeviceStates>>, config: Config) {
|
|||
for (device, state) in state.dsmr_devices() {
|
||||
format_dsmr_state(&mut response, device.hostname.as_str(), state).unwrap();
|
||||
}
|
||||
for (addr, state) in state.mi_temp() {
|
||||
format_mi_temp_state(&mut response, *addr, &mi_temp_names, state).unwrap()
|
||||
for (_, state) in state.mi_temp() {
|
||||
write!(&mut response, "{}", state.format_prometheus()).expect("formatting failed");
|
||||
}
|
||||
for (channel, state) in state.rf_temp() {
|
||||
format_rf_temp_state(&mut response, channel, &rf_temp_names, state).unwrap()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue