Make the loops in mqtt abort cleanly

This commit is contained in:
QuantumEntangledAndy committed 2021-08-10 14:53:35 +07:00
1 parent 20601b897e
commit cc517588fe
4 files changed
+13 -10

No files matched your search

+2 -2
View File
@@ -27,9 +27,9 @@ impl App {
let aborters = self.aborters.lock().unwrap();
for level in levels.split(':') {
if aborters.contains(&level.to_string()) {
return true;
return false;
}
}
false
true
}
}
+3 -1
View File
@@ -105,6 +105,7 @@ impl<'a> EventCam<'a> {
info!("{}: Logging in", camera_config.name);
camera.login(&camera_config.username, camera_config.password.as_deref())?;
info!("{}: Connected and logged in", camera_config.name);
(tx.lock()?).send(Messages::Login)?;
@@ -127,12 +128,14 @@ impl<'a> EventCam<'a> {
};
let _ = crossbeam::scope(|s| {
info!("{}: Listening to Camera Motion", camera_config.name);
s.spawn(|_| {
let _ = cam_motion.listen_on_motion(&mut motion_callback);
// If listen_on_motion returns then camera disconnect
app.abort(&camera_config.name);
});
info!("{}: Setting up camera actions", camera_config.name);
s.spawn(|_| {
let _ = message_handler.listen();
});
@@ -170,7 +173,6 @@ impl MotionOutput for MotionCallback {
}
_ => {}
}
Ok(true)
} else {
Ok(false)
+7 -7
View File
@@ -46,6 +46,7 @@ pub fn main(opt: Opt) -> Result<(), Error> {
for camera_config in &config.cameras {
if let Some(mqtt_config) = camera_config.mqtt.as_ref() {
let loop_arc_app = arc_app.clone();
info!("{}: Setting up mqtt", camera_config.name);
s.spawn(move |_| {
while loop_arc_app.running("app") {
let _ = listen_on_camera(camera_config, mqtt_config, loop_arc_app.clone());
@@ -76,9 +77,8 @@ fn listen_on_camera(
// Start listening to mqtt events
s.spawn(|_| {
if mqtt.start().is_err() {
event_cam.abort();
}
let _ = mqtt.start().is_err();
event_cam.abort();
});
// Listen on camera messages and post on mqtt
@@ -92,13 +92,13 @@ fn listen_on_camera(
}
}
Messages::MotionStop => {
if mqtt.send_message("status/motion", "on", true).is_err() {
error!("Failed to publish motion start for {}", cam_config.name);
if mqtt.send_message("status/motion", "off", true).is_err() {
error!("Failed to publish motion stop for {}", cam_config.name);
}
}
Messages::MotionStart => {
if mqtt.send_message("status/motion", "off", true).is_err() {
error!("Failed to publish motion stop for {}", cam_config.name);
if mqtt.send_message("status/motion", "on", true).is_err() {
error!("Failed to publish motion start for {}", cam_config.name);
}
}
_ => {}
+1
View File
@@ -116,6 +116,7 @@ impl Mqtt {
});
// Start polling messages
info!("{}: Starting listening to mqtt", name);
let mqtt_read_app = app;
let mqtt_reading = arc_me.clone();
let mqtt_read_name = name.to_string();