|
|
@ -7,7 +7,6 @@ use sprinklers_actors::{ |
|
|
|
use actix::{fut::wrap_future, Actor, ActorContext, Addr, AsyncContext, Handler, StreamHandler}; |
|
|
|
use actix::{fut::wrap_future, Actor, ActorContext, Addr, AsyncContext, Handler, StreamHandler}; |
|
|
|
use futures_util::TryFutureExt; |
|
|
|
use futures_util::TryFutureExt; |
|
|
|
use sprinklers_core::model::Programs; |
|
|
|
use sprinklers_core::model::Programs; |
|
|
|
use std::sync::Arc; |
|
|
|
|
|
|
|
use tokio::sync::{broadcast, watch}; |
|
|
|
use tokio::sync::{broadcast, watch}; |
|
|
|
use tracing::{trace, warn}; |
|
|
|
use tracing::{trace, warn}; |
|
|
|
|
|
|
|
|
|
|
@ -85,18 +84,38 @@ impl StreamHandler<Result<ProgramEvent, broadcast::RecvError>> for UpdateListene |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
let (program_id, running) = match event { |
|
|
|
|
|
|
|
ProgramEvent::RunStart(prog) => (prog.id, true), |
|
|
|
|
|
|
|
ProgramEvent::RunFinish(prog) | ProgramEvent::RunCancel(prog) => (prog.id, false), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
let mut mqtt_interface = self.mqtt_interface.clone(); |
|
|
|
let mut mqtt_interface = self.mqtt_interface.clone(); |
|
|
|
let fut = async move { |
|
|
|
let fut = async move { |
|
|
|
|
|
|
|
enum Publish { |
|
|
|
|
|
|
|
Running(bool), |
|
|
|
|
|
|
|
NextRun(chrono::DateTime<chrono::Local>), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let (program_id, publish) = match event { |
|
|
|
|
|
|
|
ProgramEvent::RunStart(prog) => (prog.id, Publish::Running(true)), |
|
|
|
|
|
|
|
ProgramEvent::RunFinish(prog) | ProgramEvent::RunCancel(prog) => { |
|
|
|
|
|
|
|
(prog.id, Publish::Running(false)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ProgramEvent::NextRun(prog, next_run) => (prog.id, Publish::NextRun(next_run)), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
match publish { |
|
|
|
|
|
|
|
Publish::Running(running) => { |
|
|
|
if let Err(err) = mqtt_interface |
|
|
|
if let Err(err) = mqtt_interface |
|
|
|
.publish_program_running(program_id, running) |
|
|
|
.publish_program_running(program_id, running) |
|
|
|
.await |
|
|
|
.await |
|
|
|
{ |
|
|
|
{ |
|
|
|
warn!("could not publish program running: {}", err); |
|
|
|
warn!("could not publish program running: {}", err); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Publish::NextRun(next_run) => { |
|
|
|
|
|
|
|
if let Err(err) = mqtt_interface |
|
|
|
|
|
|
|
.publish_program_next_run(program_id, next_run) |
|
|
|
|
|
|
|
.await |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
warn!("could not publish program next run: {}", err); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
ctx.spawn(wrap_future(fut)); |
|
|
|
ctx.spawn(wrap_future(fut)); |
|
|
|
} |
|
|
|
} |
|
|
|