Add basic exchange of relay states
This commit is contained in:
parent
0460e838bc
commit
8a83602d6a
10 changed files with 153 additions and 41 deletions
emgauwa-controller/src
|
@ -11,18 +11,29 @@ use tokio::sync::Notify;
|
|||
#[rtype(result = "Result<(), EmgauwaError>")]
|
||||
pub struct Reload {}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "()")]
|
||||
pub struct UpdateRelaysOn {
|
||||
pub relays_are_on: Vec<Option<bool>>,
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Controller")]
|
||||
pub struct GetThis {}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Arc<Notify>")]
|
||||
pub struct GetNotifier {}
|
||||
pub struct GetControllerNotifier {}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Arc<Notify>")]
|
||||
pub struct GetRelayNotifier {}
|
||||
|
||||
pub struct AppState {
|
||||
pub pool: Pool<Sqlite>,
|
||||
pub this: Controller,
|
||||
pub notifier: Arc<Notify>,
|
||||
pub controller_notifier: Arc<Notify>,
|
||||
pub relay_notifier: Arc<Notify>,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
|
@ -30,12 +41,17 @@ impl AppState {
|
|||
AppState {
|
||||
pool,
|
||||
this,
|
||||
notifier: Arc::new(Notify::new()),
|
||||
controller_notifier: Arc::new(Notify::new()),
|
||||
relay_notifier: Arc::new(Notify::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn notify_change(&self) {
|
||||
self.notifier.notify_one();
|
||||
pub fn notify_controller_change(&self) {
|
||||
self.controller_notifier.notify_one();
|
||||
}
|
||||
|
||||
pub fn notify_relay_change(&self) {
|
||||
self.relay_notifier.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,12 +68,28 @@ impl Handler<Reload> for AppState {
|
|||
|
||||
self.this.reload(&mut pool_conn)?;
|
||||
|
||||
self.notify_change();
|
||||
self.notify_controller_change();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler<UpdateRelaysOn> for AppState {
|
||||
type Result = ();
|
||||
|
||||
fn handle(&mut self, msg: UpdateRelaysOn, _ctx: &mut Self::Context) -> Self::Result {
|
||||
self.this
|
||||
.relays
|
||||
.iter_mut()
|
||||
.zip(msg.relays_are_on.iter())
|
||||
.for_each(|(relay, is_on)| {
|
||||
relay.is_on = *is_on;
|
||||
});
|
||||
|
||||
self.notify_relay_change();
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler<GetThis> for AppState {
|
||||
type Result = Controller;
|
||||
|
||||
|
@ -66,10 +98,18 @@ impl Handler<GetThis> for AppState {
|
|||
}
|
||||
}
|
||||
|
||||
impl Handler<GetNotifier> for AppState {
|
||||
impl Handler<GetControllerNotifier> for AppState {
|
||||
type Result = Arc<Notify>;
|
||||
|
||||
fn handle(&mut self, _msg: GetNotifier, _ctx: &mut Self::Context) -> Self::Result {
|
||||
Arc::clone(&self.notifier)
|
||||
fn handle(&mut self, _msg: GetControllerNotifier, _ctx: &mut Self::Context) -> Self::Result {
|
||||
Arc::clone(&self.controller_notifier)
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler<GetRelayNotifier> for AppState {
|
||||
type Result = Arc<Notify>;
|
||||
|
||||
fn handle(&mut self, _msg: GetRelayNotifier, _ctx: &mut Self::Context) -> Self::Result {
|
||||
Arc::clone(&self.relay_notifier)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue