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

This commit is contained in:
8 changed files with 219 additions and 47 deletions

View File

@@ -61,8 +61,8 @@ pub enum Device {
impl Device { impl Device {
pub fn report(&self) -> String { pub fn report(&self) -> String {
match self { match self {
Device::PowerSocket(v) => format!("{}", v.report()), Device::PowerSocket(v) => v.report().to_string(),
Device::Thermometer(v) => format!("{}", v.report()), Device::Thermometer(v) => v.report().to_string(),
} }
} }
} }
@@ -88,6 +88,14 @@ impl Room {
pub fn new(devices: HashMap<String, Device>) -> Self { pub fn new(devices: HashMap<String, Device>) -> Self {
Self { devices } Self { devices }
} }
pub fn get_devices(&self) -> &HashMap<String, Device> {
&self.devices
}
pub fn get_devices_mut(&mut self) -> &mut HashMap<String, Device> {
&mut self.devices
}
} }
#[derive(Debug, Default, Clone, Serialize, Deserialize)] #[derive(Debug, Default, Clone, Serialize, Deserialize)]
@@ -100,19 +108,19 @@ impl House {
Self { rooms } Self { rooms }
} }
pub fn rooms(&self) -> Vec<String> { pub fn get_rooms(&self) -> &HashMap<String, Room> {
let mut output = Vec::with_capacity(self.rooms.len()); &self.rooms
for (room, _) in self.rooms.iter() {
output.push(room.into());
}
return output;
} }
pub fn add_room(&mut self, name: String) { pub fn get_rooms_mut(&mut self) -> &mut HashMap<String, Room> {
self.rooms.insert(name, Room::default()); &mut self.rooms
} }
pub fn drop_room(&mut self, name: &str) { pub fn add_room(&mut self, name: String, room: Room) {
self.rooms.insert(name, room);
}
pub fn del_room(&mut self, name: &str) {
self.rooms.remove(name); self.rooms.remove(name);
} }
} }

View File

@@ -1,4 +1,4 @@
use axum::routing::{delete, get, put}; use axum::routing::{delete, get, post, put};
use std::{ use std::{
process::exit, process::exit,
sync::{ sync::{
@@ -44,11 +44,19 @@ async fn server_main() {
let app = axum::Router::new() let app = axum::Router::new()
// Тестовый эндпоинт для экспериментов // Тестовый эндпоинт для экспериментов
.route("/debug", get(debug::debug)) .route("/debug", get(debug::debug))
// API дома
.route("/rooms", get(house::get_rooms))
.route("/rooms", post(house::post_rooms))
// API комнат // API комнат
.route("/rooms", get(rooms::get_rooms)) .route("/room/{name}", get(room::get_room))
.route("/room", put(rooms::add_room)) .route("/room/{name}", put(room::put_room))
.route("/room/{name}", delete(rooms::remove_room)) .route("/room/{name}", delete(room::delete_room))
// TODO .route("/room/{name}/devices", get(room::get_devices))
// API устройств
.route("/room/{name}/device/{name}", get(device::get_device))
.route("/room/{name}/device/{name}", put(device::put_device))
.route("/room/{name}/device/{name}", delete(device::delete_device))
// Состояние и роут по-умолчанию
.with_state(state) .with_state(state)
.fallback(fallback); .fallback(fallback);
let addr = "127.0.0.1:8080"; let addr = "127.0.0.1:8080";
@@ -98,4 +106,6 @@ async fn shutdown_signal() {
} }
mod debug; mod debug;
mod rooms; mod device;
mod house;
mod room;

View File

@@ -1,5 +1,13 @@
use std::collections::HashMap;
use axum::{Json, extract::State}; use axum::{Json, extract::State};
pub async fn debug(State(_server_state): State<super::ServerState>) -> Json<(String, String)> { use crate::{Device, PowerSocket, Room, Thermometer};
("ONE".into(), "TWO".into()).into()
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()
} }

View File

@@ -0,0 +1,44 @@
use axum::{
Json,
extract::{Path, State},
http::StatusCode,
};
use crate::{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
}

View File

@@ -0,0 +1,21 @@
use std::collections::HashMap;
use axum::{Json, extract::State, http::StatusCode};
use crate::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
}

View File

@@ -0,0 +1,49 @@
use std::collections::HashMap;
use axum::{
Json,
extract::{Path, State},
http::StatusCode,
};
use crate::{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())
}

View File

@@ -1,25 +0,0 @@
use axum::{
Json,
extract::{Path, State},
http::StatusCode,
};
pub async fn get_rooms(State(server_state): State<super::ServerState>) -> Json<Vec<String>> {
server_state.read().await.rooms().into()
}
pub async fn add_room(
State(server_state): State<super::ServerState>,
Json(name): Json<String>,
) -> StatusCode {
server_state.write().await.add_room(name);
StatusCode::CREATED
}
pub async fn remove_room(
State(server_state): State<super::ServerState>,
Path(name): Path<String>,
) -> StatusCode {
server_state.write().await.drop_room(&name);
StatusCode::ACCEPTED
}

View File

@@ -4,11 +4,68 @@ GET http://localhost:8080/debug
### list rooms ### list rooms
GET http://localhost:8080/rooms GET http://localhost:8080/rooms
### add room ### post all rooms
PUT http://localhost:8080/room POST http://localhost:8080/rooms
Content-Type: application/json Content-Type: application/json
"ROOM" {
"ROOM0": {
"devices": {}
},
"ROOM1": {
"devices": {
"therm": {
"type": "Thermometer",
"temperature": 22
},
"psock": {
"type": "PowerSocket",
"power_rate": 11,
"on": false
}
}
}
}
### drop room ### drop room
DELETE http://localhost:8080/room/ROOM DELETE http://localhost:8080/room/ROOM
### add room
PUT http://localhost:8080/room/ROOM
Content-Type: application/json
{
"devices": {
"therm": {
"type": "Thermometer",
"temperature": 20
},
"psock": {
"type": "PowerSocket",
"power_rate": 10,
"on": true
}
}
}
### get room
GET http://localhost:8080/room/ROOM1
### get room devices
GET http://localhost:8080/room/ROOM1/devices
### get room device
GET http://localhost:8080/room/ROOM/device/TEST
### get room device
PUT http://localhost:8080/room/ROOM/device/TEST
Content-Type: application/json
{
"type": "PowerSocket",
"power_rate": 5,
"on": true
}
### get room device
DELETE http://localhost:8080/room/ROOM/device/TEST