Implemented retry+sleep mechanism in Switchbot plugin in case of errors

This should prevent race conditions in case of command timeout+quick
disconnection and retry that can result in race conditions and double
free corruptions.
This commit is contained in:
Fabio Manganiello 2020-05-06 00:24:00 +02:00
parent 99fee9ce20
commit 26f6feebb7

View file

@ -1,4 +1,5 @@
import enum import enum
import time
from platypush.message.response.bluetooth import BluetoothScanResponse from platypush.message.response.bluetooth import BluetoothScanResponse
from platypush.plugins import action from platypush.plugins import action
@ -65,8 +66,18 @@ class SwitchSwitchbotPlugin(SwitchPlugin, BluetoothBlePlugin):
def _run(self, device: str, command: Command): def _run(self, device: str, command: Command):
if device in self.configured_devices_by_name: if device in self.configured_devices_by_name:
device = self.configured_devices_by_name[device] device = self.configured_devices_by_name[device]
n_tries = 1
try:
self.write(device, command.value, handle=self.handle, channel_type='random', binary=True)
except Exception as e:
self.logger.exception(e)
n_tries -= 1
if n_tries == 0:
raise e
time.sleep(5)
self.write(device, command.value, handle=self.handle, channel_type='random', binary=True)
return self.status(device) return self.status(device)
@action @action