forked from thirtythreeforty/neolink
-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from QuantumEntangledAndy/battery
Battery message and requests
- Loading branch information
Showing
13 changed files
with
282 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ description = "A standards-compliant bridge to Reolink IP cameras" | |
version = "0.5.4" | ||
authors = ["George Hilliard <[email protected]>", "Andrew King <[email protected]>"] | ||
edition = "2018" | ||
default-run = "neolink" | ||
|
||
[workspace] | ||
members = [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
//! Handles battery related messages | ||
//! | ||
//! There are primarily two messages: | ||
//! - BatteryInfoList which the camera sends as part of its login info | ||
//! - BatteryInfo which the client can request on demand | ||
//! | ||
use super::{BcCamera, PrintFormat, Result}; | ||
use crate::{ | ||
bc::{model::*, xml::BatteryInfo}, | ||
Error, | ||
}; | ||
|
||
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, format: PrintFormat) -> Result<()> { | ||
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\ | ||
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 | ||
}) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
/// Requests the current battery status of the camera | ||
pub async fn battery_info(&self) -> Result<BatteryInfo> { | ||
let connection = self.get_connection(); | ||
|
||
let msg_num = self.new_message_num(); | ||
let mut sub = connection.subscribe(msg_num).await?; | ||
|
||
let msg = Bc { | ||
meta: BcMeta { | ||
msg_id: MSG_ID_BATTERY_INFO, | ||
channel_id: self.channel_id, | ||
msg_num, | ||
stream_type: 0, | ||
response_code: 0, | ||
class: 0x6414, | ||
}, | ||
body: BcBody::ModernMsg(ModernMsg { | ||
extension: Some(Extension { | ||
channel_id: Some(self.channel_id), | ||
..Default::default() | ||
}), | ||
payload: None, | ||
}), | ||
}; | ||
|
||
sub.send(msg).await?; | ||
let msg = sub.recv().await?; | ||
|
||
if let Bc { | ||
meta: BcMeta { | ||
response_code: 200, .. | ||
}, | ||
body: | ||
BcBody::ModernMsg(ModernMsg { | ||
payload: | ||
Some(BcPayloads::BcXml(BcXml { | ||
battery_info: Some(battery_info), | ||
.. | ||
})), | ||
.. | ||
}), | ||
} = msg | ||
{ | ||
Ok(battery_info) | ||
} else { | ||
Err(Error::UnintelligibleReply { | ||
reply: std::sync::Arc::new(Box::new(msg)), | ||
why: "The camera did not accept the battery info (maybe no battery) command.", | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.