From fc7982378a8ff243ba3745a90cd32e6802e04967 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 8 Mar 2020 23:50:23 +0100 Subject: [PATCH] The bluetooth BLE plugin must also implement get_measurement to work with the scanner backend --- platypush/plugins/bluetooth/ble.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/platypush/plugins/bluetooth/ble.py b/platypush/plugins/bluetooth/ble.py index 0c8c70cb7..a29f1a1a6 100644 --- a/platypush/plugins/bluetooth/ble.py +++ b/platypush/plugins/bluetooth/ble.py @@ -3,13 +3,15 @@ import os import subprocess import sys import time +from typing import Optional, Dict -from platypush.plugins import Plugin, action +from platypush.plugins import action +from platypush.plugins.sensor import SensorPlugin from platypush.message.response.bluetooth import BluetoothScanResponse, BluetoothDiscoverPrimaryResponse, \ BluetoothDiscoverCharacteristicsResponse -class BluetoothBlePlugin(Plugin): +class BluetoothBlePlugin(SensorPlugin): """ Bluetooth BLE (low-energy) plugin @@ -76,7 +78,7 @@ class BluetoothBlePlugin(Plugin): '\t[sudo] setcap "cap_net_raw,cap_net_admin+eip" {}'.format(exe)) @action - def scan(self, interface: str = None, duration: int = 10) -> BluetoothScanResponse: + def scan(self, interface: Optional[str] = None, duration: int = 10) -> BluetoothScanResponse: """ Scan for nearby bluetooth low-energy devices @@ -93,6 +95,19 @@ class BluetoothBlePlugin(Plugin): devices = svc.discover(duration) return BluetoothScanResponse(devices) + @action + def get_measurement(self, interface: Optional[str] = None, duration: Optional[int] = 10, *args, **kwargs) \ + -> Dict[str, dict]: + """ + Wrapper for ``scan`` that returns bluetooth devices in a format usable by sensor backends. + + :param interface: Bluetooth adapter name to use (default configured if None) + :param duration: Scan duration in seconds + :return: Device address -> info map. + """ + devices = self.scan(interface=interface, duration=duration).output + return {device['addr']: device for device in devices} + # noinspection PyArgumentList @action def connect(self, device: str, interface: str = None, wait: bool = True, channel_type: str = 'public',