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

This commit is contained in:
2 changed files with 32 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::{collections::HashMap, fmt::Display};
#[derive(Debug, Serialize, Deserialize)]
pub struct PowerSocket {
@@ -77,3 +77,33 @@ impl From<Thermometer> for Device {
Device::Thermometer(value)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Room {
devices: HashMap<String, Device>,
}
impl Room {
pub fn new(devices: HashMap<String, Device>) -> Self {
Self { devices }
}
pub fn report(&self) -> Vec<impl Display> {
let mut output = Vec::with_capacity(self.devices.len());
for (name, device) in self.devices.iter() {
output.push(format!("{} : {}", name, device.report()));
}
output
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct House {
rooms: HashMap<String, Room>,
}
impl House {
pub fn new(rooms: HashMap<String, Room>) -> Self {
Self { rooms }
}
}

View File

@@ -34,4 +34,4 @@ mod server;
pub use server::run_server;
mod house;
pub use house::{Device, PowerSocket, Thermometer};
pub use house::{Device, House, PowerSocket, Room, Thermometer};