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

This commit is contained in:
2 changed files with 10 additions and 11 deletions

View File

@@ -1,6 +1,5 @@
use axum::routing::{delete, get, put};
use std::{
collections::HashMap,
process::exit,
sync::{
Arc,
@@ -15,6 +14,7 @@ use crate::House;
mod debug;
mod rooms;
/// Запуск сервера
pub fn run_server() {
let runtime = match tokio::runtime::Builder::new_multi_thread()
.name("tokio")
@@ -37,13 +37,17 @@ 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 state: ServerState = Arc::new(RwLock::new(House::default()));
let app = axum::Router::new()
// Тестовый эндпоинт для экспериментов
.route("/debug", get(debug::debug))
// API комнат
.route("/rooms", get(rooms::get_rooms))
.route("/room", put(rooms::add_room))
.route("/room/{name}", delete(rooms::remove_room))
@@ -69,11 +73,13 @@ async fn server_main() {
info!("Shutdown server");
}
/// Эндпоинт по-умолчанию
async fn fallback() -> axum::response::Response {
use axum::response::IntoResponse;
(axum::http::StatusCode::NOT_FOUND, "404 NOT FOUND").into_response()
}
/// Аккуратное завершение работы сервера
async fn shutdown_signal() {
// let timeout = async {
// tokio::time::sleep(std::time::Duration::from_secs(10)).await;

View File

@@ -1,12 +1,5 @@
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()
pub async fn debug(State(_server_state): State<super::ServerState>) -> Json<(String, String)> {
("ONE".into(), "TWO".into()).into()
}