diff options
Diffstat (limited to 'helpers/action.py')
-rw-r--r-- | helpers/action.py | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/helpers/action.py b/helpers/action.py index a6c48e9..010a6ca 100644 --- a/helpers/action.py +++ b/helpers/action.py | |||
@@ -37,24 +37,23 @@ class Action: | |||
37 | { | 37 | { |
38 | 'trigger': 'fail', | 38 | 'trigger': 'fail', |
39 | 'source': 'loading', | 39 | 'source': 'loading', |
40 | 'dest': 'failed' | 40 | 'dest': 'failed', |
41 | 'after': 'poll_loaded' | ||
41 | }, | 42 | }, |
42 | { | 43 | { |
43 | 'trigger': 'success', | 44 | 'trigger': 'success', |
44 | 'source': 'loading', | 45 | 'source': 'loading', |
45 | 'dest': 'loaded' | 46 | 'dest': 'loaded', |
47 | 'after': 'poll_loaded' | ||
46 | }, | 48 | }, |
47 | { | 49 | { |
48 | 'trigger': 'run', | 50 | 'trigger': 'run', |
49 | 'source': 'loaded', | 51 | 'source': 'loaded', |
50 | 'dest': 'loaded_running', | 52 | 'dest': 'loaded_running', |
51 | 'after': 'finish_action' | 53 | 'after': 'finish_action', |
52 | }, | 54 | # if a child has no transitions, then it is bubbled to the parent, |
53 | { | 55 | # and we don't want that. Not useful in that machine precisely. |
54 | 'trigger': 'interrupt', | 56 | 'conditions': ['is_loaded'] |
55 | 'source': 'loaded_running', | ||
56 | 'dest': 'loaded', | ||
57 | 'before': 'trigger_interrupt' | ||
58 | }, | 57 | }, |
59 | { | 58 | { |
60 | 'trigger': 'finish_action', | 59 | 'trigger': 'finish_action', |
@@ -74,12 +73,11 @@ class Action: | |||
74 | self.arguments = kwargs | 73 | self.arguments = kwargs |
75 | self.sleep_event = None | 74 | self.sleep_event = None |
76 | self.waiting_music = None | 75 | self.waiting_music = None |
77 | self.load() | ||
78 | 76 | ||
79 | def ready(self): | 77 | def is_loaded_or_failed(self): |
80 | return self.is_loaded(allow_substates=True) | 78 | return self.is_loaded(allow_substates=True) or self.is_failed() |
81 | 79 | ||
82 | def callback_loaded(self, success): | 80 | def callback_music_loaded(self, success): |
83 | if success: | 81 | if success: |
84 | self.success() | 82 | self.success() |
85 | else: | 83 | else: |
@@ -89,19 +87,24 @@ class Action: | |||
89 | def on_enter_loading(self): | 87 | def on_enter_loading(self): |
90 | if self.action in self.ACTION_TYPES: | 88 | if self.action in self.ACTION_TYPES: |
91 | if 'music' in self.arguments: | 89 | if 'music' in self.arguments: |
92 | self.arguments['music'].subscribe_loaded(self.callback_loaded) | 90 | self.arguments['music'].subscribe_loaded(self.callback_music_loaded) |
93 | else: | 91 | else: |
94 | self.success() | 92 | self.success() |
95 | else: | 93 | else: |
96 | error_print("Unknown action {}".format(self.action)) | 94 | error_print("Unknown action {}".format(self.action)) |
97 | self.fail() | 95 | self.fail() |
98 | 96 | ||
99 | |||
100 | def on_enter_loaded_running(self): | 97 | def on_enter_loaded_running(self): |
101 | debug_print(self.description()) | 98 | debug_print(self.description()) |
102 | getattr(self, self.action)(**self.arguments) | 99 | getattr(self, self.action)(**self.arguments) |
103 | 100 | ||
104 | def trigger_interrupt(self): | 101 | def poll_loaded(self): |
102 | self.key.callback_action_ready(self, | ||
103 | self.is_loaded(allow_substates=True)) | ||
104 | |||
105 | # This one cannot be in the Machine state since it would be queued to run | ||
106 | # *after* the wait is ended... | ||
107 | def interrupt(self): | ||
105 | if getattr(self, self.action + "_interrupt", None): | 108 | if getattr(self, self.action + "_interrupt", None): |
106 | return getattr(self, self.action + "_interrupt")(**self.arguments) | 109 | return getattr(self, self.action + "_interrupt")(**self.arguments) |
107 | 110 | ||
@@ -191,11 +194,12 @@ class Action: | |||
191 | self.mapping.add_wait_id(set_wait_id, self) | 194 | self.mapping.add_wait_id(set_wait_id, self) |
192 | 195 | ||
193 | self.sleep_event = threading.Event() | 196 | self.sleep_event = threading.Event() |
197 | self.sleep_event_timer = threading.Timer(duration, self.sleep_event.set) | ||
194 | 198 | ||
195 | if music is not None: | 199 | if music is not None: |
196 | music.wait_end() | 200 | music.wait_end() |
197 | 201 | ||
198 | threading.Timer(duration, self.sleep_event.set).start() | 202 | self.sleep_event_timer.start() |
199 | self.sleep_event.wait() | 203 | self.sleep_event.wait() |
200 | 204 | ||
201 | # Action messages | 205 | # Action messages |
@@ -326,6 +330,7 @@ class Action: | |||
326 | def wait_interrupt(self, duration=0, music=None, **kwargs): | 330 | def wait_interrupt(self, duration=0, music=None, **kwargs): |
327 | if self.sleep_event is not None: | 331 | if self.sleep_event is not None: |
328 | self.sleep_event.set() | 332 | self.sleep_event.set() |
333 | self.sleep_event_timer.cancel() | ||
329 | if music is not None: | 334 | if music is not None: |
330 | music.wait_event.set() | 335 | music.wait_event.set() |
331 | 336 | ||