Add command for camera service port controls (not on old camera)

This commit is contained in:
QuantumEntangledAndy committed 2024-04-05 19:06:02 +07:00
1 parent 9187694889
commit 54f7665414
10 files changed
+858 -5

No files matched your search

+1
View File
@@ -27,4 +27,5 @@ pub enum Command {
MqttRtsp(super::mqtt::Opt),
Image(super::image::Opt),
Battery(super::battery::Opt),
Services(super::services::Opt),
}
+4
View File
@@ -46,6 +46,7 @@ mod pir;
mod ptz;
mod reboot;
mod rtsp;
mod services;
mod statusled;
mod talk;
mod utils;
@@ -122,6 +123,9 @@ async fn main() -> Result<()> {
Some(Command::Battery(opts)) => {
battery::main(opts, neo_reactor.clone()).await?;
}
Some(Command::Services(opts)) => {
services::main(opts, neo_reactor.clone()).await?;
}
}
Ok(())
+58
View File
@@ -0,0 +1,58 @@
use anyhow::{anyhow, Result};
use clap::{Parser, ValueEnum};
fn onoff_parse(src: &str) -> Result<bool> {
match src {
"true" | "on" | "yes" => Ok(true),
"false" | "off" | "no" => Ok(false),
_ => Err(anyhow!(
"Could not understand {}, check your input, should be true/false, on/off or yes/no",
src
)),
}
}
/// The services command will control the ports for http/https/rtmp/rtsp/onvif
#[derive(Parser, Debug)]
pub struct Opt {
/// The name of the camera. Must be a name in the config
pub camera: String,
/// service to change
pub service: Services,
/// The action to perform
#[command(subcommand)]
pub cmd: PortAction,
}
#[derive(Parser, Debug, Clone, ValueEnum)]
pub enum Services {
Baichuan,
Http,
Https,
Rtmp,
Rtsp,
Onvif,
}
#[derive(Parser, Debug)]
pub enum PortAction {
/// Get the current details
Get,
/// Turn the service ON
On,
/// Turn the service OFF
Off,
/// Set both port and on/off
Set {
/// The new port
port: u32,
/// On/off
#[arg(value_parser = onoff_parse, action = clap::ArgAction::Set, name = "on|off")]
enabled: bool,
},
/// Set the port
Port {
/// The new port
port: u32,
},
}
+260
View File
@@ -0,0 +1,260 @@
///
/// # Neolink Services
///
/// This module handles the controls of the services like http/https/rtmp/rtsp/onvif
/// It only works on newer cameras that have these built in
///
///
/// # Usage
///
/// ```bash
/// # To turn on http
/// neolink services --config=config.toml CameraName http on
/// # Or off
/// neolink services --config=config.toml CameraName http off
/// # Or set the port
/// neolink services --config=config.toml CameraName http port 80
/// # Or get the current port details
/// neolink services --config=config.toml CameraName http get
/// ```
///
/// Services are
/// - http
/// - https
/// - rtmp
/// - rtsp
/// - onvif (will also turn on rtsp)
///
use anyhow::{Context, Result};
mod cmdline;
use crate::common::NeoReactor;
pub(crate) use cmdline::*;
/// Entry point for the pir subcommand
///
/// Opt is the command line options
pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
let camera = reactor.get(&opt.camera).await?;
match opt.cmd {
PortAction::Get => match opt.service {
Services::Http => {
let state = camera
.run_task(|cam| {
Box::pin(async move {
cam.get_http()
.await
.context("Unable to get camera service state")
})
})
.await?;
let ser = String::from_utf8(
{
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &state).map(|_| buf.to_vec())
}
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", ser);
}
Services::Https => {
let state = camera
.run_task(|cam| {
Box::pin(async move {
cam.get_https()
.await
.context("Unable to get camera service state")
})
})
.await?;
let ser = String::from_utf8(
{
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &state).map(|_| buf.to_vec())
}
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", ser);
}
Services::Rtsp => {
let state = camera
.run_task(|cam| {
Box::pin(async move {
cam.get_rtsp()
.await
.context("Unable to get camera service state")
})
})
.await?;
let ser = String::from_utf8(
{
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &state).map(|_| buf.to_vec())
}
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", ser);
}
Services::Rtmp => {
let state = camera
.run_task(|cam| {
Box::pin(async move {
cam.get_rtmp()
.await
.context("Unable to get camera service state")
})
})
.await?;
let ser = String::from_utf8(
{
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &state).map(|_| buf.to_vec())
}
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", ser);
}
Services::Onvif => {
let state = camera
.run_task(|cam| {
Box::pin(async move {
cam.get_onvif()
.await
.context("Unable to get camera service state")
})
})
.await?;
let ser = String::from_utf8(
{
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &state).map(|_| buf.to_vec())
}
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", ser);
}
Services::Baichuan => {
let state = camera
.run_task(|cam| {
Box::pin(async move {
cam.get_serverport()
.await
.context("Unable to get camera service state")
})
})
.await?;
let ser = String::from_utf8(
{
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &state).map(|_| buf.to_vec())
}
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", ser);
}
},
action => {
let on = match &action {
PortAction::On => Some(true),
PortAction::Off => Some(false),
PortAction::Set { enabled, .. } => Some(*enabled),
_ => None,
};
let port = match &action {
PortAction::Port { port } => Some(*port),
PortAction::Set { port, .. } => Some(*port),
_ => None,
};
match opt.service {
Services::Http => {
camera
.run_task(|cam| {
Box::pin(async move {
cam.set_http(on, port)
.await
.context("Unable to set camera service state")
})
})
.await?;
}
Services::Https => {
camera
.run_task(|cam| {
Box::pin(async move {
cam.set_https(on, port)
.await
.context("Unable to set camera service state")
})
})
.await?;
}
Services::Rtsp => {
camera
.run_task(|cam| {
Box::pin(async move {
// Onvif will not work without rtsp
if let Some(false) = &on {
cam.set_onvif(Some(false), None)
.await
.context("Unable to set camera service state")?;
}
cam.set_rtsp(on, port)
.await
.context("Unable to set camera service state")
})
})
.await?;
}
Services::Rtmp => {
camera
.run_task(|cam| {
Box::pin(async move {
cam.set_rtmp(on, port)
.await
.context("Unable to set camera service state")
})
})
.await?;
}
Services::Onvif => {
camera
.run_task(|cam| {
Box::pin(async move {
// Onvif will not work without rtsp
if let Some(true) = &on {
cam.set_rtsp(Some(true), None)
.await
.context("Unable to set camera service state")?;
}
cam.set_onvif(on, port)
.await
.context("Unable to set camera service state")
})
})
.await?;
}
Services::Baichuan => {
camera
.run_task(|cam| {
Box::pin(async move {
cam.set_serverport(on, port)
.await
.context("Unable to set camera service state")
})
})
.await?;
}
}
}
}
Ok(())
}