Use reactor for all commands

This commit is contained in:
QuantumEntangledAndy committed 2023-09-15 08:37:14 +07:00
1 parent cc9c35400f
commit d9541766dc
5 files changed
+88 -61

No files matched your search

+1 -1
View File
@@ -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)]
+2 -2
View File
@@ -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?;
+53 -23
View File
@@ -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(())
}
+31 -20
View File
@@ -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(())
}
+1 -15
View File
@@ -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<BcCamera> {
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<BcCamera> {
let camera_addr = AddressOrUid::new(
&camera_config.camera_addr,
@@ -149,12 +144,3 @@ pub(crate) async fn connect_and_login(camera_config: &CameraConfig) -> Result<Bc
Ok(camera)
}
pub(crate) fn find_camera_by_name<'a>(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))
}