discover command for browsing mdns

This commit is contained in:
Alex Janka 2024-03-05 10:05:43 +11:00
parent e4ffd63a8a
commit a4e3e37e6b
3 changed files with 37 additions and 2 deletions

2
Cargo.lock generated
View file

@ -1147,8 +1147,10 @@ version = "0.2.0"
dependencies = [ dependencies = [
"clap", "clap",
"env_logger", "env_logger",
"futures-util",
"homekit-controller", "homekit-controller",
"log", "log",
"mdns",
"rocket", "rocket",
"tokio", "tokio",
] ]

View file

@ -19,3 +19,5 @@ log = "0.4"
clap = { version = "4.0", features = ["derive"] } clap = { version = "4.0", features = ["derive"] }
homekit-controller = { path = "../homekit-controller" } homekit-controller = { path = "../homekit-controller" }
rocket = { version = "0.5", features = ["json"] } rocket = { version = "0.5", features = ["json"] }
mdns = "3.0.0"
futures-util = "0.3.30"

View file

@ -3,7 +3,8 @@ extern crate rocket;
use std::{collections::HashMap, path::PathBuf, time::Duration}; use std::{collections::HashMap, path::PathBuf, time::Duration};
use clap::Parser; use clap::{Parser, Subcommand};
use futures_util::{pin_mut, StreamExt};
use homekit_controller::{spawn_discover_thread, DeviceConnection, HomekitError, ServiceType}; use homekit_controller::{spawn_discover_thread, DeviceConnection, HomekitError, ServiceType};
use server::launch; use server::launch;
@ -12,12 +13,22 @@ mod server;
#[derive(Parser, Debug, Clone)] #[derive(Parser, Debug, Clone)]
#[clap(author, version, about, long_about = None)] #[clap(author, version, about, long_about = None)]
struct Args { struct Args {
#[command(subcommand)]
command: Commands,
#[clap(long, default_value = "/etc/homekit-exporter/pairing.json")] #[clap(long, default_value = "/etc/homekit-exporter/pairing.json")]
pairing_data: PathBuf, pairing_data: PathBuf,
#[clap(long, default_value_t = 6666)] #[clap(long, default_value_t = 6666)]
port: usize, port: usize,
} }
#[derive(Subcommand, Debug, Clone)]
enum Commands {
/// Run logging server
Watch,
/// Discover devices
Discover,
}
const SENSORS: [ServiceType; 11] = [ const SENSORS: [ServiceType; 11] = [
ServiceType::AirQualitySensor, ServiceType::AirQualitySensor,
ServiceType::CarbonDioxideSensor, ServiceType::CarbonDioxideSensor,
@ -36,10 +47,30 @@ const SENSORS: [ServiceType; 11] = [
async fn rocket() -> rocket::Rocket<rocket::Build> { async fn rocket() -> rocket::Rocket<rocket::Build> {
env_logger::init(); env_logger::init();
let args = Args::parse(); let args = Args::parse();
match init(args.pairing_data).await { match args.command {
Commands::Watch => match init(args.pairing_data).await {
Ok(paired) => launch(paired, args.port), Ok(paired) => launch(paired, args.port),
Err(e) => panic!("Error {e:#?}"), Err(e) => panic!("Error {e:#?}"),
},
Commands::Discover => {
println!("discovering homekit devices via mdns");
match discover().await {
Ok(_) => todo!(),
Err(_) => todo!(),
} }
}
}
}
async fn discover() -> Result<(), mdns::Error> {
let stream = mdns::discover::all("_hap._tcp.local", Duration::from_secs(1))?.listen();
pin_mut!(stream);
while let Some(Ok(response)) = stream.next().await {
println!("found device:\n\t{response:#?}");
}
Ok(())
} }
async fn init(pairing_data: PathBuf) -> Result<HashMap<String, DeviceConnection>, HomekitError> { async fn init(pairing_data: PathBuf) -> Result<HashMap<String, DeviceConnection>, HomekitError> {