cleanup rf formatting

This commit is contained in:
Robin Appelman 2026-06-27 00:13:40 +02:00
commit 16993948cf
4 changed files with 166 additions and 153 deletions

View file

@ -4,9 +4,7 @@ mod mqtt;
mod topic;
use crate::config::{Config, ListenConfig};
use crate::device::{
Device, DeviceStates, format_device_state, format_dsmr_state, format_rf_temp_state,
};
use crate::device::{Device, DeviceStates, format_device_state, format_dsmr_state};
use crate::mqtt::mqtt_stream;
use crate::topic::Topic;
use clap::Parser;
@ -43,8 +41,7 @@ async fn main() -> Result<()> {
};
let mqtt_options = config.mqtt()?;
let device_states =
DeviceStates::new(config.names.mi_temp.clone(), config.names.rf_temp.clone());
let device_states = DeviceStates::new(config.names.mi_temp, config.names.rf_temp);
let device_states = Arc::new(Mutex::new(device_states));
ctrlc::set_handler(move || {
@ -52,7 +49,7 @@ async fn main() -> Result<()> {
})
.expect("Error setting Ctrl-C handler");
spawn(serve(device_states.clone(), config));
spawn(serve(device_states.clone(), config.listen));
loop {
let (client, stream) = mqtt_stream(mqtt_options.clone())
@ -73,9 +70,7 @@ async fn main() -> Result<()> {
}
}
async fn serve(device_states: Arc<Mutex<DeviceStates>>, config: Config) {
let rf_temp_names = config.names.rf_temp;
async fn serve(device_states: Arc<Mutex<DeviceStates>>, listen: ListenConfig) {
let state = warp::any().map(move || device_states.clone());
let metrics = warp::path!("metrics")
@ -92,14 +87,14 @@ async fn serve(device_states: Arc<Mutex<DeviceStates>>, config: Config) {
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()
for (_, state) in state.rf_temp() {
write!(&mut response, "{}", state.format_prometheus()).expect("formatting failed");
}
response
});
let cancel = async { ctrl_c().await.unwrap() };
match config.listen {
match listen {
ListenConfig::Ip { address, port } => {
warp::serve(metrics)
.bind((address, port))