mirror of
https://codeberg.org/icewind/wifi-prometheus-exporter.git
synced 2026-06-03 08:34:25 +02:00
log state changes
This commit is contained in:
parent
882602ba36
commit
537c02a31f
1 changed files with 16 additions and 4 deletions
18
src/main.rs
18
src/main.rs
|
|
@ -3,7 +3,7 @@ use rumqttc::{AsyncClient, ClientError, MqttOptions, QoS};
|
|||
use ssh2::{ErrorCode, Session};
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::OsStr;
|
||||
use std::fmt::Write;
|
||||
use std::fmt::{Display, Formatter, Write};
|
||||
use std::io::prelude::*;
|
||||
use std::net::{TcpStream, ToSocketAddrs};
|
||||
use std::path::Path;
|
||||
|
|
@ -127,6 +127,17 @@ enum Update {
|
|||
Connected,
|
||||
}
|
||||
|
||||
impl Display for Update {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
let s = match self {
|
||||
Update::New => "discovered",
|
||||
Update::Disconnected => "disconnected",
|
||||
Update::Connected => "connected",
|
||||
};
|
||||
write!(f, "{}", s)
|
||||
}
|
||||
}
|
||||
|
||||
impl DeviceStates {
|
||||
fn update(&mut self, new: Vec<String>) -> Vec<(String, Update)> {
|
||||
let mut updated = Vec::with_capacity(4);
|
||||
|
|
@ -191,8 +202,9 @@ async fn listener(
|
|||
match wifi_listener.list_connected_devices() {
|
||||
Ok(devices) => {
|
||||
let updates = connected.lock().unwrap().update(devices);
|
||||
if let Some(client) = client.as_mut() {
|
||||
for (mac, update) in updates {
|
||||
println!("{} {}", mac, update);
|
||||
if let Some(client) = client.as_mut() {
|
||||
if let Err(e) = send_update(client, mac, update).await {
|
||||
eprintln!("Error while sending mqtt update: {:?}", e);
|
||||
}
|
||||
|
|
@ -226,7 +238,7 @@ async fn send_update(
|
|||
"manufacturer": "Icewind",
|
||||
"model": "Wifi Tracker",
|
||||
"identifiers": "{mac}"
|
||||
}}
|
||||
}},
|
||||
"name": "Wifi device {mac}",
|
||||
"payload_home": "connected",
|
||||
"payload_not_home": "disconnected",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue