added device recocnition
This commit is contained in:
parent
6d5b2a6850
commit
208a82142c
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
__pycache__
|
__pycache__
|
||||||
|
devices.yml
|
||||||
8
python/src/devices.example.yml
Normal file
8
python/src/devices.example.yml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
devices:
|
||||||
|
- mac: A4:C1:38:00:00:00
|
||||||
|
name: "my_device_1"
|
||||||
|
room: "my_room_1"
|
||||||
|
|
||||||
|
- mac: A4:C1:38:00:00:00
|
||||||
|
name: "my_device_2"
|
||||||
|
room: "my_room_2"
|
||||||
@ -1,2 +1,25 @@
|
|||||||
import yaml
|
import yaml
|
||||||
|
import os
|
||||||
|
|
||||||
|
workdir, filename = os.path.split(os.path.abspath(__file__))
|
||||||
|
config_file = f"{workdir}{os.sep}devices.yml"
|
||||||
|
|
||||||
|
|
||||||
|
class Device:
|
||||||
|
def __init__(self, data):
|
||||||
|
self.mac = data['mac']
|
||||||
|
self.name = data['name']
|
||||||
|
self.room = data['room']
|
||||||
|
|
||||||
|
|
||||||
|
def get_devices():
|
||||||
|
devices_list = []
|
||||||
|
with open(file=config_file, mode='r') as file:
|
||||||
|
devices = yaml.safe_load(file)
|
||||||
|
for device in devices['devices']: devices_list.append(Device(data=device))
|
||||||
|
return devices_list
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +0,0 @@
|
|||||||
devices:
|
|
||||||
- "A4:C1:38:83:05:E8":
|
|
||||||
name: "My Sensor"
|
|
||||||
room: "My Room"
|
|
||||||
|
|
||||||
- "...":
|
|
||||||
name: "..."
|
|
||||||
room: "..."
|
|
||||||
@ -1,10 +1,9 @@
|
|||||||
import asyncio
|
|
||||||
from threading import Thread
|
|
||||||
from bluepy.btle import DefaultDelegate
|
from bluepy.btle import DefaultDelegate
|
||||||
from bluepy.btle import Scanner
|
from bluepy.btle import Scanner
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from data_class import Data
|
from data_class import Data
|
||||||
|
from devices import get_devices as get_device_from_config
|
||||||
|
|
||||||
# This is the list, where the responses will be stored from the `handleDiscovery`
|
# This is the list, where the responses will be stored from the `handleDiscovery`
|
||||||
devices = []
|
devices = []
|
||||||
@ -21,6 +20,7 @@ class ScanDelegate(DefaultDelegate):
|
|||||||
if self.is_temperature(sdid, val):
|
if self.is_temperature(sdid, val):
|
||||||
data_obj = Data(self.parse_data(val))
|
data_obj = Data(self.parse_data(val))
|
||||||
if self.is_atc_device(dev, data_obj):
|
if self.is_atc_device(dev, data_obj):
|
||||||
|
device_from_config = self.get_device(dev)
|
||||||
devices.append([dev, data_obj])
|
devices.append([dev, data_obj])
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -29,13 +29,16 @@ class ScanDelegate(DefaultDelegate):
|
|||||||
if len(val) != 30: return False
|
if len(val) != 30: return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@staticmethod
|
def is_atc_device(self, dev, data_obj):
|
||||||
def is_atc_device(dev, data_obj):
|
|
||||||
global devices
|
global devices
|
||||||
if 'A4:C1:38' not in dev.addr.upper(): return False
|
if 'A4:C1:38' not in dev.addr.upper(): return False
|
||||||
for device in devices:
|
for device in devices:
|
||||||
if str(device[0].addr) == str(dev.addr): return False
|
if str(device[0].addr) == str(dev.addr): return False
|
||||||
print("Device %s (%s), RSSI=%d dB" % (dev.addr.upper(), dev.addrType, dev.rssi))
|
|
||||||
|
device_from_config = self.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')
|
print(f'\tTemp: {data_obj.temperature}°C, Humid: {data_obj.humidity}%, Batt: {data_obj.battery_percent}%\n')
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -54,8 +57,16 @@ class ScanDelegate(DefaultDelegate):
|
|||||||
'count': bytes[14],
|
'count': bytes[14],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_device(dev):
|
||||||
|
return next((d for d in get_device_from_config() if d.mac == dev.addr.upper()), None)
|
||||||
|
|
||||||
def start_discovery(timeout=10.0):
|
|
||||||
|
class Discovery:
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def start_discovery(self, timeout=20.0):
|
||||||
global devices
|
global devices
|
||||||
print(f'Start discovery with timout {timeout}s...')
|
print(f'Start discovery with timout {timeout}s...')
|
||||||
|
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
from discovery import start_discovery
|
|
||||||
|
|
||||||
|
|
||||||
def start_loop(interval=60):
|
def start_loop(interval=60):
|
||||||
while True:
|
while True:
|
||||||
|
from discovery import start_discovery
|
||||||
start_discovery()
|
start_discovery()
|
||||||
sleep(interval)
|
sleep(interval)
|
||||||
|
|||||||
@ -1,19 +1,13 @@
|
|||||||
from discovery import start_discovery
|
from discovery import Discovery
|
||||||
from data_class import Data
|
from data_class import Data
|
||||||
|
from loop import start_loop
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
devices = start_discovery()
|
while True:
|
||||||
|
discovery = Discovery()
|
||||||
|
devices = discovery.start_discovery()
|
||||||
|
sleep(10)
|
||||||
|
|
||||||
|
|
||||||
if len(devices) > 0:
|
|
||||||
for device_list in devices:
|
|
||||||
data = device_list[1]
|
|
||||||
device = device_list[0]
|
|
||||||
|
|
||||||
data:Data
|
|
||||||
# print(f'Temp: {data.temperature}°C, Humid: {data.humidity}%, Batt: {data.battery_percent}%')
|
|
||||||
|
|
||||||
|
|
||||||
else:
|
|
||||||
print('No devices found')
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user