From 9a524afa841048e940f7c94fdcbad1d9f34d1586 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 14 Dec 2022 11:01:20 +0700 Subject: [PATCH] Start work on new commands --- src/mqtt/event_cam.rs | 146 +++++++++++++++++++++++++++++++----------- src/mqtt/mod.rs | 33 +++++++++- 2 files changed, 141 insertions(+), 38 deletions(-) diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 69f0ecb..47d1e3e 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -2,7 +2,7 @@ use super::App; use crate::config::CameraConfig; use crate::utils::AddressOrUid; use anyhow::{anyhow, Context, Result}; -use crossbeam_channel::{unbounded, Receiver, Sender, TryRecvError}; +use crossbeam_channel::{bounded, unbounded, Receiver, Sender, TryRecvError}; use log::*; use neolink_core::bc_protocol::{BcCamera, Error as BcError, LightState, MotionStatus}; use std::sync::{Arc, Mutex}; @@ -18,13 +18,32 @@ pub(crate) enum Messages { IRLedOn, IRLedOff, IRLedAuto, + Battery, + Ptz(Direction), +} + +pub(crate) enum Direction { + Up, + Down, + Left, + Right, + In, + Out, +} + +enum ToCamera { + Send(Messages), + SendAndReply { + message: Messages, + reply: Sender, + }, } pub(crate) struct EventCam<'a> { config: &'a CameraConfig, app: Arc, channel_out: Arc>>>, - channel_in: Arc>>>, + channel_in: Arc>>>, } impl<'a> EventCam<'a> { @@ -53,7 +72,7 @@ impl<'a> EventCam<'a> { if let Ok(ref mut channel_in) = self.channel_in.lock() { if let Some(channel_in) = channel_in.as_mut() { channel_in - .send(msg) + .send(ToCamera::Send(msg)) .context("Failed to send message from camera") } else { Err(anyhow!("Failed to send camera data over crossbeam")) @@ -63,6 +82,26 @@ impl<'a> EventCam<'a> { } } + pub(crate) fn send_message_with_reply(&self, msg: Messages) -> Result { + if let Ok(ref mut channel_in) = self.channel_in.lock() { + if let Some(channel_in) = channel_in.as_mut() { + let (tx, rx) = bounded(1); + channel_in + .send(ToCamera::SendAndReply { + message: msg, + reply: tx, + }) + .context("Failed to send message from camera")?; + rx.recv_timeout(std::time::Duration::from_secs(3)) + .context("Failed to recieve reply") + } else { + Err(anyhow!("Failed to send camera data over crossbeam")) + } + } else { + Err(anyhow!("Failed to lock")) + } + } + pub(crate) fn abort(&self) { self.app.abort(&self.config.name); } @@ -94,7 +133,7 @@ impl<'a> EventCam<'a> { fn cam_run( camera_config: &CameraConfig, tx: Arc>>, - rx: Arc>>, + rx: Arc>>, app: Arc, ) -> Result<()> { let camera_addr = @@ -196,7 +235,7 @@ impl MotionThread { struct MessageHandler { app: Arc, - rx: Arc>>, + rx: Arc>>, camera: Arc, name: String, } @@ -207,45 +246,78 @@ impl MessageHandler { // Try and lock don't worry if not if let Ok(ref mut channel_out) = self.rx.try_lock() { match channel_out.try_recv() { - Ok(message) => match message { - Messages::Reboot => { - if self.camera.reboot().is_err() { - error!("Failed to reboot the camera"); - self.abort() + Ok(to_camera) => { + let (replier, message) = match to_camera { + ToCamera::Send(message) => (None, message), + ToCamera::SendAndReply { message, reply } => (Some(reply), message), + }; + let reply = match message { + Messages::Reboot => { + if self.camera.reboot().is_err() { + error!("Failed to reboot the camera"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } } - } - Messages::StatusLedOn => { - if self.camera.led_light_set(true).is_err() { - error!("Failed to turn on the status light"); - self.abort(); + Messages::StatusLedOn => { + if self.camera.led_light_set(true).is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } } - } - Messages::StatusLedOff => { - if self.camera.led_light_set(false).is_err() { - error!("Failed to turn off the status light"); - self.abort(); + Messages::StatusLedOff => { + if self.camera.led_light_set(false).is_err() { + error!("Failed to turn off the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } } - } - Messages::IRLedOn => { - if self.camera.irled_light_set(LightState::On).is_err() { - error!("Failed to turn on the status light"); - self.abort(); + Messages::IRLedOn => { + if self.camera.irled_light_set(LightState::On).is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } } - } - Messages::IRLedOff => { - if self.camera.irled_light_set(LightState::Off).is_err() { - error!("Failed to turn on the status light"); - self.abort(); + Messages::IRLedOff => { + if self.camera.irled_light_set(LightState::Off).is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } } - } - Messages::IRLedAuto => { - if self.camera.irled_light_set(LightState::Auto).is_err() { - error!("Failed to turn on the status light"); - self.abort(); + Messages::IRLedAuto => { + if self.camera.irled_light_set(LightState::Auto).is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } } + Messages::Battery => { + unimplemented!() + } + Messages::Ptz(_direction) => { + unimplemented!() + } + _ => "UNKNOWN COMMAND".to_string(), + }; + if let Some(replier) = replier { + let _ = replier.send(reply); } - _ => {} - }, + } Err(TryRecvError::Empty) => (), Err(TryRecvError::Disconnected) => self.abort(), } diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 9616d3b..e264850 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -22,7 +22,7 @@ use anyhow::{anyhow, Result}; pub(crate) use app::App; pub(crate) use cmdline::Opt; use event_cam::EventCam; -pub(crate) use event_cam::Messages; +pub(crate) use event_cam::{Direction, Messages}; use mqttc::{Mqtt, MqttReplyRef}; /// Entry point for the mqtt subcommand @@ -156,6 +156,37 @@ fn listen_on_camera( error!("Failed to set camera status light off"); } } + MqttReplyRef { + topic: "control/ptz", + message, + } => { + let direction = match message.to_lowercase().as_str() { + "up" => Direction::Up, + "down" => Direction::Down, + "left" => Direction::Left, + "right" => Direction::Right, + "in" => Direction::In, + "out" => Direction::Out, + _ => { + error!("Unrecongnized PTZ direction"); + continue; + } + }; + if event_cam.send_message(Messages::Ptz(direction)).is_err() { + error!("Failed to send PTZ"); + } + } + MqttReplyRef { + topic: "query/battery", + .. + } => match event_cam.send_message_with_reply(Messages::Battery) { + Ok(reply) => { + if mqtt.send_message("status/battery", &reply, false).is_err() { + error!("Failed to send battery status reply"); + } + } + Err(_) => error!("Failed to set camera status light off"), + }, _ => {} } }