Skip to content

Commit

Permalink
fix: ensure BLE scanner is stopped by explicitly closing the generator (
Browse files Browse the repository at this point in the history
  • Loading branch information
ttu authored Nov 26, 2024
1 parent 6776401 commit ccd71b9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* CHANGE: Support only Python 3.9 and above
* FIX: Check if async adapter in use in RuuviTagReactive
* FIX: Correct typo in argument name search\_duration\_sec
* FIX: Ensure the BLE scanner is stopped by explicitly closing the generator

## [2.3.1] - 2024-03-10
* ADD: Bluez as option to RUUVI_BLE_ADAPTER environment variable
Expand Down
55 changes: 33 additions & 22 deletions ruuvitag_sensor/ruuvi.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,18 @@ async def find_ruuvitags_async(bt_device: str = "") -> Dict[Mac, MacAndSensorDat
mac_blacklist = Manager().list()
data_iter = ble.get_data(mac_blacklist, bt_device)

async for new_data in data_iter:
if new_data[0] in data:
continue

parsed_data = RuuviTagSensor._parse_data(new_data, mac_blacklist)
if parsed_data:
data[new_data[0]] = parsed_data
log.info(new_data[0])
log.info(parsed_data)
try:
async for new_data in data_iter:
if new_data[0] in data:
continue

parsed_data = RuuviTagSensor._parse_data(new_data, mac_blacklist)
if parsed_data:
data[new_data[0]] = parsed_data
log.info(new_data[0])
log.info(parsed_data)
finally:
await data_iter.aclose()

return data

Expand Down Expand Up @@ -174,11 +177,16 @@ async def get_data_for_sensors_async(
data: Dict[Mac, SensorData] = {}
start_time = time.time()

async for new_data in RuuviTagSensor.get_data_async(macs, bt_device):
mac, sensor_data = new_data
data[mac] = sensor_data
if search_duration_sec and time.time() - start_time > search_duration_sec:
break
data_iter = RuuviTagSensor.get_data_async(macs, bt_device)

try:
async for new_data in data_iter:
mac, sensor_data = new_data
data[mac] = sensor_data
if search_duration_sec and time.time() - start_time > search_duration_sec:
break
finally:
await data_iter.aclose()

return data

Expand All @@ -198,16 +206,19 @@ async def get_data_async(macs: List[str] = [], bt_device: str = "") -> AsyncGene
mac_blacklist = Manager().list()
data_iter = ble.get_data(mac_blacklist, bt_device)

async for ble_data in data_iter:
data = RuuviTagSensor._parse_data(ble_data, mac_blacklist, macs)
try:
async for ble_data in data_iter:
data = RuuviTagSensor._parse_data(ble_data, mac_blacklist, macs)

# Check MAC whitelist if advertised MAC available
if ble_data[0] and macs and not ble_data[0] in macs:
log.debug("MAC not whitelisted: %s", ble_data[0])
continue
# Check MAC whitelist if advertised MAC available
if ble_data[0] and macs and not ble_data[0] in macs:
log.debug("MAC not whitelisted: %s", ble_data[0])
continue

if data:
yield data
if data:
yield data
finally:
await data_iter.aclose()

@staticmethod
def get_data(
Expand Down
16 changes: 10 additions & 6 deletions ruuvitag_sensor/ruuvi_rx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ async def _run_get_data_background_async(macs: List[str], queue: Queue, shared_d
"""
Async background process function for RuuviTag Sensors
"""
async for data in RuuviTagSensor.get_data_async(macs, bt_device):
if not shared_data["run_flag"]:
break

data[1]["time"] = datetime.utcnow().isoformat() # type: ignore
queue.put(data)
data_iter = RuuviTagSensor.get_data_async(macs, bt_device)
try:
async for data in data_iter:
if not shared_data["run_flag"]:
break

data[1]["time"] = datetime.utcnow().isoformat() # type: ignore
queue.put(data)
finally:
await data_iter.aclose()


def _run_get_data_background(macs: List[str], queue: Queue, shared_data: DictProxy, bt_device: str):
Expand Down

0 comments on commit ccd71b9

Please sign in to comment.