Skip to content

Commit

Permalink
Ensure light entities self-destruct as required
Browse files Browse the repository at this point in the history
  • Loading branch information
brianmay committed Sep 11, 2023
1 parent 7a685aa commit 7e01443
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions brian-backend/src/lights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use robotica_common::{
lights::{self, Colors, Light2Command, PowerColor, PowerLevel, PowerState, State, HSBK},
},
};
use tokio::time::sleep;
use tokio::{select, time::sleep};
use tracing::{debug, error};

trait GetSceneEntity {
Expand Down Expand Up @@ -184,14 +184,17 @@ impl GetSceneEntity for StandardSceneEntities {

fn static_entity(pc: PowerColor, name: impl Into<String>) -> stateful::Receiver<PowerColor> {
let (tx, rx) = stateful::create_pipe(name);
tx.try_send(pc);
spawn(async move {
tx.try_send(pc);
tx.closed().await;
});
rx
}

fn busy_entity(name: impl Into<String>) -> stateful::Receiver<PowerColor> {
let (tx, rx) = stateful::create_pipe(name);
spawn(async move {
loop {
while !tx.is_closed() {
let on_color = HSBK {
hue: 0.0,
saturation: 100.0,
Expand Down Expand Up @@ -224,7 +227,7 @@ fn rainbow_entity(name: impl Into<String>) -> stateful::Receiver<PowerColor> {
let mut i = 0u16;
let num_per_cycle = 10u16;

loop {
while !tx.is_closed() {
let colors: Vec<HSBK> = (0..num_per_cycle)
.map(|j| {
let mut hue = f32::from(i + j) * 360.0 / f32::from(num_per_cycle);
Expand Down Expand Up @@ -265,8 +268,15 @@ fn mqtt_entity(
let (tx, rx) = stateful::create_pipe(name);
spawn(async move {
let mut pc_s = pc_rx.subscribe().await;
while let Ok(pc) = pc_s.recv().await {
tx.try_send(pc);
loop {
select! {
Ok(pc) = pc_s.recv() => {
tx.try_send(pc);
}
_ = tx.closed() => {
break;
}
}
}
});
rx
Expand Down

0 comments on commit 7e01443

Please sign in to comment.