Abort wake task if user pokes the bulb

This commit is contained in:
2022-10-24 19:08:05 +02:00
parent 01a7c7b5ec
commit 1172e3da1f

View File

@ -29,6 +29,10 @@ pub async fn lights_task(state: &State) {
.await .await
.expect("Failed to open lights config"); .expect("Failed to open lights config");
let (cmd, bulb_states) = BulbManager::launch(config.bulbs.clone(), config.mqtt.clone())
.await
.expect("Failed to launch bulb manager");
let mut wake_tasks: HashMap<(BulbId, Weekday), JoinHandle<()>> = lights_state let mut wake_tasks: HashMap<(BulbId, Weekday), JoinHandle<()>> = lights_state
.get() .get()
.wake_schedule .wake_schedule
@ -36,7 +40,8 @@ pub async fn lights_task(state: &State) {
.flat_map(|(bulb, schedule)| schedule.iter().map(move |(day, time)| (bulb, day, time))) .flat_map(|(bulb, schedule)| schedule.iter().map(move |(day, time)| (bulb, day, time)))
.map(|(bulb, day, time)| { .map(|(bulb, day, time)| {
let handle = spawn(wake_task( let handle = spawn(wake_task(
state.client_message.clone(), state.client_message.subscribe(),
cmd.clone(),
bulb.clone(), bulb.clone(),
*day, *day,
*time, *time,
@ -46,10 +51,6 @@ pub async fn lights_task(state: &State) {
}) })
.collect(); .collect();
let (cmd, bulb_states) = BulbManager::launch(config.bulbs.clone(), config.mqtt.clone())
.await
.expect("Failed to launch bulb manager");
loop { loop {
let notify = bulb_states.notify_on_change(); let notify = bulb_states.notify_on_change();
sleep(tokio::time::Duration::from_millis(1000 / 10)).await; // limit to 10 updates/second sleep(tokio::time::Duration::from_millis(1000 / 10)).await; // limit to 10 updates/second
@ -118,7 +119,8 @@ pub async fn lights_task(state: &State) {
} }
async fn wake_task( async fn wake_task(
channel: broadcast::Sender<ClientRequest>, mut client_messages: broadcast::Receiver<ClientRequest>,
cmd: mpsc::Sender<BulbCommand>,
id: BulbId, id: BulbId,
day: Weekday, day: Weekday,
time: NaiveTime, time: NaiveTime,
@ -145,23 +147,49 @@ async fn wake_task(
sleep((alarm - Local::now()).to_std().unwrap()).await; sleep((alarm - Local::now()).to_std().unwrap()).await;
alarm += chrono::Duration::weeks(1); alarm += chrono::Duration::weeks(1);
// slowly turn up brightness of bulb
for brightness in (1..=50).map(|i| (i as f32) * 0.01) { for brightness in (1..=50).map(|i| (i as f32) * 0.01) {
sleep(Duration::from_secs(12)).await; select! {
// abort if the client pokes the bulb
_ = wait_for_bulb_command(&id, &mut client_messages) => break,
_ = sleep(Duration::from_secs(12)) => {}
};
let message = ClientMessage::SetBulbColor { if cmd
id: id.clone(), .send(BulbCommand::SetColor(
color: BulbColor::Kelvin { BulbSelector::Id(id.clone()),
BulbColor::Kelvin {
t: 0.0, t: 0.0,
b: brightness, b: brightness,
}, },
}; ))
.await
let (response, _) = mpsc::channel(1); .is_err()
let request = ClientRequest { message, response }; {
if channel.send(request).is_err() {
return; return;
}; };
} }
} }
} }
/// Wait until we receive a client request that mutates the given bulb
async fn wait_for_bulb_command(
bulb_id: &BulbId,
client_messages: &mut broadcast::Receiver<ClientRequest>,
) {
loop {
match client_messages.recv().await {
Err(_) => return,
Ok(request) => match request.message {
ClientMessage::SetBulbColor { id, .. }
| ClientMessage::SetBulbPower { id, .. }
| ClientMessage::SetBulbWakeTime { id, .. }
if &id == bulb_id =>
{
break
}
_ => continue,
},
}
}
}