From 383a075f39095ba2f5240644f16ada7170c8674c Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 1 Dec 2018 18:43:18 +0100 Subject: [PATCH] Handling Wiimote timeout on Wiimote backend --- platypush/backend/wiimote.py | 34 +++++++++++++++++++++++++++++----- platypush/plugins/wiimote.py | 2 +- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/platypush/backend/wiimote.py b/platypush/backend/wiimote.py index 4a20ad18..18895d7a 100644 --- a/platypush/backend/wiimote.py +++ b/platypush/backend/wiimote.py @@ -27,6 +27,8 @@ class WiimoteBackend(Backend): """ _wiimote = None + _inactivity_timeout = 300 + _connection_attempts = 0 def msg_callback(self): @@ -87,10 +89,24 @@ class WiimoteBackend(Backend): return parsed_state + def close(self): + if not self._wiimote: + return + + try: + self._wiimote.close() + except: + pass + + self._wiimote = None + + def run(self): super().run() - connection_attempts = 0 + + self._connection_attempts = 0 last_state = {} + last_btn_event_time = 0 while not self.should_stop(): try: @@ -100,17 +116,25 @@ class WiimoteBackend(Backend): if changed_state: self.bus.post(WiimoteEvent(**changed_state)) - connection_attempts = 0 + + if 'buttons' in changed_state: + last_btn_event_time = time.time() + elif last_state and time.time() - \ + last_btn_event_time >= self._inactivity_timeout: + self.logger.info('Wiimote disconnected upon timeout') + self.close() + last_state = state time.sleep(0.1) except RuntimeError as e: if type(e) == RuntimeError and str(e) == 'Error opening wiimote connection': - if connection_attempts == 0: + if self._connection_attempts == 0: self.logger.info('Press 1+2 to pair your WiiMote controller') else: self.logger.exception(e) - self._wiimote = None - connection_attempts += 1 + + self.close() + self._connection_attempts += 1 # vim:sw=4:ts=4:et: diff --git a/platypush/plugins/wiimote.py b/platypush/plugins/wiimote.py index e6af6d62..b1e74d29 100644 --- a/platypush/plugins/wiimote.py +++ b/platypush/plugins/wiimote.py @@ -34,7 +34,7 @@ class WiimotePlugin(Plugin): """ Closes the connection with the WiiMote """ - self._get_wiimote().close() + get_backend('wiimote').close() @action