Add simple battery level
This commit is contained in:
2 files changed
+42
No files matched your search
@@ -33,6 +33,7 @@ pub(crate) enum Messages {
|
||||
Ptz(Direction),
|
||||
Preset(i8),
|
||||
Snap(Vec<u8>),
|
||||
BatteryLevel(u32),
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
@@ -215,6 +216,11 @@ impl EventCamThread {
|
||||
tx: self.tx.clone(),
|
||||
camera: arc_cam.clone(),
|
||||
};
|
||||
|
||||
let mut battery_thread = BatteryThread {
|
||||
tx: self.tx.clone(),
|
||||
camera: arc_cam.clone(),
|
||||
};
|
||||
|
||||
let mut keepalive_thread = KeepaliveThread {
|
||||
camera: arc_cam.clone(),
|
||||
@@ -274,6 +280,18 @@ impl EventCamThread {
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
val = async {
|
||||
info!("{}: Updating Battery Level", camera_config.name);
|
||||
battery_thread.run().await
|
||||
} => {
|
||||
if let Err(e) = val {
|
||||
error!("Battery thread aborted: {:?}", e);
|
||||
Err(e)
|
||||
} else {
|
||||
debug!("Normal finish on Battery thread");
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
val = async {
|
||||
info!("{}: Setting up camera actions", camera_config.name);
|
||||
message_handler.listen().await
|
||||
@@ -363,6 +381,22 @@ impl SnapThread {
|
||||
}
|
||||
}
|
||||
|
||||
struct BatteryLevelThread {
|
||||
tx: Sender<Messages>,
|
||||
camera: Arc<BcCamera>,
|
||||
}
|
||||
|
||||
impl BatteryLevelThread {
|
||||
async fn run(&mut self) -> Result<()> {
|
||||
let mut inter = interval(Duration::from_millis(500));
|
||||
loop {
|
||||
inter.tick().await;
|
||||
let battery = self.camera.battery_info().await?;
|
||||
self.tx.send(Messages::BatteryLevel(battery.battery_percent)).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct KeepaliveThread {
|
||||
camera: Arc<BcCamera>,
|
||||
}
|
||||
|
||||
@@ -223,6 +223,14 @@ async fn listen_on_camera(cam_config: Arc<CameraConfig>, mqtt_config: &MqttConfi
|
||||
format!("Failed to publish preview over MQTT for {}", camera_name)
|
||||
})?;
|
||||
}
|
||||
Messages::BatteryLevel(data) => {
|
||||
mqtt_sender_cam
|
||||
.send_message("status/battery_level", format!("{}", data).as_str(), true)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!("Failed to publish battery level over MQTT for {}", camera_name)
|
||||
})?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user