smart-house-web: в работе
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
use axum::routing::{delete, get, put};
|
use axum::routing::{delete, get, put};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
|
||||||
process::exit,
|
process::exit,
|
||||||
sync::{
|
sync::{
|
||||||
Arc,
|
Arc,
|
||||||
@@ -15,6 +14,7 @@ use crate::House;
|
|||||||
mod debug;
|
mod debug;
|
||||||
mod rooms;
|
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")
|
||||||
@@ -37,13 +37,17 @@ pub fn run_server() {
|
|||||||
runtime.block_on(server_main());
|
runtime.block_on(server_main());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Тип состояния
|
||||||
type ServerState = Arc<RwLock<House>>;
|
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 state: ServerState = Arc::new(RwLock::new(House::default()));
|
||||||
|
|
||||||
let app = axum::Router::new()
|
let app = axum::Router::new()
|
||||||
|
// Тестовый эндпоинт для экспериментов
|
||||||
.route("/debug", get(debug::debug))
|
.route("/debug", get(debug::debug))
|
||||||
|
// API комнат
|
||||||
.route("/rooms", get(rooms::get_rooms))
|
.route("/rooms", get(rooms::get_rooms))
|
||||||
.route("/room", put(rooms::add_room))
|
.route("/room", put(rooms::add_room))
|
||||||
.route("/room/{name}", delete(rooms::remove_room))
|
.route("/room/{name}", delete(rooms::remove_room))
|
||||||
@@ -69,11 +73,13 @@ async fn server_main() {
|
|||||||
info!("Shutdown server");
|
info!("Shutdown server");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Эндпоинт по-умолчанию
|
||||||
async fn fallback() -> axum::response::Response {
|
async fn fallback() -> axum::response::Response {
|
||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
(axum::http::StatusCode::NOT_FOUND, "404 NOT FOUND").into_response()
|
(axum::http::StatusCode::NOT_FOUND, "404 NOT FOUND").into_response()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Аккуратное завершение работы сервера
|
||||||
async fn shutdown_signal() {
|
async fn shutdown_signal() {
|
||||||
// let timeout = async {
|
// let timeout = async {
|
||||||
// tokio::time::sleep(std::time::Duration::from_secs(10)).await;
|
// tokio::time::sleep(std::time::Duration::from_secs(10)).await;
|
||||||
|
|||||||
@@ -1,12 +1,5 @@
|
|||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use axum::{Json, extract::State};
|
use axum::{Json, extract::State};
|
||||||
|
|
||||||
use crate::{Device, PowerSocket, Room, Thermometer};
|
pub async fn debug(State(_server_state): State<super::ServerState>) -> Json<(String, String)> {
|
||||||
|
("ONE".into(), "TWO".into()).into()
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user