Compare commits
7 Commits
master
...
feat/smart
| Author | SHA1 | Date | |
|---|---|---|---|
| 6a3bc7cc6d | |||
| fe9d15f3eb | |||
| 4a84a54591 | |||
| 5e431aca04 | |||
| 6721cfdc2b | |||
| dc492ccfd1 | |||
| 967698b0bc |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
/.idea/
|
/.idea/
|
||||||
/**/.DS_Store
|
/**/.DS_Store
|
||||||
|
/tmp/
|
||||||
|
|||||||
2
smart-house-web/.gitignore
vendored
Normal file
2
smart-house-web/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
/target/
|
||||||
|
/Cargo.lock
|
||||||
19
smart-house-web/Cargo.toml
Normal file
19
smart-house-web/Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
[workspace]
|
||||||
|
members = [
|
||||||
|
"backend",
|
||||||
|
"frontend",
|
||||||
|
"frontend-dioxus",
|
||||||
|
]
|
||||||
|
resolver = "3"
|
||||||
|
|
||||||
|
[workspace.dependencies]
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
opt-level = "z"
|
||||||
|
lto = "fat"
|
||||||
|
codegen-units = 1
|
||||||
|
debug-assertions = false
|
||||||
|
panic = "abort"
|
||||||
|
overflow-checks = false
|
||||||
|
incremental = false
|
||||||
|
strip = "symbols"
|
||||||
40
smart-house-web/README.md
Normal file
40
smart-house-web/README.md
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
# ДЗ 2026-04-28 - Веб-сервис умного дома
|
||||||
|
|
||||||
|
## Цель:
|
||||||
|
|
||||||
|
Превращаем умный дом в веб-сервис.
|
||||||
|
|
||||||
|
## Срок:
|
||||||
|
|
||||||
|
Сдать до: **2026-05-25**
|
||||||
|
|
||||||
|
## Описание/Пошаговая инструкция выполнения домашнего задания:
|
||||||
|
|
||||||
|
Реализовать backend сервис для управления умным домом и frontend приложение для взаимодействия с ним.
|
||||||
|
|
||||||
|
- Технология взаимодействия с backend сервисом (gRPC, REST, GraphQL, ...) выбирается произвольно.
|
||||||
|
|
||||||
|
API backend сервиса предоставляет доступ ко всему базовому функционалу библиотеки умного дома:
|
||||||
|
|
||||||
|
- [x] Добавление/удаление/перечисление комнат в доме и получение информации о конкретной комнате.
|
||||||
|
- [x] Добавление/удаление/перечисление устройств в комнате и получение информации о конкретном устройстве.
|
||||||
|
- [x] Получение отчёта о доме.
|
||||||
|
- [x] Присутствуют функциональные тесты, которые общаются с backend-ом и проверяют его ответы.
|
||||||
|
|
||||||
|
Frontend приложение:
|
||||||
|
|
||||||
|
- [ ] Отображает список комнат в доме.
|
||||||
|
- [ ] Позволяет перейти к конкретной комнате или добавить новую комнату.
|
||||||
|
- [ ] Отображает список устройств в комнате.
|
||||||
|
- [ ] Позволяет перейти к конкретному устройству или добавить новое устройство.
|
||||||
|
- [ ] Позволяет запросить отчёт о состоянии дома.
|
||||||
|
|
||||||
|
**Критерии оценки:**
|
||||||
|
|
||||||
|
- Workspace успешно собирается.
|
||||||
|
- Приложения-примеры успешно выполняются.
|
||||||
|
- Команды cargo clippy, и cargo fmt --check не выводят ошибок и предупреждений.
|
||||||
|
|
||||||
|
## Демо
|
||||||
|
|
||||||
|
**TBD**
|
||||||
15
smart-house-web/backend/Cargo.toml
Normal file
15
smart-house-web/backend/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
[package]
|
||||||
|
name = "backend"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
tracing = "0.1"
|
||||||
|
tracing-subscriber = "0.3"
|
||||||
|
tokio = { version = "1.52", features = ["rt", "rt-multi-thread", "signal", "time"] }
|
||||||
|
axum = "0.8"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
reqwest = { version = "0.13", features = ["json"] }
|
||||||
126
smart-house-web/backend/src/house.rs
Normal file
126
smart-house-web/backend/src/house.rs
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct PowerSocket {
|
||||||
|
power_rate: f32,
|
||||||
|
on: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PowerSocket {
|
||||||
|
pub fn new(power_rate: f32, on: bool) -> Self {
|
||||||
|
Self { power_rate, on }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_on(&self) -> bool {
|
||||||
|
self.on
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_on(&mut self, on: bool) {
|
||||||
|
self.on = on
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_power(&self) -> f32 {
|
||||||
|
if self.is_on() { self.power_rate } else { 0.0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn report(&self) -> String {
|
||||||
|
let state = if self.is_on() { "ON" } else { "OFF" };
|
||||||
|
let power = self.get_power();
|
||||||
|
format!("PowerSocket[ {} : {:02.1} ]", state, power)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct Thermometer {
|
||||||
|
temperature: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Thermometer {
|
||||||
|
pub fn new(temperature: f32) -> Self {
|
||||||
|
Self { temperature }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_temperature(&self) -> f32 {
|
||||||
|
self.temperature
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn report(&self) -> String {
|
||||||
|
let temperature = self.get_temperature();
|
||||||
|
format!("Thermometer[ {:02.1} ]", temperature)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(tag = "type")]
|
||||||
|
pub enum Device {
|
||||||
|
PowerSocket(PowerSocket),
|
||||||
|
Thermometer(Thermometer),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Device {
|
||||||
|
pub fn report(&self) -> String {
|
||||||
|
match self {
|
||||||
|
Device::PowerSocket(v) => v.report().to_string(),
|
||||||
|
Device::Thermometer(v) => v.report().to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<PowerSocket> for Device {
|
||||||
|
fn from(value: PowerSocket) -> Self {
|
||||||
|
Device::PowerSocket(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Thermometer> for Device {
|
||||||
|
fn from(value: Thermometer) -> Self {
|
||||||
|
Device::Thermometer(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct Room {
|
||||||
|
devices: HashMap<String, Device>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Room {
|
||||||
|
pub fn new(devices: HashMap<String, Device>) -> Self {
|
||||||
|
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)]
|
||||||
|
pub struct House {
|
||||||
|
rooms: HashMap<String, Room>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl House {
|
||||||
|
pub fn new(rooms: HashMap<String, Room>) -> Self {
|
||||||
|
Self { rooms }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_rooms(&self) -> &HashMap<String, Room> {
|
||||||
|
&self.rooms
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_rooms_mut(&mut self) -> &mut HashMap<String, Room> {
|
||||||
|
&mut self.rooms
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
73
smart-house-web/backend/src/lib.rs
Normal file
73
smart-house-web/backend/src/lib.rs
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
use std::process::exit;
|
||||||
|
use tracing::{Level, error, trace};
|
||||||
|
|
||||||
|
/// Ошибка инициализации логгера
|
||||||
|
const CODE_LOGGER_INITIALIZATION_ERROR: i32 = 1;
|
||||||
|
|
||||||
|
/// Ошибка инициализации рантайма Tokio
|
||||||
|
const CODE_TOKIO_RUNTIME_CREATION_ERROR: i32 = 2;
|
||||||
|
|
||||||
|
/// Ошибка привязки слушателя
|
||||||
|
const CODE_LISTENER_BINDING_ERROR: i32 = 3;
|
||||||
|
|
||||||
|
/// Ошибка запуска сервера
|
||||||
|
const CODE_STARTIG_SERVER_ERROR: i32 = 4;
|
||||||
|
|
||||||
|
/// Ошибка установки обработчика сигнала завершения
|
||||||
|
const CODE_CTRL_C_SIGNAL_INSTALL_ERROR: i32 = 5;
|
||||||
|
|
||||||
|
/// Инициализация логирования
|
||||||
|
pub fn init_logger() {
|
||||||
|
use tracing_subscriber::{
|
||||||
|
Layer, filter::Targets, fmt::layer, layer::SubscriberExt, registry, util::SubscriberInitExt,
|
||||||
|
};
|
||||||
|
|
||||||
|
let layer = layer()
|
||||||
|
.compact()
|
||||||
|
.with_thread_names(true)
|
||||||
|
.with_file(false)
|
||||||
|
.with_line_number(false)
|
||||||
|
.with_filter(
|
||||||
|
Targets::new()
|
||||||
|
.with_target("axum::serve", Level::INFO)
|
||||||
|
.with_target("hyper_util::client::legacy", Level::INFO)
|
||||||
|
.with_target("reqwest::retry", Level::INFO)
|
||||||
|
.with_default(Level::TRACE),
|
||||||
|
)
|
||||||
|
.boxed();
|
||||||
|
if let Err(e) = registry().with(vec![layer]).try_init() {
|
||||||
|
eprintln!("Logger initialization failed: {:?}", e);
|
||||||
|
exit(CODE_LOGGER_INITIALIZATION_ERROR);
|
||||||
|
} else {
|
||||||
|
trace!("Logger succesfully initialized");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build_runtime() -> tokio::runtime::Runtime {
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
|
match tokio::runtime::Builder::new_multi_thread()
|
||||||
|
.name("tokio")
|
||||||
|
.thread_name_fn(|| {
|
||||||
|
static LAST_ID: AtomicUsize = AtomicUsize::new(0);
|
||||||
|
let id = LAST_ID.fetch_add(1, Ordering::SeqCst);
|
||||||
|
format!("tkwr-{id}")
|
||||||
|
})
|
||||||
|
.worker_threads(2)
|
||||||
|
.thread_stack_size(256 * 1024)
|
||||||
|
.enable_all()
|
||||||
|
.build()
|
||||||
|
{
|
||||||
|
Ok(runtime) => runtime,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to create Tokio runtime: {:?}", e);
|
||||||
|
exit(crate::CODE_TOKIO_RUNTIME_CREATION_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod server;
|
||||||
|
pub use server::{axum_app, server_main};
|
||||||
|
|
||||||
|
mod house;
|
||||||
|
pub use house::{Device, House, PowerSocket, Room, Thermometer};
|
||||||
8
smart-house-web/backend/src/main.rs
Normal file
8
smart-house-web/backend/src/main.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
use backend::{axum_app, build_runtime, init_logger, server_main};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
init_logger();
|
||||||
|
let runtime = build_runtime();
|
||||||
|
let app = axum_app();
|
||||||
|
runtime.block_on(server_main(app));
|
||||||
|
}
|
||||||
87
smart-house-web/backend/src/server.rs
Normal file
87
smart-house-web/backend/src/server.rs
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
use axum::{
|
||||||
|
Router,
|
||||||
|
routing::{delete, get, post, put},
|
||||||
|
};
|
||||||
|
use std::{process::exit, sync::Arc};
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
use tracing::{error, info};
|
||||||
|
|
||||||
|
use crate::House;
|
||||||
|
|
||||||
|
/// Тип состояния
|
||||||
|
type ServerState = Arc<RwLock<House>>;
|
||||||
|
|
||||||
|
pub fn axum_app() -> Router {
|
||||||
|
let state: ServerState = Arc::new(RwLock::new(House::default()));
|
||||||
|
axum::Router::new()
|
||||||
|
// Тестовый эндпоинт для экспериментов
|
||||||
|
.route("/debug", get(debug::debug))
|
||||||
|
// API дома
|
||||||
|
.route("/rooms", get(house::get_rooms))
|
||||||
|
.route("/rooms", post(house::post_rooms))
|
||||||
|
// API комнат
|
||||||
|
.route("/room/{name}", get(room::get_room))
|
||||||
|
.route("/room/{name}", put(room::put_room))
|
||||||
|
.route("/room/{name}", delete(room::delete_room))
|
||||||
|
.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)
|
||||||
|
.fallback(fallback)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Основная функция сервера
|
||||||
|
pub async fn server_main(app: Router) {
|
||||||
|
let addr = "127.0.0.1:8080";
|
||||||
|
let listener = match tokio::net::TcpListener::bind(addr).await {
|
||||||
|
Ok(listener) => listener,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to bind listener to {}: {:?}", addr, e);
|
||||||
|
exit(crate::CODE_LISTENER_BINDING_ERROR);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
info!("Starting server at {}...", addr);
|
||||||
|
if let Err(e) = axum::serve(listener, app)
|
||||||
|
.with_graceful_shutdown(shutdown_signal())
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
error!("Failed to start server: {:?}", e);
|
||||||
|
exit(crate::CODE_STARTIG_SERVER_ERROR);
|
||||||
|
};
|
||||||
|
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;
|
||||||
|
// info!("10 seconds timeout expired");
|
||||||
|
// };
|
||||||
|
let ctrl_c = async {
|
||||||
|
tokio::signal::ctrl_c().await.map_err(|e| {
|
||||||
|
error!("Can't install Ctrl+C signal handler: {:?}", e);
|
||||||
|
exit(crate::CODE_CTRL_C_SIGNAL_INSTALL_ERROR);
|
||||||
|
});
|
||||||
|
info!("Ctrl+C pressed");
|
||||||
|
};
|
||||||
|
let pending = std::future::pending::<()>();
|
||||||
|
tokio::select! {
|
||||||
|
// _ = timeout => {},
|
||||||
|
_ = ctrl_c => {},
|
||||||
|
_ = pending => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod debug;
|
||||||
|
mod device;
|
||||||
|
mod house;
|
||||||
|
mod room;
|
||||||
13
smart-house-web/backend/src/server/debug.rs
Normal file
13
smart-house-web/backend/src/server/debug.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
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 map = HashMap::<String, Device>::from([
|
||||||
|
("thermo".into(), Thermometer::new(20.0).into()),
|
||||||
|
("psock".into(), PowerSocket::new(10.0, false).into()),
|
||||||
|
]);
|
||||||
|
Room::new(map).into()
|
||||||
|
}
|
||||||
44
smart-house-web/backend/src/server/device.rs
Normal file
44
smart-house-web/backend/src/server/device.rs
Normal 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
|
||||||
|
}
|
||||||
21
smart-house-web/backend/src/server/house.rs
Normal file
21
smart-house-web/backend/src/server/house.rs
Normal 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
|
||||||
|
}
|
||||||
49
smart-house-web/backend/src/server/room.rs
Normal file
49
smart-house-web/backend/src/server/room.rs
Normal 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())
|
||||||
|
}
|
||||||
71
smart-house-web/backend/test.http
Normal file
71
smart-house-web/backend/test.http
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
### DEBUG
|
||||||
|
GET http://localhost:8080/debug
|
||||||
|
|
||||||
|
### list rooms
|
||||||
|
GET http://localhost:8080/rooms
|
||||||
|
|
||||||
|
### post all rooms
|
||||||
|
POST http://localhost:8080/rooms
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"ROOM0": {
|
||||||
|
"devices": {}
|
||||||
|
},
|
||||||
|
"ROOM1": {
|
||||||
|
"devices": {
|
||||||
|
"therm": {
|
||||||
|
"type": "Thermometer",
|
||||||
|
"temperature": 22
|
||||||
|
},
|
||||||
|
"psock": {
|
||||||
|
"type": "PowerSocket",
|
||||||
|
"power_rate": 11,
|
||||||
|
"on": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
### drop 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
|
||||||
238
smart-house-web/backend/tests/api_smoke_test.rs
Normal file
238
smart-house-web/backend/tests/api_smoke_test.rs
Normal file
@@ -0,0 +1,238 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use backend::{Device, Room, axum_app, init_logger, server_main};
|
||||||
|
use reqwest::{Client, Response, StatusCode};
|
||||||
|
use serde_json::json;
|
||||||
|
use tokio::spawn;
|
||||||
|
use tracing::info;
|
||||||
|
|
||||||
|
type RqResult<T> = Result<T, reqwest::Error>;
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "current_thread")]
|
||||||
|
async fn smoke() -> RqResult<()> {
|
||||||
|
init_logger();
|
||||||
|
spawn(server_main(axum_app()));
|
||||||
|
|
||||||
|
let client = Client::new();
|
||||||
|
|
||||||
|
verify_rooms_state0(&client).await?;
|
||||||
|
init_rooms(&client).await?;
|
||||||
|
verify_rooms_state1(&client).await?;
|
||||||
|
delete_room0(&client).await?;
|
||||||
|
verify_rooms_state2(&client).await?;
|
||||||
|
put_room(&client).await?;
|
||||||
|
verify_rooms_state3(&client).await?;
|
||||||
|
verify_room1_state0(&client).await?;
|
||||||
|
verify_room1_devices_state0(&client).await?;
|
||||||
|
verify_room1_device_psock_state0(&client).await?;
|
||||||
|
delete_psock(&client).await?;
|
||||||
|
verify_room1_device_psock_state1(&client).await?;
|
||||||
|
put_device(&client).await?;
|
||||||
|
verify_new_device_in_place(&client).await?;
|
||||||
|
print_house_report(&client).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn print_resp(resp: Response) -> RqResult<()> {
|
||||||
|
info!(
|
||||||
|
"\n<<< StatusCode: {}\n<<< BODY: {}",
|
||||||
|
resp.status(),
|
||||||
|
resp.text().await?
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn verify_rooms_state0(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client.get("http://localhost:8080/rooms").send().await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let rooms = resp.json::<HashMap<String, Room>>().await?;
|
||||||
|
assert!(rooms.is_empty());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn init_rooms(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client
|
||||||
|
.post("http://localhost:8080/rooms")
|
||||||
|
.json(&json!({
|
||||||
|
"ROOM0": { "devices": {} },
|
||||||
|
"ROOM1": { "devices": {
|
||||||
|
"therm": { "type": "Thermometer", "temperature": 22 },
|
||||||
|
"psock": { "type": "PowerSocket", "power_rate": 11, "on": false }
|
||||||
|
} }
|
||||||
|
}))
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn verify_rooms_state1(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client.get("http://localhost:8080/rooms").send().await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let rooms = resp.json::<HashMap<String, Room>>().await?;
|
||||||
|
assert_eq!(rooms.len(), 2);
|
||||||
|
assert_eq!(rooms.get("ROOM1").unwrap().get_devices().len(), 2);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete_room0(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client
|
||||||
|
.delete("http://localhost:8080/room/ROOM0")
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::ACCEPTED);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn verify_rooms_state2(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client.get("http://localhost:8080/rooms").send().await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let rooms = resp.json::<HashMap<String, Room>>().await?;
|
||||||
|
assert_eq!(rooms.len(), 1);
|
||||||
|
assert_eq!(rooms.get("ROOM1").unwrap().get_devices().len(), 2);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn put_room(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client
|
||||||
|
.put("http://localhost:8080/room/ROOM")
|
||||||
|
.json(&json!({ "devices": {
|
||||||
|
"therm": { "type": "Thermometer", "temperature": 20 },
|
||||||
|
"psock": { "type": "PowerSocket", "power_rate": 10, "on": true }
|
||||||
|
} }))
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn verify_rooms_state3(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client.get("http://localhost:8080/rooms").send().await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let rooms = resp.json::<HashMap<String, Room>>().await?;
|
||||||
|
assert_eq!(rooms.len(), 2);
|
||||||
|
assert_eq!(rooms.get("ROOM").unwrap().get_devices().len(), 2);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn verify_room1_state0(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client
|
||||||
|
.get("http://localhost:8080/room/ROOM1")
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let room = resp.json::<Room>().await?;
|
||||||
|
assert_eq!(room.get_devices().len(), 2);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn verify_room1_devices_state0(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client
|
||||||
|
.get("http://localhost:8080/room/ROOM1/devices")
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let device_map = resp.json::<HashMap<String, Device>>().await?;
|
||||||
|
assert_eq!(device_map.len(), 2);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn verify_room1_device_psock_state0(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client
|
||||||
|
.get("http://localhost:8080/room/ROOM1/device/psock")
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let device = resp.json::<Device>().await?;
|
||||||
|
|
||||||
|
let Device::PowerSocket(power_socket) = device else {
|
||||||
|
panic!("PowerSocket expected");
|
||||||
|
};
|
||||||
|
assert_eq!(power_socket.get_power(), 0.0);
|
||||||
|
assert_eq!(power_socket.is_on(), false);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete_psock(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client
|
||||||
|
.delete("http://localhost:8080/room/ROOM1/device/psock")
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::ACCEPTED);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn verify_room1_device_psock_state1(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client
|
||||||
|
.get("http://localhost:8080/room/ROOM1/device/psock")
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn put_device(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client
|
||||||
|
.put("http://localhost:8080/room/ROOM1/device/TEST")
|
||||||
|
.json(&json!({
|
||||||
|
"type": "PowerSocket",
|
||||||
|
"power_rate": 5,
|
||||||
|
"on": true
|
||||||
|
}))
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn verify_new_device_in_place(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client.get("http://localhost:8080/rooms").send().await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let rooms = resp.json::<HashMap<String, Room>>().await?;
|
||||||
|
assert_eq!(rooms.len(), 2);
|
||||||
|
assert_eq!(rooms.get("ROOM1").unwrap().get_devices().len(), 2);
|
||||||
|
let device = rooms
|
||||||
|
.get("ROOM1")
|
||||||
|
.unwrap()
|
||||||
|
.get_devices()
|
||||||
|
.get("TEST")
|
||||||
|
.unwrap();
|
||||||
|
let Device::PowerSocket(power_socket) = device else {
|
||||||
|
panic!("TEST device must be a PowerSocket")
|
||||||
|
};
|
||||||
|
assert!(power_socket.is_on());
|
||||||
|
assert_eq!(power_socket.get_power(), 5.0);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn print_house_report(client: &Client) -> RqResult<()> {
|
||||||
|
let resp = client.get("http://localhost:8080/rooms").send().await?;
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
print_resp(resp).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
16
smart-house-web/frontend-dioxus/Cargo.toml
Normal file
16
smart-house-web/frontend-dioxus/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
[package]
|
||||||
|
name = "frontend-old"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
dioxus = { version = "0.7", features = ["fullstack", "router"] }
|
||||||
|
reqwest = { version = "0.13", features = ["json"] }
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["web"]
|
||||||
|
web = ["dioxus/web"]
|
||||||
|
desktop = ["dioxus/desktop"]
|
||||||
|
mobile = ["dioxus/mobile"]
|
||||||
|
server = ["dioxus/server"]
|
||||||
21
smart-house-web/frontend-dioxus/Dioxus.toml
Normal file
21
smart-house-web/frontend-dioxus/Dioxus.toml
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
[application]
|
||||||
|
|
||||||
|
[web.app]
|
||||||
|
|
||||||
|
# HTML title tag content
|
||||||
|
title = "Smart House"
|
||||||
|
|
||||||
|
# include `assets` in web platform
|
||||||
|
[web.resource]
|
||||||
|
|
||||||
|
# Additional CSS style files
|
||||||
|
style = []
|
||||||
|
|
||||||
|
# Additional JavaScript files
|
||||||
|
script = []
|
||||||
|
|
||||||
|
[web.resource.dev]
|
||||||
|
|
||||||
|
# Javascript code file
|
||||||
|
# serve: [dev-server] only
|
||||||
|
script = []
|
||||||
BIN
smart-house-web/frontend-dioxus/assets/favicon.ico
Normal file
BIN
smart-house-web/frontend-dioxus/assets/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 130 KiB |
20
smart-house-web/frontend-dioxus/assets/header.svg
Normal file
20
smart-house-web/frontend-dioxus/assets/header.svg
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 23 KiB |
155
smart-house-web/frontend-dioxus/assets/main.css
Normal file
155
smart-house-web/frontend-dioxus/assets/main.css
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
html,
|
||||||
|
body {
|
||||||
|
background-color: #0e0e0e;
|
||||||
|
color: white;
|
||||||
|
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
overflow: hidden;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#main {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
height: 100%;
|
||||||
|
justify-content: space-between;
|
||||||
|
}
|
||||||
|
|
||||||
|
#dogview {
|
||||||
|
max-height: 80vh;
|
||||||
|
flex-grow: 1;
|
||||||
|
width: 100%;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
#dogview img {
|
||||||
|
display: block;
|
||||||
|
max-width: 50%;
|
||||||
|
max-height: 50%;
|
||||||
|
transform: scale(1.8);
|
||||||
|
border-radius: 5px;
|
||||||
|
border: 1px solid rgb(233, 233, 233);
|
||||||
|
box-shadow: 0px 0px 5px 1px rgb(216, 216, 216, 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
#title {
|
||||||
|
text-align: center;
|
||||||
|
padding-top: 10px;
|
||||||
|
border-bottom: 1px solid #a8a8a8;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
justify-content: space-evenly;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
#title a {
|
||||||
|
text-decoration: none;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
a#heart {
|
||||||
|
background-color: white;
|
||||||
|
color: red;
|
||||||
|
padding: 5px;
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#title span {
|
||||||
|
width: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#title h1 {
|
||||||
|
margin: 0.25em;
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
|
|
||||||
|
#buttons {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 20px;
|
||||||
|
/* padding-top: 20px; */
|
||||||
|
padding-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#skip {
|
||||||
|
background-color: gray;
|
||||||
|
}
|
||||||
|
#save {
|
||||||
|
background-color: green;
|
||||||
|
}
|
||||||
|
|
||||||
|
#skip,
|
||||||
|
#save {
|
||||||
|
padding: 5px 30px 5px 30px;
|
||||||
|
border-radius: 3px;
|
||||||
|
font-size: 2rem;
|
||||||
|
font-weight: bold;
|
||||||
|
color: rgb(230, 230, 230);
|
||||||
|
}
|
||||||
|
|
||||||
|
#navbar {
|
||||||
|
border: 1px solid rgb(233, 233, 233);
|
||||||
|
border-width: 1px 0px 0px 0px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
justify-content: space-evenly;
|
||||||
|
padding: 20px;
|
||||||
|
gap: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#navbar a {
|
||||||
|
background-color: #a8a8a8;
|
||||||
|
border-radius: 5px;
|
||||||
|
border: 1px solid black;
|
||||||
|
text-decoration: none;
|
||||||
|
color: black;
|
||||||
|
padding: 10px 30px 10px 30px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#favorites {
|
||||||
|
flex-grow: 1;
|
||||||
|
overflow: hidden;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#favorites-container {
|
||||||
|
overflow-y: auto;
|
||||||
|
overflow-x: hidden;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 10px;
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.favorite-dog {
|
||||||
|
max-height: 180px;
|
||||||
|
max-width: 60%;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.favorite-dog img {
|
||||||
|
max-height: 150px;
|
||||||
|
border-radius: 5px;
|
||||||
|
margin: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.favorite-dog:hover button {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.favorite-dog button {
|
||||||
|
display: none;
|
||||||
|
position: absolute;
|
||||||
|
bottom: 10px;
|
||||||
|
left: 10px;
|
||||||
|
z-index: 10;
|
||||||
|
}
|
||||||
8
smart-house-web/frontend-dioxus/clippy.toml
Normal file
8
smart-house-web/frontend-dioxus/clippy.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
await-holding-invalid-types = [
|
||||||
|
"generational_box::GenerationalRef",
|
||||||
|
{ path = "generational_box::GenerationalRef", reason = "Reads should not be held over an await point. This will cause any writes to fail while the await is pending since the read borrow is still active." },
|
||||||
|
"generational_box::GenerationalRefMut",
|
||||||
|
{ path = "generational_box::GenerationalRefMut", reason = "Write should not be held over an await point. This will cause any reads or writes to fail while the await is pending since the write borrow is still active." },
|
||||||
|
"dioxus_signals::WriteLock",
|
||||||
|
{ path = "dioxus_signals::WriteLock", reason = "Write should not be held over an await point. This will cause any reads or writes to fail while the await is pending since the write borrow is still active." },
|
||||||
|
]
|
||||||
0
smart-house-web/frontend-dioxus/src/backend.rs
Normal file
0
smart-house-web/frontend-dioxus/src/backend.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
pub fn Favorites() -> Element {
|
||||||
|
rsx! { "favorites!" }
|
||||||
|
}
|
||||||
9
smart-house-web/frontend-dioxus/src/components/mod.rs
Normal file
9
smart-house-web/frontend-dioxus/src/components/mod.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#![allow(unused_imports)]
|
||||||
|
|
||||||
|
mod favorites;
|
||||||
|
mod nav;
|
||||||
|
mod view;
|
||||||
|
|
||||||
|
pub use favorites::*;
|
||||||
|
pub use nav::*;
|
||||||
|
pub use view::*;
|
||||||
15
smart-house-web/frontend-dioxus/src/components/nav.rs
Normal file
15
smart-house-web/frontend-dioxus/src/components/nav.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
use crate::Route;
|
||||||
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
pub fn NavBar() -> Element {
|
||||||
|
rsx! {
|
||||||
|
div { id: "title",
|
||||||
|
Link { to: Route::DogView,
|
||||||
|
h1 { "🌭 HotDog! " }
|
||||||
|
}
|
||||||
|
Link { to: Route::Favorites, id: "heart", "♥️" }
|
||||||
|
}
|
||||||
|
Outlet::<Route> {}
|
||||||
|
}
|
||||||
|
}
|
||||||
76
smart-house-web/frontend-dioxus/src/main.rs
Normal file
76
smart-house-web/frontend-dioxus/src/main.rs
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
|
use crate::components::{Favorites, NavBar};
|
||||||
|
|
||||||
|
const FAVICON: Asset = asset!("/assets/favicon.ico");
|
||||||
|
const MAIN_CSS: Asset = asset!("/assets/main.css");
|
||||||
|
|
||||||
|
mod backend;
|
||||||
|
mod components;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
dioxus::launch(App);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct TitleState(String);
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn App() -> Element {
|
||||||
|
use_context_provider(|| TitleState("HotDog".to_string()));
|
||||||
|
rsx! {
|
||||||
|
document::Link { rel: "icon", href: FAVICON }
|
||||||
|
document::Stylesheet { href: MAIN_CSS }
|
||||||
|
Router::<Route> { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn Title() -> Element {
|
||||||
|
let title = use_context::<TitleState>();
|
||||||
|
rsx! {
|
||||||
|
div { id: "title",
|
||||||
|
h1 { "{title.0}! 🌭" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
struct DogApi {
|
||||||
|
message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn DogView() -> Element {
|
||||||
|
let mut img_src = use_resource(|| async move {
|
||||||
|
reqwest::get("https://dog.ceo/api/breeds/image/random")
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.json::<DogApi>()
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.message
|
||||||
|
});
|
||||||
|
|
||||||
|
rsx! {
|
||||||
|
div { id: "dogview",
|
||||||
|
img { src: img_src.cloned().unwrap_or_default() }
|
||||||
|
}
|
||||||
|
div { id: "buttons",
|
||||||
|
button { onclick: move |_| img_src.restart(), id: "skip", "skip" }
|
||||||
|
button { onclick: move |_| img_src.restart(), id: "save", "save!" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Routable, Clone, PartialEq)]
|
||||||
|
enum Route {
|
||||||
|
#[layout(NavBar)]
|
||||||
|
#[route("/")]
|
||||||
|
DogView,
|
||||||
|
|
||||||
|
#[route("/favorites")]
|
||||||
|
Favorites,
|
||||||
|
}
|
||||||
1
smart-house-web/frontend/.gitignore
vendored
Normal file
1
smart-house-web/frontend/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/dist/
|
||||||
8
smart-house-web/frontend/Cargo.toml
Normal file
8
smart-house-web/frontend/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "frontend"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
console_error_panic_hook = "0.1"
|
||||||
|
leptos = { version = "0.8", features = ["csr"] }
|
||||||
10
smart-house-web/frontend/README.md
Normal file
10
smart-house-web/frontend/README.md
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# Фронтэнд умного дома
|
||||||
|
|
||||||
|
Запуск сервера разработки (sh):
|
||||||
|
|
||||||
|
RUSTFLAGS="--cfg erase_components" trunk serve
|
||||||
|
|
||||||
|
Запуск сервера разработки (powershell):
|
||||||
|
|
||||||
|
$env:RUSTFLAGS = '--cfg erase_components'
|
||||||
|
trunk serve
|
||||||
11
smart-house-web/frontend/index.html
Normal file
11
smart-house-web/frontend/index.html
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<style>
|
||||||
|
.red {
|
||||||
|
color: red;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body></body>
|
||||||
|
</html>
|
||||||
159
smart-house-web/frontend/src/main.rs
Normal file
159
smart-house-web/frontend/src/main.rs
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
#![allow(non_snake_case)]
|
||||||
|
use leptos::prelude::*;
|
||||||
|
|
||||||
|
// Iteration is a very common task in most applications.
|
||||||
|
// So how do you take a list of data and render it in the DOM?
|
||||||
|
// This example will show you the two ways:
|
||||||
|
// 1) for mostly-static lists, using Rust iterators
|
||||||
|
// 2) for lists that grow, shrink, or move items, using <For/>
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn App() -> impl IntoView {
|
||||||
|
view! {
|
||||||
|
<h1>"Iteration"</h1>
|
||||||
|
<h2>"Static List"</h2>
|
||||||
|
<p>"Use this pattern if the list itself is static."</p>
|
||||||
|
<StaticList length=5/>
|
||||||
|
<h2>"Dynamic List"</h2>
|
||||||
|
<p>"Use this pattern if the rows in your list will change."</p>
|
||||||
|
<DynamicList initial_length=5/>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A list of counters, without the ability
|
||||||
|
/// to add or remove any.
|
||||||
|
#[component]
|
||||||
|
fn StaticList(
|
||||||
|
/// How many counters to include in this list.
|
||||||
|
length: usize,
|
||||||
|
) -> impl IntoView {
|
||||||
|
// create counter signals that start at incrementing numbers
|
||||||
|
let counters = (1..=length).map(|idx| RwSignal::new(idx));
|
||||||
|
|
||||||
|
// when you have a list that doesn't change, you can
|
||||||
|
// manipulate it using ordinary Rust iterators
|
||||||
|
// and collect it into a Vec<_> to insert it into the DOM
|
||||||
|
let counter_buttons = counters
|
||||||
|
.map(|count| {
|
||||||
|
view! {
|
||||||
|
<li>
|
||||||
|
<button
|
||||||
|
on:click=move |_| *count.write() += 1
|
||||||
|
>
|
||||||
|
{count}
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
// Note that if `counter_buttons` were a reactive list
|
||||||
|
// and its value changed, this would be very inefficient:
|
||||||
|
// it would rerender every row every time the list changed.
|
||||||
|
view! {
|
||||||
|
<ul>{counter_buttons}</ul>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A list of counters that allows you to add or
|
||||||
|
/// remove counters.
|
||||||
|
#[component]
|
||||||
|
fn DynamicList(
|
||||||
|
/// The number of counters to begin with.
|
||||||
|
initial_length: usize,
|
||||||
|
) -> impl IntoView {
|
||||||
|
// This dynamic list will use the <For/> component.
|
||||||
|
// <For/> is a keyed list. This means that each row
|
||||||
|
// has a defined key. If the key does not change, the row
|
||||||
|
// will not be re-rendered. When the list changes, only
|
||||||
|
// the minimum number of changes will be made to the DOM.
|
||||||
|
|
||||||
|
// `next_counter_id` will let us generate unique IDs
|
||||||
|
// we do this by simply incrementing the ID by one
|
||||||
|
// each time we create a counter
|
||||||
|
let mut next_counter_id = initial_length;
|
||||||
|
|
||||||
|
// we generate an initial list as in <StaticList/>
|
||||||
|
// but this time we include the ID along with the signal
|
||||||
|
// see NOTE in add_counter below re: ArcRwSignal
|
||||||
|
let initial_counters = (0..initial_length)
|
||||||
|
.map(|id| (id, ArcRwSignal::new(id + 1)))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
// now we store that initial list in a signal
|
||||||
|
// this way, we'll be able to modify the list over time,
|
||||||
|
// adding and removing counters, and it will change reactively
|
||||||
|
let (counters, set_counters) = signal(initial_counters);
|
||||||
|
|
||||||
|
let add_counter = move |_| {
|
||||||
|
// create a signal for the new counter
|
||||||
|
// we use ArcRwSignal here, instead of RwSignal
|
||||||
|
// ArcRwSignal is a reference-counted type, rather than the arena-allocated
|
||||||
|
// signal types we've been using so far.
|
||||||
|
// When we're creating a collection of signals like this, using ArcRwSignal
|
||||||
|
// allows each signal to be deallocated when its row is removed.
|
||||||
|
let sig = ArcRwSignal::new(next_counter_id + 1);
|
||||||
|
// add this counter to the list of counters
|
||||||
|
set_counters.update(move |counters| {
|
||||||
|
// since `.update()` gives us `&mut T`
|
||||||
|
// we can just use normal Vec methods like `push`
|
||||||
|
counters.push((next_counter_id, sig))
|
||||||
|
});
|
||||||
|
// increment the ID so it's always unique
|
||||||
|
next_counter_id += 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
view! {
|
||||||
|
<div>
|
||||||
|
<button on:click=add_counter>
|
||||||
|
"Add Counter"
|
||||||
|
</button>
|
||||||
|
<ul>
|
||||||
|
// The <For/> component is central here
|
||||||
|
// This allows for efficient, key list rendering
|
||||||
|
<For
|
||||||
|
// `each` takes any function that returns an iterator
|
||||||
|
// this should usually be a signal or derived signal
|
||||||
|
// if it's not reactive, just render a Vec<_> instead of <For/>
|
||||||
|
each=move || counters.get()
|
||||||
|
// the key should be unique and stable for each row
|
||||||
|
// using an index is usually a bad idea, unless your list
|
||||||
|
// can only grow, because moving items around inside the list
|
||||||
|
// means their indices will change and they will all rerender
|
||||||
|
key=|counter| counter.0
|
||||||
|
// `children` receives each item from your `each` iterator
|
||||||
|
// and returns a view
|
||||||
|
children=move |(id, count)| {
|
||||||
|
// we can convert our ArcRwSignal to a Copy-able RwSignal
|
||||||
|
// for nicer DX when moving it into the view
|
||||||
|
let count = RwSignal::from(count);
|
||||||
|
view! {
|
||||||
|
<li>
|
||||||
|
<button
|
||||||
|
on:click=move |_| *count.write() += 1
|
||||||
|
>
|
||||||
|
{count}
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
on:click=move |_| {
|
||||||
|
set_counters
|
||||||
|
.write()
|
||||||
|
.retain(|(counter_id, _)| {
|
||||||
|
counter_id != &id
|
||||||
|
});
|
||||||
|
}
|
||||||
|
>
|
||||||
|
"Remove"
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
leptos::mount::mount_to_body(App)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user