Skip to content

Commit

Permalink
expose deviceexception, do automatic retry with an incremented messag…
Browse files Browse the repository at this point in the history
…e id in case receival from the device fails, drop contextmanager and force (re)discovery for each send()
  • Loading branch information
rytilahti committed Jul 22, 2017
1 parent c18e0e9 commit d2c10fa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion mirobo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from mirobo.containers import VacuumStatus, ConsumableStatus, CleaningDetails, CleaningSummary, Timer
from mirobo.vacuum import Vacuum, VacuumException
from mirobo.plug import Plug
from mirobo.device import Device
from mirobo.device import Device, DeviceException
20 changes: 11 additions & 9 deletions mirobo/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,23 @@ def __init__(self, ip: str, token: str,
self._devtype = None
self._serial = None

def __enter__(self) -> 'Device':
def do_discover(self):
"""Does a discover to fetch the devtype and serial."""
m = Device.discover(self.ip)
if m is not None:
self._devtype = m.header.value.devtype
self._serial = m.header.value.serial
self._device_ts = m.header.value.ts
if self.debug > 1:
_LOGGER.debug(m)
_LOGGER.debug("Discovered %s %s with ts: %s" % (self._devtype,
self._serial,
self._device_ts))
else:
_LOGGER.error("Unable to discover a device at address %s", self.ip)
raise DeviceException("Unable to discover the device %s" % self.ip)

return self

def __exit__(self, exc_type, exc_val, exc_tb) -> None:
pass
return m

@staticmethod
def discover(addr: str=None) -> Any:
Expand Down Expand Up @@ -88,10 +87,9 @@ def discover(addr: str=None) -> Any:
_LOGGER.warning("error while reading discover results: %s", ex)
break

def send(self, command: str, parameters: Any=None) -> Any:
def send(self, command: str, parameters: Any=None, retry_count=3) -> Any:
"""Build and send the given command."""
if self._devtype is None or self._serial is None:
self.__enter__() # when called outside of cm, initialize.
self.do_discover()

cmd = {
"id": self._id,
Expand Down Expand Up @@ -139,7 +137,11 @@ def send(self, command: str, parameters: Any=None) -> Any:
m.data.value))
return m.data.value["result"]
except OSError as ex:
_LOGGER.error("got error when receiving: %s", ex)
_LOGGER.error("Got error when receiving: %s", ex)
if retry_count > 0:
_LOGGER.warning("Retrying with incremented id, retries left: %s" % retry_count)
self.__id += 100
return self.send(command, parameters, retry_count-1)
raise DeviceException from ex

@property
Expand Down

0 comments on commit d2c10fa

Please sign in to comment.