|
|
|
@ -5,20 +5,22 @@ use sprinklers_actors::{
@@ -5,20 +5,22 @@ use sprinklers_actors::{
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
use actix::{fut::wrap_future, Actor, ActorContext, Addr, AsyncContext, Handler, StreamHandler}; |
|
|
|
|
use futures_util::TryFutureExt; |
|
|
|
|
use sprinklers_core::model::Programs; |
|
|
|
|
use std::sync::Arc; |
|
|
|
|
use tokio::sync::{broadcast, watch}; |
|
|
|
|
use tracing::{trace, warn}; |
|
|
|
|
|
|
|
|
|
struct UpdateListenerActor { |
|
|
|
|
mqtt_interface: MqttInterface, |
|
|
|
|
has_published_program_states: bool, |
|
|
|
|
old_programs: Option<Programs>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl UpdateListenerActor { |
|
|
|
|
fn new(mqtt_interface: MqttInterface) -> Self { |
|
|
|
|
Self { |
|
|
|
|
mqtt_interface, |
|
|
|
|
has_published_program_states: false, |
|
|
|
|
old_programs: None, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -116,27 +118,31 @@ impl StreamHandler<Programs> for UpdateListenerActor {
@@ -116,27 +118,31 @@ impl StreamHandler<Programs> for UpdateListenerActor {
|
|
|
|
|
fn handle(&mut self, programs: Programs, ctx: &mut Self::Context) { |
|
|
|
|
let mut mqtt_interface = self.mqtt_interface.clone(); |
|
|
|
|
|
|
|
|
|
let has_published_program_states = self.has_published_program_states; |
|
|
|
|
self.has_published_program_states = true; |
|
|
|
|
let old_programs = self.old_programs.replace(programs.clone()); |
|
|
|
|
|
|
|
|
|
let fut = async move { |
|
|
|
|
if let Err(err) = mqtt_interface.publish_programs(&programs).await { |
|
|
|
|
warn!("could not publish programs: {:?}", err); |
|
|
|
|
} |
|
|
|
|
match old_programs { |
|
|
|
|
None => { |
|
|
|
|
mqtt_interface.publish_programs(&programs).await?; |
|
|
|
|
|
|
|
|
|
// Some what of a hack
|
|
|
|
|
// Initialize program running states to false the first time we
|
|
|
|
|
// receive programs
|
|
|
|
|
if !has_published_program_states { |
|
|
|
|
for program_id in programs.keys() { |
|
|
|
|
if let Err(err) = mqtt_interface |
|
|
|
|
mqtt_interface |
|
|
|
|
.publish_program_running(*program_id, false) |
|
|
|
|
.await |
|
|
|
|
{ |
|
|
|
|
warn!("could not publish program running: {:?}", err); |
|
|
|
|
.await?; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Some(old_programs) => { |
|
|
|
|
mqtt_interface |
|
|
|
|
.publish_programs_diff(&old_programs, &programs) |
|
|
|
|
.await?; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
.unwrap_or_else(|err: eyre::Report| warn!("could not publish programs: {:?}", err)); |
|
|
|
|
ctx.spawn(wrap_future(fut)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|