Skip to content

naneos.partector_ble.partector_ble_connection

PartectorBleConnection

Source code in src/naneos/partector_ble/partector_ble_connection.py
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
class PartectorBleConnection:
    SERVICE_UUID = "0bd51666-e7cb-469b-8e4d-2742f1ba77cc"
    CHAR_UUIDS = {
        "std": "e7add780-b042-4876-aae1-112855353cc1",
        "aux": "e7add781-b042-4876-aae1-112855353cc1",
        "write": "e7add782-b042-4876-aae1-112855353cc1",
        "read": "e7add783-b042-4876-aae1-112855353cc1",
        "size_dist": "e7add784-b042-4876-aae1-112855353cc1",
    }

    # static methods ###############################################################################
    @staticmethod
    def create_connection_queue() -> asyncio.Queue[NaneosDeviceDataPoint]:
        """Create a queue for the connection data."""
        # Increased maxsize to 500 to handle bursts from multiple devices
        # Prevents message loss on Raspberry Pi with many concurrent connections
        queue_connection: asyncio.Queue[NaneosDeviceDataPoint] = asyncio.Queue(maxsize=500)

        return queue_connection

    # == Lifecycle and Context Management ==========================================================
    def __init__(
        self,
        device: BLEDevice,
        loop: asyncio.AbstractEventLoop,
        serial_number: int,
        queue: asyncio.Queue[NaneosDeviceDataPoint],
    ) -> None:
        """
        Initializes the BLE connection with the given device, event loop, and queue.

        Args:
            device (BLEDevice): The BLE device to connect to.
            loop (asyncio.AbstractEventLoop): The event loop to run the connection in.
            serial_number (int): The serial number of the device.
        """
        self.SERIAL_NUMBER = serial_number
        self._device_type = NaneosDeviceDataPoint.DEV_TYPE_P2  # Thats the deafault value
        self._data = NaneosDeviceDataPoint()
        self._next_ts = 0.0
        self._last_aux_data_ts = time.time()
        self._queue = queue

        # Decode queue to decouple decoding from BLE callbacks
        # This prevents blocking the event loop when decoding heavy data
        self._decode_queue: asyncio.Queue = asyncio.Queue(maxsize=200)

        self._device = device
        self._loop = loop
        self._task: asyncio.Task | None = None
        self._stop_event = asyncio.Event()
        self._stop_event.set()  # stopped by default
        self._client = BleakClient(device, self._disconnect_callback, timeout=10)

    async def __aenter__(self) -> PartectorBleConnection:
        self.start()
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
        await self.stop()

    # == Public Methods ============================================================================
    def start(self) -> None:
        """Starts the scanner."""
        if not self._stop_event.is_set():
            logger.warning("SN{self._serial_number}: start() called while already running")
            return
        self._stop_event.clear()
        self._task = self._loop.create_task(self._run())

    async def stop(self) -> None:
        """Stops the scanner."""
        self._stop_event.set()
        if self._task and not self._task.done():
            await self._task
        logger.info(f"SN{self.SERIAL_NUMBER}: PartectorBleConnection stopped")

    async def _run(self) -> None:
        waiting_seconds = 0

        try:
            self._next_ts = int(time.time()) + 1.0

            # Create decode task to run in parallel
            # This prevents decoding from blocking the event loop
            self._loop.create_task(self._decode_routine())

            while not self._stop_event.is_set():
                try:
                    if self._last_aux_data_ts + 60 < time.time():
                        logger.info(
                            f"SN{self.SERIAL_NUMBER}: No aux data received for 60 seconds, disconnecting to reset."
                        )
                        await self._disconnect_gracefully()
                        self._last_aux_data_ts = time.time()
                        waiting_seconds = 5  # wait 5 seconds before reconnecting

                    waiting_seconds = max(0, waiting_seconds - 1)
                    wait = self._next_ts - time.time()
                    if wait > 0:
                        await asyncio.sleep(wait)
                        self._next_ts += 1.0
                    else:
                        if self._client.is_connected:
                            logger.info(f"SN{self.SERIAL_NUMBER}: Waiting time negative: {wait}")
                        self._next_ts = int(time.time()) + 1.0

                    if self._client.is_connected:
                        if (
                            self._device_type == NaneosDeviceDataPoint.DEV_TYPE_P2PRO
                            and self._data.particle_number_10nm is None
                            and self._data.particle_number_16nm is None
                            and self._data.particle_number_26nm is None
                            and self._data.particle_number_43nm is None
                            and self._data.particle_number_70nm is None
                            and self._data.particle_number_114nm is None
                            and self._data.particle_number_185nm is None
                            and self._data.particle_number_300nm is None
                        ):
                            self._data.particle_number_concentration = None
                            self._data.average_particle_diameter = None

                        self._queue.put_nowait(self._data)
                        self._data = NaneosDeviceDataPoint(
                            device_type=self._device_type,
                            serial_number=self.SERIAL_NUMBER,
                            connection_type=NaneosDeviceDataPoint.CONN_TYPE_CONNECTED,
                            # TODO: add firware version from device here
                        )
                        continue

                    if waiting_seconds == 0:
                        await self._client.connect(timeout=5)  # 5 seconds for windows...
                        if self._client.is_connected:
                            await self._client.start_notify(
                                self.CHAR_UUIDS["std"], self._callback_std
                            )
                            await self._client.start_notify(
                                self.CHAR_UUIDS["aux"], self._callback_aux
                            )
                            await self._client.start_notify(
                                self.CHAR_UUIDS["size_dist"], self._callback_size_dist
                            )
                        logger.info(f"SN{self.SERIAL_NUMBER}: Connected to {self._device.address}")

                    self._next_ts = int(time.time()) + 1.0
                except asyncio.TimeoutError:
                    logger.info(f"SN{self.SERIAL_NUMBER}: Connection timeout.")
                    waiting_seconds = 30
                    await asyncio.sleep(0.5)
                except BleakDeviceNotFoundError:
                    logger.info(f"SN{self.SERIAL_NUMBER}: Device not found or probably old BLE.")
                    waiting_seconds = 30
                    await asyncio.sleep(0.5)
                except Exception as e:
                    # if exception contains "not found" increase waiting time to 30 seconds and do not spam
                    if "not found" in str(e).lower():
                        logger.info(
                            f"SN{self.SERIAL_NUMBER}: Device not found or probably old BLE: {e}"
                        )
                        waiting_seconds = 30
                    else:
                        logger.warning(f"SN{self.SERIAL_NUMBER}: Unknown exception: {e}")

                    await asyncio.sleep(0.5)
        except asyncio.CancelledError:
            logger.warning(f"SN{self.SERIAL_NUMBER}: _run task cancelled.")
        except Exception as e:
            logger.exception(f"SN{self.SERIAL_NUMBER}: _run task failed: {e}")
        finally:
            await self._disconnect_gracefully()

    async def _decode_routine(self) -> None:
        """Asynchronously decodes BLE data from the decode queue.

        This runs in parallel with the main connection loop, preventing
        decoding from blocking the event loop when handling multiple connections.
        """
        while not self._stop_event.is_set():
            try:
                # Non-blocking check with timeout to allow graceful shutdown
                try:
                    char_type, data = await asyncio.wait_for(self._decode_queue.get(), timeout=0.5)
                except asyncio.TimeoutError:
                    continue

                # Update timestamp for all decodings
                self._data.unix_timestamp = int(time.time() * 1000)

                # Decode based on characteristic type
                if char_type == "std":
                    self._data = PartectorBleDecoderStd.decode(data, data_structure=self._data)
                    logger.debug(f"SN{self.SERIAL_NUMBER}: Decoded std: {data.hex()}")

                elif char_type == "aux":
                    # Check for aux error data
                    if len(data) >= 2 and data[0] == 255 and data[1] == 255:
                        self._data = PartectorBleDecoderAuxError.decode(
                            data, data_structure=self._data
                        )
                    else:
                        self._data = PartectorBleDecoderAux.decode(data, data_structure=self._data)
                    logger.debug(f"SN{self.SERIAL_NUMBER}: Decoded aux: {data.hex()}")

                elif char_type == "size_dist":
                    self._device_type = NaneosDeviceDataPoint.DEV_TYPE_P2PRO
                    self._data = PartectorBleDecoderSize.decode(data, data_structure=self._data)
                    logger.debug(f"SN{self.SERIAL_NUMBER}: Decoded size_dist: {data.hex()}")

            except Exception as e:
                logger.warning(f"SN{self.SERIAL_NUMBER}: Error in decode routine: {e}")

    async def _disconnect_gracefully(self) -> None:
        if not self._client.is_connected:
            return

        try:
            await asyncio.wait_for(self._client.stop_notify(self.CHAR_UUIDS["std"]), timeout=1)
            await asyncio.sleep(0.5)  # wait for windows to free resources
            await asyncio.wait_for(self._client.stop_notify(self.CHAR_UUIDS["aux"]), timeout=1)
            await asyncio.sleep(0.5)  # wait for windows to free resources
            await asyncio.wait_for(
                self._client.stop_notify(self.CHAR_UUIDS["size_dist"]), timeout=1
            )
            await asyncio.sleep(0.5)  # wait for windows to free resources
        except Exception as e:
            logger.debug(f"SN{self.SERIAL_NUMBER}: Failed to stop notify: {e}")

        try:
            await asyncio.wait_for(self._client.disconnect(), timeout=1)
            await asyncio.sleep(0.5)  # wait for windows to free resources
        except Exception as e:
            logger.debug(f"SN{self.SERIAL_NUMBER}: Failed to disconnect: {e}")

    def _disconnect_callback(self, client: BleakClient) -> None:
        """Callback on disconnect."""
        logger.debug(f"SN{self.SERIAL_NUMBER}: Disconnect callback called")

    def _callback_std(self, characteristic: BleakGATTCharacteristic, data: bytearray) -> None:
        """Callback on data received (std characteristic).

        Non-blocking: puts data in decode queue instead of decoding directly.
        Actual decoding happens asynchronously in _decode_routine().
        """
        try:
            self._decode_queue.put_nowait(("std", bytes(data)))
        except asyncio.QueueFull:
            logger.warning(f"SN{self.SERIAL_NUMBER}: Decode queue full, dropping std data")

    def _callback_aux(self, characteristic: BleakGATTCharacteristic, data: bytearray) -> None:
        """Callback on data received (aux characteristic).

        Non-blocking: puts data in decode queue instead of decoding directly.
        Actual decoding happens asynchronously in _decode_routine().
        """
        self._last_aux_data_ts = time.time()
        try:
            self._decode_queue.put_nowait(("aux", bytes(data)))
        except asyncio.QueueFull:
            logger.warning(f"SN{self.SERIAL_NUMBER}: Decode queue full, dropping aux data")

    def _callback_size_dist(self, characteristic: BleakGATTCharacteristic, data: bytearray) -> None:
        """Callback on data received (size_dist characteristic).

        Non-blocking: puts data in decode queue instead of decoding directly.
        Actual decoding happens asynchronously in _decode_routine().
        """
        try:
            self._decode_queue.put_nowait(("size_dist", bytes(data)))
        except asyncio.QueueFull:
            logger.warning(f"SN{self.SERIAL_NUMBER}: Decode queue full, dropping size_dist data")

__init__(device, loop, serial_number, queue)

Initializes the BLE connection with the given device, event loop, and queue.

Parameters:

Name Type Description Default
device BLEDevice

The BLE device to connect to.

required
loop AbstractEventLoop

The event loop to run the connection in.

required
serial_number int

The serial number of the device.

required
Source code in src/naneos/partector_ble/partector_ble_connection.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def __init__(
    self,
    device: BLEDevice,
    loop: asyncio.AbstractEventLoop,
    serial_number: int,
    queue: asyncio.Queue[NaneosDeviceDataPoint],
) -> None:
    """
    Initializes the BLE connection with the given device, event loop, and queue.

    Args:
        device (BLEDevice): The BLE device to connect to.
        loop (asyncio.AbstractEventLoop): The event loop to run the connection in.
        serial_number (int): The serial number of the device.
    """
    self.SERIAL_NUMBER = serial_number
    self._device_type = NaneosDeviceDataPoint.DEV_TYPE_P2  # Thats the deafault value
    self._data = NaneosDeviceDataPoint()
    self._next_ts = 0.0
    self._last_aux_data_ts = time.time()
    self._queue = queue

    # Decode queue to decouple decoding from BLE callbacks
    # This prevents blocking the event loop when decoding heavy data
    self._decode_queue: asyncio.Queue = asyncio.Queue(maxsize=200)

    self._device = device
    self._loop = loop
    self._task: asyncio.Task | None = None
    self._stop_event = asyncio.Event()
    self._stop_event.set()  # stopped by default
    self._client = BleakClient(device, self._disconnect_callback, timeout=10)

create_connection_queue() staticmethod

Create a queue for the connection data.

Source code in src/naneos/partector_ble/partector_ble_connection.py
33
34
35
36
37
38
39
40
@staticmethod
def create_connection_queue() -> asyncio.Queue[NaneosDeviceDataPoint]:
    """Create a queue for the connection data."""
    # Increased maxsize to 500 to handle bursts from multiple devices
    # Prevents message loss on Raspberry Pi with many concurrent connections
    queue_connection: asyncio.Queue[NaneosDeviceDataPoint] = asyncio.Queue(maxsize=500)

    return queue_connection

start()

Starts the scanner.

Source code in src/naneos/partector_ble/partector_ble_connection.py
84
85
86
87
88
89
90
def start(self) -> None:
    """Starts the scanner."""
    if not self._stop_event.is_set():
        logger.warning("SN{self._serial_number}: start() called while already running")
        return
    self._stop_event.clear()
    self._task = self._loop.create_task(self._run())

stop() async

Stops the scanner.

Source code in src/naneos/partector_ble/partector_ble_connection.py
92
93
94
95
96
97
async def stop(self) -> None:
    """Stops the scanner."""
    self._stop_event.set()
    if self._task and not self._task.done():
        await self._task
    logger.info(f"SN{self.SERIAL_NUMBER}: PartectorBleConnection stopped")