Better management of inactivity timeout on Wiimote

This commit is contained in:
Fabio Manganiello 2018-12-01 19:22:10 +01:00
parent 66b6f0bc8b
commit 199e9fdba1

View file

@ -31,6 +31,7 @@ class WiimoteBackend(Backend):
_wiimote = None _wiimote = None
_inactivity_timeout = 300 _inactivity_timeout = 300
_connection_attempts = 0 _connection_attempts = 0
_last_btn_event_time = 0
def get_wiimote(self): def get_wiimote(self):
@ -40,6 +41,7 @@ class WiimoteBackend(Backend):
self._wiimote.rpt_mode = cwiid.RPT_ACC | cwiid.RPT_BTN | cwiid.RPT_MOTIONPLUS self._wiimote.rpt_mode = cwiid.RPT_ACC | cwiid.RPT_BTN | cwiid.RPT_MOTIONPLUS
self.logger.info('WiiMote connected') self.logger.info('WiiMote connected')
self._last_btn_event_time = time.time()
self.bus.post(WiimoteConnectionEvent()) self.bus.post(WiimoteConnectionEvent())
return self._wiimote return self._wiimote
@ -98,7 +100,6 @@ class WiimoteBackend(Backend):
self._connection_attempts = 0 self._connection_attempts = 0
last_state = {} last_state = {}
last_btn_event_time = 0
while not self.should_stop(): while not self.should_stop():
try: try:
@ -110,9 +111,9 @@ class WiimoteBackend(Backend):
self.bus.post(WiimoteEvent(**changed_state)) self.bus.post(WiimoteEvent(**changed_state))
if 'buttons' in changed_state: if 'buttons' in changed_state:
last_btn_event_time = time.time() self._last_btn_event_time = time.time()
elif last_state and time.time() - \ elif last_state and time.time() - \
last_btn_event_time >= self._inactivity_timeout: self._last_btn_event_time >= self._inactivity_timeout:
self.logger.info('Wiimote disconnected upon timeout') self.logger.info('Wiimote disconnected upon timeout')
self.close() self.close()