add broadcast device discovery
This commit is contained in:
60
python/src/ble_discovery.py
Normal file
60
python/src/ble_discovery.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from bluepy.btle import DefaultDelegate
|
||||
from bluepy.btle import Scanner
|
||||
|
||||
from data_class import Data
|
||||
from devices import get_device
|
||||
|
||||
# This is the list, where the responses will be stored from the `handleDiscovery`
|
||||
devices = []
|
||||
|
||||
|
||||
class ScanDelegate(DefaultDelegate):
|
||||
def __init__(self):
|
||||
DefaultDelegate.__init__(self)
|
||||
|
||||
def handleDiscovery(self, dev, isNewDev, isNewData):
|
||||
global devices
|
||||
|
||||
for (sdid, desc, val) in dev.getScanData():
|
||||
if self.is_temperature(sdid, val):
|
||||
data_obj = Data(val)
|
||||
|
||||
if self.is_atc_device(dev, data_obj):
|
||||
device_from_config = get_device(dev)
|
||||
devices.append([dev, data_obj, device_from_config])
|
||||
|
||||
@staticmethod
|
||||
def is_temperature(sdid, val):
|
||||
if sdid != 22: return False
|
||||
if len(val) != 30: return False
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def is_atc_device(dev, data_obj):
|
||||
global devices
|
||||
if 'A4:C1:38' not in dev.addr.upper(): return False
|
||||
for device in devices:
|
||||
if str(device[0].addr) == str(dev.addr): return False
|
||||
|
||||
device_from_config = get_device(dev)
|
||||
|
||||
try:print(f"Device: {dev.addr.upper()} ({dev.addrType}), RSSI: {dev.rssi}dB, Room: {device_from_config.room}")
|
||||
except:print(f"Device: {dev.addr.upper()} ({dev.addrType}), RSSI: {dev.rssi}dB, Room: ?")
|
||||
print(f'\tTemp: {data_obj.temperature}°C, Humid: {data_obj.humidity}%, Batt: {data_obj.battery_percent}%\n')
|
||||
return True
|
||||
|
||||
|
||||
def cleanup():
|
||||
global devices
|
||||
devices = []
|
||||
|
||||
|
||||
def start_discovery(timeout=20):
|
||||
cleanup()
|
||||
global devices
|
||||
print(f'Start discovery with timout {timeout}s...')
|
||||
|
||||
scanner = Scanner().withDelegate(ScanDelegate())
|
||||
scanner.scan(timeout=timeout, passive=False)
|
||||
|
||||
return devices
|
||||
Reference in New Issue
Block a user