online

This commit is contained in:
JMARyA 2025-04-30 16:51:49 +02:00
parent 6e31b95417
commit 46cf2f4572
5 changed files with 24 additions and 2 deletions

View file

@ -7,6 +7,11 @@ use owl::{Serialize, get, query};
use rumqttc::AsyncClient;
use sage::PersonaIdentity;
fn is_within_80_seconds(time: chrono::DateTime<chrono::Utc>) -> bool {
let now = chrono::Utc::now();
now.signed_duration_since(time).num_seconds() <= 80
}
/// Handle herd MQTT
pub async fn handle_mqtt(topic: String, data: Vec<u8>) {
log::info!("Received client request from {topic}");
@ -23,7 +28,17 @@ pub async fn handle_mqtt(topic: String, data: Vec<u8>) {
match cat {
"online" => {
log::info!("Device {client} reported ONLINE");
if let Some(online) = crate::ONLINE.get().unwrap().get(client) {
if !is_within_80_seconds(*online) {
log::info!("Device {client} came back ONLINE");
}
} else {
log::info!("Device {client} went ONLINE");
}
crate::ONLINE
.get()
.unwrap()
.insert(client.to_string(), chrono::Utc::now());
}
"respond" => {
let resp: ServerResponse = serde_json::from_slice(&dec.payload).unwrap();