Make command timeout for workers configurable (#62)
This commit is contained in:
+2
-1
@@ -1,5 +1,6 @@
|
||||
class BaseWorker:
|
||||
def __init__(self, **args):
|
||||
def __init__(self, command_timeout, **args):
|
||||
self.command_timeout = command_timeout
|
||||
for arg, value in args.items():
|
||||
setattr(self, arg, value)
|
||||
self._setup()
|
||||
|
||||
@@ -84,8 +84,8 @@ class BlescanmultiWorker(BaseWorker):
|
||||
scan_timeout = 10. # type: float
|
||||
scan_passive = True # type: str or bool
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(BlescanmultiWorker, self).__init__(**kwargs)
|
||||
def __init__(self, command_timeout, **kwargs):
|
||||
super(BlescanmultiWorker, self).__init__(command_timeout, **kwargs)
|
||||
self.scanner = Scanner().withDelegate(ScanDelegate())
|
||||
self.last_status = [
|
||||
BleDeviceStatus(self, mac, name) for name, mac in self.devices.items()
|
||||
|
||||
@@ -6,6 +6,9 @@ from workers.base import BaseWorker
|
||||
REQUIREMENTS = ['git+https://github.com/zewelor/linak_bt_desk.git@aa9412f98b3044be34c70e89d02721e6813ea731#egg=linak_bt_desk']
|
||||
|
||||
class LinakdeskWorker(BaseWorker):
|
||||
|
||||
SCAN_TIMEOUT = 20
|
||||
|
||||
def _setup(self):
|
||||
from linak_dpg_bt import LinakDesk
|
||||
|
||||
@@ -15,7 +18,7 @@ class LinakdeskWorker(BaseWorker):
|
||||
return [MqttMessage(topic=self.format_topic('height/cm'), payload=self._get_height())]
|
||||
|
||||
def _get_height(self):
|
||||
with timeout(20, exception=TimeoutError):
|
||||
with timeout(SCAN_TIMEOUT, exception=TimeoutError('Retrieving the height from {} device {} timed out after {} seconds'.format(repr(self), self.mac, SCAN_TIMEOUT))):
|
||||
self.desk.read_dpg_data()
|
||||
return self.desk.current_height_with_offset.cm
|
||||
|
||||
|
||||
+5
-2
@@ -11,6 +11,9 @@ REQUIREMENTS = ['bluepy']
|
||||
# sudo setcap 'cap_net_raw,cap_net_admin+eip' /usr/local/lib/python3.6/dist-packages/bluepy/bluepy-helper
|
||||
|
||||
class MiscaleWorker(BaseWorker):
|
||||
|
||||
SCAN_TIMEOUT = 5
|
||||
|
||||
def status_update(self):
|
||||
return [MqttMessage(topic=self.format_topic('weight/kg'), payload=self._get_weight())]
|
||||
|
||||
@@ -19,9 +22,9 @@ class MiscaleWorker(BaseWorker):
|
||||
|
||||
scan_processor = ScanProcessor(self.mac)
|
||||
scanner = btle.Scanner().withDelegate(scan_processor)
|
||||
scanner.scan(5, passive=True)
|
||||
scanner.scan(SCAN_TIMEOUT, passive=True)
|
||||
|
||||
with timeout(5, exception=TimeoutError):
|
||||
with timeout(SCAN_TIMEOUT, exception=TimeoutError('Retrieving the weight from {} device {} timed out after {} seconds'.format(repr(self), self.mac, SCAN_TIMEOUT))):
|
||||
while scan_processor.weight is None:
|
||||
time.sleep(1)
|
||||
return scan_processor.weight
|
||||
|
||||
Reference in New Issue
Block a user