Added a reboot command to neolink

This commit is contained in:
QuantumEntangledAndy committed 2021-08-02 15:20:31 +07:00
1 parent 5d4488ad33
commit bc6c6adccd
8 files changed
+133 -1

No files matched your search

+1
View File
@@ -12,4 +12,5 @@ pub struct Opt {
pub enum Command {
Rtsp(super::rtsp::Opt),
StatusLight(super::statusled::Opt),
Reboot(super::reboot::Opt),
}
+2
View File
@@ -7,4 +7,6 @@ pub enum 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),
}
+4
View File
@@ -13,6 +13,7 @@ use structopt::StructOpt;
mod cmdline;
mod errors;
mod reboot;
mod rtsp;
mod statusled;
@@ -37,6 +38,9 @@ fn main() -> Result<(), Error> {
Command::StatusLight(opts) => {
statusled::main(opts)?;
}
Command::Reboot(opts) => {
reboot::main(opts)?;
}
}
Ok(())
+12
View File
@@ -0,0 +1,12 @@
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,
}
+29
View File
@@ -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<CameraConfig>,
}
#[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<String>,
#[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
}
+20
View File
@@ -0,0 +1,20 @@
use err_derive::Error;
/// The main error for the reboot 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),
}
+64
View File
@@ -0,0 +1,64 @@
///
/// # Neolink Reboot
///
/// This module handles the reboot subcommand
///
/// The subcommand attepts to reboot the camera.
///
/// # Usage
///
/// ```bash
/// neolink reboot --config=config.toml 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 reboot 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);
camera.reboot()?;
}
}
if !cam_found {
error!(
"No camera with the name {} was found in the config",
opt.camera
);
}
Ok(())
}
+1 -1
View File
@@ -1,6 +1,6 @@
use err_derive::Error;
/// The main error for the rtsp subcommand
/// The main error for the status-light subcommand
#[derive(Debug, Error)]
#[allow(clippy::large_enum_variant)]
pub enum Error {