Compare commits

..

No commits in common. "e4ffd63a8aa68d3e56850821a02497cc044bde7f" and "55c51c8a0a7d6bb67627e03f82a0c925a2c0d45e" have entirely different histories.

7 changed files with 56 additions and 44 deletions

2
Cargo.lock generated
View file

@ -1143,7 +1143,7 @@ dependencies = [
[[package]]
name = "homekit-exporter"
version = "0.2.0"
version = "0.1.0"
dependencies = [
"clap",
"env_logger",

View file

@ -12,6 +12,7 @@ use tokio::{
use crate::{
pairing_data::{Accessory, ServiceCharacteristic},
spawn_discover_thread,
tlv8::TlvEncode,
ConnectionError, HomekitError, MdnsDiscoveredList,
};
@ -52,7 +53,14 @@ async fn reconnect(
.ok_or(ConnectionError::Discovery(DiscoveryError::NotFound))?
.clone();
let socket = TcpStream::connect(format!("{hostname}:{port}")).await?;
let socket = match 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)
}
@ -276,6 +284,7 @@ impl AccessorySocket {
async fn get_next(&mut self) -> Result<Vec<u8>, HomekitError> {
// max packet size + authtag size + associated data size
let mut buf = [0; 1024 + 16 + 2];
// let mut buf = [0; 1536];
let mut read_num = self.socket.read(&mut buf).await?;
let mut tries = 0;

View file

@ -62,13 +62,10 @@ impl PythonPairingData {
pub type MdnsDiscoveredList = Arc<RwLock<HashMap<String, (String, u16)>>>;
pub fn spawn_discover_thread() -> Result<MdnsDiscoveredList, DiscoveryError> {
let discovered = Arc::new(RwLock::new(HashMap::new()));
let r_discovered = discovered.clone();
pub fn spawn_discover_thread(discovered: MdnsDiscoveredList) -> Result<(), DiscoveryError> {
let stream = mdns::discover::all("_hap._tcp.local", Duration::from_secs(1))?.listen();
tokio::task::spawn(async move {
pin_mut!(stream);
loop {
while let Some(Ok(response)) = stream.next().await {
let all = response
.answers
@ -112,10 +109,8 @@ pub fn spawn_discover_thread() -> Result<MdnsDiscoveredList, DiscoveryError> {
}
}
}
tokio::time::sleep(Duration::from_secs(5 * 60)).await;
}
});
Ok(r_discovered)
Ok(())
}
struct DevicePairingData {

View file

@ -117,6 +117,12 @@ pub trait TlvEncode {
fn encode(&self) -> Vec<u8>;
}
// impl<const N: usize> TlvEncode for &[(u8, Vec<u8>); N] {
// fn encode(&self) -> Vec<u8> {
// (*self as &[(u8, Vec<u8>)]).encode()
// }
// }
impl<T> TlvEncode for T
where
T: AsRef<[(u8, Vec<u8>)]>,

View file

@ -1,6 +1,6 @@
[package]
name = "homekit-exporter"
version = "0.2.0"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
description = "Prometheus exporter for HomeKit sensors"

View file

@ -1,7 +1,7 @@
# Maintainer: Alex Janka <alex@alexjanka.com>
pkgname=homekit-logger
pkgver=0.2.0
pkgver=0.1.0
pkgrel=1
pkgdesc="Prometheus exporter for HomeKit sensors"
arch=('x86_64' 'aarch64')

View file

@ -1,11 +1,12 @@
#[macro_use]
extern crate rocket;
use std::{collections::HashMap, path::PathBuf, time::Duration};
use std::{collections::HashMap, path::PathBuf, sync::Arc, time::Duration};
use clap::Parser;
use homekit_controller::{spawn_discover_thread, DeviceConnection, HomekitError, ServiceType};
use server::launch;
use tokio::sync::RwLock;
mod server;
@ -43,7 +44,8 @@ async fn rocket() -> rocket::Rocket<rocket::Build> {
}
async fn init(pairing_data: PathBuf) -> Result<HashMap<String, DeviceConnection>, HomekitError> {
let discovered = spawn_discover_thread()?;
let discovered = Arc::new(RwLock::new(HashMap::new()));
spawn_discover_thread(discovered.clone())?;
tokio::time::sleep(Duration::from_secs(1)).await;
if pairing_data.is_file() {
let devices = homekit_controller::load(pairing_data)?;