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

fix: ensure BLE scanner is stopped by explicitly closing the generator #263

Merged
merged 1 commit into from
Nov 26, 2024
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
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