Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage leasing resources properly #92

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions spot_wrapper/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,8 @@ def resetEStop(self) -> None:
"""Get keepalive for eStop"""
self._estop_endpoint = EstopEndpoint(self._estop_client, SPOT_CLIENT_NAME, self._estop_timeout)
self._estop_endpoint.force_simple_setup() # Set this endpoint as the robot's sole estop.
if self._estop_keepalive:
self._estop_keepalive.shutdown()
self._estop_keepalive = EstopKeepAlive(self._estop_endpoint)

def assertEStop(self, severe: bool = True) -> typing.Tuple[bool, str]:
Expand Down Expand Up @@ -975,21 +977,44 @@ def releaseEStop(self) -> None:
"""Stop eStop keepalive"""
if self._estop_keepalive:
self._estop_keepalive.stop()
self._estop_keepalive.shutdown()
self._estop_keepalive = None
self._estop_endpoint = None

def getLease(self) -> None:
def takeLease(self) -> typing.Tuple[bool, typing.Optional[Lease]]:
"""Take lease for the robot, which may have been taken already."""
lease = self._lease_client.take()
have_new_lease = (self._lease is None and lease is not None) or (
str(lease.lease_proto) != str(self._lease.lease_proto)
)
if have_new_lease:
if self._lease_keepalive is not None:
self._lease_keepalive.shutdown()
self._lease_keepalive = LeaseKeepAlive(self._lease_client)
self._lease = lease
return have_new_lease, self._lease

def getLease(self) -> typing.Optional[Lease]:
"""Get a lease for the robot and keep the lease alive automatically."""
if self._use_take_lease:
self._lease = self._lease_client.take()
lease = self._lease_client.take()
else:
self._lease = self._lease_client.acquire()

self._lease_keepalive = LeaseKeepAlive(self._lease_client)
lease = self._lease_client.acquire()
have_new_lease = (self._lease is None and lease is not None) or (
str(lease.lease_proto) != str(self._lease.lease_proto)
)
if have_new_lease:
if self._lease_keepalive is not None:
self._lease_keepalive.shutdown()
self._lease_keepalive = LeaseKeepAlive(self._lease_client)
self._lease = lease
return self._lease

def releaseLease(self) -> None:
"""Return the lease on the body."""
if self._lease:
self._lease_keepalive.shutdown()
self._lease_keepalive = None
self._lease_client.return_lease(self._lease)
self._lease = None

Expand Down
Loading