Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Hacksaar
rinx
Commits
60d4e504
Commit
60d4e504
authored
Mar 06, 2016
by
Ralf
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make UnLocking conform with the general structure of a state
parent
0518a4ac
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
29 deletions
+38
-29
src/brain/situations.rs
src/brain/situations.rs
+38
-29
No files found.
src/brain/situations.rs
View file @
60d4e504
...
...
@@ -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,36 +26,44 @@ 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
}
else
{
LockAction
::
PressUnlock
});
t
.sleep
(
Duration
::
from_millis
(
100
));
lock_actor
.act
(
LockAction
::
Release
);
lock_actor
let
locker
=
Wakeable
::
spawn
(
actors
.lock
,
(),
move
|
t
,
mut
lock_actor
,
_
|
{
lock_actor
.act
(
if
locking
{
LockAction
::
PressLock
}
else
{
LockAction
::
PressUnlock
});
let
r
=
handle_events_timeout!
(
env
,
Duration
::
from_secs
(
1
),
EventHandler
::
new_safe
(
handlers
::
user_command_reject_handler
),
EventHandler
::
new
(|
ev
|
{
match
ev
{
Event
::
DoorLocked
(
b
)
if
b
==
locking
=>
{
HandlerResult
::
QuitLoop
}
_
=>
HandlerResult
::
Continue
,
t
.sleep
(
Duration
::
from_millis
(
100
));
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
),
EventHandler
::
new
(|
ev
|
{
match
ev
{
Event
::
DoorLocked
(
b
)
if
b
==
locking
=>
{
HandlerResult
::
QuitLoop
}
}));
actors
.lock
=
locker
.terminate
();
if
r
!=
EventsResult
::
Timeout
{
assert
!
(
r
==
EventsResult
::
ShuttingDown
||
env
.sensors
()
.door_locked
()
==
Some
(
locking
));
break
;
// the retries
_
=>
HandlerResult
::
Continue
,
}
}));
// Either we timed out, or we succeeded in (un)locking
actors
.lock
=
locker
.terminate
();
(
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? */
})
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment