mirror of
https://codeberg.org/icewind/taspromto.git
synced 2026-08-02 12:14:45 +02:00
unify formattign
This commit is contained in:
parent
e13243f9a5
commit
ce9c143250
2 changed files with 35 additions and 26 deletions
|
|
@ -34,6 +34,22 @@ pub struct DeviceStates {
|
||||||
|
|
||||||
pub trait FormattableDevice {
|
pub trait FormattableDevice {
|
||||||
fn format_open_metrics(&self) -> impl Display + '_;
|
fn format_open_metrics(&self) -> impl Display + '_;
|
||||||
|
|
||||||
|
fn format_dyn(&self) -> DynFormattable<'_> {
|
||||||
|
DynFormattable {
|
||||||
|
inner: Box::new(self.format_open_metrics()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DynFormattable<'a> {
|
||||||
|
inner: Box<dyn Display + 'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FormattableDevice for DynFormattable<'_> {
|
||||||
|
fn format_open_metrics(&self) -> impl Display + '_ {
|
||||||
|
&self.inner
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DeviceStates {
|
impl DeviceStates {
|
||||||
|
|
@ -48,12 +64,25 @@ impl DeviceStates {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn devices(&self) -> impl Iterator<Item = &impl FormattableDevice> {
|
pub fn devices(&self) -> impl Iterator<Item = impl FormattableDevice> {
|
||||||
self.devices.values()
|
self.devices
|
||||||
}
|
.values()
|
||||||
|
.map(FormattableDevice::format_dyn)
|
||||||
pub fn dsmr_devices(&self) -> impl Iterator<Item = &impl FormattableDevice> {
|
.chain(
|
||||||
self.dsmr_devices.values()
|
self.dsmr_devices
|
||||||
|
.values()
|
||||||
|
.map(FormattableDevice::format_dyn),
|
||||||
|
)
|
||||||
|
.chain(
|
||||||
|
self.mi_temp_devices
|
||||||
|
.values()
|
||||||
|
.map(FormattableDevice::format_dyn),
|
||||||
|
)
|
||||||
|
.chain(
|
||||||
|
self.rf_temp_devices
|
||||||
|
.values()
|
||||||
|
.map(FormattableDevice::format_dyn),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, device: Device, json: JsonValue) {
|
pub fn update(&mut self, device: Device, json: JsonValue) {
|
||||||
|
|
@ -151,14 +180,6 @@ impl DeviceStates {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mi_temp(&self) -> impl Iterator<Item = &impl FormattableDevice> {
|
|
||||||
self.mi_temp_devices.values()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn rf_temp(&self) -> impl Iterator<Item = &impl FormattableDevice> {
|
|
||||||
self.rf_temp_devices.values()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn retain(&mut self, cleanup_time: Instant, ping_time: Instant, client: &AsyncClient) {
|
pub fn retain(&mut self, cleanup_time: Instant, ping_time: Instant, client: &AsyncClient) {
|
||||||
self.devices.retain(|device, state| {
|
self.devices.retain(|device, state| {
|
||||||
if state.last_seen < cleanup_time {
|
if state.last_seen < cleanup_time {
|
||||||
|
|
|
||||||
12
src/main.rs
12
src/main.rs
|
|
@ -82,18 +82,6 @@ async fn serve(device_states: Arc<Mutex<DeviceStates>>, listen: ListenConfig) {
|
||||||
write!(&mut response, "{}", state.format_open_metrics())
|
write!(&mut response, "{}", state.format_open_metrics())
|
||||||
.expect("formatting failed");
|
.expect("formatting failed");
|
||||||
}
|
}
|
||||||
for state in state.dsmr_devices() {
|
|
||||||
write!(&mut response, "{}", state.format_open_metrics())
|
|
||||||
.expect("formatting failed");
|
|
||||||
}
|
|
||||||
for state in state.mi_temp() {
|
|
||||||
write!(&mut response, "{}", state.format_open_metrics())
|
|
||||||
.expect("formatting failed");
|
|
||||||
}
|
|
||||||
for state in state.rf_temp() {
|
|
||||||
write!(&mut response, "{}", state.format_open_metrics())
|
|
||||||
.expect("formatting failed");
|
|
||||||
}
|
|
||||||
response
|
response
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue