Commit 60d4e504 authored by Ralf's avatar Ralf
Browse files

make UnLocking conform with the general structure of a state

parent 0518a4ac
......@@ -10,11 +10,12 @@ use brain::{Actors, Situation};
#[derive(Debug)]
pub struct UnLocking {
locking: bool,
remaining_retries: u32,
}
impl UnLocking {
pub fn new(locking: bool) -> UnLocking {
UnLocking { locking: locking }
pub fn new(locking: bool, remaining_retries: u32) -> UnLocking {
UnLocking { locking: locking, remaining_retries: remaining_retries }
}
}
......@@ -25,7 +26,6 @@ impl Situation for UnLocking {
-> (Actors, Option<Box<Situation>>) {
// TODO: abort if already locked
let locking = self.locking;
for _ in 0..3 {
let locker = Wakeable::spawn(actors.lock, (), move |t, mut lock_actor, _| {
lock_actor.act(if locking {
LockAction::PressLock
......@@ -36,6 +36,7 @@ impl Situation for UnLocking {
lock_actor.act(LockAction::Release);
lock_actor
});
// for max. 1 second, handle whatever events we see
let r = handle_events_timeout!(env,
Duration::from_secs(1),
EventHandler::new_safe(handlers::user_command_reject_handler),
......@@ -47,14 +48,22 @@ impl Situation for UnLocking {
_ => HandlerResult::Continue,
}
}));
// Either we timed out, or we succeeded in (un)locking
actors.lock = locker.terminate();
if r != EventsResult::Timeout {
assert!(r == EventsResult::ShuttingDown ||
env.sensors().door_locked() == Some(locking));
break; // the retries
(actors,
match r {
EventsResult::ShuttingDown => None,
EventsResult::Timeout =>
{
if self.remaining_retries > 0 {
Some(Box::new(UnLocking::new(self.locking, self.remaining_retries - 1)))
} else {
None /* FIXME: What do do when all attempts failed? */
}
}
(actors, None) // FIXME
EventsResult::Quit =>
None /* FIXME: Which state to go in next? */
})
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment