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

This commit is contained in:

View File

@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt::Display}; use std::collections::HashMap;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct PowerSocket { pub struct PowerSocket {
@@ -24,7 +24,7 @@ impl PowerSocket {
if self.is_on() { self.power_rate } else { 0.0 } if self.is_on() { self.power_rate } else { 0.0 }
} }
pub fn report(&self) -> impl Display { pub fn report(&self) -> String {
let state = if self.is_on() { "ON" } else { "OFF" }; let state = if self.is_on() { "ON" } else { "OFF" };
let power = self.get_power(); let power = self.get_power();
format!("PowerSocket[ {} : {:02.1} ]", state, power) format!("PowerSocket[ {} : {:02.1} ]", state, power)
@@ -45,7 +45,7 @@ impl Thermometer {
self.temperature self.temperature
} }
pub fn report(&self) -> impl Display { pub fn report(&self) -> String {
let temperature = self.get_temperature(); let temperature = self.get_temperature();
format!("Thermometer[ {:02.1} ]", temperature) format!("Thermometer[ {:02.1} ]", temperature)
} }
@@ -58,7 +58,7 @@ pub enum Device {
} }
impl Device { impl Device {
pub fn report(&self) -> impl Display { pub fn report(&self) -> String {
match self { match self {
Device::PowerSocket(v) => format!("{}", v.report()), Device::PowerSocket(v) => format!("{}", v.report()),
Device::Thermometer(v) => format!("{}", v.report()), Device::Thermometer(v) => format!("{}", v.report()),
@@ -88,7 +88,7 @@ impl Room {
Self { devices } Self { devices }
} }
pub fn report(&self) -> Vec<impl Display> { pub fn report(&self) -> Vec<String> {
let mut output = Vec::with_capacity(self.devices.len()); let mut output = Vec::with_capacity(self.devices.len());
for (name, device) in self.devices.iter() { for (name, device) in self.devices.iter() {
output.push(format!("{} : {}", name, device.report())); output.push(format!("{} : {}", name, device.report()));
@@ -106,4 +106,13 @@ impl House {
pub fn new(rooms: HashMap<String, Room>) -> Self { pub fn new(rooms: HashMap<String, Room>) -> Self {
Self { rooms } Self { rooms }
} }
pub fn report(&self) -> Vec<(String, Vec<String>)> {
let mut output = Vec::with_capacity(self.rooms.len());
for (name, room) in self.rooms.iter() {
let room_report = room.report();
output.push((format!("{}", name), room_report));
}
output
}
} }