mirror of
https://codeberg.org/icewind/taspromto.git
synced 2026-08-02 12:14:45 +02:00
start splitting device module
This commit is contained in:
parent
ccbac033d3
commit
2936b3cf22
2 changed files with 95 additions and 84 deletions
92
src/device/mitemp.rs
Normal file
92
src/device/mitemp.rs
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
use jzon::JsonValue;
|
||||
|
||||
use crate::device::BDAddr;
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
fmt::{self, Debug, Write},
|
||||
time::Instant,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MiTempState {
|
||||
temperature: f32,
|
||||
humidity: f32,
|
||||
dew_point: f32,
|
||||
battery: u8,
|
||||
pub last_seen: Instant,
|
||||
}
|
||||
|
||||
impl Default for MiTempState {
|
||||
fn default() -> Self {
|
||||
MiTempState {
|
||||
temperature: 0.0,
|
||||
humidity: 0.0,
|
||||
dew_point: 0.0,
|
||||
battery: 0,
|
||||
last_seen: Instant::now(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MiTempState {
|
||||
pub fn update(&mut self, json: &JsonValue) {
|
||||
self.last_seen = Instant::now();
|
||||
if let Some(temperature) = json["Temperature"].as_number().map(f32::from) {
|
||||
self.temperature = temperature;
|
||||
}
|
||||
if let Some(humidity) = json["Humidity"].as_number().map(f32::from) {
|
||||
self.humidity = humidity;
|
||||
}
|
||||
if let Some(battery) = json["Battery"]
|
||||
.as_number()
|
||||
.and_then(|num| u8::try_from(num).ok())
|
||||
{
|
||||
self.battery = battery;
|
||||
}
|
||||
if let Some(dew_point) = json["DewPoint"].as_number().map(f32::from) {
|
||||
self.dew_point = dew_point;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_mi_temp_state<W: Write>(
|
||||
mut writer: W,
|
||||
addr: BDAddr,
|
||||
names: &BTreeMap<BDAddr, String>,
|
||||
state: &MiTempState,
|
||||
) -> 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.
|
||||
|
||||
let name = if let Some(name) = names.get(&addr) {
|
||||
name
|
||||
} else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
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 state.humidity > 0.0 {
|
||||
writeln!(
|
||||
writer,
|
||||
"sensor_humidity{{mac=\"{}\", name=\"{}\"}} {}",
|
||||
addr, name, state.humidity
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1,3 +1,6 @@
|
|||
mod mitemp;
|
||||
|
||||
pub use crate::device::mitemp::{MiTempState, format_mi_temp_state};
|
||||
use color_eyre::{Report, Result, eyre::WrapErr};
|
||||
use jzon::JsonValue;
|
||||
use rumqttc::{AsyncClient, QoS};
|
||||
|
|
@ -287,48 +290,6 @@ impl DeviceState {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MiTempState {
|
||||
temperature: f32,
|
||||
humidity: f32,
|
||||
dew_point: f32,
|
||||
battery: u8,
|
||||
pub last_seen: Instant,
|
||||
}
|
||||
|
||||
impl Default for MiTempState {
|
||||
fn default() -> Self {
|
||||
MiTempState {
|
||||
temperature: 0.0,
|
||||
humidity: 0.0,
|
||||
dew_point: 0.0,
|
||||
battery: 0,
|
||||
last_seen: Instant::now(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MiTempState {
|
||||
pub fn update(&mut self, json: &JsonValue) {
|
||||
self.last_seen = Instant::now();
|
||||
if let Some(temperature) = json["Temperature"].as_number().map(f32::from) {
|
||||
self.temperature = temperature;
|
||||
}
|
||||
if let Some(humidity) = json["Humidity"].as_number().map(f32::from) {
|
||||
self.humidity = humidity;
|
||||
}
|
||||
if let Some(battery) = json["Battery"]
|
||||
.as_number()
|
||||
.and_then(|num| u8::try_from(num).ok())
|
||||
{
|
||||
self.battery = battery;
|
||||
}
|
||||
if let Some(dew_point) = json["DewPoint"].as_number().map(f32::from) {
|
||||
self.dew_point = dew_point;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_device_state<W: Write>(
|
||||
mut writer: W,
|
||||
device: &Device,
|
||||
|
|
@ -432,48 +393,6 @@ pub fn format_device_state<W: Write>(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn format_mi_temp_state<W: Write>(
|
||||
mut writer: W,
|
||||
addr: BDAddr,
|
||||
names: &BTreeMap<BDAddr, String>,
|
||||
state: &MiTempState,
|
||||
) -> 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.
|
||||
|
||||
let name = if let Some(name) = names.get(&addr) {
|
||||
name
|
||||
} else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
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 state.humidity > 0.0 {
|
||||
writeln!(
|
||||
writer,
|
||||
"sensor_humidity{{mac=\"{}\", name=\"{}\"}} {}",
|
||||
addr, name, state.humidity
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct TempState {
|
||||
temperature: f32,
|
||||
Loading…
Add table
Add a link
Reference in a new issue