Skip to content

Commit

Permalink
Merge pull request #2 from betaboon/fix-get-status-observation
Browse files Browse the repository at this point in the history
Fix get status observation
  • Loading branch information
betaboon authored Mar 17, 2021
2 parents 6e4ced5 + 30b85b8 commit ecadd31
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
13 changes: 13 additions & 0 deletions aioairctrl/coap/aiocoap_monkeypatch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import asyncio
import functools

import aiocoap
from aiocoap import error
from aiocoap.numbers.constants import EXCHANGE_LIFETIME


def _deduplicate_message(self, message):
key = (message.remote, message.mid)
self.log.debug("MP: New unique message received")
self.loop.call_later(EXCHANGE_LIFETIME, functools.partial(self._recent_messages.pop, key))
self._recent_messages[key] = None
return False


aiocoap.messagemanager.MessageManager._deduplicate_message = _deduplicate_message


def __del__(self):
Expand Down
15 changes: 10 additions & 5 deletions aioairctrl/coap/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ async def get_status(self):
return state_reported["state"]["reported"]

async def observe_status(self):
def decrypt_status(response):
payload_encrypted = response.payload.decode()
payload = self._encryption_context.decrypt(payload_encrypted)
logger.debug("observation status: %s", payload)
status = json.loads(payload)
return status["state"]["reported"]

logger.debug("observing status")
request = Message(
code=GET,
Expand All @@ -80,12 +87,10 @@ async def observe_status(self):
)
request.opt.observe = 0
requester = self._client_context.request(request)
response = await requester.response
yield decrypt_status(response)
async for response in requester.observation:
payload_encrypted = response.payload.decode()
payload = self._encryption_context.decrypt(payload_encrypted)
logger.debug("observation status: %s", payload)
status = json.loads(payload)
yield status["state"]["reported"]
yield decrypt_status(response)

async def set_control_value(self, key, value, retry_count=5, resync=True) -> None:
return await self.set_control_values(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
license="MIT",
packages=find_packages(),
install_requires=[
"aiocoap==0.4b3",
"aiocoap==0.4.1",
"pycryptodomex",
],
entry_points={
Expand Down

0 comments on commit ecadd31

Please sign in to comment.