Pass error up from mqtt, add more detailed error messages
This commit is contained in:
3 files changed
+291
-379
No files matched your search
+66
-99
@@ -1,4 +1,3 @@
|
||||
use super::App;
|
||||
use crate::config::CameraConfig;
|
||||
use crate::utils::AddressOrUid;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
@@ -80,28 +79,23 @@ impl EventCamSender {
|
||||
pub(crate) struct EventCam {
|
||||
sender: EventCamSender,
|
||||
channel_out: Receiver<Messages>,
|
||||
set: JoinSet<Result<()>>,
|
||||
}
|
||||
|
||||
impl EventCam {
|
||||
pub(crate) async fn new(config: Arc<CameraConfig>, app: Arc<App>) -> Self {
|
||||
pub(crate) async fn new(config: Arc<CameraConfig>) -> Self {
|
||||
// Channels from the camera
|
||||
let (tx, rx) = channel(40);
|
||||
|
||||
// Channels to the camera
|
||||
let (stx, srx) = channel(40);
|
||||
|
||||
let name = config.name.clone();
|
||||
let mut set = JoinSet::<Result<()>>::new();
|
||||
let thread_app = app.clone();
|
||||
let thread_config = config.clone();
|
||||
let mut eventcam_thread = EventCamThread {
|
||||
rx: srx,
|
||||
tx,
|
||||
app: app.clone(),
|
||||
};
|
||||
let thread_config = config;
|
||||
let mut eventcam_thread = EventCamThread { rx: srx, tx };
|
||||
set.spawn(async move {
|
||||
let mut wait_for = tokio::time::Duration::from_micros(125);
|
||||
while thread_app.running(&name) {
|
||||
loop {
|
||||
// Ignore errors and just loop
|
||||
tokio::task::yield_now().await;
|
||||
if let Err(e) = eventcam_thread.cam_run(&thread_config).await {
|
||||
@@ -110,43 +104,37 @@ impl EventCam {
|
||||
wait_for *= 2;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
|
||||
// Watch the thread and abort the app if it fails
|
||||
let join_app = app;
|
||||
let join_name = config.name.to_string();
|
||||
tokio::task::spawn(async move {
|
||||
while let Some(joined) = set.join_next().await {
|
||||
tokio::task::yield_now().await;
|
||||
match joined {
|
||||
Err(e) => {
|
||||
error!("EventCam panicked: {:?}", e);
|
||||
join_app.abort(&join_name);
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
error!("EventCam stopped with error: {:?}", e);
|
||||
join_app.abort(&join_name);
|
||||
}
|
||||
Ok(Ok(())) => {
|
||||
info!("Normal shutdown of EventCam");
|
||||
}
|
||||
}
|
||||
info!("EventCam Loop End");
|
||||
}
|
||||
});
|
||||
|
||||
Self {
|
||||
sender: EventCamSender { channel_in: stx },
|
||||
channel_out: rx,
|
||||
set,
|
||||
}
|
||||
}
|
||||
|
||||
/// This will also error is the join set errors
|
||||
pub(crate) async fn poll(&mut self) -> Result<Messages> {
|
||||
self.channel_out
|
||||
.recv()
|
||||
.await
|
||||
.context("Camera failed to poll")
|
||||
let (incoming, set) = (&mut self.channel_out, &mut self.set);
|
||||
tokio::select! {
|
||||
v = incoming.recv() => v.with_context(|| "Camera Polling error"),
|
||||
v = async {
|
||||
while let Some(res) = set.join_next().await {
|
||||
match res {
|
||||
Err(e) => {
|
||||
set.abort_all();
|
||||
return Err(e.into());
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
set.abort_all();
|
||||
return Err(e);
|
||||
}
|
||||
Ok(Ok(())) => {}
|
||||
}
|
||||
}
|
||||
Err(anyhow!("Camera background thread dropped without error"))
|
||||
} => v.with_context(|| "Camera Threads aborted"),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_sender(&self) -> EventCamSender {
|
||||
@@ -154,10 +142,15 @@ impl EventCam {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for EventCam {
|
||||
fn drop(&mut self) {
|
||||
self.set.abort_all();
|
||||
}
|
||||
}
|
||||
|
||||
struct EventCamThread {
|
||||
rx: Receiver<ToCamera>,
|
||||
tx: Sender<Messages>,
|
||||
app: Arc<App>,
|
||||
}
|
||||
|
||||
impl EventCamThread {
|
||||
@@ -202,18 +195,13 @@ impl EventCamThread {
|
||||
let arc_cam = Arc::new(camera);
|
||||
|
||||
let mut motion_thread = MotionThread {
|
||||
app: self.app.clone(),
|
||||
tx: self.tx.clone(),
|
||||
camera: arc_cam.clone(),
|
||||
name: camera_config.name.to_string(),
|
||||
};
|
||||
|
||||
let thread_app = self.app.clone();
|
||||
let mut message_handler = MessageHandler {
|
||||
app: self.app.clone(),
|
||||
rx: &mut self.rx,
|
||||
camera: arc_cam,
|
||||
name: camera_config.name.to_string(),
|
||||
};
|
||||
|
||||
tokio::select! {
|
||||
@@ -241,33 +229,22 @@ impl EventCamThread {
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
val = async {
|
||||
while thread_app.running(&format!("app:{}", camera_config.name)) {
|
||||
tokio::time::sleep(tokio::time::Duration::from_micros(500)).await
|
||||
}
|
||||
} => {
|
||||
info!("Normal finish on cam run");
|
||||
Result::Ok(())
|
||||
},
|
||||
}?;
|
||||
self.app.abort(&camera_config.name);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct MotionThread {
|
||||
app: Arc<App>,
|
||||
tx: Sender<Messages>,
|
||||
camera: Arc<BcCamera>,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl MotionThread {
|
||||
async fn run(&mut self) -> Result<()> {
|
||||
let mut motion_data = self.camera.listen_on_motion().await?;
|
||||
let mut queued_motion = motion_data.consume_motion_events()?;
|
||||
while self.app.running(&format!("app:{}", self.name)) {
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
for motion_status in queued_motion.drain(..) {
|
||||
match motion_status {
|
||||
@@ -288,20 +265,17 @@ impl MotionThread {
|
||||
}
|
||||
queued_motion.push(motion_data.next_motion().await?);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct MessageHandler<'a> {
|
||||
app: Arc<App>,
|
||||
rx: &'a mut Receiver<ToCamera>,
|
||||
camera: Arc<BcCamera>,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl<'a> MessageHandler<'a> {
|
||||
async fn listen(&mut self) -> Result<()> {
|
||||
while self.app.running(&format!("app:{}", self.name)) {
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
match self.rx.recv().await {
|
||||
Some(to_camera) => {
|
||||
@@ -309,64 +283,64 @@ impl<'a> MessageHandler<'a> {
|
||||
ToCamera::Send(message) => (None, message),
|
||||
ToCamera::SendAndReply { message, reply } => (Some(reply), message),
|
||||
};
|
||||
let mut error = None;
|
||||
let reply = match message {
|
||||
Messages::Reboot => {
|
||||
if self.camera.reboot().await.is_err() {
|
||||
error!("Failed to reboot the camera");
|
||||
self.abort();
|
||||
if let Err(e) = self.camera.reboot().await {
|
||||
error = Some(format!("Failed to reboot the camera: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::StatusLedOn => {
|
||||
if self.camera.led_light_set(true).await.is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
if let Err(e) = self.camera.led_light_set(true).await {
|
||||
error =
|
||||
Some(format!("Failed to turn on the status light: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::StatusLedOff => {
|
||||
if self.camera.led_light_set(false).await.is_err() {
|
||||
error!("Failed to turn off the status light");
|
||||
self.abort();
|
||||
if let Err(e) = self.camera.led_light_set(false).await {
|
||||
error =
|
||||
Some(format!("Failed to turn off the status light: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::IRLedOn => {
|
||||
if self.camera.irled_light_set(LightState::On).await.is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
if let Err(e) = self.camera.irled_light_set(LightState::On).await {
|
||||
error =
|
||||
Some(format!("Failed to turn on the status light: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::IRLedOff => {
|
||||
if self.camera.irled_light_set(LightState::Off).await.is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
if let Err(e) = self.camera.irled_light_set(LightState::Off).await {
|
||||
error =
|
||||
Some(format!("Failed to turn on the status light: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::IRLedAuto => {
|
||||
if self.camera.irled_light_set(LightState::Auto).await.is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
if let Err(e) = self.camera.irled_light_set(LightState::Auto).await {
|
||||
error =
|
||||
Some(format!("Failed to turn on the status light: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::Battery => match self.camera.battery_info().await {
|
||||
Err(_) => {
|
||||
error!("Failed to get battery status");
|
||||
Err(e) => {
|
||||
error!("Failed to get battery status: {:?}", e);
|
||||
"FAIL".to_string()
|
||||
}
|
||||
Ok(battery_info) => {
|
||||
@@ -391,8 +365,8 @@ impl<'a> MessageHandler<'a> {
|
||||
}
|
||||
},
|
||||
Messages::PIRQuery => match self.camera.get_pirstate().await {
|
||||
Err(_) => {
|
||||
error!("Failed to get pir status");
|
||||
Err(e) => {
|
||||
error!("Failed to get pir status: {:?}", e);
|
||||
"FAIL".to_string()
|
||||
}
|
||||
Ok(pir_info) => {
|
||||
@@ -417,18 +391,16 @@ impl<'a> MessageHandler<'a> {
|
||||
}
|
||||
},
|
||||
Messages::PIROn => {
|
||||
if self.camera.pir_set(true).await.is_err() {
|
||||
error!("Failed to turn on the pir");
|
||||
self.abort();
|
||||
if let Err(e) = self.camera.pir_set(true).await {
|
||||
error = Some(format!("Failed to turn on the pir: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::PIROff => {
|
||||
if self.camera.pir_set(false).await.is_err() {
|
||||
error!("Failed to turn on the pir");
|
||||
self.abort();
|
||||
if let Err(e) = self.camera.pir_set(false).await {
|
||||
error = Some(format!("Failed to turn on the pir: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
@@ -443,9 +415,8 @@ impl<'a> MessageHandler<'a> {
|
||||
Direction::In(amount) => (BcDirection::In, amount),
|
||||
Direction::Out(amount) => (BcDirection::Out, amount),
|
||||
};
|
||||
if self.camera.send_ptz(bc_direction, amount).await.is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
if let Err(e) = self.camera.send_ptz(bc_direction, amount).await {
|
||||
error = Some(format!("Failed to send PTZ: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
@@ -456,18 +427,14 @@ impl<'a> MessageHandler<'a> {
|
||||
if let Some(replier) = replier {
|
||||
let _ = replier.send(reply).await;
|
||||
}
|
||||
if let Some(error) = error {
|
||||
return Err(anyhow!("{}", error));
|
||||
}
|
||||
}
|
||||
None => {
|
||||
self.abort();
|
||||
return Err(anyhow!("Message handller channel dropped"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn abort(&self) {
|
||||
self.app.abort(&self.name);
|
||||
}
|
||||
}
|
||||
+177
-226
@@ -1,4 +1,3 @@
|
||||
use log::*;
|
||||
///
|
||||
/// # Neolink MQTT
|
||||
///
|
||||
@@ -54,27 +53,24 @@ use log::*;
|
||||
// `credentials` are the username and password required to identify with the mqtt server
|
||||
//
|
||||
use std::sync::Arc;
|
||||
use tokio::time::{sleep, Duration};
|
||||
|
||||
mod app;
|
||||
mod cmdline;
|
||||
mod event_cam;
|
||||
mod mqttc;
|
||||
|
||||
use crate::config::{CameraConfig, Config, MqttConfig};
|
||||
use anyhow::{anyhow, Result};
|
||||
pub(crate) use app::App;
|
||||
use anyhow::{anyhow, Context, Error, Result};
|
||||
pub(crate) use cmdline::Opt;
|
||||
use event_cam::EventCam;
|
||||
pub(crate) use event_cam::{Direction, Messages};
|
||||
use log::*;
|
||||
use mqttc::{Mqtt, MqttReplyRef};
|
||||
|
||||
/// Entry point for the mqtt subcommand
|
||||
///
|
||||
/// Opt is the command line options
|
||||
pub(crate) async fn main(_: Opt, config: Config) -> Result<()> {
|
||||
let app = App::new();
|
||||
let arc_app = Arc::new(app);
|
||||
|
||||
if config.cameras.iter().all(|config| config.mqtt.is_none()) {
|
||||
return Err(anyhow!(
|
||||
"MQTT command run, but no cameras configured with MQTT settings. Exiting."
|
||||
@@ -89,262 +85,217 @@ pub(crate) async fn main(_: Opt, config: Config) -> Result<()> {
|
||||
.collect::<Vec<_>>()
|
||||
{
|
||||
if let Some(mqtt_config) = camera_config.mqtt.as_ref().map(|a| Arc::new(a.clone())) {
|
||||
let loop_arc_app = arc_app.clone();
|
||||
info!("{}: Setting up mqtt", camera_config.name);
|
||||
set.spawn(async move {
|
||||
while loop_arc_app.running("app") {
|
||||
let mut wait_for = Duration::from_micros(125);
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
let _ =
|
||||
listen_on_camera(camera_config.clone(), &mqtt_config, loop_arc_app.clone())
|
||||
.await;
|
||||
if let Err(e) = listen_on_camera(camera_config.clone(), &mqtt_config).await {
|
||||
warn!("Error: {:?}. Retrying", e);
|
||||
}
|
||||
sleep(wait_for).await;
|
||||
wait_for *= 2;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(result) = set.join_next().await {
|
||||
while let Some(result) = set.join_next().await {
|
||||
result?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn listen_on_camera(
|
||||
cam_config: Arc<CameraConfig>,
|
||||
mqtt_config: &MqttConfig,
|
||||
arc_app: Arc<App>,
|
||||
) -> Result<()> {
|
||||
async fn listen_on_camera(cam_config: Arc<CameraConfig>, mqtt_config: &MqttConfig) -> Result<()> {
|
||||
// Camera thread
|
||||
let mut event_cam = EventCam::new(cam_config.clone(), arc_app.clone()).await;
|
||||
let mut mqtt = Mqtt::new(mqtt_config, &cam_config.name, arc_app.clone()).await;
|
||||
let mut set = tokio::task::JoinSet::new();
|
||||
let mut event_cam = EventCam::new(cam_config.clone()).await;
|
||||
let mut mqtt = Mqtt::new(mqtt_config, &cam_config.name).await;
|
||||
|
||||
let mqtt_sender_cam = mqtt.get_sender();
|
||||
let mqtt_sender_mqtt = mqtt.get_sender();
|
||||
let event_cam_sender = event_cam.get_sender();
|
||||
|
||||
// Listen on mqtt messages and post on camera
|
||||
let event_cam_sender = event_cam.get_sender();
|
||||
let app = arc_app.clone();
|
||||
let camera_name = cam_config.name.clone();
|
||||
let mqtt_sender = mqtt.get_sender();
|
||||
set.spawn(async move {
|
||||
while app.running(&format!("app: {}", camera_name)) {
|
||||
|
||||
let mqtt_to_cam = async {
|
||||
while let Ok(msg) = mqtt.poll().await {
|
||||
tokio::task::yield_now().await;
|
||||
if let Ok(msg) = mqtt.poll().await {
|
||||
match msg.as_ref() {
|
||||
MqttReplyRef {
|
||||
topic: "control/led",
|
||||
message: "on",
|
||||
} => {
|
||||
if event_cam_sender
|
||||
.send_message(Messages::StatusLedOn)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to set camera status light on");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/led",
|
||||
message: "off",
|
||||
} => {
|
||||
if event_cam_sender
|
||||
.send_message(Messages::StatusLedOff)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to set camera status light off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ir",
|
||||
message: "on",
|
||||
} => {
|
||||
if event_cam_sender
|
||||
.send_message(Messages::IRLedOn)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to set camera status light on");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ir",
|
||||
message: "off",
|
||||
} => {
|
||||
if event_cam_sender
|
||||
.send_message(Messages::IRLedOff)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to set camera status light off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ir",
|
||||
message: "auto",
|
||||
} => {
|
||||
if event_cam_sender
|
||||
.send_message(Messages::IRLedAuto)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to set camera status light off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/reboot",
|
||||
..
|
||||
} => {
|
||||
if event_cam_sender
|
||||
.send_message(Messages::Reboot)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to set camera status light off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ptz",
|
||||
message,
|
||||
} => {
|
||||
let lowercase_message = message.to_lowercase();
|
||||
let mut words = lowercase_message.split_whitespace();
|
||||
if let Some(direction_txt) = words.next() {
|
||||
let amount = words.next().unwrap_or("32.0");
|
||||
if let Ok(amount) = amount.parse::<f32>() {
|
||||
let direction = match direction_txt {
|
||||
"up" => Direction::Up(amount),
|
||||
"down" => Direction::Down(amount),
|
||||
"left" => Direction::Left(amount),
|
||||
"right" => Direction::Right(amount),
|
||||
"in" => Direction::In(amount),
|
||||
"out" => Direction::Out(amount),
|
||||
_ => {
|
||||
error!("Unrecongnized PTZ direction");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if event_cam_sender
|
||||
.send_message(Messages::Ptz(direction))
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to send PTZ");
|
||||
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")?;
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/led",
|
||||
message: "off",
|
||||
} => {
|
||||
event_cam_sender
|
||||
.send_message(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")?;
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ir",
|
||||
message: "off",
|
||||
} => {
|
||||
event_cam_sender
|
||||
.send_message(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")?;
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/reboot",
|
||||
..
|
||||
} => {
|
||||
event_cam_sender
|
||||
.send_message(Messages::Reboot)
|
||||
.await
|
||||
.with_context(|| "Failed to set camera status light off")?;
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ptz",
|
||||
message,
|
||||
} => {
|
||||
let lowercase_message = message.to_lowercase();
|
||||
let mut words = lowercase_message.split_whitespace();
|
||||
if let Some(direction_txt) = words.next() {
|
||||
let amount = words.next().unwrap_or("32.0");
|
||||
if let Ok(amount) = amount.parse::<f32>() {
|
||||
let direction = match direction_txt {
|
||||
"up" => Direction::Up(amount),
|
||||
"down" => Direction::Down(amount),
|
||||
"left" => Direction::Left(amount),
|
||||
"right" => Direction::Right(amount),
|
||||
"in" => Direction::In(amount),
|
||||
"out" => Direction::Out(amount),
|
||||
_ => {
|
||||
error!("Unrecongnized PTZ direction");
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
error!("No PTZ direction speed was not a valid number");
|
||||
}
|
||||
};
|
||||
event_cam_sender
|
||||
.send_message(Messages::Ptz(direction))
|
||||
.await
|
||||
.with_context(|| "Failed to send PTZ")?;
|
||||
} else {
|
||||
error!("No PTZ Direction given. Please add up/down/left/right/in/out");
|
||||
error!("No PTZ direction speed was not a valid number");
|
||||
}
|
||||
} else {
|
||||
error!("No PTZ Direction given. Please add up/down/left/right/in/out");
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/pir",
|
||||
message: "on",
|
||||
} => {
|
||||
if event_cam_sender
|
||||
.send_message(Messages::PIROn)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to set pir on");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/pir",
|
||||
message: "off",
|
||||
} => {
|
||||
if event_cam_sender
|
||||
.send_message(Messages::PIROff)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to set pir off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "query/battery",
|
||||
..
|
||||
} => match event_cam_sender
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/pir",
|
||||
message: "on",
|
||||
} => {
|
||||
event_cam_sender
|
||||
.send_message(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")?;
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "query/battery",
|
||||
..
|
||||
} => {
|
||||
let reply = event_cam_sender
|
||||
.send_message_with_reply(Messages::Battery)
|
||||
.await
|
||||
{
|
||||
Ok(reply) => {
|
||||
if mqtt_sender
|
||||
.send_message("status/battery", &reply, false)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to send battery status reply");
|
||||
}
|
||||
}
|
||||
Err(_) => error!("Failed to get battery status"),
|
||||
},
|
||||
MqttReplyRef {
|
||||
topic: "query/pir", ..
|
||||
} => match event_cam_sender
|
||||
.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")?;
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "query/pir", ..
|
||||
} => {
|
||||
let reply = event_cam_sender
|
||||
.send_message_with_reply(Messages::PIRQuery)
|
||||
.await
|
||||
{
|
||||
Ok(reply) => {
|
||||
if mqtt_sender
|
||||
.send_message("status/pir", &reply, false)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to send pir status reply");
|
||||
}
|
||||
}
|
||||
Err(_) => error!("Failed to get pir status"),
|
||||
},
|
||||
_ => {}
|
||||
.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")?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
Result::<(), Error>::Ok(())
|
||||
};
|
||||
|
||||
// Listen on camera messages and post on mqtt
|
||||
let camera_name = cam_config.name.clone();
|
||||
let app = arc_app.clone();
|
||||
|
||||
set.spawn(async move {
|
||||
while app.running(&format!("app: {}", camera_name)) {
|
||||
let cam_to_mqtt = async {
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
if let Ok(msg) = event_cam.poll().await {
|
||||
match msg {
|
||||
Messages::Login => {
|
||||
if mqtt_sender_cam
|
||||
.send_message("status", "connected", true)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to post connect over MQTT for {}", camera_name);
|
||||
}
|
||||
}
|
||||
Messages::MotionStop => {
|
||||
if mqtt_sender_cam
|
||||
.send_message("status/motion", "off", true)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to publish motion stop for {}", camera_name);
|
||||
}
|
||||
}
|
||||
Messages::MotionStart => {
|
||||
if mqtt_sender_cam
|
||||
.send_message("status/motion", "on", true)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to publish motion start for {}", camera_name);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
match event_cam.poll().await? {
|
||||
Messages::Login => {
|
||||
mqtt_sender_cam
|
||||
.send_message("status", "connected", true)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!("Failed to post connect over MQTT for {}", camera_name)
|
||||
})?;
|
||||
}
|
||||
Messages::MotionStop => {
|
||||
mqtt_sender_cam
|
||||
.send_message("status/motion", "off", true)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!("Failed to publish motion stop for {}", camera_name)
|
||||
})?;
|
||||
}
|
||||
Messages::MotionStart => {
|
||||
mqtt_sender_cam
|
||||
.send_message("status/motion", "on", true)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!("Failed to publish motion start for {}", camera_name)
|
||||
})?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
while set.join_next().await.is_some() {}
|
||||
tokio::select! {
|
||||
v = mqtt_to_cam => {v},
|
||||
v = cam_to_mqtt => {v},
|
||||
}?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
+48
-54
@@ -1,4 +1,3 @@
|
||||
use super::App;
|
||||
use crate::config::MqttConfig;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use log::*;
|
||||
@@ -15,6 +14,7 @@ pub(crate) struct Mqtt {
|
||||
client: Arc<AsyncClient>,
|
||||
name: String,
|
||||
incoming: Receiver<MqttReply>,
|
||||
set: JoinSet<Result<()>>,
|
||||
drop_message: Option<MqttReply>,
|
||||
}
|
||||
|
||||
@@ -38,7 +38,6 @@ pub(crate) struct MqttReplyRef<'a> {
|
||||
}
|
||||
|
||||
struct MqttReciever {
|
||||
app: Arc<App>,
|
||||
client: MqttSender,
|
||||
incoming_tx: Sender<MqttReply>,
|
||||
name: String,
|
||||
@@ -96,37 +95,33 @@ impl MqttReciever {
|
||||
// This acts as an event loop
|
||||
let name = self.name.clone();
|
||||
info!("Starting MQTT Client for {}", name);
|
||||
while self.app.running(&format!("app:{}", name)) {
|
||||
if let Ok(notification) = connection.poll().await {
|
||||
// if let Ok(notification) = notification {
|
||||
match notification {
|
||||
Event::Incoming(Incoming::ConnAck(connected)) => {
|
||||
if ConnectReturnCode::Success == connected.code {
|
||||
self.client
|
||||
.update_status()
|
||||
.await
|
||||
.context("Failed to update status")?;
|
||||
// We succesfully logged in. Now ask for the cameras subscription.
|
||||
self.client
|
||||
.subscribe()
|
||||
.await
|
||||
.context("Failed to subscribe")?;
|
||||
}
|
||||
loop {
|
||||
let notification = connection
|
||||
.poll()
|
||||
.await
|
||||
.with_context(|| "MQTT connection dropped")?;
|
||||
match notification {
|
||||
Event::Incoming(Incoming::ConnAck(connected)) => {
|
||||
if ConnectReturnCode::Success == connected.code {
|
||||
self.client
|
||||
.update_status()
|
||||
.await
|
||||
.context("Failed to update status")?;
|
||||
// We succesfully logged in. Now ask for the cameras subscription.
|
||||
self.client
|
||||
.subscribe()
|
||||
.await
|
||||
.context("Failed to subscribe")?;
|
||||
}
|
||||
Event::Incoming(Incoming::Publish(published_message)) => {
|
||||
if self.handle_message(published_message).await.is_err() {
|
||||
error!("Failed to forward messages in mqtt");
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
// }
|
||||
} else {
|
||||
error!("Mqtt connection failure");
|
||||
return Err(anyhow!("Mqtt connection failure"));
|
||||
Event::Incoming(Incoming::Publish(published_message)) => {
|
||||
if self.handle_message(published_message).await.is_err() {
|
||||
error!("Failed to forward messages in mqtt");
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_message(&mut self, published_message: Publish) -> Result<()> {
|
||||
@@ -153,7 +148,7 @@ impl MqttReciever {
|
||||
}
|
||||
|
||||
impl Mqtt {
|
||||
pub(crate) async fn new(config: &MqttConfig, name: &str, app: Arc<App>) -> Self {
|
||||
pub(crate) async fn new(config: &MqttConfig, name: &str) -> Self {
|
||||
let (incoming_tx, incoming) = channel::<MqttReply>(100);
|
||||
let mut mqttoptions = MqttOptions::new(
|
||||
format!("Neolink-{}", name),
|
||||
@@ -211,7 +206,6 @@ impl Mqtt {
|
||||
let mut set = JoinSet::<Result<()>>::new();
|
||||
|
||||
let mut reciever = MqttReciever {
|
||||
app: app.clone(),
|
||||
incoming_tx,
|
||||
name: name.to_string(),
|
||||
client: MqttSender {
|
||||
@@ -221,32 +215,11 @@ impl Mqtt {
|
||||
};
|
||||
set.spawn(async move { reciever.run(&mut connection).await });
|
||||
|
||||
let join_app = app;
|
||||
let join_name = name.to_string();
|
||||
tokio::task::spawn(async move {
|
||||
while let Some(joined) = set.join_next().await {
|
||||
tokio::task::yield_now().await;
|
||||
match joined {
|
||||
Err(e) => {
|
||||
error!("MQTT reciever panicked: {:?}", e);
|
||||
join_app.abort(&join_name);
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
error!("MQTT stopped with error: {:?}", e);
|
||||
join_app.abort(&join_name);
|
||||
}
|
||||
Ok(Ok(())) => {
|
||||
info!("Normal shutdown of MQTT");
|
||||
}
|
||||
}
|
||||
}
|
||||
info!("MQTT Loop End");
|
||||
});
|
||||
|
||||
Self {
|
||||
client,
|
||||
name: name.to_string(),
|
||||
incoming,
|
||||
set,
|
||||
// Send the drop message on clean disconnect
|
||||
drop_message: Some(MqttReply {
|
||||
topic: "status".to_string(),
|
||||
@@ -262,8 +235,28 @@ impl Mqtt {
|
||||
}
|
||||
}
|
||||
|
||||
/// This will also error is the join set errors
|
||||
pub(crate) async fn poll(&mut self) -> Result<MqttReply> {
|
||||
self.incoming.recv().await.context("Mqtt Polling error")
|
||||
let (incoming, set) = (&mut self.incoming, &mut self.set);
|
||||
tokio::select! {
|
||||
v = incoming.recv() => v.with_context(|| "Mqtt Polling error"),
|
||||
v = async {
|
||||
while let Some(res) = set.join_next().await {
|
||||
match res {
|
||||
Err(e) => {
|
||||
set.abort_all();
|
||||
return Err(e.into());
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
set.abort_all();
|
||||
return Err(e);
|
||||
}
|
||||
Ok(Ok(())) => {}
|
||||
}
|
||||
}
|
||||
Err(anyhow!("MQTT background thread dropped without error"))
|
||||
} => v.with_context(|| "MQTT Threads aborted"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,5 +271,6 @@ impl Drop for Mqtt {
|
||||
error!("Failed to send offline message to mqtt");
|
||||
}
|
||||
}
|
||||
self.set.abort_all();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user