Actually make the shellies switch

This commit is contained in:
Gregor Reitzenstein 2020-09-17 15:45:43 +02:00
parent 4bd62216e2
commit f4d5a70841

View File

@ -3,6 +3,7 @@ use slog::Logger;
use crate::config::Settings; use crate::config::Settings;
use crate::registries::{Registries, Actuator, ActBox, StatusSignal}; use crate::registries::{Registries, Actuator, ActBox, StatusSignal};
use crate::error::Result; use crate::error::Result;
use crate::machine::Status;
use std::pin::Pin; use std::pin::Pin;
use futures::prelude::*; use futures::prelude::*;
@ -59,14 +60,20 @@ impl Stream for Shelly {
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> { fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
let unpin = Pin::into_inner(self); let unpin = Pin::into_inner(self);
if let Some(ref mut s) = unpin.signal { if let Some(ref mut s) = unpin.signal {
let status = ready!(Signal::poll_change(Pin::new(s), cx)); if let Some(status) = ready!(Signal::poll_change(Pin::new(s), cx)) {
let topic = format!("shellies/{}/relay/0/command", unpin.name); let topic = format!("shellies/{}/relay/0/command", unpin.name);
let msg = mqtt::Message::new(topic, "on", 0); 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(|_| ()); let f = unpin.client.publish(msg).map(|_| ());
Poll::Ready(Some(Box::pin(f))) return Poll::Ready(Some(Box::pin(f)));
} else { }
}
Poll::Pending Poll::Pending
} }
}
} }