diff --git a/src/cmdline.rs b/src/cmdline.rs index 88879db..d21b213 100644 --- a/src/cmdline.rs +++ b/src/cmdline.rs @@ -11,4 +11,5 @@ pub struct Opt { #[derive(StructOpt, Debug)] pub enum Command { Rtsp(super::rtsp::Opt), + StatusLight(super::statusled::Opt), } diff --git a/src/errors.rs b/src/errors.rs index a864978..2220c4e 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -4,5 +4,7 @@ use err_derive::Error; #[allow(clippy::large_enum_variant)] pub enum Error { #[error(display = "RTSP Error")] - RtspError(#[error(source)] super::rtsp::Error), + Rtsp(#[error(source)] super::rtsp::Error), + #[error(display = "Status LED Error")] + StatusLight(#[error(source)] super::statusled::Error), } diff --git a/src/main.rs b/src/main.rs index b87b979..8b9ebdc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ use structopt::StructOpt; mod cmdline; mod errors; mod rtsp; +mod statusled; use cmdline::{Command, Opt}; use errors::Error; @@ -33,6 +34,9 @@ fn main() -> Result<(), Error> { Command::Rtsp(opts) => { rtsp::main(opts)?; } + Command::StatusLight(opts) => { + statusled::main(opts)?; + } } Ok(()) diff --git a/src/rtsp/cmdline.rs b/src/rtsp/cmdline.rs index 4e9a3dd..2a7092b 100644 --- a/src/rtsp/cmdline.rs +++ b/src/rtsp/cmdline.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use structopt::StructOpt; -/// The command line for the rtsp subcommand +/// The rtsp command will serve all cameras in the config over the rtsp protocol #[derive(StructOpt, Debug)] pub struct Opt { /// The path to the config file diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index ae42952..1e47998 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -1,7 +1,7 @@ /// /// # Neolink RTSP /// -/// This crate serves as the rtsp source for the +/// This module serves the rtsp streams for the /// `neolink rtsp` subcommand /// use gio::TlsAuthenticationMode; diff --git a/src/statusled/cmdline.rs b/src/statusled/cmdline.rs new file mode 100644 index 0000000..20d7827 --- /dev/null +++ b/src/statusled/cmdline.rs @@ -0,0 +1,24 @@ +use super::errors::ParseOnOffError; +use std::path::PathBuf; +use structopt::StructOpt; + +fn onoff_parse(src: &str) -> Result { + match src { + "true" | "on" | "yes" => Ok(true), + "false" | "off" | "no" => Ok(false), + _ => Err(ParseOnOffError), + } +} + +/// The status-light command will control the blue status light on the camera +#[derive(StructOpt, Debug)] +pub struct Opt { + /// The path to the config file + #[structopt(short, long, parse(from_os_str))] + pub config: PathBuf, + /// The name of the camera to change the lights of. Must be a name in the config + pub camera: String, + /// Whether to turn the light on or off + #[structopt(parse(try_from_str = onoff_parse), name = "on|off")] + pub on: bool, +} diff --git a/src/statusled/config.rs b/src/statusled/config.rs new file mode 100644 index 0000000..de60ef2 --- /dev/null +++ b/src/statusled/config.rs @@ -0,0 +1,29 @@ +use serde::Deserialize; +use std::clone::Clone; +use validator::Validate; +use validator_derive::Validate; + +#[derive(Debug, Deserialize, Validate, Clone)] +pub(crate) struct Config { + #[validate] + pub(crate) cameras: Vec, +} + +#[derive(Debug, Deserialize, Validate, Clone)] +pub(crate) struct CameraConfig { + pub(crate) name: String, + + #[serde(rename = "address")] + pub(crate) camera_addr: String, + + pub(crate) username: String, + pub(crate) password: Option, + + #[validate(range(min = 0, max = 31, message = "Invalid channel", code = "channel_id"))] + #[serde(default = "default_channel_id")] + pub(crate) channel_id: u8, +} + +fn default_channel_id() -> u8 { + 0 +} diff --git a/src/statusled/errors.rs b/src/statusled/errors.rs new file mode 100644 index 0000000..bd2f2a5 --- /dev/null +++ b/src/statusled/errors.rs @@ -0,0 +1,24 @@ +use err_derive::Error; + +/// The main error for the rtsp subcommand +#[derive(Debug, Error)] +#[allow(clippy::large_enum_variant)] +pub enum Error { + /// Raised when the config file fails to deserlize + #[error(display = "Configuration parsing error")] + Config(#[error(source)] toml::de::Error), + /// Raised when `neolink_core` raises an error + #[error(display = "Communication error")] + Protocol(#[error(source)] neolink_core::Error), + /// Raised when there is an IO error such as unable to find + /// config file + #[error(display = "I/O error")] + Io(#[error(source)] std::io::Error), + /// Raised when the config file fails validataion + #[error(display = "Validation error")] + Validation(#[error(source)] validator::ValidationErrors), +} + +#[derive(Error, Debug)] +#[error(display = "Failed to parse on/off flag")] +pub(super) struct ParseOnOffError; diff --git a/src/statusled/mod.rs b/src/statusled/mod.rs new file mode 100644 index 0000000..1ee8654 --- /dev/null +++ b/src/statusled/mod.rs @@ -0,0 +1,55 @@ +/// +/// # Neolink Status LED +/// +/// This module handles the controls of the blue led status light +/// +use log::*; +use neolink_core::bc_protocol::BcCamera; +use std::fs; +use validator::Validate; + +mod cmdline; +mod config; +mod errors; + +pub(crate) use cmdline::Opt; +use config::Config; +pub(crate) use errors::Error; + +/// Entry point for the ledstatus subcommand +/// +/// Opt is the command line options +pub fn main(opt: Opt) -> Result<(), Error> { + let config: Config = toml::from_str(&fs::read_to_string(opt.config)?)?; + + config.validate()?; + + let mut cam_found = false; + for camera_config in &config.cameras { + if opt.camera == camera_config.name { + cam_found = true; + let mut camera = + BcCamera::new_with_addr(&camera_config.camera_addr, camera_config.channel_id)?; + + info!( + "{}: Connecting to camera at {}", + camera_config.name, camera_config.camera_addr + ); + + camera.login(&camera_config.username, camera_config.password.as_deref())?; + + info!("{}: Connected and logged in", camera_config.name); + + camera.led_light_set(opt.on)?; + } + } + + if !cam_found { + error!( + "No camera with the name {} was found in the config", + opt.camera + ); + } + + Ok(()) +}