ДЗ smart-house-web завершено

This commit is contained in:
28 changed files with 1671 additions and 0 deletions
@@ -0,0 +1,13 @@
use std::collections::HashMap;
use axum::{Json, extract::State};
use smart_house::{Device, PowerSocket, Room, Thermometer};
pub async fn debug(State(_server_state): State<super::ServerState>) -> Json<Room> {
let map = HashMap::<String, Device>::from([
("thermo".into(), Thermometer::new(20.0).into()),
("psock".into(), PowerSocket::new(10.0, false).into()),
]);
Room::new(map).into()
}
@@ -0,0 +1,44 @@
use axum::{
Json,
extract::{Path, State},
http::StatusCode,
};
use smart_house::{Device, Room};
pub async fn get_device(
State(server_state): State<super::ServerState>,
Path((room, device)): Path<(String, String)>,
) -> Result<Json<Device>, StatusCode> {
let house = server_state.read().await;
let Some(room) = house.get_rooms().get(&room) else {
return Err(StatusCode::NOT_FOUND);
};
let Some(device) = room.get_devices().get(&device) else {
return Err(StatusCode::NOT_FOUND);
};
Ok(device.clone().into())
}
pub async fn put_device(
State(server_state): State<super::ServerState>,
Path((room, name)): Path<(String, String)>,
Json(device): Json<Device>,
) -> StatusCode {
let mut house = server_state.write().await;
let room = house.get_rooms_mut().entry(room).or_insert(Room::default());
room.get_devices_mut().insert(name, device);
StatusCode::CREATED
}
pub async fn delete_device(
State(server_state): State<super::ServerState>,
Path((room, device)): Path<(String, String)>,
) -> StatusCode {
let mut house = server_state.write().await;
let Some(room) = house.get_rooms_mut().get_mut(&room) else {
return StatusCode::ACCEPTED;
};
room.get_devices_mut().remove(&device);
StatusCode::ACCEPTED
}
@@ -0,0 +1,21 @@
use std::collections::HashMap;
use axum::{Json, extract::State, http::StatusCode};
use smart_house::Room;
pub async fn get_rooms(
State(server_state): State<super::ServerState>,
) -> Json<HashMap<String, Room>> {
server_state.read().await.get_rooms().clone().into()
}
pub async fn post_rooms(
State(server_state): State<super::ServerState>,
Json(map): Json<HashMap<String, Room>>,
) -> StatusCode {
for (name, room) in map.into_iter() {
server_state.write().await.add_room(name, room);
}
StatusCode::CREATED
}
@@ -0,0 +1,49 @@
use std::collections::HashMap;
use axum::{
Json,
extract::{Path, State},
http::StatusCode,
};
use smart_house::{Device, Room};
pub async fn get_room(
State(server_state): State<super::ServerState>,
Path(name): Path<String>,
) -> Result<Json<Room>, StatusCode> {
let house = server_state.read().await;
let Some(room) = house.get_rooms().get(&name) else {
return Err(StatusCode::NOT_FOUND);
};
Ok(room.clone().into())
}
pub async fn put_room(
State(server_state): State<super::ServerState>,
Path(name): Path<String>,
Json(room): Json<Room>,
) -> StatusCode {
let mut house = server_state.write().await;
house.add_room(name, room);
StatusCode::CREATED
}
pub async fn delete_room(
State(server_state): State<super::ServerState>,
Path(name): Path<String>,
) -> StatusCode {
server_state.write().await.del_room(&name);
StatusCode::ACCEPTED
}
pub async fn get_devices(
State(server_state): State<super::ServerState>,
Path(name): Path<String>,
) -> Result<Json<HashMap<String, Device>>, StatusCode> {
let house = server_state.read().await;
let Some(room) = house.get_rooms().get(&name) else {
return Err(StatusCode::NOT_FOUND);
};
Ok(room.get_devices().clone().into())
}