diff --git a/crates/core/src/bc_protocol/battery.rs b/crates/core/src/bc_protocol/battery.rs index 83697e8..ec4a91d 100644 --- a/crates/core/src/bc_protocol/battery.rs +++ b/crates/core/src/bc_protocol/battery.rs @@ -19,58 +19,60 @@ impl BcCamera { let connection = self.get_connection(); connection .handle_msg(MSG_ID_BATTERY_INFO_LIST, move |bc| { - if let Bc { - body: - BcBody::ModernMsg(ModernMsg { - payload: - Some(BcPayloads::BcXml(BcXml { - battery_list: Some(battery_list), - .. - })), - .. - }), - .. - } = bc - { - for battery in battery_list.battery_info.iter() { - match format { - PrintFormat::None => {} - PrintFormat::Human => { - println!( - "==Battery==\n\ + Box::pin(async move { + if let Bc { + body: + BcBody::ModernMsg(ModernMsg { + payload: + Some(BcPayloads::BcXml(BcXml { + battery_list: Some(battery_list), + .. + })), + .. + }), + .. + } = bc + { + for battery in battery_list.battery_info.iter() { + match format { + PrintFormat::None => {} + PrintFormat::Human => { + println!( + "==Battery==\n\ Charge: {}%,\n\ Temperature: {}°C,\n\ LowPower: {},\n\ Adapter: {},\n\ ChargeStatus: {},\n\ ", - battery.battery_percent, - battery.temperature, - if battery.low_power == 1 { - "true" - } else { - "false" - }, - battery.adapter_status, - battery.charge_status, - ); - } - PrintFormat::Xml => { - let bat_ser = String::from_utf8( - yaserde::ser::serialize_with_writer( - battery, - vec![], - &Default::default(), + battery.battery_percent, + battery.temperature, + if battery.low_power == 1 { + "true" + } else { + "false" + }, + battery.adapter_status, + battery.charge_status, + ); + } + PrintFormat::Xml => { + let bat_ser = String::from_utf8( + yaserde::ser::serialize_with_writer( + battery, + vec![], + &Default::default(), + ) + .expect("Should Ser the struct"), ) - .expect("Should Ser the struct"), - ) - .expect("Should be UTF8"); - println!("{}", bat_ser); + .expect("Should be UTF8"); + println!("{}", bat_ser); + } } } } - } - None + Option::::None + }) }) .await?; Ok(()) diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index b2038d7..bae0408 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -1,10 +1,12 @@ use super::BcSubscription; use crate::{bc::model::*, Error, Result}; +use futures::future::BoxFuture; use futures::sink::{Sink, SinkExt}; use futures::stream::{Stream, StreamExt}; use log::*; use std::collections::btree_map::Entry; use std::collections::BTreeMap; +use std::sync::Arc; use tokio::sync::mpsc::{channel, Sender}; use tokio::task::yield_now; use tokio_stream::wrappers::ReceiverStream; @@ -12,13 +14,14 @@ use tokio_util::sync::PollSender; use tokio::{sync::RwLock, task::JoinSet}; -type MsgHandler = Box Option + Send + Sync>; +type MsgHandler = dyn 'static + Send + Sync + for<'a> Fn(&'a Bc) -> BoxFuture<'a, Option>; + #[derive(Default)] struct Subscriber { /// Subscribers based on their Num num: BTreeMap>>, /// Subscribers based on their ID - id: BTreeMap, + id: BTreeMap>, } pub(crate) type BcConnSink = Box + Send + Sync + Unpin>; @@ -95,10 +98,10 @@ impl BcConnection { /// and return either None or Some(Bc) reply pub async fn handle_msg(&self, msg_id: u32, handler: T) -> Result<()> where - T: Fn(&Bc) -> Option + Send + Sync + 'static, + T: 'static + Send + Sync + for<'a> Fn(&'a Bc) -> BoxFuture<'a, Option>, { self.poll_commander - .send(PollCommand::AddHandler(msg_id, Box::new(handler))) + .send(PollCommand::AddHandler(msg_id, Arc::new(handler))) .await?; Ok(()) } @@ -133,7 +136,7 @@ impl BcConnection { enum PollCommand { Bc(Box>), - AddHandler(u32, MsgHandler), + AddHandler(u32, Arc), RemoveHandler(u32), AddSubscriber(u16, Sender>), } @@ -160,7 +163,7 @@ impl Poller { self.subscribers.num.get(&msg_num), ) { (Some(occ), _) => { - if let Some(reply) = occ(&response) { + if let Some(reply) = occ(&response).await { assert!(reply.meta.msg_num == response.meta.msg_num); self.sink.send(Ok(reply)).await?; } diff --git a/crates/core/src/bc_protocol/floodlight_status.rs b/crates/core/src/bc_protocol/floodlight_status.rs index f95e399..d682a40 100644 --- a/crates/core/src/bc_protocol/floodlight_status.rs +++ b/crates/core/src/bc_protocol/floodlight_status.rs @@ -10,27 +10,30 @@ impl BcCamera { let connection = self.get_connection(); connection .handle_msg(MSG_ID_FLOODLIGHT_STATUS_LIST, move |bc| { - if let Bc { - meta: - BcMeta { - msg_id: MSG_ID_FLOODLIGHT_STATUS_LIST, - .. - }, - body: - BcBody::ModernMsg(ModernMsg { - payload: - Some(BcPayloads::BcXml(BcXml { - floodlight_status_list: Some(list), - .. - })), - .. - }), - } = bc - { - let send_this: FloodlightStatusList = list.clone(); - let _ = tx.blocking_send(send_this); - } - None + let tx = tx.clone(); + Box::pin(async move { + if let Bc { + meta: + BcMeta { + msg_id: MSG_ID_FLOODLIGHT_STATUS_LIST, + .. + }, + body: + BcBody::ModernMsg(ModernMsg { + payload: + Some(BcPayloads::BcXml(BcXml { + floodlight_status_list: Some(list), + .. + })), + .. + }), + } = bc + { + let send_this: FloodlightStatusList = list.clone(); + let _ = tx.send(send_this).await; + } + None + }) }) .await?; diff --git a/crates/core/src/bc_protocol/keepalive.rs b/crates/core/src/bc_protocol/keepalive.rs index 89a9ca7..3de5cbc 100644 --- a/crates/core/src/bc_protocol/keepalive.rs +++ b/crates/core/src/bc_protocol/keepalive.rs @@ -10,18 +10,20 @@ impl BcCamera { let connection = self.get_connection(); connection .handle_msg(MSG_ID_UDP_KEEP_ALIVE, |bc| { - Some(Bc { - meta: BcMeta { - msg_id: MSG_ID_UDP_KEEP_ALIVE, - channel_id: bc.meta.channel_id, - msg_num: bc.meta.msg_num, - stream_type: bc.meta.stream_type, - response_code: 200, - class: 0x6414, - }, - body: BcBody::ModernMsg(ModernMsg { - ..Default::default() - }), + Box::pin(async move { + Some(Bc { + meta: BcMeta { + msg_id: MSG_ID_UDP_KEEP_ALIVE, + channel_id: bc.meta.channel_id, + msg_num: bc.meta.msg_num, + stream_type: bc.meta.stream_type, + response_code: 200, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + ..Default::default() + }), + }) }) }) .await?;