Skip to content

Commit

Permalink
Added repeating discovery call
Browse files Browse the repository at this point in the history
  • Loading branch information
chipp committed Aug 17, 2024
1 parent b53df9b commit 03592b8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bin/elisheba/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
10 changes: 10 additions & 0 deletions lib/sonoff/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"));
Expand Down
27 changes: 25 additions & 2 deletions lib/sonoff/src/client/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
.join(", ")
);
info!(
"{} left to resolve: [{}]",
hosts.len(),
hosts
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
.join(", ")
);

last_update = Instant::now();
}
Expand All @@ -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();
}
}
Expand Down

0 comments on commit 03592b8

Please sign in to comment.