From c19bba9235386cbf9183039e87ea4324df919c02 Mon Sep 17 00:00:00 2001 From: Kevin David Date: Sun, 4 Dec 2022 18:03:29 -0500 Subject: [PATCH] Improve MQTT documentation, example config Also print an error message when the configuration isn't present but the `mqtt` subcommand is used. Without this, the program just exits without any messsages. --- .vscode/launch.json | 18 ++++++++++++++++++ sample_config.toml | 4 ++++ src/mqtt/cmdline.rs | 1 - src/mqtt/mod.rs | 15 ++++++++++----- 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..945cf95 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug", + "program": "${workspaceRoot}/target/debug/${workspaceRootFolderName}", + "args": ["mqtt", "--config", "target/config.toml"], + "cwd": "${workspaceRoot}", + "sourceLanguages": [ + "rust" + ], + "env": { + "RUST_LOG": "debug" + } + } + ]} \ No newline at end of file diff --git a/sample_config.toml b/sample_config.toml index 14a572d..c6193f2 100644 --- a/sample_config.toml +++ b/sample_config.toml @@ -35,6 +35,10 @@ name = "driveway" username = "admin" password = "12345678" address = "192.168.1.187:9000" +# mqtt.broker_addr = "192.168.1.122" +# mqtt.port = 1883 +# mqtt.credentials = ["mqtt_user", "mqtt_password"] + # If you use a battery camera: **Instead** of an `address` supply the uid # as follows # uid = "ABCD01234567890EFG" diff --git a/src/mqtt/cmdline.rs b/src/mqtt/cmdline.rs index b4bc3dd..aba365d 100644 --- a/src/mqtt/cmdline.rs +++ b/src/mqtt/cmdline.rs @@ -1,5 +1,4 @@ use structopt::StructOpt; -/// The reboot command will reboot the camera #[derive(StructOpt, Debug)] pub struct Opt {} diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 453e491..67e1339 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -1,15 +1,13 @@ use log::*; /// -/// # Neolink Reboot +/// # Neolink MQTT /// -/// This module handles the reboot subcommand -/// -/// The subcommand attepts to reboot the camera. +/// Handles incoming and outgoing MQTT messages /// /// # Usage /// /// ```bash -/// neolink reboot --config=config.toml CameraName +/// neolink mqtt --config=config.toml /// ``` /// use std::sync::Arc; @@ -34,11 +32,14 @@ pub(crate) fn main(_: Opt, config: Config) -> Result<()> { let app = App::new(); let arc_app = Arc::new(app); + let mut mqtt_count: u8 = 0; + 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); + mqtt_count = mqtt_count + 1; s.spawn(move |_| { while loop_arc_app.running("app") { let _ = listen_on_camera(camera_config, mqtt_config, loop_arc_app.clone()); @@ -48,6 +49,10 @@ pub(crate) fn main(_: Opt, config: Config) -> Result<()> { } }); + if mqtt_count == 0 { + error!("MQTT command run, but no cameras configured with MQTT settings. Exiting."); + } + Ok(()) }