Skip to content

Commit

Permalink
Merge pull request #217 from thirtythreeforty/unify-structs
Browse files Browse the repository at this point in the history
Re-unify single-camera command structs and code
  • Loading branch information
thirtythreeforty authored Nov 1, 2021
2 parents f10f9f9 + 81b148f commit 324d14e
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 413 deletions.
8 changes: 6 additions & 2 deletions src/cmdline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ use structopt::{clap::AppSettings, StructOpt};

/// A standards-compliant bridge to Reolink IP cameras
#[derive(StructOpt, Debug)]
#[structopt(name = "neolink", setting(AppSettings::ArgRequiredElseHelp))]
#[structopt(
name = "neolink",
setting(AppSettings::ArgRequiredElseHelp),
setting(AppSettings::UnifiedHelpMessage)
)]
pub struct Opt {
#[structopt(short, long, parse(from_os_str))]
#[structopt(short, long, global(true), parse(from_os_str))]
pub config: Option<PathBuf>,
#[structopt(subcommand)]
pub cmd: Option<Command>,
Expand Down
File renamed without changes.
44 changes: 27 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@
//! It contains sub commands for running an rtsp proxy which can be used on Reolink cameras
//! that do not nativly support RTSP.
//!
use anyhow::Result;
use anyhow::{Context, Result};
use env_logger::Env;
use log::*;
use std::fs;
use structopt::StructOpt;
use validator::Validate;

mod cmdline;
mod config;
mod reboot;
mod rtsp;
mod statusled;
mod talk;
mod utils;

use cmdline::{Command, Opt};
use config::Config;

fn main() -> Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
Expand All @@ -32,30 +36,36 @@ fn main() -> Result<()> {

let opt = Opt::from_args();

match (opt.cmd, opt.config) {
(None, None) => {
// Should be caught at the clap validation
unreachable!();
}
(None, Some(config)) => {
let conf_path = opt.config.context("Must supply --config file")?;
let config: Config = toml::from_str(
&fs::read_to_string(&conf_path)
.with_context(|| format!("Failed to read {:?}", conf_path))?,
)
.with_context(|| format!("Failed to parse the {:?} config file", conf_path))?;

config
.validate()
.with_context(|| format!("Failed to validate the {:?} config file", conf_path))?;

match opt.cmd {
None => {
warn!(
"Deprecated command line option. Please use: `neolink rtsp --config={:?}`",
config
);
rtsp::main(rtsp::Opt { config })?;
rtsp::main(rtsp::Opt {}, config)?;
}
(Some(_), Some(_)) => error!("--config should be given after the subcommand"),
(Some(Command::Rtsp(opts)), None) => {
rtsp::main(opts)?;
Some(Command::Rtsp(opts)) => {
rtsp::main(opts, config)?;
}
(Some(Command::StatusLight(opts)), None) => {
statusled::main(opts)?;
Some(Command::StatusLight(opts)) => {
statusled::main(opts, config)?;
}
(Some(Command::Reboot(opts)), None) => {
reboot::main(opts)?;
Some(Command::Reboot(opts)) => {
reboot::main(opts, config)?;
}
(Some(Command::Talk(opts)), None) => {
talk::main(opts)?;
Some(Command::Talk(opts)) => {
talk::main(opts, config)?;
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/reboot/cmdline.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
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,
/// The name of the camera to change the lights of. Must be a name in the config
pub camera: String,
}
45 changes: 0 additions & 45 deletions src/reboot/config.rs

This file was deleted.

68 changes: 9 additions & 59 deletions src/reboot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,72 +11,22 @@
/// neolink reboot --config=config.toml CameraName
/// ```
///
use anyhow::{anyhow, Context, Result};
use log::*;
use std::fs;
use validator::Validate;
use anyhow::{Context, Result};

mod cmdline;
mod config;

use crate::utils::AddressOrUid;
use super::config::Config;
use crate::utils::find_and_connect;
pub(crate) use cmdline::Opt;
use config::Config;

/// Entry point for the reboot subcommand
///
/// Opt is the command line options
pub fn main(opt: Opt) -> Result<()> {
let config: Config = toml::from_str(
&fs::read_to_string(&opt.config)
.with_context(|| format!("Failed to read {:?}", &opt.config))?,
)
.with_context(|| format!("Failed to load {:?} as a config file", &opt.config))?;
pub(crate) fn main(opt: Opt, config: Config) -> Result<()> {
let camera = find_and_connect(&config, &opt.camera)?;

config
.validate()
.with_context(|| format!("Failed to validate the {:?} config file", &opt.config))?;

let mut cam_found = false;
for camera_config in &config.cameras {
if opt.camera == camera_config.name {
cam_found = true;
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())
.with_context(|| format!("Failed to login to {}", camera_config.name))?;

info!("{}: Connected and logged in", camera_config.name);

camera
.reboot()
.context("Could not send reboot command to the camera")?;
}
}

if !cam_found {
Err(anyhow!(
"Camera {} was not in the config file {:?}",
&opt.camera,
&opt.config
))
} else {
Ok(())
}
camera
.reboot()
.context("Could not send reboot command to the camera")?;
Ok(())
}
7 changes: 1 addition & 6 deletions src/rtsp/cmdline.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use std::path::PathBuf;
use structopt::StructOpt;

/// 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
#[structopt(short, long, parse(from_os_str))]
pub config: PathBuf,
}
pub struct Opt {}
17 changes: 2 additions & 15 deletions src/rtsp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,24 @@ use log::*;
use neolink_core::bc_protocol::{BcCamera, Stream};
use neolink_core::Never;
use std::collections::HashSet;
use std::fs;
use std::sync::Arc;
use std::time::Duration;
use validator::Validate;

// mod adpcm;
/// The command line parameters for this subcommand
mod cmdline;
mod config;
/// The errors this subcommand can raise
mod gst;

use super::config::{CameraConfig, Config, UserConfig};
use crate::utils::AddressOrUid;
pub(crate) use cmdline::Opt;
use config::{CameraConfig, Config, UserConfig};
use gst::{GstOutputs, RtspServer, TlsAuthenticationMode};

/// Entry point for the rtsp subcommand
///
/// Opt is the command line options
pub fn main(opt: Opt) -> Result<()> {
let config: Config = toml::from_str(
&fs::read_to_string(&opt.config)
.with_context(|| format!("Failed to read {:?}", &opt.config))?,
)
.with_context(|| format!("Failed to load {:?} as a config file", &opt.config))?;

config
.validate()
.with_context(|| format!("Failed to validate the {:?} config file", &opt.config))?;

pub(crate) fn main(_opt: Opt, config: Config) -> Result<()> {
let rtsp = &RtspServer::new();

set_up_tls(&config, rtsp);
Expand Down
4 changes: 0 additions & 4 deletions src/statusled/cmdline.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::{anyhow, Result};
use std::path::PathBuf;
use structopt::StructOpt;

fn onoff_parse(src: &str) -> Result<bool> {
Expand All @@ -16,9 +15,6 @@ fn onoff_parse(src: &str) -> Result<bool> {
/// 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
Expand Down
45 changes: 0 additions & 45 deletions src/statusled/config.rs

This file was deleted.

Loading

0 comments on commit 324d14e

Please sign in to comment.