Skip to content

Commit

Permalink
Bring back NoConnectionClient
Browse files Browse the repository at this point in the history
Having this class be the default for the client
attributes (telemetry_watcher, ctrl_client, etc) automatically makes
the error messages much better.
  • Loading branch information
sindrehan committed Jun 21, 2023
1 parent 25d090e commit bf14633
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions blueye/sdk/drone.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ def set_drone_time(self, time: int):
self._parent_drone._req_rep_client.sync_time(time)


class _NoConnectionClient:
"""A client that raises a ConnectionError if you use any of its functions"""

def __getattr__(self, name):
def method(*args, **kwargs):
raise ConnectionError(
"The connection to the drone is not established, "
"try calling the connect method before retrying"
)

return method


class Telemetry:
def __init__(self, parent_drone: "Drone"):
self._parent_drone = parent_drone
Expand Down Expand Up @@ -161,6 +174,10 @@ def __init__(
self.connected = False
self.client_id: int = None
self.in_control: bool = False
self._watchdog_publisher = _NoConnectionClient()
self._telemetry_watcher = _NoConnectionClient()
self._req_rep_client = _NoConnectionClient()
self._ctrl_client = _NoConnectionClient()
if auto_connect is True:
self.connect(timeout=timeout, disconnect_other_clients=disconnect_other_clients)

Expand Down Expand Up @@ -254,10 +271,10 @@ def disconnect(self):
self._req_rep_client.stop()
self._ctrl_client.stop()

self._watchdog_publisher = None
self._telemetry_watcher = None
self._req_rep_client = None
self._ctrl_client = None
self._watchdog_publisher = _NoConnectionClient()
self._telemetry_watcher = _NoConnectionClient()
self._req_rep_client = _NoConnectionClient()
self._ctrl_client = _NoConnectionClient()

self.connected = False

Expand Down

0 comments on commit bf14633

Please sign in to comment.