smart-house-web: в работе
This commit is contained in:
@@ -79,7 +79,7 @@ impl From<Thermometer> for Device {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
|
||||
pub struct Room {
|
||||
devices: HashMap<String, Device>,
|
||||
}
|
||||
@@ -99,4 +99,20 @@ impl House {
|
||||
pub fn new(rooms: HashMap<String, Room>) -> Self {
|
||||
Self { rooms }
|
||||
}
|
||||
|
||||
pub fn rooms(&self) -> Vec<String> {
|
||||
let mut output = Vec::with_capacity(self.rooms.len());
|
||||
for (room, _) in self.rooms.iter() {
|
||||
output.push(room.into());
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
pub fn add_room(&mut self, name: String) {
|
||||
self.rooms.insert(name, Room::default());
|
||||
}
|
||||
|
||||
pub fn drop_room(&mut self, name: &str) {
|
||||
self.rooms.remove(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use axum::routing::get;
|
||||
use axum::routing::{delete, get, put};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
process::exit,
|
||||
@@ -45,6 +45,8 @@ async fn server_main() {
|
||||
let app = axum::Router::new()
|
||||
.route("/debug", get(debug::debug))
|
||||
.route("/rooms", get(rooms::get_rooms))
|
||||
.route("/room", put(rooms::add_room))
|
||||
.route("/room/{name}", delete(rooms::remove_room))
|
||||
// TODO
|
||||
.with_state(state)
|
||||
.fallback(fallback);
|
||||
|
||||
@@ -1,8 +1,25 @@
|
||||
use axum::{Json, extract::State};
|
||||
use axum::{
|
||||
Json,
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
};
|
||||
|
||||
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!()
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
###
|
||||
### DEBUG
|
||||
GET http://localhost:8080/debug
|
||||
|
||||
### list rooms
|
||||
GET http://localhost:8080/rooms
|
||||
|
||||
### add room
|
||||
PUT http://localhost:8080/room
|
||||
Content-Type: application/json
|
||||
|
||||
{}
|
||||
"ROOM"
|
||||
|
||||
### drop room
|
||||
DELETE http://localhost:8080/room/ROOM
|
||||
|
||||
Reference in New Issue
Block a user