diff --git a/smart-house-web/backend/src/house.rs b/smart-house-web/backend/src/house.rs index 96ab237..be6b972 100644 --- a/smart-house-web/backend/src/house.rs +++ b/smart-house-web/backend/src/house.rs @@ -52,6 +52,7 @@ impl Thermometer { } #[derive(Debug, Serialize, Deserialize)] +#[serde(tag = "type")] pub enum Device { PowerSocket(PowerSocket), Thermometer(Thermometer), @@ -115,4 +116,8 @@ impl House { } output } + + pub fn get_rooms(&self) -> &HashMap { + &self.rooms + } } diff --git a/smart-house-web/backend/src/server.rs b/smart-house-web/backend/src/server.rs index d3c9d85..a8f5182 100644 --- a/smart-house-web/backend/src/server.rs +++ b/smart-house-web/backend/src/server.rs @@ -1,9 +1,20 @@ +use axum::routing::get; use std::{ + collections::HashMap, process::exit, - sync::atomic::{AtomicUsize, Ordering}, + sync::{ + Arc, + atomic::{AtomicUsize, Ordering}, + }, }; +use tokio::sync::RwLock; use tracing::{error, info}; +use crate::House; + +mod debug; +mod rooms; + pub fn run_server() { let runtime = match tokio::runtime::Builder::new_multi_thread() .name("tokio") @@ -26,9 +37,16 @@ pub fn run_server() { runtime.block_on(server_main()); } +type ServerState = Arc>; + async fn server_main() { + let state: ServerState = Arc::new(RwLock::new(House::new(HashMap::new()))); + let app = axum::Router::new() + .route("/debug", get(debug::debug)) + .route("/rooms", get(rooms::get_rooms)) // TODO + .with_state(state) .fallback(fallback); let addr = "127.0.0.1:8080"; let listener = match tokio::net::TcpListener::bind(addr).await { diff --git a/smart-house-web/backend/src/server/debug.rs b/smart-house-web/backend/src/server/debug.rs new file mode 100644 index 0000000..76d72a8 --- /dev/null +++ b/smart-house-web/backend/src/server/debug.rs @@ -0,0 +1,12 @@ +use std::collections::HashMap; + +use axum::{Json, extract::State}; + +use crate::{Device, PowerSocket, Room, Thermometer}; + +pub async fn debug(State(_server_state): State) -> Json { + let mut map = HashMap::::with_capacity(16); + map.insert("therm_0".into(), Thermometer::new(22.5).into()); + map.insert("psock_0".into(), PowerSocket::new(12.1, false).into()); + Room::new(map).into() +} diff --git a/smart-house-web/backend/src/server/rooms.rs b/smart-house-web/backend/src/server/rooms.rs new file mode 100644 index 0000000..a3d7faa --- /dev/null +++ b/smart-house-web/backend/src/server/rooms.rs @@ -0,0 +1,8 @@ +use axum::{Json, extract::State}; + +use crate::Room; + +pub async fn get_rooms(State(server_state): State) -> Json> { + let _x = server_state.read().await.get_rooms(); + todo!() +} diff --git a/smart-house-web/backend/test.http b/smart-house-web/backend/test.http new file mode 100644 index 0000000..49bb354 --- /dev/null +++ b/smart-house-web/backend/test.http @@ -0,0 +1,5 @@ +### +GET http://localhost:8080/debug +Content-Type: application/json + +{}