diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index c34bdb8..eca6cfb 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -463,7 +463,7 @@ pub struct AlarmHandleItem { } /// TalkConfig xml -#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)] pub struct TalkConfig { /// XML Version #[yaserde(attribute)] diff --git a/src/main.rs b/src/main.rs index e1aea34..cc87064 100644 --- a/src/main.rs +++ b/src/main.rs @@ -110,10 +110,10 @@ async fn main() -> Result<()> { pir::main(opts, neo_reactor.clone()).await?; } Some(Command::Ptz(opts)) => { - ptz::main(opts, config).await?; + ptz::main(opts, neo_reactor.clone()).await?; } Some(Command::Talk(opts)) => { - talk::main(opts, config).await?; + talk::main(opts, neo_reactor.clone()).await?; } Some(Command::Mqtt(opts)) => { mqtt::main(opts, neo_reactor.clone()).await?; diff --git a/src/ptz/mod.rs b/src/ptz/mod.rs index 2755ed0..b289d86 100644 --- a/src/ptz/mod.rs +++ b/src/ptz/mod.rs @@ -23,32 +23,44 @@ use tokio::time::{sleep, Duration}; mod cmdline; -use super::config::Config; +use crate::common::NeoReactor; use crate::ptz::cmdline::CmdDirection; use crate::ptz::cmdline::PtzCommand; -use crate::utils::find_and_connect; pub(crate) use cmdline::Opt; use neolink_core::bc_protocol::Direction; /// Entry point for the ptz subcommand /// /// Opt is the command line options -pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { - let camera = find_and_connect(&config, &opt.camera).await?; +pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> { + let camera = reactor.get(&opt.camera).await?; match opt.cmd { PtzCommand::Preset { preset_id } => { if let Some(preset_id) = preset_id { camera - .moveto_ptz_preset(preset_id) - .await - .context("Unable to set PTZ preset") - .expect("TODO: panic message"); + .run_task(|cam| { + Box::pin(async move { + cam.moveto_ptz_preset(preset_id) + .await + .context("Unable to move to PTZ preset")?; + Ok(()) + }) + }) + .await?; } else { let preset_list = camera - .get_ptz_preset() - .await - .context("Unable to get PTZ presets")?; + .run_task(|cam| { + Box::pin(async move { + let preset_list = cam + .get_ptz_preset() + .await + .context("Unable to get PTZ presets")?; + Ok(preset_list) + }) + }) + .await?; + println!("Available presets:\nID Name"); for preset in preset_list.preset_list.preset { println!("{:<2} {:?}", preset.id, preset.name); @@ -57,10 +69,16 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { } PtzCommand::Assign { preset_id, name } => { camera - .set_ptz_preset(preset_id, name) - .await - .context("Unable to set PTZ preset") - .expect("TODO: panic message"); + .run_task(|cam| { + let name = name.clone(); + Box::pin(async move { + cam.set_ptz_preset(preset_id, name) + .await + .context("Unable to set PTZ preset")?; + Ok(()) + }) + }) + .await?; } PtzCommand::Control { amount, @@ -80,19 +98,31 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { let seconds = amount as f32 / speed; let duration = Duration::from_secs_f32(seconds); camera - .send_ptz(direction, speed) - .await - .context("Unable to execute PTZ move command")?; + .run_task(|cam| { + Box::pin(async move { + cam.send_ptz(direction, speed) + .await + .context("Unable to execute PTZ move command")?; + Ok(()) + }) + }) + .await?; + sleep(duration).await; camera - .send_ptz(Direction::Stop, 0_f32) - .await - .context("Unable to execute PTZ move command")?; + .run_task(|cam| { + Box::pin(async move { + cam.send_ptz(Direction::Stop, 0_f32) + .await + .context("Unable to execute PTZ move command")?; + Ok(()) + }) + }) + .await?; } }; - let _ = camera.logout().await; - camera.shutdown().await?; + camera.shutdown().await; Ok(()) } diff --git a/src/talk/mod.rs b/src/talk/mod.rs index 73c933c..75ba0b7 100644 --- a/src/talk/mod.rs +++ b/src/talk/mod.rs @@ -17,42 +17,47 @@ use neolink_core::bc::xml::TalkConfig; mod cmdline; mod gst; -use super::config::Config; -use crate::utils::{connect_and_login, find_camera_by_name}; +use crate::common::NeoReactor; pub(crate) use cmdline::Opt; /// Entry point for the talk subcommand /// /// Opt is the command line options -pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { - let camera_config = find_camera_by_name(&config, &opt.camera)?; - let camera = connect_and_login(camera_config).await?; +pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> { + let camera = reactor.get(&opt.camera).await?; + let config = camera.config().await?.borrow().clone(); + let name = config.name.clone(); let talk_ability = camera - .talk_ability() + .run_task(|cam| { + Box::pin(async move { + let talk_ability = cam.talk_ability().await?; + Ok(talk_ability) + }) + }) .await - .with_context(|| format!("Camera {} does not support talk", camera_config.name))?; + .with_context(|| format!("Camera {} does not support talk", name))?; + if talk_ability.duplex_list.is_empty() || talk_ability.audio_stream_mode_list.is_empty() || talk_ability.audio_config_list.is_empty() { - return Err(anyhow!( - "Camera {} does not support talk", - camera_config.name - )); + return Err(anyhow!("Camera {} does not support talk", name)); } // Just copy that data from the first talk ability in the config have never seen more // than one ability - let config = 0; + let config_id = 0; let talk_config = TalkConfig { - channel_id: camera_config.channel_id, - duplex: talk_ability.duplex_list[config].duplex.clone(), - audio_stream_mode: talk_ability.audio_stream_mode_list[config] + channel_id: config.channel_id, + duplex: talk_ability.duplex_list[config_id].duplex.clone(), + audio_stream_mode: talk_ability.audio_stream_mode_list[config_id] .audio_stream_mode .clone(), - audio_config: talk_ability.audio_config_list[config].audio_config.clone(), + audio_config: talk_ability.audio_config_list[config_id] + .audio_config + .clone(), ..Default::default() }; @@ -61,7 +66,7 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { if block_size == 0 || sample_rate == 0 { return Err(anyhow!( "The camera {} does not support talk with adpcm", - camera_config.name + name )); } @@ -82,12 +87,18 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { }; camera - .talk_stream(rx, talk_config) + .run_task(|cam| { + let rx = rx.clone(); + let talk_config = talk_config.clone(); + Box::pin(async move { + cam.talk_stream(rx, talk_config).await?; + Ok(()) + }) + }) .await .context("Talk stream ended early")?; - let _ = camera.logout().await; - camera.shutdown().await?; + camera.shutdown().await; Ok(()) } diff --git a/src/utils.rs b/src/utils.rs index 5179d3a..3913247 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -2,7 +2,7 @@ //! use log::*; -use super::config::{CameraConfig, Config}; +use super::config::CameraConfig; use anyhow::{anyhow, Context, Error, Result}; use neolink_core::bc_protocol::{ BcCamera, BcCameraOpt, ConnectionProtocol, Credentials, DiscoveryMethods, MaxEncryption, @@ -107,11 +107,6 @@ impl AddressOrUid { } } -pub(crate) async fn find_and_connect(config: &Config, name: &str) -> Result { - let camera_config = find_camera_by_name(config, name)?; - connect_and_login(camera_config).await -} - pub(crate) async fn connect_and_login(camera_config: &CameraConfig) -> Result { let camera_addr = AddressOrUid::new( &camera_config.camera_addr, @@ -149,12 +144,3 @@ pub(crate) async fn connect_and_login(camera_config: &CameraConfig) -> Result(config: &'a Config, name: &str) -> Result<&'a CameraConfig> { - config - .cameras - .iter() - .filter(|c| c.enabled) - .find(|c| c.name == name) - .ok_or_else(|| anyhow!("Camera {} not found in the config file", name)) -}