mirror of
https://codeberg.org/icewind/lightflash.git
synced 2026-06-03 18:14:06 +02:00
flake update:
This commit is contained in:
parent
4254707d82
commit
2ff729bfa5
6 changed files with 38 additions and 32 deletions
|
|
@ -75,7 +75,6 @@ impl TryFrom<RawMqttConfig> for MqttConfig {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ConfigError {
|
||||
#[error("Failed to read config file {}: {error:#}", path.display())]
|
||||
|
|
|
|||
30
src/main.rs
30
src/main.rs
|
|
@ -67,21 +67,20 @@ async fn main() -> MainResult {
|
|||
|
||||
loop {
|
||||
let notification = event_loop.poll().await?;
|
||||
if let Event::Incoming(Packet::Publish(packet)) = notification {
|
||||
if let Some(triggered) = packet
|
||||
if let Event::Incoming(Packet::Publish(packet)) = notification
|
||||
&& let Some(triggered) = packet
|
||||
.topic
|
||||
.as_str()
|
||||
.strip_prefix("tele/")
|
||||
.and_then(|topic| topic.strip_suffix("/SENSOR"))
|
||||
{
|
||||
for switch in &switches {
|
||||
if switch.name == triggered {
|
||||
let payload: SwitchPayload = serde_json::from_slice(&packet.payload)?;
|
||||
if payload.doorbell.starts_with("ON") {
|
||||
spawn(flash(mqtt_client.clone(), switch.clone()));
|
||||
} else if payload.doorbell.starts_with("OFF") {
|
||||
switch.active.store(false, Ordering::SeqCst);
|
||||
}
|
||||
{
|
||||
for switch in &switches {
|
||||
if switch.name == triggered {
|
||||
let payload: SwitchPayload = serde_json::from_slice(&packet.payload)?;
|
||||
if payload.doorbell.starts_with("ON") {
|
||||
spawn(flash(mqtt_client.clone(), switch.clone()));
|
||||
} else if payload.doorbell.starts_with("OFF") {
|
||||
switch.active.store(false, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -130,5 +129,12 @@ async fn flash(mqtt_client: Arc<AsyncClient>, switch: Arc<Switch>) {
|
|||
|
||||
async fn toggle_light(mqtt_client: &AsyncClient, light: &str) -> Result<(), ClientError> {
|
||||
debug!(light, "toggling light");
|
||||
mqtt_client.publish(format!("cmnd/{light}/POWER"), QoS::ExactlyOnce, false, "toggle").await
|
||||
mqtt_client
|
||||
.publish(
|
||||
format!("cmnd/{light}/POWER"),
|
||||
QoS::ExactlyOnce,
|
||||
false,
|
||||
"toggle",
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue