From f4d5a70841214aac70f3ec9eea4d4d8f889840db Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Thu, 17 Sep 2020 15:45:43 +0200 Subject: [PATCH] Actually make the shellies switch --- src/modules/shelly.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/modules/shelly.rs b/src/modules/shelly.rs index d7da62b..5cb4640 100644 --- a/src/modules/shelly.rs +++ b/src/modules/shelly.rs @@ -3,6 +3,7 @@ use slog::Logger; use crate::config::Settings; use crate::registries::{Registries, Actuator, ActBox, StatusSignal}; use crate::error::Result; +use crate::machine::Status; use std::pin::Pin; use futures::prelude::*; @@ -59,14 +60,20 @@ impl Stream for Shelly { fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { let unpin = Pin::into_inner(self); if let Some(ref mut s) = unpin.signal { - let status = ready!(Signal::poll_change(Pin::new(s), cx)); - let topic = format!("shellies/{}/relay/0/command", unpin.name); - let msg = mqtt::Message::new(topic, "on", 0); - let f = unpin.client.publish(msg).map(|_| ()); + if let Some(status) = ready!(Signal::poll_change(Pin::new(s), cx)) { + let topic = format!("shellies/{}/relay/0/command", unpin.name); + let pl = match status { + Status::Free => "off", + Status::Occupied => "on", + Status::Blocked => "off", + }; + let msg = mqtt::Message::new(topic, pl, 0); + let f = unpin.client.publish(msg).map(|_| ()); - Poll::Ready(Some(Box::pin(f))) - } else { - Poll::Pending + return Poll::Ready(Some(Box::pin(f))); + } } + + Poll::Pending } }