From 03592b8f2f45d5b11693922ce89fb93c881870f7 Mon Sep 17 00:00:00 2001 From: Vladimir Burdukov Date: Sat, 17 Aug 2024 11:39:03 +0300 Subject: [PATCH] Added repeating discovery call --- Makefile | 2 +- bin/elisheba/src/main.rs | 2 +- lib/sonoff/src/client.rs | 10 ++++++++++ lib/sonoff/src/client/discovery.rs | 27 +++++++++++++++++++++++++-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 2021a98..b2a0b06 100644 --- a/Makefile +++ b/Makefile @@ -139,7 +139,7 @@ test_isabel_libs_arm64: --load run_elisheba: RUST_LOG = elisheba=debug,sonoff=debug,info -run_elisheba: KEYS = $(shell op read "op://private/elisheba devices/notesPlain") +run_elisheba: KEYS = 10020750eb=$(shell op read "op://private/elisheba devices/10020750eb"),1002074ed2=$(shell op read "op://private/elisheba devices/1002074ed2") run_elisheba: MQTT_ADDRESS = mqtt://localhost:1883 run_elisheba: MQTT_USER = elisheba run_elisheba: MQTT_PASS = 123mqtt diff --git a/bin/elisheba/src/main.rs b/bin/elisheba/src/main.rs index e810fa6..8672179 100644 --- a/bin/elisheba/src/main.rs +++ b/bin/elisheba/src/main.rs @@ -10,7 +10,7 @@ use transport::state::StateUpdate; use transport::{connect_mqtt, Topic}; use futures_util::StreamExt; -use log::{error, info, trace}; +use log::{debug, error, info, trace}; use paho_mqtt::{AsyncClient as MqClient, MessageBuilder, QOS_1}; use tokio::signal::unix::{signal, SignalKind}; use tokio::{task, time}; diff --git a/lib/sonoff/src/client.rs b/lib/sonoff/src/client.rs index 1881664..99c5fd9 100644 --- a/lib/sonoff/src/client.rs +++ b/lib/sonoff/src/client.rs @@ -220,18 +220,26 @@ impl Client { let host = host.ok_or(Error::MissingHostname)?; let host = host.to_string(); + debug!("got host {host}"); + let mut manager = self.manager.lock().await; let result = if let Some(device) = manager.devices.get_mut(&host) { + debug!("host exists {host}"); + if let Some(ipv4) = ipv4 { + debug!("updating ipv4 {ipv4}"); device.addr.set_ip(ipv4); } if let Some(port) = port { + debug!("updating port {port}"); device.addr.set_port(port); } if let Some(mut info) = info { + debug!("updating meta {info:?}"); + let key = self .keys .get(&device.id) @@ -243,6 +251,8 @@ impl Client { HandleResult::UpdatedDevice(host, device.clone()) } else { + debug!("new host {host}"); + let service = service.ok_or(Error::MissingService)?; if service != "_ewelink._tcp.local" { return Ok(HandleResult::Ignored("Unknown service")); diff --git a/lib/sonoff/src/client/discovery.rs b/lib/sonoff/src/client/discovery.rs index feb1fd1..6522553 100644 --- a/lib/sonoff/src/client/discovery.rs +++ b/lib/sonoff/src/client/discovery.rs @@ -48,8 +48,23 @@ impl Client { break; } - info!("{} left to discover", ids.len()); - info!("{} left to resolve", hosts.len()); + info!( + "{} left to discover: [{}]", + ids.len(), + ids.iter() + .map(|s| s.as_str()) + .collect::>() + .join(", ") + ); + info!( + "{} left to resolve: [{}]", + hosts.len(), + hosts + .iter() + .map(|s| s.as_str()) + .collect::>() + .join(", ") + ); last_update = Instant::now(); } @@ -60,10 +75,18 @@ impl Client { if last_update.elapsed().as_secs() > 2 { for host in hosts.iter() { debug!("query device again: {}", host); + let query = create_query_packet(&host); socket.send_to(&query, MDNS_ADDR).await?; } + if !ids.is_empty() { + debug!("discover devices again"); + + let discover = create_discovery_packet(SERVICE); + socket.send_to(&discover, MDNS_ADDR).await?; + } + last_update = Instant::now(); } }