From ff6789f09cc55f2fc2005ec8dc394cd3bbac81ca Mon Sep 17 00:00:00 2001 From: ttu Date: Mon, 25 Dec 2023 11:52:13 +0100 Subject: [PATCH] fix: force use bluez with env variable --- CHANGELOG.md | 1 + README.md | 20 ++++++++++++++--- ruuvitag_sensor/adapters/__init__.py | 33 ++++++++++++++++++++-------- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7771066..b2f1d9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Changelog ### [Unreleased] +* FIX: Force the use of Bluez-adapter with environment variable * FIX: Add RSSI value to Bleson adapter payload ## [2.3.0] - 2023-09-23 diff --git a/README.md b/README.md index 225816d..331f47b 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,9 @@ RuuviTag Sensor Python Package * [Sync-methods](#usage) * [Observable streams](#usage) * [Install guide](#BlueZ) - * __NOTE:__ The BlueZ-adapter implementation uses deprecated BlueZ tools that are no longer supported. Even though BlueZ is still the default adapter, it is recommended to use the Bleak-communication adapter with Linux. Bleak will be the default adapter for Linux in the next major release. + * __NOTE:__ The BlueZ-adapter implementation uses deprecated BlueZ tools that are no longer supported. + * Even though BlueZ is still the default adapter, it is recommended to use the Bleak-communication adapter with Linux. Bleak will be the default adapter for Linux in the next major release. + * Bleson-adapter supports sync-methods, but please be aware that it is not fully supported due to the alpha release status of the Bleson communication module. See [Bleson](#Bleson) for more information. * Python 3.7+ * For Python 2.x or <3.7 support, check [installation instructions](#python-2x-and-36-and-below) for an older version @@ -423,6 +425,18 @@ $ sudo apt-get install bluez bluez-hcidump `ruuvitag-sensor` package uses internally _hciconfig_, _hcitool_ and _hcidump_. These tools are deprecated. In case tools are missing, an older version of BlueZ is required ([Issue](https://github.com/ttu/ruuvitag-sensor/issues/31)) +If you wish to test the library on Windows or macOS, enable it with `RUUVI_BLE_ADAPTER` environment variable. + +```sh +$ export RUUVI_BLE_ADAPTER="bluez" +``` + +And install ptyprocess. + +```sh +python -m pip install ptyprocess +``` + #### BlueZ limitations `ruuvitag-sensor` package uses BlueZ to listen to broadcasted BL information (uses _hciconf_, _hcitool_, _hcidump_). Implementation does not handle well all unexpected errors or changes, e.g. when the adapter is busy, rebooted or powered down. @@ -479,12 +493,12 @@ Bleak-adapter has a development-time generator for dummy data, which can be usef Current state and known bugs in [issue #78](https://github.com/ttu/ruuvitag-sensor/issues/78). -Bleson works with Linux, macOS and partially with Windows. +[Bleson](https://github.com/TheCellule/python-bleson) works with Linux, macOS and partially with Windows. Bleson is not installed automatically with `ruuvitag-sensor` package. Install it manually from GitHub. ```sh -$ pip install git+https://github.com/TheCellule/python-bleson +$ python -m pip install git+https://github.com/TheCellule/python-bleson ``` Add environment variable `RUUVI_BLE_ADAPTER` with value `bleson`. E.g. diff --git a/ruuvitag_sensor/adapters/__init__.py b/ruuvitag_sensor/adapters/__init__.py index f69142f..59272ce 100644 --- a/ruuvitag_sensor/adapters/__init__.py +++ b/ruuvitag_sensor/adapters/__init__.py @@ -9,24 +9,39 @@ def get_ble_adapter(): - if "bleak" in os.environ.get("RUUVI_BLE_ADAPTER", "").lower(): - from ruuvitag_sensor.adapters.bleak_ble import BleCommunicationBleak + forced_ble_adapter = os.environ.get("RUUVI_BLE_ADAPTER", "").lower() + use_ruuvi_nix_from_file = "RUUVI_NIX_FROMFILE" in os.environ + is_ci_env = "CI" in os.environ - return BleCommunicationBleak() - if "bleson" in os.environ.get("RUUVI_BLE_ADAPTER", "").lower(): - from ruuvitag_sensor.adapters.bleson import BleCommunicationBleson + if forced_ble_adapter: + if "bleak" in forced_ble_adapter: + from ruuvitag_sensor.adapters.bleak_ble import BleCommunicationBleak + + return BleCommunicationBleak() + if "bleson" in forced_ble_adapter: + from ruuvitag_sensor.adapters.bleson import BleCommunicationBleson + + return BleCommunicationBleson() + if "bluez" in forced_ble_adapter: + from ruuvitag_sensor.adapters.nix_hci import BleCommunicationNix - return BleCommunicationBleson() - if "RUUVI_NIX_FROMFILE" in os.environ: + return BleCommunicationNix() + + raise RuntimeError(f"Unknown BLE adapter: {forced_ble_adapter}") + + if use_ruuvi_nix_from_file: # Emulate BleCommunicationNix by reading hcidump data from a file from ruuvitag_sensor.adapters.nix_hci_file import BleCommunicationNixFile return BleCommunicationNixFile() - if "CI" in os.environ: - # Use BleCommunicationDummy for CI as it can't use bluez + + if is_ci_env: + # Use BleCommunicationDummy for CI as it can't use BlueZ from ruuvitag_sensor.adapters.dummy import BleCommunicationDummy return BleCommunicationDummy() + + # Use default adapter for platform if sys.platform.startswith("win") or sys.platform.startswith("darwin"): from ruuvitag_sensor.adapters.bleak_ble import BleCommunicationBleak