smart-house-web: бэкенд
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user