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

stop observing #85

Merged
merged 2 commits into from
May 9, 2021
Merged
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 deletions pyairctrl/coap_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@ class CoAPAirClient(HTTPAirClientBase):
def __init__(self, host, port=5683, debug=False):
super().__init__(host, port, debug)
self.client = self._create_coap_client(self.server, self.port)
self.response = None
self._sync()

def __del__(self):
# TODO call a close method explicitly instead
self.client.stop()
if self.response:
self.client.cancel_observing(self.response, True)
self.client.stop()

def _create_coap_client(self, host, port):
return HelperClient(server=(host, port))
Expand Down Expand Up @@ -142,8 +145,8 @@ def _get(self):
try:
request = self.client.mk_request(defines.Codes.GET, path)
request.observe = 0
response = self.client.send_request(request, None, 2)
encrypted_payload = response.payload
self.response = self.client.send_request(request, None, 2)
encrypted_payload = self.response.payload
decrypted_payload = self._decrypt_payload(encrypted_payload)
except WrongDigestException:
print("Message from device got corrupted")
Expand Down
3 changes: 3 additions & 0 deletions pyairctrl/plain_coap_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def _send_over_socket(self, destination, packet):

def _get(self):
path = "/sys/dev/status"
response = None
try:
client = self._create_coap_client(self.server, self.port)
self._send_hello_sequence(client)
Expand All @@ -56,6 +57,8 @@ def _get(self):
request.observe = 0
response = client.send_request(request, None, 2)
finally:
if response:
client.cancel_observing(response, True)
client.stop()

if response:
Expand Down