discover thread: runs discovery every 5 minutes
This commit is contained in:
parent
b2b2db6ac8
commit
e4ffd63a8a
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1143,7 +1143,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "homekit-exporter"
|
name = "homekit-exporter"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
|
|
@ -12,7 +12,6 @@ use tokio::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
pairing_data::{Accessory, ServiceCharacteristic},
|
pairing_data::{Accessory, ServiceCharacteristic},
|
||||||
spawn_discover_thread,
|
|
||||||
tlv8::TlvEncode,
|
tlv8::TlvEncode,
|
||||||
ConnectionError, HomekitError, MdnsDiscoveredList,
|
ConnectionError, HomekitError, MdnsDiscoveredList,
|
||||||
};
|
};
|
||||||
|
@ -53,14 +52,7 @@ async fn reconnect(
|
||||||
.ok_or(ConnectionError::Discovery(DiscoveryError::NotFound))?
|
.ok_or(ConnectionError::Discovery(DiscoveryError::NotFound))?
|
||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
let socket = match TcpStream::connect(format!("{hostname}:{port}")).await {
|
let socket = TcpStream::connect(format!("{hostname}:{port}")).await?;
|
||||||
Ok(socket) => socket,
|
|
||||||
Err(_) => {
|
|
||||||
spawn_discover_thread(discovered.clone())?;
|
|
||||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
|
||||||
TcpStream::connect(format!("{hostname}:{port}")).await?
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(socket)
|
Ok(socket)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -62,55 +62,60 @@ impl PythonPairingData {
|
||||||
|
|
||||||
pub type MdnsDiscoveredList = Arc<RwLock<HashMap<String, (String, u16)>>>;
|
pub type MdnsDiscoveredList = Arc<RwLock<HashMap<String, (String, u16)>>>;
|
||||||
|
|
||||||
pub fn spawn_discover_thread(discovered: MdnsDiscoveredList) -> Result<(), DiscoveryError> {
|
pub fn spawn_discover_thread() -> Result<MdnsDiscoveredList, DiscoveryError> {
|
||||||
|
let discovered = Arc::new(RwLock::new(HashMap::new()));
|
||||||
|
let r_discovered = discovered.clone();
|
||||||
let stream = mdns::discover::all("_hap._tcp.local", Duration::from_secs(1))?.listen();
|
let stream = mdns::discover::all("_hap._tcp.local", Duration::from_secs(1))?.listen();
|
||||||
tokio::task::spawn(async move {
|
tokio::task::spawn(async move {
|
||||||
pin_mut!(stream);
|
pin_mut!(stream);
|
||||||
while let Some(Ok(response)) = stream.next().await {
|
loop {
|
||||||
let all = response
|
while let Some(Ok(response)) = stream.next().await {
|
||||||
.answers
|
let all = response
|
||||||
.iter()
|
.answers
|
||||||
.chain(response.additional.iter())
|
.iter()
|
||||||
.collect::<Vec<_>>();
|
.chain(response.additional.iter())
|
||||||
if let Some((name, id)) = all.iter().find_map(|record| {
|
.collect::<Vec<_>>();
|
||||||
if let RecordKind::TXT(v) = &record.kind {
|
if let Some((name, id)) = all.iter().find_map(|record| {
|
||||||
if let Some(id_string) = v.iter().find(|v| v.contains("id=")) {
|
if let RecordKind::TXT(v) = &record.kind {
|
||||||
let id = id_string[3..].to_string();
|
if let Some(id_string) = v.iter().find(|v| v.contains("id=")) {
|
||||||
return Some((record.name.clone(), id));
|
let id = id_string[3..].to_string();
|
||||||
}
|
return Some((record.name.clone(), id));
|
||||||
}
|
|
||||||
None
|
|
||||||
}) {
|
|
||||||
if let Some((target, port)) = all.iter().find_map(|record| {
|
|
||||||
if record.name == name {
|
|
||||||
if let RecordKind::SRV {
|
|
||||||
priority: _,
|
|
||||||
weight: _,
|
|
||||||
port,
|
|
||||||
target,
|
|
||||||
} = &record.kind
|
|
||||||
{
|
|
||||||
return Some((target.clone(), *port));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}) {
|
}) {
|
||||||
if let Some(ip) = all.iter().find_map(|record| {
|
if let Some((target, port)) = all.iter().find_map(|record| {
|
||||||
if record.name == target {
|
if record.name == name {
|
||||||
if let RecordKind::A(ip) = record.kind {
|
if let RecordKind::SRV {
|
||||||
return Some(ip);
|
priority: _,
|
||||||
|
weight: _,
|
||||||
|
port,
|
||||||
|
target,
|
||||||
|
} = &record.kind
|
||||||
|
{
|
||||||
|
return Some((target.clone(), *port));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}) {
|
}) {
|
||||||
let mut connections = discovered.write().await;
|
if let Some(ip) = all.iter().find_map(|record| {
|
||||||
connections.insert(id, (ip.to_string(), port));
|
if record.name == target {
|
||||||
|
if let RecordKind::A(ip) = record.kind {
|
||||||
|
return Some(ip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}) {
|
||||||
|
let mut connections = discovered.write().await;
|
||||||
|
connections.insert(id, (ip.to_string(), port));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
tokio::time::sleep(Duration::from_secs(5 * 60)).await;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Ok(())
|
Ok(r_discovered)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DevicePairingData {
|
struct DevicePairingData {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "homekit-exporter"
|
name = "homekit-exporter"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
description = "Prometheus exporter for HomeKit sensors"
|
description = "Prometheus exporter for HomeKit sensors"
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# Maintainer: Alex Janka <alex@alexjanka.com>
|
# Maintainer: Alex Janka <alex@alexjanka.com>
|
||||||
|
|
||||||
pkgname=homekit-logger
|
pkgname=homekit-logger
|
||||||
pkgver=0.1.0
|
pkgver=0.2.0
|
||||||
pkgrel=1
|
pkgrel=1
|
||||||
pkgdesc="Prometheus exporter for HomeKit sensors"
|
pkgdesc="Prometheus exporter for HomeKit sensors"
|
||||||
arch=('x86_64' 'aarch64')
|
arch=('x86_64' 'aarch64')
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
use std::{collections::HashMap, path::PathBuf, sync::Arc, time::Duration};
|
use std::{collections::HashMap, path::PathBuf, time::Duration};
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use homekit_controller::{spawn_discover_thread, DeviceConnection, HomekitError, ServiceType};
|
use homekit_controller::{spawn_discover_thread, DeviceConnection, HomekitError, ServiceType};
|
||||||
use server::launch;
|
use server::launch;
|
||||||
use tokio::sync::RwLock;
|
|
||||||
|
|
||||||
mod server;
|
mod server;
|
||||||
|
|
||||||
|
@ -44,8 +43,7 @@ async fn rocket() -> rocket::Rocket<rocket::Build> {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn init(pairing_data: PathBuf) -> Result<HashMap<String, DeviceConnection>, HomekitError> {
|
async fn init(pairing_data: PathBuf) -> Result<HashMap<String, DeviceConnection>, HomekitError> {
|
||||||
let discovered = Arc::new(RwLock::new(HashMap::new()));
|
let discovered = spawn_discover_thread()?;
|
||||||
spawn_discover_thread(discovered.clone())?;
|
|
||||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||||
if pairing_data.is_file() {
|
if pairing_data.is_file() {
|
||||||
let devices = homekit_controller::load(pairing_data)?;
|
let devices = homekit_controller::load(pairing_data)?;
|
||||||
|
|
Loading…
Reference in a new issue