smart-house-web: в работе
This commit is contained in:
@@ -52,6 +52,7 @@ impl Thermometer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
#[serde(tag = "type")]
|
||||||
pub enum Device {
|
pub enum Device {
|
||||||
PowerSocket(PowerSocket),
|
PowerSocket(PowerSocket),
|
||||||
Thermometer(Thermometer),
|
Thermometer(Thermometer),
|
||||||
@@ -115,4 +116,8 @@ impl House {
|
|||||||
}
|
}
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_rooms(&self) -> &HashMap<String, Room> {
|
||||||
|
&self.rooms
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,20 @@
|
|||||||
|
use axum::routing::get;
|
||||||
use std::{
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
process::exit,
|
process::exit,
|
||||||
sync::atomic::{AtomicUsize, Ordering},
|
sync::{
|
||||||
|
Arc,
|
||||||
|
atomic::{AtomicUsize, Ordering},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
use tokio::sync::RwLock;
|
||||||
use tracing::{error, info};
|
use tracing::{error, info};
|
||||||
|
|
||||||
|
use crate::House;
|
||||||
|
|
||||||
|
mod debug;
|
||||||
|
mod rooms;
|
||||||
|
|
||||||
pub fn run_server() {
|
pub fn run_server() {
|
||||||
let runtime = match tokio::runtime::Builder::new_multi_thread()
|
let runtime = match tokio::runtime::Builder::new_multi_thread()
|
||||||
.name("tokio")
|
.name("tokio")
|
||||||
@@ -26,9 +37,16 @@ pub fn run_server() {
|
|||||||
runtime.block_on(server_main());
|
runtime.block_on(server_main());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ServerState = Arc<RwLock<House>>;
|
||||||
|
|
||||||
async fn server_main() {
|
async fn server_main() {
|
||||||
|
let state: ServerState = Arc::new(RwLock::new(House::new(HashMap::new())));
|
||||||
|
|
||||||
let app = axum::Router::new()
|
let app = axum::Router::new()
|
||||||
|
.route("/debug", get(debug::debug))
|
||||||
|
.route("/rooms", get(rooms::get_rooms))
|
||||||
// TODO
|
// TODO
|
||||||
|
.with_state(state)
|
||||||
.fallback(fallback);
|
.fallback(fallback);
|
||||||
let addr = "127.0.0.1:8080";
|
let addr = "127.0.0.1:8080";
|
||||||
let listener = match tokio::net::TcpListener::bind(addr).await {
|
let listener = match tokio::net::TcpListener::bind(addr).await {
|
||||||
|
|||||||
12
smart-house-web/backend/src/server/debug.rs
Normal file
12
smart-house-web/backend/src/server/debug.rs
Normal 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()
|
||||||
|
}
|
||||||
8
smart-house-web/backend/src/server/rooms.rs
Normal file
8
smart-house-web/backend/src/server/rooms.rs
Normal 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!()
|
||||||
|
}
|
||||||
5
smart-house-web/backend/test.http
Normal file
5
smart-house-web/backend/test.http
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
###
|
||||||
|
GET http://localhost:8080/debug
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{}
|
||||||
Reference in New Issue
Block a user