Prototype of the first automation stull ugly

This commit is contained in:
Valère Plantevin
2026-05-12 14:00:12 -04:00
parent 20d59ed0ba
commit 7f54aea439
9 changed files with 152 additions and 4 deletions

View File

@@ -214,6 +214,50 @@ async fn main() -> anyhow::Result<()> {
None
};
let presence_slot_opt = slots.iter().find(|s| s.sensor_type == SensorType::Presence).cloned();
let conn_clone = client.conn.clone();
if let Some(presence_slot) = presence_slot_opt {
tokio::spawn(async move {
if let Ok(listener) = tokio::net::TcpListener::bind("0.0.0.0:9002").await {
tracing::info!("Simulator HTTP trigger API listening on 0.0.0.0:9002");
while let Ok((mut socket, _)) = listener.accept().await {
let conn = conn_clone.clone();
let slot = presence_slot.clone();
tokio::spawn(async move {
let mut buf = [0; 1024];
use tokio::io::{AsyncReadExt, AsyncWriteExt};
if let Ok(n) = socket.read(&mut buf).await {
let req = String::from_utf8_lossy(&buf[..n]);
if req.starts_with("OPTIONS") {
let res = "HTTP/1.1 204 No Content\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: POST, OPTIONS\r\n\r\n";
let _ = socket.write_all(res.as_bytes()).await;
} else if req.starts_with("POST /trigger") {
if let Ok(mut send) = conn.open_uni().await {
let msg = QuicMessage {
device_id: slot.device_id,
sensor_id: slot.sensor_id,
raw_value: 0.0,
timestamp_us: now_us(),
sequence_number: 0,
sensor_type: slot.sensor_type.as_u8(),
};
let _ = send.write_all(&msg.to_bytes()).await;
let _ = send.finish();
tracing::info!("HTTP API triggered: pushed Presence=0.0 over T2");
}
let res = "HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\n\r\nTriggered";
let _ = socket.write_all(res.as_bytes()).await;
} else {
let res = "HTTP/1.1 404 Not Found\r\nAccess-Control-Allow-Origin: *\r\n\r\n";
let _ = socket.write_all(res.as_bytes()).await;
}
}
});
}
}
});
}
let started = Instant::now();
let mut t1_sent: u64 = 0;
let mut send_errors: u64 = 0;