feat(backend/control-box): add a socket for the box

This commit is contained in:
catvayor 2024-10-13 11:51:27 +02:00
parent dfbaf2fd65
commit 2067df8968
Signed by: lbailly
GPG key ID: CE3E645251AC63F3

View file

@ -9,6 +9,12 @@ use tokio::task;
use tokio::time::{sleep, Duration};
use tower_http::cors::{Any, CorsLayer};
use axum::extract::State;
use serde::Deserialize;
use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader;
use tokio::net::TcpListener;
pub fn create_router() -> Router {
let db = model::make_db();
@ -43,6 +49,46 @@ pub fn create_router() -> Router {
}
});
let db_listener = db.clone();
task::spawn(async move {
//TODO: parametrisation
tracing::debug!("Trying to bind at 10.10.10.13:1235");
let listener = TcpListener::bind("10.10.10.1:1235")
.await
.expect("Failed to listen for direct connection.");
loop {
match listener.accept().await {
Ok((socket, addr)) => {
let db_socket = db_listener.clone();
let mut buf_reader = BufReader::new(socket);
let mut buf = String::new();
tracing::debug!("Accepted {addr:?}");
task::spawn(async move {
loop {
buf.clear();
let _ = buf_reader.read_line(&mut buf).await;
match model::DMXBeamChange::deserialize(
&mut serde_json::Deserializer::from_str(&buf),
) {
Ok(data) => handler::edit_motor_value_handler(
State(db_socket.clone()),
axum::Json(data),
)
.await
.unwrap_or(()),
Err(e) => tracing::error!("Error reading incoming data: {e:?}"),
}
}
})
}
Err(e) => {
task::spawn(async move { tracing::error!("Failed to get client: {e:?}") })
}
};
}
});
Router::new()
.route("/api/healthcheck", get(handler::healthcheck_handler))
.route(