Use reactor with simple commands
This commit is contained in:
6 files changed
+87
-39
No files matched your search
+15
-8
@@ -14,20 +14,28 @@ use anyhow::{Context, Result};
|
||||
|
||||
mod cmdline;
|
||||
|
||||
use crate::common::neocam::NeoReactor;
|
||||
|
||||
use super::config::Config;
|
||||
use crate::utils::find_and_connect;
|
||||
pub(crate) use cmdline::Opt;
|
||||
|
||||
/// Entry point for the battery 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, config: Config, reactor: NeoReactor) -> Result<()> {
|
||||
let config = config.get_camera_config(&opt.camera)?;
|
||||
let camera = reactor.get_or_insert(config.clone()).await?;
|
||||
|
||||
let state = camera
|
||||
.battery_info()
|
||||
.await
|
||||
.context("Unable to get camera Battery state")?;
|
||||
.run_task(|cam| {
|
||||
Box::pin(async move {
|
||||
cam.battery_info()
|
||||
.await
|
||||
.context("Unable to get camera Battery state")
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
|
||||
let ser = String::from_utf8(
|
||||
yaserde::ser::serialize_with_writer(&state, vec![], &Default::default())
|
||||
.expect("Should Ser the struct"),
|
||||
@@ -35,8 +43,7 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> {
|
||||
.expect("Should be UTF8");
|
||||
println!("{}", ser);
|
||||
|
||||
let _ = camera.logout().await;
|
||||
camera.shutdown().await?;
|
||||
camera.shutdown().await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
+39
-12
@@ -291,7 +291,8 @@ impl NeoInstance {
|
||||
enum NeoReactorCommand {
|
||||
HangUp,
|
||||
Get(String, OneshotSender<Result<NeoInstance>>),
|
||||
GetOrInsert(String, OneshotSender<Result<NeoInstance>>, CameraConfig),
|
||||
GetOrInsert(CameraConfig, OneshotSender<Result<NeoInstance>>),
|
||||
UpdateOrInsert(CameraConfig, OneshotSender<Result<NeoInstance>>),
|
||||
}
|
||||
|
||||
struct NeoReactorData {
|
||||
@@ -345,7 +346,8 @@ impl NeoReactor {
|
||||
.await;
|
||||
let _ = sender.send(new);
|
||||
}
|
||||
NeoReactorCommand::GetOrInsert(name, sender, config) => {
|
||||
NeoReactorCommand::GetOrInsert(config, sender) => {
|
||||
let name = config.name.clone();
|
||||
let new = match instances.entry(name) {
|
||||
Entry::Occupied(occ) => occ.get().instance.subscribe().await,
|
||||
Entry::Vacant(vac) => {
|
||||
@@ -360,6 +362,26 @@ impl NeoReactor {
|
||||
}
|
||||
};
|
||||
let _ = sender.send(new);
|
||||
},
|
||||
NeoReactorCommand::UpdateOrInsert(config, sender) => {
|
||||
let name = config.name.clone();
|
||||
let new = match instances.entry(name) {
|
||||
Entry::Occupied(occ) => {
|
||||
occ.get().config_sender.send(config)?;
|
||||
occ.get().instance.subscribe().await
|
||||
},
|
||||
Entry::Vacant(vac) => {
|
||||
let (config_sender, instance) = NeoCam::init(config).await?;
|
||||
vac.insert(NeoReactorData {
|
||||
instance,
|
||||
config_sender,
|
||||
})
|
||||
.instance
|
||||
.subscribe()
|
||||
.await
|
||||
}
|
||||
};
|
||||
let _ = sender.send(new);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -372,6 +394,7 @@ impl NeoReactor {
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
/// Get camera by name but do not create
|
||||
pub(crate) async fn get(&self, name: &str) -> Result<NeoInstance> {
|
||||
let (sender_tx, sender_rx) = oneshot();
|
||||
self.commander
|
||||
@@ -381,18 +404,22 @@ impl NeoReactor {
|
||||
sender_rx.await?
|
||||
}
|
||||
|
||||
pub(crate) async fn get_or_insert(
|
||||
&self,
|
||||
name: &str,
|
||||
config: CameraConfig,
|
||||
) -> Result<NeoInstance> {
|
||||
/// Get or create a camera
|
||||
pub(crate) async fn get_or_insert(&self, config: CameraConfig) -> Result<NeoInstance> {
|
||||
let (sender_tx, sender_rx) = oneshot();
|
||||
self.commander
|
||||
.send(NeoReactorCommand::GetOrInsert(
|
||||
name.to_string(),
|
||||
sender_tx,
|
||||
config,
|
||||
))
|
||||
.send(NeoReactorCommand::GetOrInsert(config, sender_tx))
|
||||
.await?;
|
||||
|
||||
sender_rx.await?
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
/// Update a camera to a new config or create a camera
|
||||
pub(crate) async fn update_or_insert(&self, config: CameraConfig) -> Result<NeoInstance> {
|
||||
let (sender_tx, sender_rx) = oneshot();
|
||||
self.commander
|
||||
.send(NeoReactorCommand::UpdateOrInsert(config, sender_tx))
|
||||
.await?;
|
||||
|
||||
sender_rx.await?
|
||||
|
||||
+3
-3
@@ -100,10 +100,10 @@ async fn main() -> Result<()> {
|
||||
rtsp::main(opts, config).await?;
|
||||
}
|
||||
Some(Command::StatusLight(opts)) => {
|
||||
statusled::main(opts, config).await?;
|
||||
statusled::main(opts, config, neo_reactor.clone()).await?;
|
||||
}
|
||||
Some(Command::Reboot(opts)) => {
|
||||
reboot::main(opts, config).await?;
|
||||
reboot::main(opts, config, neo_reactor.clone()).await?;
|
||||
}
|
||||
Some(Command::Pir(opts)) => {
|
||||
pir::main(opts, config, neo_reactor.clone()).await?;
|
||||
@@ -121,7 +121,7 @@ async fn main() -> Result<()> {
|
||||
image::main(opts, config).await?;
|
||||
}
|
||||
Some(Command::Battery(opts)) => {
|
||||
battery::main(opts, config).await?;
|
||||
battery::main(opts, config, neo_reactor.clone()).await?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ pub(crate) use cmdline::Opt;
|
||||
/// Opt is the command line options
|
||||
pub(crate) async fn main(opt: Opt, config: Config, reactor: NeoReactor) -> Result<()> {
|
||||
let config = config.get_camera_config(&opt.camera)?;
|
||||
let camera = reactor.get_or_insert(&opt.camera, config.clone()).await?;
|
||||
let camera = reactor.get_or_insert(config.clone()).await?;
|
||||
|
||||
if let Some(on) = opt.on {
|
||||
camera
|
||||
|
||||
+14
-7
@@ -16,21 +16,28 @@ use anyhow::{Context, Result};
|
||||
mod cmdline;
|
||||
|
||||
use super::config::Config;
|
||||
use crate::utils::find_and_connect;
|
||||
use crate::common::neocam::NeoReactor;
|
||||
pub(crate) use cmdline::Opt;
|
||||
|
||||
/// Entry point for the reboot 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, config: Config, reactor: NeoReactor) -> Result<()> {
|
||||
let config = config.get_camera_config(&opt.camera)?;
|
||||
let camera = reactor.get_or_insert(config.clone()).await?;
|
||||
|
||||
camera
|
||||
.reboot()
|
||||
.await
|
||||
.context("Could not send reboot command to the camera")?;
|
||||
.run_task(|camera| {
|
||||
Box::pin(async move {
|
||||
camera
|
||||
.reboot()
|
||||
.await
|
||||
.context("Could not send reboot command to the camera")
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
|
||||
camera.shutdown().await?;
|
||||
camera.shutdown().await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
+15
-8
@@ -20,22 +20,29 @@ use anyhow::{Context, Result};
|
||||
mod cmdline;
|
||||
|
||||
use super::config::Config;
|
||||
use crate::utils::find_and_connect;
|
||||
use crate::common::neocam::NeoReactor;
|
||||
pub(crate) use cmdline::Opt;
|
||||
|
||||
/// Entry point for the ledstatus 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, config: Config, reactor: NeoReactor) -> Result<()> {
|
||||
let config = config.get_camera_config(&opt.camera)?;
|
||||
let camera = reactor.get_or_insert(config.clone()).await?;
|
||||
|
||||
let on = opt.on;
|
||||
camera
|
||||
.led_light_set(opt.on)
|
||||
.await
|
||||
.context("Unable to set camera light state")?;
|
||||
.run_task(|camera| {
|
||||
Box::pin(async move {
|
||||
camera
|
||||
.led_light_set(on)
|
||||
.await
|
||||
.context("Unable to set camera light state")
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
|
||||
let _ = camera.logout().await;
|
||||
camera.shutdown().await?;
|
||||
camera.shutdown().await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user