Skip to content

Commit f6644e8

Browse files
committed
Use notification for getting response from Device
1 parent 107b688 commit f6644e8

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

tion/s3.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,20 @@ def process_mode(mode_code: int) -> str:
9595

9696
try:
9797
self._do_action(self._connect)
98-
self.notify.read()
98+
self._enable_notifications()
9999
self._do_action(self._try_write, request=get_status_command())
100-
byte_response = self._do_action(self.__try_get_state)
101-
result = decode_response(byte_response)
100+
101+
i = 0
102+
while i < 10:
103+
if self._btle.waitForNotifications(1.0):
104+
byte_response = self._delegation.data
105+
result = decode_response(byte_response)
106+
break
107+
i += 1
108+
else:
109+
_LOGGER.debug("Waiting too long for data")
110+
self.notify.read()
111+
102112
except TionException as e:
103113
_LOGGER.error(str(e))
104114
result = {"code": 400, "error": "Got exception " + str(e)}
@@ -134,7 +144,6 @@ def encode_status(status: str) -> int:
134144
return new_settings
135145
try:
136146
self._do_action(self._connect)
137-
self.notify.read()
138147
self._do_action(self._try_write, request=encode_request(request))
139148
except TionException as e:
140149
_LOGGER.error(str(e))

tion/tion.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,29 @@
33
from typing import Callable
44

55
from bluepy import btle
6+
from bluepy.btle import DefaultDelegate
67

78
_LOGGER = logging.getLogger(__name__)
89

910

11+
class TionDelegation(DefaultDelegate):
12+
def __init__(self):
13+
self._data = None
14+
DefaultDelegate.__init__(self)
15+
16+
def handleNotification(self, cHandle, data):
17+
self._data = data
18+
_LOGGER.debug("%s", data)
19+
20+
def handleDiscovery(self, dev, isNewDev, isNewData):
21+
if isNewDev:
22+
_LOGGER.debug("Discovered device %s", dev.addr)
23+
elif isNewData:
24+
_LOGGER.debug("Received new data from %s", dev.addr)
25+
@property
26+
def data(self):
27+
return self._data
28+
1029
class TionException(Exception):
1130
def __init__(self, expression, message):
1231
self.expression = expression
@@ -21,6 +40,7 @@ class tion:
2140
def __init__(self, mac: str):
2241
self._mac = mac
2342
self._btle: btle.Peripheral = btle.Peripheral(None)
43+
self._delegation = TionDelegation()
2444

2545
@abc.abstractmethod
2646
def _send_request(self, request: bytearray) -> bytearray:
@@ -148,3 +168,17 @@ def _do_action(self, action: Callable, max_tries: int = 3, *args, **kwargs):
148168
raise TionException(action.__name__, message)
149169

150170
return response
171+
172+
def _enable_notifications(self):
173+
_LOGGER.debug("Enabling notification")
174+
setup_data = b"\x01\x00"
175+
176+
_LOGGER.debug("Notify handler is %s", self.notify.getHandle())
177+
notify_handle = self.notify.getHandle() + 1
178+
179+
_LOGGER.debug("Will write %s to %s handle", setup_data, notify_handle)
180+
result = self._btle.writeCharacteristic(notify_handle, setup_data, withResponse=True)
181+
_LOGGER.debug("Result is %s", result)
182+
self._btle.withDelegate(self._delegation)
183+
self.notify.read()
184+
return result

0 commit comments

Comments
 (0)