diff --git a/src/cmdline.rs b/src/cmdline.rs index b584623..80a65a0 100644 --- a/src/cmdline.rs +++ b/src/cmdline.rs @@ -13,4 +13,5 @@ pub enum Command { Rtsp(super::rtsp::Opt), StatusLight(super::statusled::Opt), Reboot(super::reboot::Opt), + Talk(super::talk::Opt), } diff --git a/src/errors.rs b/src/errors.rs index 10b62cf..009115e 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -9,4 +9,6 @@ pub enum 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), } diff --git a/src/main.rs b/src/main.rs index 78b9b5c..475475b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ mod errors; mod reboot; mod rtsp; mod statusled; +mod talk; use cmdline::{Command, Opt}; use errors::Error; @@ -41,6 +42,9 @@ fn main() -> Result<(), Error> { Command::Reboot(opts) => { reboot::main(opts)?; } + Command::Talk(opts) => { + talk::main(opts)?; + } } Ok(()) diff --git a/src/talk/cmdline.rs b/src/talk/cmdline.rs new file mode 100644 index 0000000..273b7aa --- /dev/null +++ b/src/talk/cmdline.rs @@ -0,0 +1,25 @@ +use std::path::PathBuf; +use structopt::StructOpt; + +/// The talk command will send adpcm data to the camera to say +/// +/// This data should be encoded in adpcm with dvi4 layout +/// +/// `gst-launch` can be used to prepare this data +#[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, + /// The path to the adpcm data to send in DVI-4 layout + #[structopt(short, long, parse(from_os_str), name = "media-path")] + pub adpcm_file: PathBuf, + /// The block size used to encode the adpcm data (recommended 512) + #[structopt(short, long)] + pub block_size: u16, + /// The sample rate used to encode the adpcm data (recommended 16000) + #[structopt(short, long)] + pub sample_rate: u16, +} diff --git a/src/talk/config.rs b/src/talk/config.rs new file mode 100644 index 0000000..de60ef2 --- /dev/null +++ b/src/talk/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/talk/errors.rs b/src/talk/errors.rs new file mode 100644 index 0000000..162c442 --- /dev/null +++ b/src/talk/errors.rs @@ -0,0 +1,20 @@ +use err_derive::Error; + +/// The main error for the status-light 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), +} diff --git a/src/talk/mod.rs b/src/talk/mod.rs new file mode 100644 index 0000000..8bb0024 --- /dev/null +++ b/src/talk/mod.rs @@ -0,0 +1,66 @@ +/// +/// # Neolink Talk +/// +/// This module can be used to send adpcm data for the camera to play +/// +/// The adpcm data needs to be in DVI-4 layout +/// +/// # Usage +/// +/// ```bash +/// neolink status-light --config=config.toml --adpcm-file=data.adpcm --sample-rate=16000 --block-size=512 CameraName +/// ``` +/// +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 talk 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; + info!( + "{}: Connecting to camera at {}", + camera_config.name, camera_config.camera_addr + ); + + let mut camera = + BcCamera::new_with_addr(&camera_config.camera_addr, camera_config.channel_id)?; + + info!("{}: Logging in", camera_config.name); + camera.login(&camera_config.username, camera_config.password.as_deref())?; + + info!("{}: Connected and logged in", camera_config.name); + + let data = fs::read(&opt.adpcm_file)?; + + camera.talk(&data, opt.block_size, opt.sample_rate)?; + } + } + + if !cam_found { + error!( + "No camera with the name {} was found in the config", + opt.camera + ); + } + + Ok(()) +}