Merge pull request #66 from QuantumEntangledAndy/fix/mqtt_asyncio

Update mqtt to use tokio
This commit is contained in:
Andrew King authored and GitHub committed 2023-04-20 15:39:40 +07:00
commit 81a573ba6d
11 files changed
+743 -723

No files matched your search

+52 -103
View File
@@ -124,12 +124,6 @@ impl BcCamera {
/// Try to connect to the camera via appropaite methods and return
/// the location that should be used
async fn find_camera(options: &BcCameraOpt) -> Result<CameraLocation> {
let mut tcp_set = tokio::task::JoinSet::new();
let mut local_set = tokio::task::JoinSet::new();
let mut remote_set = tokio::task::JoinSet::new();
let mut map_set = tokio::task::JoinSet::new();
let mut relay_set = tokio::task::JoinSet::new();
if let ConnectionProtocol::Tcp | ConnectionProtocol::TcpUdp = options.protocol {
let mut sockets = vec![];
match options.port {
@@ -145,15 +139,15 @@ impl BcCamera {
}
}
}
info!("{}: Trying TCP discovery", options.name);
for socket in sockets.drain(..) {
let channel_id: u8 = options.channel_id;
let name: String = options.name.clone();
tcp_set.spawn(async move {
Discovery::check_tcp(socket, channel_id).await.map(|_| {
info!("{}: TCP Discovery success at {:?}", name, &socket);
socket
})
});
if let Ok(addr) = Discovery::check_tcp(socket, channel_id).await.map(|_| {
info!("{}: TCP Discovery success at {:?}", options.name, &socket);
socket
}) {
return Ok(CameraLocation::Tcp(addr));
}
}
}
@@ -187,108 +181,63 @@ impl BcCamera {
if allow_local {
let uid_local = uid.clone();
let name: String = options.name.clone();
local_set.spawn(async move {
trace!("{}: Starting Local discovery", name);
let result = Discovery::local(&uid_local, Some(sockets)).await;
if let Ok(disc) = &result {
info!(
"{}: Local discovery success {} at {}",
name,
uid_local,
disc.get_addr()
);
}
result
});
info!("{}: Trying local discovery", options.name);
let result = Discovery::local(&uid_local, Some(sockets)).await;
if let Ok(disc) = result {
info!(
"{}: Local discovery success {} at {}",
options.name,
uid_local,
disc.get_addr()
);
return Ok(CameraLocation::Udp(disc));
}
}
if allow_remote {
let uid_remote = uid.clone();
let name: String = options.name.clone();
remote_set.spawn(async move {
trace!("Starting Remote discovery");
let result = Discovery::remote(&uid_remote).await;
if let Ok(disc) = &result {
info!(
"{}: Remote discovery success {} at {}",
name,
uid_remote,
disc.get_addr()
);
}
result
});
info!("{}: Trying remote discovery", options.name);
let result = Discovery::remote(&uid_remote).await;
if let Ok(disc) = result {
info!(
"{}: Remote discovery success {} at {}",
options.name,
uid_remote,
disc.get_addr()
);
return Ok(CameraLocation::Udp(disc));
}
}
if allow_map {
let uid_map = uid.clone();
let name: String = options.name.clone();
map_set.spawn(async move {
trace!("Starting Map");
let result = Discovery::map(&uid_map).await;
if let Ok(disc) = &result {
info!("{}: Map success {} at {}", name, uid_map, disc.get_addr());
}
result
});
info!("{}: Trying map discovery", options.name);
let result = Discovery::map(&uid_map).await;
if let Ok(disc) = result {
info!(
"{}: Map success {} at {}",
options.name,
uid_map,
disc.get_addr()
);
return Ok(CameraLocation::Udp(disc));
}
}
if allow_relay {
let uid_relay = uid.clone();
let name: String = options.name.clone();
relay_set.spawn(async move {
trace!("Starting Relay");
let result = Discovery::relay(&uid_relay).await;
if let Ok(disc) = &result {
info!(
"{}: Relay success {} at {}",
name,
uid_relay,
disc.get_addr()
);
}
result
});
info!("{}: Trying relay discovery", options.name);
let result = Discovery::relay(&uid_relay).await;
if let Ok(disc) = result {
info!(
"{}: Relay success {} at {}",
options.name,
uid_relay,
disc.get_addr()
);
return Ok(CameraLocation::Udp(disc));
}
}
}
// Wait for all TCP to finish
while let Some(result) = tcp_set.join_next().await {
if let Ok(Ok(addr)) = result {
return Ok(CameraLocation::Tcp(addr));
}
}
debug!("TCP fail");
// Tcp failed see if Local faired any better
// Wait for all local to finish
while let Some(result) = local_set.join_next().await {
if let Ok(Ok(discovery)) = result {
return Ok(CameraLocation::Udp(discovery));
}
}
debug!("Local fail");
// Local failed see if Remote faired any better
// Wait for all remote to finish
while let Some(result) = remote_set.join_next().await {
if let Ok(Ok(discovery)) = result {
return Ok(CameraLocation::Udp(discovery));
}
}
debug!("Remote fail");
// Remote failed see if Map faired any better
// Wait for all Map to finish
while let Some(result) = map_set.join_next().await {
if let Ok(Ok(discovery)) = result {
return Ok(CameraLocation::Udp(discovery));
}
}
debug!("Map fail");
// Map failed see if Relay faired any better
// Wait for all Relay to finish
while let Some(result) = relay_set.join_next().await {
if let Ok(Ok(discovery)) = result {
return Ok(CameraLocation::Udp(discovery));
}
}
debug!("Relay (All) fail");
info!("{}: Discovery failed", options.name);
// Nothing works
Err(Error::CannotInitCamera)
}
@@ -101,6 +101,7 @@ impl Discoverer {
let thread_subscriber = subsribers.clone();
set.spawn(async move {
loop {
tokio::task::yield_now().await;
match reader.next().await {
Some(Ok((BcUdp::Discovery(bcudp), addr))) => {
let mut tid = bcudp.tid;
@@ -852,6 +853,7 @@ impl Discoverer {
let mut interval = interval(Duration::from_secs(1));
self.handle.write().await.spawn(async move {
loop {
tokio::task::yield_now().await;
interval.tick().await;
let msg = BcUdp::Discovery(UdpDiscovery {
tid,
@@ -880,6 +882,7 @@ impl Discoverer {
let mut interval = interval(Duration::from_secs(1));
self.handle.write().await.spawn(async move {
loop {
tokio::task::yield_now().await;
interval.tick().await;
let msg = BcUdp::Discovery(UdpDiscovery {
tid,
+1
View File
@@ -216,6 +216,7 @@ impl BcCamera {
let mut sub = connection.subscribe(msg_num).await?;
loop {
tokio::task::yield_now().await;
let msg = sub.recv().await;
let status = match msg {
Ok(motion_msg) => {
+1
View File
@@ -105,6 +105,7 @@ impl BcCamera {
let channel_id = self.channel_id;
let handle = task::spawn(async move {
tokio::task::yield_now().await;
let mut sub_video = connection.subscribe(msg_num).await?;
// On an E1 and swann cameras:
+1
View File
@@ -65,6 +65,7 @@ async fn output(pipeline: Pipeline) -> Result<GstSender> {
let mut set = JoinSet::new();
set.spawn(async move {
while let Some(control) = reciever.recv().await {
tokio::task::yield_now().await;
match control {
GstControl::Data(buf) => {
let mut gst_buf = gstreamer::Buffer::with_size(buf.len()).unwrap();
+296 -282
View File
@@ -1,20 +1,18 @@
use super::App;
use crate::config::CameraConfig;
use crate::utils::AddressOrUid;
use anyhow::{anyhow, Context, Result};
use log::*;
use neolink_core::bc_protocol::{
BcCamera, Direction as BcDirection, Error as BcError, LightState, MotionStatus,
BcCamera, Direction as BcDirection, Error as BcError, LightState, MaxEncryption, MotionStatus,
};
use std::sync::Arc;
use tokio::sync::{
mpsc::{channel, error::TryRecvError, Receiver, Sender},
Mutex,
use tokio::{
sync::mpsc::{channel, Receiver, Sender},
task::JoinSet,
};
#[derive(Debug, Copy, Clone)]
pub(crate) enum Messages {
None,
Login,
MotionStop,
MotionStart,
@@ -50,102 +48,114 @@ enum ToCamera {
},
}
pub(crate) struct EventCam {
config: Arc<CameraConfig>,
app: Arc<App>,
channel_out: Arc<Mutex<Option<Receiver<Messages>>>>,
channel_in: Arc<Mutex<Option<Sender<ToCamera>>>>,
#[derive(Clone)]
pub(crate) struct EventCamSender {
channel_in: Sender<ToCamera>,
}
impl EventCam {
pub(crate) fn new(config: Arc<CameraConfig>, app: Arc<App>) -> Self {
Self {
config,
app,
channel_out: Arc::new(Mutex::new(None)),
channel_in: Arc::new(Mutex::new(None)),
}
}
pub(crate) async fn poll(&self) -> Result<Messages> {
if let Ok(ref mut channel_out) = self.channel_out.try_lock() {
if let Some(channel_out) = channel_out.as_mut() {
channel_out.recv().await.context("Camera failed to poll")
} else {
Ok(Messages::None)
}
} else {
Ok(Messages::None)
}
}
impl EventCamSender {
#[allow(dead_code)]
pub(crate) async fn send_message(&self, msg: Messages) -> Result<()> {
let mut channel_in = self.channel_in.lock().await;
if let Some(channel_in) = channel_in.as_mut() {
channel_in
.send(ToCamera::Send(msg))
.await
.context("Failed to send message from camera")
} else {
Err(anyhow!("Failed to send camera data over channel"))
}
self.channel_in
.send(ToCamera::Send(msg))
.await
.context("Failed to send message from camera")
}
pub(crate) async fn send_message_with_reply(&self, msg: Messages) -> Result<String> {
let mut channel_in = self.channel_in.lock().await;
if let Some(channel_in) = channel_in.as_mut() {
let (tx, mut rx) = channel(1);
channel_in
.send(ToCamera::SendAndReply {
message: msg,
reply: tx,
})
.await
.context("Failed to send message from camera")?;
tokio::time::timeout(std::time::Duration::from_secs(3), rx.recv())
.await
.context("Failed to recieve reply Timeout")?
.ok_or_else(|| anyhow!("Failed to recieve reply"))
} else {
Err(anyhow!("Failed to send camera data over crossbeam"))
}
let (tx, mut rx) = channel(1);
self.channel_in
.send(ToCamera::SendAndReply {
message: msg,
reply: tx,
})
.await
.context("Failed to send message from camera")?;
tokio::time::timeout(std::time::Duration::from_secs(3), rx.recv())
.await
.context("Failed to recieve reply Timeout")?
.ok_or_else(|| anyhow!("Failed to recieve reply"))
}
}
pub(crate) struct EventCam {
sender: EventCamSender,
channel_out: Receiver<Messages>,
set: JoinSet<Result<()>>,
}
pub(crate) fn abort(&self) {
self.app.abort(&self.config.name);
}
pub(crate) async fn start_listening(&self) {
let loop_config = self.config.clone();
impl EventCam {
pub(crate) async fn new(config: Arc<CameraConfig>) -> Self {
// Channels from the camera
let (tx, rx) = channel(40);
let loop_tx = Arc::new(Mutex::new(tx));
*self.channel_out.lock().await = Some(rx);
// Channels to the camera
let (stx, srx) = channel(40);
let loop_rx = Arc::new(Mutex::new(srx));
*self.channel_in.lock().await = Some(stx);
while self.app.running(&self.config.name) {
// Ignore errors and just loop
let _ = Self::cam_run(
&loop_config,
loop_tx.clone(),
loop_rx.clone(),
self.app.clone(),
)
.await;
let mut set = JoinSet::<Result<()>>::new();
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);
loop {
// Ignore errors and just loop
tokio::task::yield_now().await;
if let Err(e) = eventcam_thread.cam_run(&thread_config).await {
warn!("Camera thread error: {:?}. Restarting...", e);
tokio::time::sleep(wait_for).await;
wait_for *= 2;
}
}
});
Self {
sender: EventCamSender { channel_in: stx },
channel_out: rx,
set,
}
}
async fn cam_run(
camera_config: &CameraConfig,
tx: Arc<Mutex<Sender<Messages>>>,
rx: Arc<Mutex<Receiver<ToCamera>>>,
app: Arc<App>,
) -> Result<()> {
/// This will also error is the join set errors
pub(crate) async fn poll(&mut self) -> Result<Messages> {
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 {
self.sender.clone()
}
}
impl Drop for EventCam {
fn drop(&mut self) {
self.set.abort_all();
}
}
struct EventCamThread {
rx: Receiver<ToCamera>,
tx: Sender<Messages>,
}
impl EventCamThread {
async fn cam_run(&mut self, camera_config: &CameraConfig) -> Result<()> {
let camera_addr = AddressOrUid::new(
&camera_config.camera_addr,
&camera_config.camera_uid,
@@ -168,47 +178,58 @@ impl EventCam {
})?;
info!("{}: Logging in", camera_config.name);
let max_encryption = match camera_config.max_encryption.to_lowercase().as_str() {
"none" => MaxEncryption::None,
"bcencrypt" => MaxEncryption::BcEncrypt,
"aes" => MaxEncryption::Aes,
_ => MaxEncryption::Aes,
};
camera
.login()
.login_with_maxenc(max_encryption)
.await
.context("Failed to login to the camera")?;
info!("{}: Connected and logged in", camera_config.name);
(tx.lock().await).send(Messages::Login).await?;
self.tx.send(Messages::Login).await?;
// Shararble cameras
let arc_cam = Arc::new(camera);
let mut motion_thread = MotionThread {
app: app.clone(),
tx,
tx: self.tx.clone(),
camera: arc_cam.clone(),
name: camera_config.name.to_string(),
};
let mut message_handler = MessageHandler {
app: app.clone(),
rx,
rx: &mut self.rx,
camera: arc_cam,
name: camera_config.name.to_string(),
};
let camera_name = camera_config.name.clone();
info!("{}: Listening to Camera Motion", camera_config.name);
let handle_motion = tokio::task::spawn(async move {
if let Err(e) = motion_thread.run().await {
error!("Motion thread aborted: {:?}", e);
}
app.abort(&camera_name);
});
info!("{}: Setting up camera actions", camera_config.name);
let handle_camera = tokio::task::spawn(async move {
message_handler.listen().await;
});
tokio::select! {
val = async {handle_motion.await} => {val},
val = async {handle_camera.await} => {val}
val = async {
info!("{}: Listening to Camera Motion", camera_config.name);
motion_thread.run().await
} => {
if let Err(e) = val {
error!("Motion thread aborted: {:?}", e);
Err(e)
} else {
debug!("Normal finish on motion thread");
Ok(())
}
},
val = async {
info!("{}: Setting up camera actions", camera_config.name);
message_handler.listen().await
} => {
if let Err(e) = val {
error!("Camera message handler aborted: {:?}", e);
Err(e)
} else {
debug!("Normal finish on camera message thread");
Ok(())
}
},
}?;
Ok(())
@@ -216,26 +237,26 @@ impl EventCam {
}
struct MotionThread {
app: Arc<App>,
tx: Arc<Mutex<Sender<Messages>>>,
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?;
while self.app.running(&format!("app:{}", self.name)) {
for motion_status in motion_data.consume_motion_events()?.drain(..) {
let mut queued_motion = motion_data.consume_motion_events()?;
loop {
tokio::task::yield_now().await;
for motion_status in queued_motion.drain(..) {
match motion_status {
MotionStatus::Start(_) => {
(self.tx.lock().await)
self.tx
.send(Messages::MotionStart)
.await
.map_err(|_| BcError::Other("Failed to send Message"))?;
}
MotionStatus::Stop(_) => {
(self.tx.lock().await)
self.tx
.send(Messages::MotionStop)
.await
.map_err(|_| BcError::Other("Failed to send Message"))?;
@@ -243,185 +264,178 @@ impl MotionThread {
_ => {}
}
}
queued_motion.push(motion_data.next_motion().await?);
}
Ok(())
}
}
struct MessageHandler {
app: Arc<App>,
rx: Arc<Mutex<Receiver<ToCamera>>>,
struct MessageHandler<'a> {
rx: &'a mut Receiver<ToCamera>,
camera: Arc<BcCamera>,
name: String,
}
impl MessageHandler {
async fn listen(&mut self) {
while self.app.running(&format!("app:{}", self.name)) {
// 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(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().await.is_err() {
error!("Failed to reboot the camera");
self.abort();
"FAIL".to_string()
} else {
"OK".to_string()
}
impl<'a> MessageHandler<'a> {
async fn listen(&mut self) -> Result<()> {
loop {
tokio::task::yield_now().await;
match self.rx.recv().await {
Some(to_camera) => {
let (replier, message) = match to_camera {
ToCamera::Send(message) => (None, message),
ToCamera::SendAndReply { message, reply } => (Some(reply), message),
};
let mut error = None;
let reply = match message {
Messages::Reboot => {
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();
"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();
"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();
"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();
"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();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::Battery => match self.camera.battery_info().await {
Err(_) => {
error!("Failed to get battery status");
"FAIL".to_string()
}
Ok(battery_info) => {
let bytes_res = yaserde::ser::serialize_with_writer(
&battery_info,
vec![],
&Default::default(),
);
match bytes_res {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => str,
Err(_) => {
error!("Failed to encode battery status");
"FAIL".to_string()
}
},
Err(_) => {
error!("Failed to serialise battery status");
"FAIL".to_string()
}
}
}
},
Messages::PIRQuery => match self.camera.get_pirstate().await {
Err(_) => {
error!("Failed to get pir status");
"FAIL".to_string()
}
Ok(pir_info) => {
let bytes_res = yaserde::ser::serialize_with_writer(
&pir_info,
vec![],
&Default::default(),
);
match bytes_res {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => str,
Err(_) => {
error!("Failed to encode pir status");
"FAIL".to_string()
}
},
Err(_) => {
error!("Failed to serialise pir status");
"FAIL".to_string()
}
}
}
},
Messages::PIROn => {
if self.camera.pir_set(true).await.is_err() {
error!("Failed to turn on the pir");
self.abort();
"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();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::Ptz(direction) => {
let (bc_direction, amount) = match direction {
Direction::Up(amount) => (BcDirection::Up, amount),
Direction::Down(amount) => (BcDirection::Down, amount),
Direction::Left(amount) => (BcDirection::Left, amount),
Direction::Right(amount) => (BcDirection::Right, amount),
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();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
_ => "UNKNOWN COMMAND".to_string(),
};
if let Some(replier) = replier {
let _ = replier.send(reply).await;
}
Messages::StatusLedOn => {
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 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 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 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 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(e) => {
error!("Failed to get battery status: {:?}", e);
"FAIL".to_string()
}
Ok(battery_info) => {
let bytes_res = yaserde::ser::serialize_with_writer(
&battery_info,
vec![],
&Default::default(),
);
match bytes_res {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => str,
Err(_) => {
error!("Failed to encode battery status");
"FAIL".to_string()
}
},
Err(_) => {
error!("Failed to serialise battery status");
"FAIL".to_string()
}
}
}
},
Messages::PIRQuery => match self.camera.get_pirstate().await {
Err(e) => {
error!("Failed to get pir status: {:?}", e);
"FAIL".to_string()
}
Ok(pir_info) => {
let bytes_res = yaserde::ser::serialize_with_writer(
&pir_info,
vec![],
&Default::default(),
);
match bytes_res {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => str,
Err(_) => {
error!("Failed to encode pir status");
"FAIL".to_string()
}
},
Err(_) => {
error!("Failed to serialise pir status");
"FAIL".to_string()
}
}
}
},
Messages::PIROn => {
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 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()
}
}
Messages::Ptz(direction) => {
let (bc_direction, amount) = match direction {
Direction::Up(amount) => (BcDirection::Up, amount),
Direction::Down(amount) => (BcDirection::Down, amount),
Direction::Left(amount) => (BcDirection::Left, amount),
Direction::Right(amount) => (BcDirection::Right, amount),
Direction::In(amount) => (BcDirection::In, amount),
Direction::Out(amount) => (BcDirection::Out, amount),
};
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()
}
}
_ => "UNKNOWN COMMAND".to_string(),
};
if let Some(replier) = replier {
let _ = replier.send(reply).await;
}
Err(TryRecvError::Empty) => (),
Err(TryRecvError::Disconnected) => self.abort(),
if let Some(error) = error {
return Err(anyhow!("{}", error));
}
}
None => {
return Err(anyhow!("Message handller channel dropped"));
}
}
}
}
fn abort(&self) {
self.app.abort(&self.name);
}
}
+217 -193
View File
@@ -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,219 +85,247 @@ 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 _ =
listen_on_camera(camera_config.clone(), &mqtt_config, loop_arc_app.clone())
.await;
let mut wait_for = Duration::from_micros(125);
loop {
tokio::task::yield_now().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 arc_event_cam = Arc::new(EventCam::new(cam_config.clone(), arc_app.clone()));
let arc_mqtt = Mqtt::new(mqtt_config, &cam_config.name, arc_app.clone());
let mut set = tokio::task::JoinSet::new();
// Start listening to camera events
let event_cam = arc_event_cam.clone();
set.spawn(async move {
event_cam.start_listening().await; // Loop forever
event_cam.abort(); // Just to ensure everything aborts
});
let mut event_cam = EventCam::new(cam_config.clone()).await;
let mut mqtt = Mqtt::new(mqtt_config, &cam_config.name).await;
// Start listening to mqtt events
let event_cam = arc_event_cam.clone();
let mqtt = arc_mqtt.clone();
set.spawn(async move {
let _ = mqtt.start().is_err();
event_cam.abort();
});
// Listen on camera messages and post on mqtt
let camera_name = cam_config.name.clone();
let event_cam = arc_event_cam.clone();
let mqtt = arc_mqtt.clone();
let app = arc_app.clone();
set.spawn(async move {
while app.running(&format!("app: {}", camera_name)) {
if let Ok(msg) = event_cam.poll().await {
match msg {
Messages::Login => {
if mqtt.send_message("status", "connected", true).is_err() {
error!("Failed to post connect over MQTT for {}", camera_name);
}
}
Messages::MotionStop => {
if mqtt.send_message("status/motion", "off", true).is_err() {
error!("Failed to publish motion stop for {}", camera_name);
}
}
Messages::MotionStart => {
if mqtt.send_message("status/motion", "on", true).is_err() {
error!("Failed to publish motion start for {}", camera_name);
}
}
_ => {}
}
}
}
});
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 = arc_event_cam.clone();
let mqtt = arc_mqtt.clone();
let app = arc_app.clone();
let camera_name = cam_config.name.clone();
set.spawn(async move {
while app.running(&format!("app: {}", camera_name)) {
if let Ok(msg) = mqtt.poll() {
match msg.as_ref() {
MqttReplyRef {
topic: "control/led",
message: "on",
} => {
if event_cam.send_message(Messages::StatusLedOn).await.is_err() {
error!("Failed to set camera status light on");
}
}
MqttReplyRef {
topic: "control/led",
message: "off",
} => {
if event_cam
.send_message(Messages::StatusLedOff)
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",
} => {
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::StatusLedOn)
.await
.is_err()
{
error!("Failed to set camera status light off");
}
}
MqttReplyRef {
topic: "control/ir",
message: "on",
} => {
if event_cam.send_message(Messages::IRLedOn).await.is_err() {
error!("Failed to set camera status light on");
}
}
MqttReplyRef {
topic: "control/ir",
message: "off",
} => {
if event_cam.send_message(Messages::IRLedOff).await.is_err() {
error!("Failed to set camera status light off");
}
}
MqttReplyRef {
topic: "control/ir",
message: "auto",
} => {
if event_cam.send_message(Messages::IRLedAuto).await.is_err() {
error!("Failed to set camera status light off");
}
}
MqttReplyRef {
topic: "control/reboot",
..
} => {
if event_cam.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
.send_message(Messages::Ptz(direction))
.await
.is_err()
{
error!("Failed to send PTZ");
.with_context(|| "Failed to set camera status light on")?,
);
}
MqttReplyRef {
topic: "control/led",
message: "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",
} => {
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",
} => {
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",
} => {
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",
..
} => {
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",
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");
}
};
reply = Some(
event_cam_sender
.send_message_with_reply(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.send_message(Messages::PIROn).await.is_err() {
error!("Failed to set pir on");
}
}
MqttReplyRef {
topic: "control/pir",
message: "off",
} => {
if event_cam.send_message(Messages::PIROff).await.is_err() {
error!("Failed to set pir off");
}
}
MqttReplyRef {
topic: "query/battery",
..
} => match event_cam.send_message_with_reply(Messages::Battery).await {
Ok(reply) => {
if mqtt.send_message("status/battery", &reply, false).is_err() {
error!("Failed to send battery status reply");
}
}
Err(_) => error!("Failed to get battery status"),
},
MqttReplyRef {
topic: "query/pir", ..
} => match event_cam.send_message_with_reply(Messages::PIRQuery).await {
Ok(reply) => {
if mqtt.send_message("status/pir", &reply, false).is_err() {
error!("Failed to send pir status reply");
}
}
Err(_) => error!("Failed to get pir status"),
},
_ => {}
}
MqttReplyRef {
topic: "control/pir",
message: "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",
} => {
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::PIROff)
.await
.with_context(|| "Failed to set pir off")?,
);
}
MqttReplyRef {
topic: "query/battery",
..
} => {
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", ..
} => {
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(())
};
while set.join_next().await.is_some() {}
let cam_to_mqtt = async {
loop {
tokio::task::yield_now().await;
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)
})?;
}
_ => {}
}
}
};
tokio::select! {
v = mqtt_to_cam => {v},
v = cam_to_mqtt => {v},
}?;
Ok(())
}
+169 -145
View File
@@ -1,23 +1,21 @@
use super::App;
use crate::config::MqttConfig;
use anyhow::{Context, Result};
use crossbeam_channel::{unbounded, Receiver, RecvError, Sender};
use anyhow::{anyhow, Context, Result};
use log::*;
use rumqttc::{
Client, ClientError, ConnectReturnCode, Connection, Event, Incoming, Key, LastWill,
AsyncClient, ClientError, ConnectReturnCode, Event, EventLoop, Incoming, Key, LastWill,
MqttOptions, Publish, QoS, TlsConfiguration, Transport,
};
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc::{channel, Receiver, Sender};
use tokio::task::JoinSet;
pub(crate) struct Mqtt {
app: Arc<App>,
client: Mutex<Client>,
connection: Mutex<Connection>,
client: Arc<AsyncClient>,
name: String,
incoming: (Sender<Publish>, Receiver<Publish>),
msg_channel: (Sender<MqttReply>, Receiver<MqttReply>),
drop_message: Mutex<Option<MqttReply>>,
incoming: Receiver<MqttReply>,
set: JoinSet<Result<()>>,
drop_message: Option<MqttReply>,
}
pub(crate) struct MqttReply {
@@ -39,10 +37,119 @@ pub(crate) struct MqttReplyRef<'a> {
pub(crate) message: &'a str,
}
struct MqttReciever {
client: MqttSender,
incoming_tx: Sender<MqttReply>,
name: String,
}
pub(crate) struct MqttSender {
client: Arc<AsyncClient>,
name: String,
}
impl MqttSender {
pub async fn send_message(
&self,
sub_topic: &str,
message: &str,
retain: bool,
) -> Result<(), ClientError> {
self.client
.publish(
format!("neolink/{}/{}", self.name, sub_topic),
QoS::AtLeastOnce,
retain,
message,
)
.await?;
Ok(())
}
async fn subscribe(&self) -> Result<(), ClientError> {
self.client
.subscribe(format!("neolink/{}/#", self.name), QoS::AtMostOnce)
.await?;
Ok(())
}
async fn update_status(&self) -> Result<()> {
self.send_message("status", "disconnected", true).await?;
Ok(())
}
pub fn try_send_message(&self, sub_topic: &str, message: &str, retain: bool) -> Result<()> {
self.client
.try_publish(
format!("neolink/{}/{}", self.name, sub_topic),
QoS::AtLeastOnce,
retain,
message,
)
.map_err(|e| e.into())
}
}
impl MqttReciever {
async fn run(&mut self, connection: &mut EventLoop) -> Result<()> {
// This acts as an event loop
let name = self.name.clone();
info!("Starting MQTT Client for {}", name);
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");
}
}
_ => {}
}
}
}
async fn handle_message(&mut self, published_message: Publish) -> Result<()> {
if let Some(sub_topic) = published_message
.topic
.strip_prefix(&format!("neolink/{}/", &self.name))
{
if self
.incoming_tx
.send(MqttReply {
topic: sub_topic.to_string(),
message: String::from_utf8_lossy(published_message.payload.as_ref())
.into_owned(),
})
.await
.is_err()
{
error!("Failed to send messages up the mqtt msg channel");
}
}
Ok(())
}
}
impl Mqtt {
pub(crate) fn new(config: &MqttConfig, name: &str, app: Arc<App>) -> Arc<Self> {
let incoming = unbounded::<Publish>();
let msg_channel = unbounded::<MqttReply>();
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),
&config.broker_addr,
@@ -91,162 +198,79 @@ impl Mqtt {
true,
));
let (client, connection) = Client::new(mqttoptions, 10);
let (client, mut connection) = AsyncClient::new(mqttoptions, 10);
let me = Self {
app: app.clone(),
client: Mutex::new(client),
connection: Mutex::new(connection),
name: name.to_string(),
incoming,
msg_channel,
// Send the drop message on clean disconnect
drop_message: Mutex::new(Some(MqttReply {
topic: "status".to_string(),
message: "offline".to_string(),
})),
};
let arc_me = Arc::new(me);
let client = Arc::new(client);
// Start the mqtt server
let mqtt_running = arc_me.clone();
let mqtt_app = app.clone();
let mqtt_name = name.to_string();
std::thread::spawn(move || {
let _ = (*mqtt_running).start();
mqtt_app.abort(&mqtt_name)
});
let mut set = JoinSet::<Result<()>>::new();
// Start polling messages
info!("{}: Starting listening to mqtt", name);
let mqtt_read_app = app;
let mqtt_reading = arc_me.clone();
let mqtt_read_name = name.to_string();
std::thread::spawn(move || {
while mqtt_read_app.running(&format!("app:{}", mqtt_read_name)) {
if (*mqtt_reading).handle_message().is_err() {
error!(
"Failed to get messages from mqtt client {}",
(mqtt_read_name)
);
}
}
});
let mut reciever = MqttReciever {
incoming_tx,
name: name.to_string(),
client: MqttSender {
client: client.clone(),
name: name.to_string(),
},
};
set.spawn(async move { reciever.run(&mut connection).await });
arc_me
}
#[allow(unused)]
pub(crate) fn set_drop_message(&mut self, topic: &str, message: &str) {
self.drop_message.lock().unwrap().replace(MqttReply {
topic: topic.to_string(),
message: message.to_string(),
});
}
fn subscribe(&self) -> Result<(), ClientError> {
let mut client = self.client.lock().unwrap();
client.subscribe(format!("neolink/{}/#", self.name), QoS::AtMostOnce)?;
Ok(())
}
fn update_status(&self) -> Result<(), ClientError> {
self.send_message("status", "disconnected", true)?;
Ok(())
}
pub fn send_message(
&self,
sub_topic: &str,
message: &str,
retain: bool,
) -> Result<(), ClientError> {
let mut client = self.client.lock().unwrap();
client.publish(
format!("neolink/{}/{}", self.name, sub_topic),
QoS::AtLeastOnce,
retain,
message,
)?;
Ok(())
}
fn handle_message(&self) -> Result<(), RecvError> {
let (_, receiver) = &self.incoming;
let published_message = receiver.recv()?;
if let Some(sub_topic) = published_message
.topic
.strip_prefix(&format!("neolink/{}/", &self.name))
{
if self
.msg_channel
.0
.send(MqttReply {
topic: sub_topic.to_string(),
message: String::from_utf8_lossy(published_message.payload.as_ref())
.into_owned(),
})
.is_err()
{
error!("Failed to send messages up the mqtt msg channel");
}
Self {
client,
name: name.to_string(),
incoming,
set,
// Send the drop message on clean disconnect
drop_message: Some(MqttReply {
topic: "status".to_string(),
message: "offline".to_string(),
}),
}
Ok(())
}
pub fn get_message_listener(&self) -> Receiver<MqttReply> {
self.msg_channel.1.clone()
pub fn get_sender(&self) -> MqttSender {
MqttSender {
client: self.client.clone(),
name: self.name.to_string(),
}
}
pub(crate) fn poll(&self) -> Result<MqttReply> {
self.get_message_listener()
.recv()
.context("Mqtt Polling error")
}
pub(crate) fn start(&self) -> Result<()> {
// This acts as an event loop
let mut connection = self.connection.lock().unwrap();
let (sender, _) = &self.incoming;
info!("Starting MQTT Client for {}", self.name);
while self.app.running(&format!("app:{}", self.name)) {
for (_i, notification) in connection.iter().enumerate() {
if let Ok(notification) = notification {
match notification {
Event::Incoming(Incoming::ConnAck(connected)) => {
if ConnectReturnCode::Success == connected.code {
self.update_status().context("Failed to update status")?;
// We succesfully logged in. Now ask for the cameras subscription.
self.subscribe().context("Failed to subscribe")?;
}
/// This will also error is the join set errors
pub(crate) async fn poll(&mut self) -> Result<MqttReply> {
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());
}
Event::Incoming(Incoming::Publish(published_message)) => {
if sender.send(published_message).is_err() {
error!("Failed to publish motion message on mqtt");
}
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"),
}
Ok(())
}
}
impl Drop for Mqtt {
fn drop(&mut self) {
let drop_message = self.drop_message.lock().unwrap();
if let Some(drop_message) = drop_message.as_ref() {
if let Some(drop_message) = self.drop_message.as_ref() {
if self
.send_message(&drop_message.topic, &drop_message.message, true)
.get_sender()
.try_send_message(&drop_message.topic, &drop_message.message, true)
.is_err()
{
error!("Failed to send offline message to mqtt");
}
}
self.set.abort_all();
}
}
+1
View File
@@ -141,6 +141,7 @@ impl Default for NeoMediaFactoryImpl {
};
threads.spawn(async move {
loop {
tokio::task::yield_now().await;
match sender.run().await {
Err(e) => {
warn!("Media send thead failed... restarting. Cause: {:?}", e);
+1
View File
@@ -97,6 +97,7 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> {
let name = camera.get_name();
let mut backoff = Duration::from_micros(125);
loop {
tokio::task::yield_now().await;
let failure = camera_main(camera).await;
match failure {
Err(CameraFailureKind::Fatal(e)) => {
+1
View File
@@ -52,6 +52,7 @@ impl Camera<Streaming> {
.await?;
set.spawn(async move {
loop {
tokio::task::yield_now().await;
// debug!("Straming: Get");
let data = timeout(Duration::from_secs(15), stream_data.get_data()).await??;
// debug!("Straming: Got");