Add a debug option to the camera config
This commit is contained in:
9 files changed
+74
-23
No files matched your search
@@ -15,6 +15,12 @@ pub(crate) struct BcCodex {
|
||||
}
|
||||
|
||||
impl BcCodex {
|
||||
pub(crate) fn new_with_debug(credentials: Credentials) -> Self {
|
||||
let mut context = BcContext::new(credentials);
|
||||
|
||||
context.debug_on();
|
||||
Self { context }
|
||||
}
|
||||
pub(crate) fn new(credentials: Credentials) -> Self {
|
||||
Self {
|
||||
context: BcContext::new(credentials),
|
||||
|
||||
+16
-10
@@ -116,10 +116,12 @@ fn bc_modern_msg<'a>(
|
||||
let mut encrypted_len = None;
|
||||
// Now we'll take the buffer that Nom gave a ref to and parse it.
|
||||
let extension = if ext_len > 0 {
|
||||
log::trace!(
|
||||
"Extension Txt: {:?}",
|
||||
String::from_utf8(processed_ext_buf.to_vec()).unwrap_or("Not Text".to_string())
|
||||
);
|
||||
if context.debug {
|
||||
println!(
|
||||
"Extension Txt: {:?}",
|
||||
String::from_utf8(processed_ext_buf.to_vec()).unwrap_or("Not Text".to_string())
|
||||
);
|
||||
}
|
||||
// Apply the XML parse function, but throw away the reference to decrypted in the Ok and
|
||||
// Err case. This error-error-error thing is the same idiom Nom uses internally.
|
||||
let parsed = Extension::try_parse(processed_ext_buf).map_err(|_| {
|
||||
@@ -182,8 +184,9 @@ fn bc_modern_msg<'a>(
|
||||
if context.in_bin_mode.contains(&(header.msg_num)) || in_binary {
|
||||
payload = match (context.get_encrypted(), encrypted_len) {
|
||||
(EncryptionProtocol::FullAes(_), Some(encrypted_len)) => {
|
||||
log::trace!("Binary: {:X?}", &processed_payload_buf[0..30]);
|
||||
|
||||
// if if context.debug {
|
||||
// log::trace!("Binary: {:X?}", &processed_payload_buf[0..30]);
|
||||
// }
|
||||
Some(BcPayloads::Binary(
|
||||
processed_payload_buf[0..(encrypted_len as usize)].to_vec(),
|
||||
))
|
||||
@@ -191,10 +194,13 @@ fn bc_modern_msg<'a>(
|
||||
_ => Some(BcPayloads::Binary(payload_buf.to_vec())),
|
||||
};
|
||||
} else {
|
||||
log::trace!(
|
||||
"Payload Txt: {:?}",
|
||||
String::from_utf8(processed_payload_buf.to_vec()).unwrap_or("Not Text".to_string())
|
||||
);
|
||||
if context.debug {
|
||||
println!(
|
||||
"Payload Txt: {:?}",
|
||||
String::from_utf8(processed_payload_buf.to_vec())
|
||||
.unwrap_or("Not Text".to_string())
|
||||
);
|
||||
}
|
||||
let xml = BcXml::try_parse(processed_payload_buf.as_slice()).map_err(|_| {
|
||||
error!("header.msg_id: {}", header.msg_id);
|
||||
error!(
|
||||
|
||||
@@ -210,6 +210,7 @@ pub(crate) struct BcContext {
|
||||
pub(crate) credentials: Credentials,
|
||||
pub(crate) in_bin_mode: HashSet<u16>,
|
||||
pub(crate) encryption_protocol: EncryptionProtocol,
|
||||
pub(crate) debug: bool,
|
||||
}
|
||||
|
||||
impl Bc {
|
||||
@@ -251,6 +252,7 @@ impl BcContext {
|
||||
credentials,
|
||||
in_bin_mode: HashSet::new(),
|
||||
encryption_protocol: EncryptionProtocol::Unencrypted,
|
||||
debug: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -260,6 +262,7 @@ impl BcContext {
|
||||
credentials: Default::default(),
|
||||
in_bin_mode: HashSet::new(),
|
||||
encryption_protocol,
|
||||
debug: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,6 +282,10 @@ impl BcContext {
|
||||
pub(crate) fn binary_off(&mut self, msg_id: u16) {
|
||||
self.in_bin_mode.remove(&msg_id);
|
||||
}
|
||||
|
||||
pub(crate) fn debug_on(&mut self) {
|
||||
self.debug = true;
|
||||
}
|
||||
}
|
||||
|
||||
impl BcHeader {
|
||||
|
||||
@@ -87,6 +87,8 @@ pub struct BcCameraOpt {
|
||||
pub discovery: DiscoveryMethods,
|
||||
/// Credentials for login
|
||||
pub credentials: Credentials,
|
||||
/// Toggle debug print of underlying data
|
||||
pub debug: bool,
|
||||
}
|
||||
|
||||
/// Used to choose the print format of various status messages like battery levels
|
||||
@@ -314,16 +316,20 @@ impl BcCamera {
|
||||
let (sink, source): (BcConnSink, BcConnSource) = {
|
||||
match BcCamera::find_camera(options).await? {
|
||||
CameraLocation::Tcp(addr) => {
|
||||
let (x, r) = TcpSource::new(addr, &username, passwd.as_ref())
|
||||
let (x, r) = TcpSource::new(addr, &username, passwd.as_ref(), options.debug)
|
||||
.await?
|
||||
.split();
|
||||
(Box::new(x), Box::new(r))
|
||||
}
|
||||
CameraLocation::Udp(discovery) => {
|
||||
let (x, r) =
|
||||
UdpSource::new_from_discovery(discovery, &username, passwd.as_ref())
|
||||
.await?
|
||||
.split();
|
||||
let (x, r) = UdpSource::new_from_discovery(
|
||||
discovery,
|
||||
&username,
|
||||
passwd.as_ref(),
|
||||
options.debug,
|
||||
)
|
||||
.await?
|
||||
.split();
|
||||
(Box::new(x), Box::new(r))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1030,8 +1030,11 @@ impl Discovery {
|
||||
pub(crate) async fn check_tcp(&self, addr: SocketAddr, channel_id: u8) -> Result<()> {
|
||||
let username = "admin";
|
||||
let password = Some("123456");
|
||||
let mut tcp_source =
|
||||
timeout(*MAXIMUM_WAIT, TcpSource::new(addr, username, password)).await??;
|
||||
let mut tcp_source = timeout(
|
||||
*MAXIMUM_WAIT,
|
||||
TcpSource::new(addr, username, password, false),
|
||||
)
|
||||
.await??;
|
||||
|
||||
let md5_username = md5_string(username, Md5Trunc::ZeroLast);
|
||||
let md5_password = password
|
||||
|
||||
@@ -18,11 +18,17 @@ impl TcpSource {
|
||||
addr: SocketAddr,
|
||||
username: T,
|
||||
password: Option<U>,
|
||||
debug: bool,
|
||||
) -> Result<TcpSource> {
|
||||
let stream = connect_to(addr).await?;
|
||||
|
||||
let codex = if debug {
|
||||
BcCodex::new_with_debug(Credentials::new(username, password))
|
||||
} else {
|
||||
BcCodex::new(Credentials::new(username, password))
|
||||
};
|
||||
Ok(Self {
|
||||
inner: Framed::new(stream, BcCodex::new(Credentials::new(username, password))),
|
||||
inner: Framed::new(stream, codex),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,15 +43,20 @@ impl UdpSource {
|
||||
camera_id: i32,
|
||||
username: T,
|
||||
password: Option<U>,
|
||||
debug: bool,
|
||||
) -> Result<Self> {
|
||||
let stream = Arc::new(connect().await?);
|
||||
|
||||
Self::new_from_socket(stream, addr, client_id, camera_id, username, password).await
|
||||
Self::new_from_socket(
|
||||
stream, addr, client_id, camera_id, username, password, debug,
|
||||
)
|
||||
.await
|
||||
}
|
||||
pub(crate) async fn new_from_discovery<T: Into<String>, U: Into<String>>(
|
||||
discovery: DiscoveryResult,
|
||||
username: T,
|
||||
password: Option<U>,
|
||||
debug: bool,
|
||||
) -> Result<Self> {
|
||||
// Ensure that the discovery keep alive are all stopped here
|
||||
// We now handle all coms in UdpSource
|
||||
@@ -62,6 +67,7 @@ impl UdpSource {
|
||||
discovery.camera_id,
|
||||
username,
|
||||
password,
|
||||
debug,
|
||||
)
|
||||
.await
|
||||
}
|
||||
@@ -73,14 +79,17 @@ impl UdpSource {
|
||||
camera_id: i32,
|
||||
username: T,
|
||||
password: Option<U>,
|
||||
debug: bool,
|
||||
) -> Result<Self> {
|
||||
let bcudp_source = BcUdpSource::new_from_socket(stream, addr).await?;
|
||||
let payload_source = bcudp_source.into_payload_source(client_id, camera_id);
|
||||
let async_read = payload_source.into_async_read().compat();
|
||||
let framed = Framed::new(
|
||||
async_read,
|
||||
BcCodex::new(Credentials::new(username, password)),
|
||||
);
|
||||
let codex = if debug {
|
||||
BcCodex::new_with_debug(Credentials::new(username, password))
|
||||
} else {
|
||||
BcCodex::new(Credentials::new(username, password))
|
||||
};
|
||||
let framed = Framed::new(async_read, codex);
|
||||
|
||||
Ok(Self { inner: framed })
|
||||
}
|
||||
|
||||
@@ -126,6 +126,9 @@ pub(crate) struct CameraConfig {
|
||||
|
||||
#[serde(default = "default_true", alias = "enable")]
|
||||
pub(crate) enabled: bool,
|
||||
|
||||
#[serde(default = "default_false", alias = "verbose")]
|
||||
pub(crate) debug: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
@@ -191,6 +194,10 @@ const fn default_true() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
const fn default_false() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn default_mqtt() -> Option<MqttConfig> {
|
||||
None
|
||||
}
|
||||
|
||||
@@ -97,6 +97,7 @@ impl AddressOrUid {
|
||||
username: camera_config.username.clone(),
|
||||
password: camera_config.password.clone(),
|
||||
},
|
||||
debug: camera_config.debug,
|
||||
};
|
||||
|
||||
trace!("Camera Info: {:?}", options);
|
||||
|
||||
Reference in new issue
Block a user