This commit is contained in:
Robin Appelman 2026-03-26 16:29:00 +01:00
commit c3b5238a51
13 changed files with 532 additions and 603 deletions

View file

@ -38,7 +38,7 @@ pub struct NamesConfig {
#[serde(rename = "mitemp")]
pub mi_temp: BTreeMap<BDAddr, String>,
#[serde(rename = "rftemp")]
pub rf_temp: HashMap<RfDeviceId<'static>, String>,
pub rf_temp: HashMap<RfDeviceId, String>,
}
#[derive(Debug, Deserialize)]

View file

@ -17,8 +17,8 @@ pub struct DeviceStates {
pub devices: HashMap<Device, DeviceState>,
pub dsmr_devices: HashMap<Device, DsmrState>,
pub mi_temp_devices: BTreeMap<BDAddr, MiTempState>,
pub rf_temp_devices: HashMap<RfDeviceId<'static>, TempState>,
active_rf_temp_id: RfDeviceId<'static>,
pub rf_temp_devices: HashMap<RfDeviceId, TempState>,
active_rf_temp_id: RfDeviceId,
}
impl DeviceStates {
@ -65,10 +65,7 @@ impl DeviceStates {
pub fn update_rf(&mut self, payload: &str) {
if let Some(data) = parse_rf_payload(payload) {
let state = self
.rf_temp_devices
.entry(data.device_id().to_owned())
.or_default();
let state = self.rf_temp_devices.entry(data.device_id()).or_default();
state.humidity = data.humidity;
state.temperature = data.temperature;
} else {
@ -79,7 +76,7 @@ impl DeviceStates {
pub fn update_rtl(&mut self, device: &str, field: &str, payload: &str) {
if self.active_rf_temp_id.name != device {
self.active_rf_temp_id = RfDeviceId::default();
self.active_rf_temp_id.name = device.to_string().into();
self.active_rf_temp_id.name = device.into();
}
match field {
"id" => self.active_rf_temp_id.id = payload.parse().unwrap_or_default(),
@ -92,7 +89,7 @@ impl DeviceStates {
fn update_active_rtl(&mut self, field: &str, payload: &str) {
let state = self
.rf_temp_devices
.entry(self.active_rf_temp_id.to_owned())
.entry(self.active_rf_temp_id.clone())
.or_default();
match field {
"temperature_F" => {
@ -110,7 +107,7 @@ impl DeviceStates {
self.mi_temp_devices.iter()
}
pub fn rf_temp(&self) -> impl Iterator<Item = (&RfDeviceId<'static>, &TempState)> {
pub fn rf_temp(&self) -> impl Iterator<Item = (&RfDeviceId, &TempState)> {
self.rf_temp_devices.iter()
}
@ -333,7 +330,7 @@ pub fn format_device_state<W: Write>(
mut writer: W,
device: &Device,
state: &DeviceState,
) -> std::fmt::Result {
) -> fmt::Result {
if state.name.is_empty() {
println!("{} has no name set, skipping", device.hostname);
return Ok(());
@ -437,7 +434,7 @@ pub fn format_mi_temp_state<W: Write>(
addr: BDAddr,
names: &BTreeMap<BDAddr, String>,
state: &MiTempState,
) -> std::fmt::Result {
) -> 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.
@ -485,7 +482,7 @@ pub fn format_rf_temp_state<W: Write>(
channel: &RfDeviceId,
names: &HashMap<RfDeviceId, String>,
state: &TempState,
) -> std::fmt::Result {
) -> fmt::Result {
let name = if let Some(name) = names.get(channel) {
name
} else {
@ -510,11 +507,7 @@ pub fn format_rf_temp_state<W: Write>(
Ok(())
}
pub fn format_dsmr_state<W: Write>(
mut writer: W,
device: &str,
state: &DsmrState,
) -> std::fmt::Result {
pub fn format_dsmr_state<W: Write>(mut writer: W, device: &str, state: &DsmrState) -> fmt::Result {
let power_total = state.power_total_tariff_1.unwrap_or_default()
+ state.power_total_tariff_2.unwrap_or_default();
if power_total > 0.0 {
@ -715,7 +708,7 @@ pub fn format_pms_state<W: Write>(
device: &Device,
device_state: &DeviceState,
state: &PMSState,
) -> std::fmt::Result {
) -> fmt::Result {
let name = &device_state.name;
writeln!(
@ -792,33 +785,23 @@ struct RfPayload<'a> {
}
impl<'a> RfPayload<'a> {
pub fn device_id(&self) -> RfDeviceId<'a> {
pub fn device_id(&self) -> RfDeviceId {
RfDeviceId {
name: Cow::Borrowed(self.name),
name: self.name.into(),
id: self.id,
channel: self.channel,
}
}
}
#[derive(Hash, PartialEq, Eq, Debug, Clone, Default)]
pub struct RfDeviceId<'a> {
name: Cow<'a, str>,
#[derive(Hash, PartialEq, Eq, Debug, Default, Clone)]
pub struct RfDeviceId {
name: String,
id: u16,
channel: u8,
}
impl RfDeviceId<'_> {
pub fn to_owned(&self) -> RfDeviceId<'static> {
RfDeviceId {
name: Cow::Owned(self.name.to_string()),
id: self.id,
channel: self.channel,
}
}
}
impl<'de> Deserialize<'de> for RfDeviceId<'static> {
impl<'de> Deserialize<'de> for RfDeviceId {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
@ -828,7 +811,7 @@ impl<'de> Deserialize<'de> for RfDeviceId<'static> {
}
}
impl FromStr for RfDeviceId<'static> {
impl FromStr for RfDeviceId {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
@ -837,14 +820,14 @@ impl FromStr for RfDeviceId<'static> {
let id = parts.next().unwrap_or_default().parse()?;
let channel = parts.next().unwrap_or_default().parse()?;
Ok(RfDeviceId {
name: name.to_string().into(),
name: name.into(),
id,
channel,
})
}
}
fn parse_rf_payload(payload: &str) -> Option<RfPayload> {
fn parse_rf_payload<'a>(payload: &'a str) -> Option<RfPayload<'a>> {
let mut parts = payload.split(";").skip(2);
let name = parts.next()?;
let id = parts.next()?.strip_prefix("ID=")?.parse().ok()?;

View file

@ -72,8 +72,8 @@ async fn main() -> Result<()> {
}
async fn serve(device_states: Arc<Mutex<DeviceStates>>, config: Config) {
let mi_temp_names = config.names.mi_temp.clone();
let rf_temp_names = config.names.rf_temp.clone();
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());
@ -104,7 +104,7 @@ async fn serve(device_states: Arc<Mutex<DeviceStates>>, config: Config) {
ListenConfig::Unix { socket: path } => {
let listener = UnixListener::bind(path).unwrap();
let incoming = UnixListenerStream::new(listener);
warp::serve(metrics).run_incoming(incoming).await;
warp::serve(metrics).incoming(incoming);
}
}
}