From 85b449dcdc069c8a849dae5a3d0037bf2f37274c Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Tue, 11 Oct 2022 22:58:16 +0200 Subject: [PATCH] Add wake schedule to BulbMode/BulbState server msg --- backend/src/tasks/lights.rs | 18 +++++++++++++----- common/src/lib.rs | 5 ++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/backend/src/tasks/lights.rs b/backend/src/tasks/lights.rs index ab5f902..8d921d5 100644 --- a/backend/src/tasks/lights.rs +++ b/backend/src/tasks/lights.rs @@ -16,7 +16,7 @@ use crate::{ClientRequest, State}; #[derive(Default, Clone, PartialEq, Eq, Serialize, Deserialize)] struct LightsState { - wake_schedule: HashMap<(BulbId, Weekday), NaiveTime>, + wake_schedule: HashMap>, } pub async fn lights_task(state: &State) { @@ -33,7 +33,8 @@ pub async fn lights_task(state: &State) { .get() .wake_schedule .iter() - .map(|((bulb, day), time)| { + .flat_map(|(bulb, schedule)| schedule.iter().map(move |(day, time)| (bulb, day, time))) + .map(|(bulb, day, time)| { let handle = spawn(wake_task( state.client_message.clone(), bulb.clone(), @@ -54,8 +55,11 @@ pub async fn lights_task(state: &State) { sleep(tokio::time::Duration::from_millis(1000 / 10)).await; // limit to 10 updates/second select! { _ = notify => { + let lights_state = lights_state.get(); for (id, mode) in bulb_states.bulbs().await.clone().into_iter() { - if let Err(e) = server_message.send(ServerMessage::BulbMode { id, mode }) { + let wake_schedule = lights_state.wake_schedule.get(&id).cloned().unwrap_or_default(); + let msg = ServerMessage::BulbState { id, mode, wake_schedule }; + if let Err(e) = server_message.send(msg) { error!("broadcast channel error: {e}"); return; } @@ -83,8 +87,11 @@ pub async fn lights_task(state: &State) { error!("GetBulbs response channel error: {e}"); return; } + let lights_state = lights_state.get(); for (id, mode) in bulb_states.bulbs().await.clone().into_iter() { - if let Err(e) = request.response.send(ServerMessage::BulbMode { id, mode }).await { + let wake_schedule = lights_state.wake_schedule.get(&id).cloned().unwrap_or_default(); + let msg = ServerMessage::BulbState { id, mode, wake_schedule }; + if let Err(e) = request.response.send(msg).await { error!("GetBulbs response channel error: {e}"); return; } @@ -92,7 +99,8 @@ pub async fn lights_task(state: &State) { } ClientMessage::SetBulbWakeTime { id, day, time } => { if let Err(e) = lights_state.update(|lights_state| { - lights_state.wake_schedule.insert((id.clone(), day), time); + let schedule = lights_state.wake_schedule.entry(id.clone()).or_default(); + schedule.insert(day, time); }).await { error!("Failed to save wake schedule: {e}"); }; diff --git a/common/src/lib.rs b/common/src/lib.rs index 0916a7d..4d5f129 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use chrono::{NaiveTime, Weekday}; use lighter_lib::{BulbColor, BulbId, BulbMode}; use serde::{Deserialize, Serialize}; @@ -10,9 +12,10 @@ pub enum ServerMessage { }, /// Update the state of a bulb - BulbMode { + BulbState { id: BulbId, mode: BulbMode, + wake_schedule: HashMap, }, BulbMap(BulbMap),