From 4f50a962e6c07aba9b66d126e6eb52e091b23e6c Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Thu, 20 Apr 2023 07:32:30 +0700 Subject: [PATCH] Pass fail/OK messages back to mqtt --- src/mqtt/event_cam.rs | 1 + src/mqtt/mod.rs | 136 ++++++++++++++++++++++++++---------------- 2 files changed, 84 insertions(+), 53 deletions(-) diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 8d0228d..fe0ae26 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -54,6 +54,7 @@ pub(crate) struct EventCamSender { } impl EventCamSender { + #[allow(dead_code)] pub(crate) async fn send_message(&self, msg: Messages) -> Result<()> { self.channel_in .send(ToCamera::Send(msg)) diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 554a234..dcdb2ab 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -122,60 +122,74 @@ async fn listen_on_camera(cam_config: Arc, mqtt_config: &MqttConfi let mqtt_to_cam = async { while let Ok(msg) = mqtt.poll().await { tokio::task::yield_now().await; + let mut reply = None; + let mut reply_topic = None; match msg.as_ref() { MqttReplyRef { topic: "control/led", message: "on", } => { - event_cam_sender - .send_message(Messages::StatusLedOn) - .await - .with_context(|| "Failed to set camera status light on")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::StatusLedOn) + .await + .with_context(|| "Failed to set camera status light on")?, + ); } MqttReplyRef { topic: "control/led", message: "off", } => { - event_cam_sender - .send_message(Messages::StatusLedOff) - .await - .with_context(|| "Failed to set camera status light off")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::StatusLedOff) + .await + .with_context(|| "Failed to set camera status light off")?, + ); } MqttReplyRef { topic: "control/ir", message: "on", } => { - event_cam_sender - .send_message(Messages::IRLedOn) - .await - .with_context(|| "Failed to set camera status light on")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::IRLedOn) + .await + .with_context(|| "Failed to set camera status light on")?, + ); } MqttReplyRef { topic: "control/ir", message: "off", } => { - event_cam_sender - .send_message(Messages::IRLedOff) - .await - .with_context(|| "Failed to set camera status light off")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::IRLedOff) + .await + .with_context(|| "Failed to set camera status light off")?, + ); } MqttReplyRef { topic: "control/ir", message: "auto", } => { - event_cam_sender - .send_message(Messages::IRLedAuto) - .await - .with_context(|| "Failed to set camera status light off")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::IRLedAuto) + .await + .with_context(|| "Failed to set camera status light off")?, + ); } MqttReplyRef { topic: "control/reboot", .. } => { - event_cam_sender - .send_message(Messages::Reboot) - .await - .with_context(|| "Failed to set camera status light off")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::Reboot) + .await + .with_context(|| "Failed to set camera status light off")?, + ); } MqttReplyRef { topic: "control/ptz", @@ -198,10 +212,12 @@ async fn listen_on_camera(cam_config: Arc, mqtt_config: &MqttConfi continue; } }; - event_cam_sender - .send_message(Messages::Ptz(direction)) - .await - .with_context(|| "Failed to send PTZ")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::Ptz(direction)) + .await + .with_context(|| "Failed to send PTZ")?, + ); } else { error!("No PTZ direction speed was not a valid number"); } @@ -213,48 +229,62 @@ async fn listen_on_camera(cam_config: Arc, mqtt_config: &MqttConfi topic: "control/pir", message: "on", } => { - event_cam_sender - .send_message(Messages::PIROn) - .await - .with_context(|| "Failed to set pir on")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::PIROn) + .await + .with_context(|| "Failed to set pir on")?, + ); } MqttReplyRef { topic: "control/pir", message: "off", } => { - event_cam_sender - .send_message(Messages::PIROff) - .await - .with_context(|| "Failed to set pir off")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::PIROff) + .await + .with_context(|| "Failed to set pir off")?, + ); } MqttReplyRef { topic: "query/battery", .. } => { - let reply = event_cam_sender - .send_message_with_reply(Messages::Battery) - .await - .with_context(|| "Failed to get battery status")?; - mqtt_sender_mqtt - .send_message("status/battery", &reply, false) - .await - .with_context(|| "Failed to send battery status reply")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::Battery) + .await + .with_context(|| "Failed to get battery status")?, + ); + reply_topic = Some("status/battery"); } MqttReplyRef { topic: "query/pir", .. } => { - let reply = event_cam_sender - .send_message_with_reply(Messages::PIRQuery) - .await - .with_context(|| "Failed to get pir status")?; - - mqtt_sender_mqtt - .send_message("status/pir", &reply, false) - .await - .with_context(|| "Failed to send pir status reply")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::PIRQuery) + .await + .with_context(|| "Failed to get pir status")?, + ); + reply_topic = Some("status/pir"); } _ => {} } + if let Some(reply) = reply { + if let Some(topic) = reply_topic { + mqtt_sender_mqtt + .send_message(topic, &reply, false) + .await + .with_context(|| "Failed to send Camera reply to Mqtt")?; + } else { + mqtt_sender_mqtt + .send_message(&msg.topic, &reply, false) + .await + .with_context(|| "Failed to send Camera reply to Mqtt")?; + } + } } Result::<(), Error>::Ok(()) };