Merge branch 'mqtt_cmd'
This commit is contained in:
14 files changed
+1343
-288
No files matched your search
@@ -94,6 +94,7 @@ jobs:
|
||||
apt-get install --assume-yes --no-install-recommends \
|
||||
build-essential \
|
||||
gcc-${{ matrix.gcc }} \
|
||||
g++-${{ matrix.gcc }} \
|
||||
libgstrtspserver-1.0-dev:${{ matrix.arch }} \
|
||||
libgstreamer1.0-dev:${{ matrix.arch }} \
|
||||
libgtk2.0-dev:${{ matrix.arch }} \
|
||||
|
||||
Generated
+562
-287
File diff suppressed because it is too large.
Load diff
@@ -39,4 +39,7 @@ xml-rs = "0.8"
|
||||
validator = "0.10"
|
||||
validator_derive = "0.10"
|
||||
byte-slice-cast = "1.0.0"
|
||||
rumqttc = "0.8.0"
|
||||
tokio = { version = "1.0", features = ["rt", "macros", "io-util"] }
|
||||
crossbeam-channel = "0.5.1"
|
||||
anyhow = "1.0.42"
|
||||
@@ -27,6 +27,7 @@ pub(crate) use connection::*;
|
||||
pub use errors::Error;
|
||||
pub use ledstate::LightState;
|
||||
pub use pirstate::PirState;
|
||||
pub use motion::{MotionOutput, MotionOutputError, MotionStatus};
|
||||
pub use resolution::*;
|
||||
pub use motion::{MotionOutput, MotionOutputError, MotionStatus};
|
||||
pub use stream::{Stream, StreamOutput, StreamOutputError};
|
||||
|
||||
@@ -22,4 +22,5 @@ pub enum Command {
|
||||
Reboot(super::reboot::Opt),
|
||||
Pir(super::pir::Opt),
|
||||
Talk(super::talk::Opt),
|
||||
Mqtt(super::mqtt::Opt),
|
||||
}
|
||||
@@ -73,6 +73,10 @@ pub(crate) struct CameraConfig {
|
||||
#[validate(range(min = 0, max = 31, message = "Invalid channel", code = "channel_id"))]
|
||||
#[serde(default = "default_channel_id")]
|
||||
pub(crate) channel_id: u8,
|
||||
|
||||
#[validate]
|
||||
#[serde(default = "default_mqtt")]
|
||||
pub(crate) mqtt: Option<MqttConfig>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
@@ -85,6 +89,38 @@ pub(crate) struct UserConfig {
|
||||
pub(crate) pass: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone, Validate)]
|
||||
#[validate(schema(function = "validate_mqtt_config", skip_on_field_errors = true))]
|
||||
pub(crate) struct MqttConfig {
|
||||
#[serde(alias = "server")]
|
||||
pub(crate) broker_addr: String,
|
||||
|
||||
pub(crate) port: u16,
|
||||
|
||||
#[serde(default)]
|
||||
pub(crate) credentials: Option<(String, String)>,
|
||||
|
||||
#[serde(default)]
|
||||
pub(crate) ca: Option<std::path::PathBuf>,
|
||||
|
||||
#[serde(default)]
|
||||
pub(crate) client_auth: Option<(std::path::PathBuf, std::path::PathBuf)>,
|
||||
}
|
||||
|
||||
fn validate_mqtt_config(config: &MqttConfig) -> Result<(), ValidationError> {
|
||||
if config.ca.is_some() && config.client_auth.is_some() {
|
||||
Err(ValidationError::new(
|
||||
"Cannot have both ca and client_auth set",
|
||||
))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn default_mqtt() -> Option<MqttConfig> {
|
||||
None
|
||||
}
|
||||
|
||||
fn default_bind_addr() -> String {
|
||||
"0.0.0.0".to_string()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
use err_derive::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
pub enum Error {
|
||||
#[error(display = "RTSP Error")]
|
||||
Rtsp(#[error(source)] super::rtsp::Error),
|
||||
#[error(display = "Status LED Error")]
|
||||
StatusLight(#[error(source)] super::statusled::Error),
|
||||
#[error(display = "Reboot Error")]
|
||||
Reboot(#[error(source)] super::reboot::Error),
|
||||
#[error(display = "Talk Error")]
|
||||
Talk(#[error(source)] super::talk::Error),
|
||||
#[error(display = "Talk Error")]
|
||||
Mqtt(#[error(source)] super::mqtt::Error),
|
||||
}
|
||||
@@ -17,6 +17,7 @@ use validator::Validate;
|
||||
mod cmdline;
|
||||
mod config;
|
||||
mod pir;
|
||||
mod mqtt;
|
||||
mod reboot;
|
||||
mod rtsp;
|
||||
mod statusled;
|
||||
@@ -71,6 +72,9 @@ fn main() -> Result<()> {
|
||||
Some(Command::Talk(opts)) => {
|
||||
talk::main(opts, config)?;
|
||||
}
|
||||
Some(Command::Mqtt(opts)) => {
|
||||
mqtt::main(opts, config)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
use std::collections::HashSet;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
// Handles app wide things like aborting all connections
|
||||
pub(crate) struct App {
|
||||
aborters: Arc<Mutex<HashSet<String>>>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {
|
||||
aborters: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
// Abort at a level
|
||||
// e.g. about camera_event_loop
|
||||
pub(crate) fn abort(&self, level: &str) {
|
||||
let mut aborters = self.aborters.lock().unwrap();
|
||||
aborters.insert(level.to_string());
|
||||
}
|
||||
|
||||
// Checks if any level has aborted
|
||||
// e.g. app:camera_a::camera_event_loop
|
||||
// will check if app, camera_a or camera_event_loop has been aborted
|
||||
pub(crate) fn running(&self, levels: &str) -> bool {
|
||||
let aborters = self.aborters.lock().unwrap();
|
||||
for level in levels.split(':') {
|
||||
if aborters.contains(&level.to_string()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
|
||||
/// The reboot command will reboot the camera
|
||||
#[derive(StructOpt, Debug)]
|
||||
pub struct Opt {
|
||||
/// The path to the config file
|
||||
#[structopt(short, long, parse(from_os_str))]
|
||||
pub config: PathBuf,
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
use super::App;
|
||||
use crate::config::CameraConfig;
|
||||
use crate::utils::AddressOrUid;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use crossbeam_channel::{unbounded, Receiver, Sender, TryRecvError};
|
||||
use log::*;
|
||||
use neolink_core::bc_protocol::{
|
||||
BcCamera, Error as BcError, LightState, MotionOutput, MotionOutputError, MotionStatus,
|
||||
};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
pub(crate) enum Messages {
|
||||
None,
|
||||
Login,
|
||||
MotionStop,
|
||||
MotionStart,
|
||||
Reboot,
|
||||
StatusLedOn,
|
||||
StatusLedOff,
|
||||
IRLedOn,
|
||||
IRLedOff,
|
||||
IRLedAuto,
|
||||
}
|
||||
|
||||
pub(crate) struct EventCam<'a> {
|
||||
config: &'a CameraConfig,
|
||||
app: Arc<App>,
|
||||
channel_out: Arc<Mutex<Option<Receiver<Messages>>>>,
|
||||
channel_in: Arc<Mutex<Option<Sender<Messages>>>>,
|
||||
}
|
||||
|
||||
impl<'a> EventCam<'a> {
|
||||
pub(crate) fn new(config: &'a CameraConfig, app: Arc<App>) -> Self {
|
||||
Self {
|
||||
config,
|
||||
app,
|
||||
channel_out: Arc::new(Mutex::new(None)),
|
||||
channel_in: Arc::new(Mutex::new(None)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) 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().context("Camera failed to poll")
|
||||
} else {
|
||||
Ok(Messages::None)
|
||||
}
|
||||
} else {
|
||||
Ok(Messages::None)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn send_message(&self, msg: Messages) -> Result<()> {
|
||||
if let Ok(ref mut channel_in) = self.channel_in.lock() {
|
||||
if let Some(channel_in) = channel_in.as_mut() {
|
||||
channel_in
|
||||
.send(msg)
|
||||
.context("Failed to send message from camera")
|
||||
} 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);
|
||||
}
|
||||
|
||||
pub(crate) fn start_listening(&self) {
|
||||
let loop_config = self.config;
|
||||
|
||||
// Channels from the camera
|
||||
let (tx, rx) = unbounded();
|
||||
let loop_tx = Arc::new(Mutex::new(tx));
|
||||
*self.channel_out.lock().unwrap() = Some(rx);
|
||||
|
||||
// Channels to the camera
|
||||
let (stx, srx) = unbounded();
|
||||
let loop_rx = Arc::new(Mutex::new(srx));
|
||||
*self.channel_in.lock().unwrap() = 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(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn cam_run(
|
||||
camera_config: &CameraConfig,
|
||||
tx: Arc<Mutex<Sender<Messages>>>,
|
||||
rx: Arc<Mutex<Receiver<Messages>>>,
|
||||
app: Arc<App>,
|
||||
) -> Result<()> {
|
||||
let camera_addr =
|
||||
AddressOrUid::new(&camera_config.camera_addr, &camera_config.camera_uid).unwrap();
|
||||
|
||||
info!(
|
||||
"{}: Connecting to camera at {}",
|
||||
camera_config.name, camera_addr
|
||||
);
|
||||
let mut camera = camera_addr
|
||||
.connect_camera(camera_config.channel_id)
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Failed to connect to camera {} at {} on channel {}",
|
||||
camera_config.name, camera_addr, camera_config.channel_id
|
||||
)
|
||||
})?;
|
||||
|
||||
info!("{}: Logging in", camera_config.name);
|
||||
camera
|
||||
.login(&camera_config.username, camera_config.password.as_deref())
|
||||
.context("Failed to login to the camera")?;
|
||||
info!("{}: Connected and logged in", camera_config.name);
|
||||
|
||||
(tx.lock().map_err(|_| anyhow!("Failed to lock"))?).send(Messages::Login)?;
|
||||
|
||||
// Shararble cameras
|
||||
let arc_cam = Arc::new(camera);
|
||||
let cam_motion = arc_cam.clone();
|
||||
let cam_mesg = arc_cam;
|
||||
|
||||
let mut motion_callback = MotionCallback {
|
||||
app: app.clone(),
|
||||
tx,
|
||||
name: camera_config.name.to_string(),
|
||||
};
|
||||
|
||||
let mut message_handler = MessageHandler {
|
||||
app: app.clone(),
|
||||
rx,
|
||||
camera: cam_mesg,
|
||||
name: camera_config.name.to_string(),
|
||||
};
|
||||
|
||||
let _ = crossbeam::scope(|s| {
|
||||
info!("{}: Listening to Camera Motion", camera_config.name);
|
||||
s.spawn(|_| {
|
||||
let _ = cam_motion.listen_on_motion(&mut motion_callback);
|
||||
// If listen_on_motion returns then camera disconnect
|
||||
app.abort(&camera_config.name);
|
||||
});
|
||||
|
||||
info!("{}: Setting up camera actions", camera_config.name);
|
||||
s.spawn(|_| {
|
||||
let _ = message_handler.listen();
|
||||
});
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct MotionCallback {
|
||||
app: Arc<App>,
|
||||
tx: Arc<Mutex<Sender<Messages>>>,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl MotionOutput for MotionCallback {
|
||||
fn motion_recv(&mut self, motion_status: MotionStatus) -> MotionOutputError {
|
||||
if self.app.running(&format!("app:{}", self.name)) {
|
||||
match motion_status {
|
||||
MotionStatus::Start => {
|
||||
(self
|
||||
.tx
|
||||
.lock()
|
||||
.map_err(|_| BcError::Other("Failed to lock mutex"))?)
|
||||
.send(Messages::MotionStart)
|
||||
.map_err(|_| BcError::Other("Failed to send Message"))?;
|
||||
}
|
||||
MotionStatus::Stop => {
|
||||
(self
|
||||
.tx
|
||||
.lock()
|
||||
.map_err(|_| BcError::Other("Failed to lock mutex"))?)
|
||||
.send(Messages::MotionStop)
|
||||
.map_err(|_| BcError::Other("Failed to send Message"))?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct MessageHandler {
|
||||
app: Arc<App>,
|
||||
rx: Arc<Mutex<Receiver<Messages>>>,
|
||||
camera: Arc<BcCamera>,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl MessageHandler {
|
||||
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(message) => match message {
|
||||
Messages::Reboot => {
|
||||
if self.camera.reboot().is_err() {
|
||||
error!("Failed to reboot the camera");
|
||||
self.abort()
|
||||
}
|
||||
}
|
||||
Messages::StatusLedOn => {
|
||||
if self.camera.led_light_set(true).is_err() {
|
||||
error!("Failed to turn on 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();
|
||||
}
|
||||
}
|
||||
Messages::IRLedOn => {
|
||||
if self.camera.irled_light_set(LightState::On).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();
|
||||
}
|
||||
}
|
||||
Messages::IRLedAuto => {
|
||||
if self.camera.irled_light_set(LightState::Auto).is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Err(TryRecvError::Empty) => (),
|
||||
Err(TryRecvError::Disconnected) => self.abort(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn abort(&self) {
|
||||
self.app.abort(&self.name);
|
||||
}
|
||||
}
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
use log::*;
|
||||
///
|
||||
/// # Neolink Reboot
|
||||
///
|
||||
/// This module handles the reboot subcommand
|
||||
///
|
||||
/// The subcommand attepts to reboot the camera.
|
||||
///
|
||||
/// # Usage
|
||||
///
|
||||
/// ```bash
|
||||
/// neolink reboot --config=config.toml CameraName
|
||||
/// ```
|
||||
///
|
||||
use std::sync::Arc;
|
||||
|
||||
mod app;
|
||||
mod cmdline;
|
||||
mod event_cam;
|
||||
mod mqttc;
|
||||
|
||||
use crate::config::{CameraConfig, Config, MqttConfig};
|
||||
use anyhow::Result;
|
||||
pub(crate) use app::App;
|
||||
pub(crate) use cmdline::Opt;
|
||||
use event_cam::EventCam;
|
||||
pub(crate) use event_cam::Messages;
|
||||
use mqttc::{Mqtt, MqttReplyRef};
|
||||
|
||||
/// Entry point for the reboot subcommand
|
||||
///
|
||||
/// Opt is the command line options
|
||||
pub(crate) fn main(_: Opt, config: Config) -> Result<()> {
|
||||
let app = App::new();
|
||||
let arc_app = Arc::new(app);
|
||||
|
||||
let _ = crossbeam::scope(|s| {
|
||||
for camera_config in &config.cameras {
|
||||
if let Some(mqtt_config) = camera_config.mqtt.as_ref() {
|
||||
let loop_arc_app = arc_app.clone();
|
||||
info!("{}: Setting up mqtt", camera_config.name);
|
||||
s.spawn(move |_| {
|
||||
while loop_arc_app.running("app") {
|
||||
let _ = listen_on_camera(camera_config, mqtt_config, loop_arc_app.clone());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn listen_on_camera(
|
||||
cam_config: &CameraConfig,
|
||||
mqtt_config: &MqttConfig,
|
||||
app: Arc<App>,
|
||||
) -> Result<()> {
|
||||
// Camera thread
|
||||
let event_cam = EventCam::new(cam_config, app.clone());
|
||||
let mqtt = Mqtt::new(mqtt_config, &cam_config.name, app.clone());
|
||||
|
||||
let _ = crossbeam::scope(|s| {
|
||||
// Start listening to camera events
|
||||
s.spawn(|_| {
|
||||
event_cam.start_listening(); // Loop forever
|
||||
event_cam.abort(); // Just to ensure everything aborts
|
||||
});
|
||||
|
||||
// Start listening to mqtt events
|
||||
s.spawn(|_| {
|
||||
let _ = mqtt.start().is_err();
|
||||
event_cam.abort();
|
||||
});
|
||||
|
||||
// Listen on camera messages and post on mqtt
|
||||
s.spawn(|_| {
|
||||
while app.running(&format!("app: {}", cam_config.name)) {
|
||||
if let Ok(msg) = event_cam.poll() {
|
||||
match msg {
|
||||
Messages::Login => {
|
||||
if mqtt.send_message("status", "connected", true).is_err() {
|
||||
error!("Failed to post connect over MQTT for {}", cam_config.name);
|
||||
}
|
||||
}
|
||||
Messages::MotionStop => {
|
||||
if mqtt.send_message("status/motion", "off", true).is_err() {
|
||||
error!("Failed to publish motion stop for {}", cam_config.name);
|
||||
}
|
||||
}
|
||||
Messages::MotionStart => {
|
||||
if mqtt.send_message("status/motion", "on", true).is_err() {
|
||||
error!("Failed to publish motion start for {}", cam_config.name);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Listen on mqtt messages and post on camera
|
||||
s.spawn(|_| {
|
||||
while app.running(&format!("app: {}", cam_config.name)) {
|
||||
if let Ok(msg) = mqtt.poll() {
|
||||
match msg.as_ref() {
|
||||
MqttReplyRef {
|
||||
topic: "control/led",
|
||||
message: "on",
|
||||
} => {
|
||||
if event_cam.send_message(Messages::StatusLedOn).is_err() {
|
||||
error!("Failed to set camera status light on");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/led",
|
||||
message: "off",
|
||||
} => {
|
||||
if event_cam.send_message(Messages::StatusLedOff).is_err() {
|
||||
error!("Failed to set camera status light off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ir",
|
||||
message: "on",
|
||||
} => {
|
||||
if event_cam.send_message(Messages::IRLedOn).is_err() {
|
||||
error!("Failed to set camera status light off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ir",
|
||||
message: "off",
|
||||
} => {
|
||||
if event_cam.send_message(Messages::IRLedOff).is_err() {
|
||||
error!("Failed to set camera status light off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ir",
|
||||
message: "auto",
|
||||
} => {
|
||||
if event_cam.send_message(Messages::IRLedAuto).is_err() {
|
||||
error!("Failed to set camera status light off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/reboot",
|
||||
..
|
||||
} => {
|
||||
if event_cam.send_message(Messages::Reboot).is_err() {
|
||||
error!("Failed to set camera status light off");
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
use super::App;
|
||||
use crate::config::MqttConfig;
|
||||
use anyhow::{Context, Result};
|
||||
use crossbeam_channel::{unbounded, Receiver, RecvError, Sender};
|
||||
use log::*;
|
||||
use rumqttc::{
|
||||
Client, ClientError, ConnectReturnCode, Connection, Event, Incoming, Key, LastWill,
|
||||
MqttOptions, Publish, QoS, TlsConfiguration, Transport,
|
||||
};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
pub(crate) struct Mqtt {
|
||||
app: Arc<App>,
|
||||
client: Mutex<Client>,
|
||||
connection: Mutex<Connection>,
|
||||
name: String,
|
||||
incoming: (Sender<Publish>, Receiver<Publish>),
|
||||
msg_channel: (Sender<MqttReply>, Receiver<MqttReply>),
|
||||
drop_message: Mutex<Option<MqttReply>>,
|
||||
}
|
||||
|
||||
pub(crate) struct MqttReply {
|
||||
pub(crate) topic: String,
|
||||
pub(crate) message: String,
|
||||
}
|
||||
|
||||
impl MqttReply {
|
||||
pub(crate) fn as_ref(&self) -> MqttReplyRef {
|
||||
MqttReplyRef {
|
||||
topic: &self.topic,
|
||||
message: &self.message,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct MqttReplyRef<'a> {
|
||||
pub(crate) topic: &'a str,
|
||||
pub(crate) message: &'a str,
|
||||
}
|
||||
|
||||
impl Mqtt {
|
||||
pub(crate) fn new(config: &MqttConfig, name: &str, app: Arc<App>) -> Arc<Self> {
|
||||
let incoming = unbounded::<Publish>();
|
||||
let msg_channel = unbounded::<MqttReply>();
|
||||
let mut mqttoptions = MqttOptions::new(
|
||||
format!("Neolink-{}", name),
|
||||
&config.broker_addr,
|
||||
config.port,
|
||||
);
|
||||
|
||||
// Use TLS if ca path is set
|
||||
if let Some(ca_path) = &config.ca {
|
||||
if let Ok(ca) = std::fs::read(ca_path) {
|
||||
// Use client_auth if they have cert and key
|
||||
let client_auth = if let Some((cert_path, key_path)) = &config.client_auth {
|
||||
if let (Ok(cert_buf), Ok(key_buf)) =
|
||||
(std::fs::read(cert_path), std::fs::read(key_path))
|
||||
{
|
||||
Some((cert_buf, Key::RSA(key_buf)))
|
||||
} else {
|
||||
error!("Failed to set client tls");
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let transport = Transport::Tls(TlsConfiguration::Simple {
|
||||
ca,
|
||||
alpn: None,
|
||||
client_auth,
|
||||
});
|
||||
mqttoptions.set_transport(transport);
|
||||
} else {
|
||||
error!("Failed to set CA");
|
||||
}
|
||||
};
|
||||
|
||||
if let Some((username, password)) = &config.credentials {
|
||||
mqttoptions.set_credentials(username, password);
|
||||
}
|
||||
|
||||
mqttoptions.set_keep_alive(5);
|
||||
|
||||
// On unclean disconnect send this
|
||||
mqttoptions.set_last_will(LastWill::new(
|
||||
format!("neolink/{}/status", name),
|
||||
"offline",
|
||||
QoS::AtLeastOnce,
|
||||
true,
|
||||
));
|
||||
|
||||
let (client, connection) = Client::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);
|
||||
|
||||
// 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)
|
||||
});
|
||||
|
||||
// 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)
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_message_listener(&self) -> Receiver<MqttReply> {
|
||||
self.msg_channel.1.clone()
|
||||
}
|
||||
|
||||
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")?;
|
||||
}
|
||||
}
|
||||
Event::Incoming(Incoming::Publish(published_message)) => {
|
||||
if sender.send(published_message).is_err() {
|
||||
error!("Failed to publish motion message on mqtt");
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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 self
|
||||
.send_message(&drop_message.topic, &drop_message.message, true)
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to send offline message to mqtt");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ pub(crate) use cmdline::Opt;
|
||||
///
|
||||
/// Opt is the command line options
|
||||
pub(crate) fn main(opt: Opt, config: Config) -> Result<()> {
|
||||
let mut camera = find_and_connect(&config, &opt.camera)?;
|
||||
let camera = find_and_connect(&config, &opt.camera)?;
|
||||
|
||||
camera
|
||||
.led_light_set(opt.on)
|
||||
|
||||
Reference in new issue
Block a user