From 387887ac8e2b3aa74824997a98e3debe59d844e9 Mon Sep 17 00:00:00 2001 From: Alexander Baranov Date: Sun, 10 May 2026 11:51:29 +0300 Subject: [PATCH] =?UTF-8?q?smart-house-web:=20=D0=B2=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- smart-house-web/backend/Cargo.toml | 2 +- smart-house-web/backend/src/house.rs | 71 +++++++++++++++++++++++++++- smart-house-web/backend/src/lib.rs | 1 + 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/smart-house-web/backend/Cargo.toml b/smart-house-web/backend/Cargo.toml index 2514c2a..d64875f 100644 --- a/smart-house-web/backend/Cargo.toml +++ b/smart-house-web/backend/Cargo.toml @@ -8,5 +8,5 @@ tracing = "0.1" tracing-subscriber = "0.3" tokio = { version = "1.52", features = ["rt", "rt-multi-thread", "signal", "time"] } axum = "0.8" -serde = "1.0" +serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/smart-house-web/backend/src/house.rs b/smart-house-web/backend/src/house.rs index 39f542e..b1007ac 100644 --- a/smart-house-web/backend/src/house.rs +++ b/smart-house-web/backend/src/house.rs @@ -1,4 +1,8 @@ -struct PowerSocket { +use serde::{Deserialize, Serialize}; +use std::fmt::Display; + +#[derive(Debug, Serialize, Deserialize)] +pub struct PowerSocket { power_rate: f32, on: bool, } @@ -7,4 +11,69 @@ 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) -> impl Display { + let state = if self.is_on() { "ON" } else { "OFF" }; + let power = self.get_power(); + format!("PowerSocket[ {} : {:02.1} ]", state, power) + } +} + +#[derive(Debug, 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) -> impl Display { + let temperature = self.get_temperature(); + format!("Thermometer[ {:02.1} ]", temperature) + } +} + +#[derive(Debug, Serialize, Deserialize)] +pub enum Device { + PowerSocket(PowerSocket), + Thermometer(Thermometer), +} + +impl Device { + pub fn report(&self) -> impl Display { + match self { + Device::PowerSocket(v) => format!("{}", v.report()), + Device::Thermometer(v) => format!("{}", v.report()), + } + } +} + +impl From for Device { + fn from(value: PowerSocket) -> Self { + Device::PowerSocket(value) + } +} + +impl From for Device { + fn from(value: Thermometer) -> Self { + Device::Thermometer(value) + } } diff --git a/smart-house-web/backend/src/lib.rs b/smart-house-web/backend/src/lib.rs index e0bae3c..5686d22 100644 --- a/smart-house-web/backend/src/lib.rs +++ b/smart-house-web/backend/src/lib.rs @@ -34,3 +34,4 @@ mod server; pub use server::run_server; mod house; +pub use house::{Device, PowerSocket, Thermometer};