Add configurable battery printing

This commit is contained in:
QuantumEntangledAndy committed 2023-03-02 15:55:15 +07:00
1 parent 1bcf1cfe85
commit 92447b63b8
9 files changed
+109 -29

No files matched your search

Generated
+1
View File
@@ -1272,6 +1272,7 @@ dependencies = [
"nom",
"rand",
"regex",
"serde",
"socket2 0.3.19",
"time",
"tokio",
+1
View File
@@ -25,6 +25,7 @@ md5 = "0.7"
nom = { version = "6.1.2", features = ["alloc"] }
rand = "0.8.4"
regex = "1.5.4"
serde = { version = "1.0", features = ["derive"] }
socket2 = "0.3"
time = "0.2"
tokio = { version = "1.24.2", features = ["full"] }
+31 -2
View File
@@ -1,6 +1,7 @@
use crate::bc;
use futures::stream::StreamExt;
use log::*;
use serde::{Deserialize, Serialize};
use std::net::ToSocketAddrs;
use std::sync::atomic::{AtomicBool, AtomicU16, Ordering};
@@ -51,6 +52,20 @@ pub struct BcCamera {
credentials: Credentials,
}
/// Used to choode the print format of various status messages like battery levels
///
/// Currently this is just the format of battery levels but if we ever got more status
/// messages then they will also use this information
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum PrintFormat {
/// None, don't print
None,
/// A human readable output
Human,
/// Xml formatted
Xml,
}
impl BcCamera {
///
/// Create a new camera interface with this address and channel ID
@@ -70,6 +85,7 @@ impl BcCamera {
channel_id: u8,
username: V,
passwd: Option<W>,
aux_info_format: PrintFormat,
) -> Result<Self> {
let username: String = username.into();
let passwd: Option<String> = passwd.map(|t| t.into());
@@ -83,6 +99,7 @@ impl BcCamera {
channel_id,
&username,
passwd.as_ref(),
aux_info_format,
)
.await
{
@@ -112,12 +129,14 @@ impl BcCamera {
username: U,
passwd: Option<V>,
discovery_method: DiscoveryMethods,
aux_info_format: PrintFormat,
) -> Result<Self> {
Self::new(
SocketAddrOrUid::Uid(uid.to_string(), discovery_method),
channel_id,
username,
passwd,
aux_info_format,
)
.await
}
@@ -147,6 +166,7 @@ impl BcCamera {
channel_id: u8,
username: V,
passwd: Option<W>,
aux_info_format: PrintFormat,
) -> Result<Self> {
let addr_iter = match host.to_socket_addrs_or_uid() {
Ok(iter) => iter,
@@ -155,7 +175,15 @@ impl BcCamera {
let username: String = username.into();
let passwd: Option<String> = passwd.map(|t| t.into());
for addr_or_uid in addr_iter {
if let Ok(cam) = Self::new(addr_or_uid, channel_id, &username, passwd.as_ref()).await {
if let Ok(cam) = Self::new(
addr_or_uid,
channel_id,
&username,
passwd.as_ref(),
aux_info_format,
)
.await
{
return Ok(cam);
}
}
@@ -185,6 +213,7 @@ impl BcCamera {
channel_id: u8,
username: U,
passwd: Option<V>,
aux_info_format: PrintFormat,
) -> Result<Self> {
let username: String = username.into();
let passwd: Option<String> = passwd.map(|t| t.into());
@@ -307,7 +336,7 @@ impl BcCamera {
credentials: Credentials::new(username, passwd),
};
me.keepalive().await?;
me.monitor_battery().await?;
me.monitor_battery(aux_info_format).await?;
Ok(me)
}
+38 -22
View File
@@ -5,21 +5,20 @@
//! - BatteryInfo which the client can request on demand
//!
use super::{BcCamera, Result};
use super::{BcCamera, PrintFormat, Result};
use crate::{
bc::{model::*, xml::BatteryInfo},
Error,
};
use log::*;
impl BcCamera {
/// Create a handller to respond to battery messages
/// These messages are sent by the camera on login and maybe
/// also on low battery events
pub async fn monitor_battery(&self) -> Result<()> {
pub async fn monitor_battery(&self, format: PrintFormat) -> Result<()> {
let connection = self.get_connection();
connection
.handle_msg(MSG_ID_BATTERY_INFO_LIST, |bc| {
.handle_msg(MSG_ID_BATTERY_INFO_LIST, move |bc| {
if let Bc {
body:
BcBody::ModernMsg(ModernMsg {
@@ -34,24 +33,41 @@ impl BcCamera {
} = bc
{
for battery in battery_list.battery_info.iter() {
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,
);
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(),
)
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", bat_ser);
}
}
}
}
None
+12
View File
@@ -63,6 +63,18 @@ address = "192.168.1.187:9000"
#
# discovery = "relay"
# Certain types of camera emit status messages (such as battery levels)
#
# By default we hide these status messages from the user but you can instead requst that
# they be printed to stdout using print_format
#
# Valid values are:
# - None
# - Human
# - Xml
#
# print_format = "None"
[[cameras]]
name = "storage shed"
+8
View File
@@ -1,4 +1,5 @@
use lazy_static::lazy_static;
use neolink_core::bc_protocol::PrintFormat;
use regex::Regex;
use serde::Deserialize;
use std::clone::Clone;
@@ -99,6 +100,9 @@ pub(crate) struct CameraConfig {
#[serde(default = "default_strict")]
/// If strict then the media stream will error in the event that the media packets are not as expected
pub(crate) strict: bool,
#[serde(default = "default_print", alias = "print")]
pub(crate) print_format: PrintFormat,
}
#[derive(Debug, Deserialize, Validate, Clone)]
@@ -143,6 +147,10 @@ fn default_mqtt() -> Option<MqttConfig> {
None
}
fn default_print() -> PrintFormat {
PrintFormat::None
}
fn default_discovery() -> String {
"Relay".to_string()
}
+1
View File
@@ -159,6 +159,7 @@ impl EventCam {
camera_config.channel_id,
&camera_config.username,
camera_config.password.as_ref(),
camera_config.print_format,
)
.await
.with_context(|| {
+1
View File
@@ -67,6 +67,7 @@ impl RtspCamera {
config.channel_id,
&config.username,
config.password.as_ref(),
config.print_format,
)
.await
.with_context(|| {
+16 -5
View File
@@ -4,7 +4,7 @@ use log::*;
use super::config::{CameraConfig, Config};
use anyhow::{anyhow, Context, Error, Result};
use neolink_core::bc_protocol::{BcCamera, DiscoveryMethods, MaxEncryption};
use neolink_core::bc_protocol::{BcCamera, DiscoveryMethods, MaxEncryption, PrintFormat};
use std::fmt::{Display, Error as FmtError, Formatter};
pub(crate) enum AddressOrUid {
@@ -56,14 +56,24 @@ impl AddressOrUid {
channel_id: u8,
username: T,
passwd: Option<U>,
aux_print_format: PrintFormat,
) -> Result<BcCamera, Error> {
match self {
AddressOrUid::Address(host) => {
Ok(BcCamera::new_with_addr(host, channel_id, username, passwd).await?)
}
AddressOrUid::Uid(host, method) => {
Ok(BcCamera::new_with_uid(host, channel_id, username, passwd, *method).await?)
Ok(
BcCamera::new_with_addr(host, channel_id, username, passwd, aux_print_format)
.await?,
)
}
AddressOrUid::Uid(host, method) => Ok(BcCamera::new_with_uid(
host,
channel_id,
username,
passwd,
*method,
aux_print_format,
)
.await?),
}
}
}
@@ -90,6 +100,7 @@ pub(crate) async fn connect_and_login(camera_config: &CameraConfig) -> Result<Bc
camera_config.channel_id,
&camera_config.username,
camera_config.password.as_ref(),
camera_config.print_format,
)
.await
.with_context(|| {