Compare commits
2 Commits
01a7c7b5ec
...
9b7541f71d
| Author | SHA1 | Date | |
|---|---|---|---|
|
9b7541f71d
|
|||
|
1172e3da1f
|
@ -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);
|
||||||
|
|
||||||
for brightness in (1..=50).map(|i| (i as f32) * 0.01) {
|
// slowly turn up brightness of bulb
|
||||||
sleep(Duration::from_secs(12)).await;
|
for brightness in (1..=75).map(|i| (i as f32) * 0.01) {
|
||||||
|
select! {
|
||||||
let message = ClientMessage::SetBulbColor {
|
// abort if the client pokes the bulb
|
||||||
id: id.clone(),
|
_ = wait_for_bulb_command(&id, &mut client_messages) => break,
|
||||||
color: BulbColor::Kelvin {
|
_ = sleep(Duration::from_secs(12)) => {}
|
||||||
t: 0.0,
|
|
||||||
b: brightness,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let (response, _) = mpsc::channel(1);
|
if cmd
|
||||||
let request = ClientRequest { message, response };
|
.send(BulbCommand::SetColor(
|
||||||
|
BulbSelector::Id(id.clone()),
|
||||||
if channel.send(request).is_err() {
|
BulbColor::Kelvin {
|
||||||
|
t: 0.0,
|
||||||
|
b: brightness,
|
||||||
|
},
|
||||||
|
))
|
||||||
|
.await
|
||||||
|
.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,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user