smart-house-web: в работе

This commit is contained in:
5 changed files with 49 additions and 1 deletions

View File

@@ -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<String, Room> {
&self.rooms
}
}

View File

@@ -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<RwLock<House>>;
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 {

View File

@@ -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<super::ServerState>) -> Json<Room> {
let mut map = HashMap::<String, Device>::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()
}

View File

@@ -0,0 +1,8 @@
use axum::{Json, extract::State};
use crate::Room;
pub async fn get_rooms(State(server_state): State<super::ServerState>) -> Json<Vec<Room>> {
let _x = server_state.read().await.get_rooms();
todo!()
}

View File

@@ -0,0 +1,5 @@
###
GET http://localhost:8080/debug
Content-Type: application/json
{}