Skip to content

Commit

Permalink
feat: provide abortDiscover handler
Browse files Browse the repository at this point in the history
feat: abort service discovery once bridge selected

this change implements functionality that aborts the discovery daemon
once a bridge has been selected. we also provide a dedicated
`abortDiscover` handler so that the discovery daemon may be started and
stopped at will.
  • Loading branch information
cedricschwyter committed Jul 8, 2023
1 parent 0e5a935 commit dadd53e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version = "0.0.4"
description = "A Tauri App to manage your Philips Hue devices from anywhere"
authors = [
"Kyrill Gobber <kyrill@gobber.ch>",
"Soryn Bächli <>",
"Cedric Schwyter <cedricschwyter@bluewin.ch>",
"Soryn Bächli <contact@soryn.dev>",
"Cedric Schwyter <cedric@schwyter.io>",
]
license = ""
repository = "https://github.com/KyrillGobber/huehuehue"
Expand Down
13 changes: 11 additions & 2 deletions src-tauri/core/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub async fn set_selected_bridge(
state: State<'_, HueHueHueState>,
) -> Result<(), HueHueHueError> {
let mut huehuehue = state.0.lock().await;
huehuehue.set_selected_bridge(mdns_name);
huehuehue.set_selected_bridge(mdns_name)?;

Ok(())
}
Expand All @@ -248,6 +248,14 @@ pub async fn get_discovered_bridges(
.collect())
}

#[tauri::command]
#[specta::specta]
pub async fn abort_discover(state: State<'_, HueHueHueState>) -> Result<(), HueHueHueError> {
let mut huehuehue = state.0.lock().await;

huehuehue.abort_discover()
}

handlers!(
get_lights,
get_light,
Expand Down Expand Up @@ -304,5 +312,6 @@ handlers!(
get_smart_scenes,
get_smart_scene,
set_selected_bridge,
get_discovered_bridges
get_discovered_bridges,
abort_discover
);
28 changes: 23 additions & 5 deletions src-tauri/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use mdns::RecordKind;
use reachable::*;
use reqwest::Client;
use std::{collections::HashMap, net::IpAddr, sync::Arc, time::Duration};
use tokio::{sync::Mutex, task::JoinHandle};
use tokio::{
sync::Mutex,
task::{AbortHandle, JoinHandle},
};

const HUE_BRIDGE_SERVICE_NAME: &str = "_hue._tcp.local";
const HUE_BRIDGE_SERVICE_QUERY_INTERVAL_SECONDS: u64 = 1;
Expand All @@ -23,6 +26,7 @@ pub struct HueHueHue {
bridges: Arc<Mutex<HashMap<String, IpAddr>>>,
selected_bridge: String,
client: Client,
discovery_abort_handle: Option<AbortHandle>,
}

pub struct HueHueHueState(pub Mutex<HueHueHue>);
Expand Down Expand Up @@ -54,8 +58,10 @@ impl HueHueHue {
}
}

pub fn set_selected_bridge(&mut self, mdns_name: String) {
pub fn set_selected_bridge(&mut self, mdns_name: String) -> Result<(), HueHueHueError> {
self.selected_bridge = mdns_name;

self.abort_discover()
}

pub async fn get_discovered_bridges(&self) -> HashMap<String, IpAddr> {
Expand All @@ -69,9 +75,17 @@ impl HueHueHue {
)
}

pub fn discover(&self) -> JoinHandle<Result<(), HueHueHueError>> {
pub fn abort_discover(&mut self) -> Result<(), HueHueHueError> {
if let Some(handle) = &self.discovery_abort_handle {
handle.abort();
}

Ok(())
}

pub fn discover(&mut self) -> JoinHandle<Result<(), HueHueHueError>> {
let addrs = self.bridges.clone();
tokio::spawn(async move {
let discovery_handle = tokio::spawn(async move {
info!(
"initiating hue bridge discovery mDNS query service for address \"{}\"...",
HUE_BRIDGE_SERVICE_NAME
Expand Down Expand Up @@ -103,6 +117,10 @@ impl HueHueHue {
}

Ok(())
})
});

self.discovery_abort_handle = Some(discovery_handle.abort_handle());

discovery_handle
}
}
2 changes: 1 addition & 1 deletion src-tauri/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub async fn main() -> Result<(), HueHueHueError> {
return Ok(());
}
}
let huehuehue: HueHueHue = HueHueHue::with_config(&config);
let mut huehuehue: HueHueHue = HueHueHue::with_config(&config);
info!("starting huehuehue...");
huehuehue.discover();
huehuehue_handlers!(tauri::Builder::default())
Expand Down

0 comments on commit dadd53e

Please sign in to comment.