added docker and wrote some readme stuff. nobody will ever read this... anyway
This commit is contained in:
0
python/src/__init__.py
Normal file
0
python/src/__init__.py
Normal file
23
python/src/data_class.py
Normal file
23
python/src/data_class.py
Normal file
@@ -0,0 +1,23 @@
|
||||
class Data:
|
||||
def __init__(self, data:dict):
|
||||
self.timestamp = data['timestamp']
|
||||
self.mac = data['mac']
|
||||
self.temperature = data['temperature']
|
||||
self.humidity = data['humidity']
|
||||
self.battery_percent = data['battery_percent']
|
||||
self.battery_volt = data['battery_volt']
|
||||
self.count = data['count']
|
||||
|
||||
def print_data(self):
|
||||
print(self.to_json())
|
||||
|
||||
def to_json(self):
|
||||
return {
|
||||
'timestamp': self.timestamp,
|
||||
'mac': self.mac,
|
||||
'temperature': self.temperature,
|
||||
'humidity': self.humidity,
|
||||
'battery_percent': self.battery_percent,
|
||||
'battery_volt': self.battery_volt,
|
||||
'count': self.count,
|
||||
}
|
||||
63
python/src/discovery.py
Normal file
63
python/src/discovery.py
Normal file
@@ -0,0 +1,63 @@
|
||||
from bluepy.btle import DefaultDelegate
|
||||
from bluepy.btle import Scanner
|
||||
from datetime import datetime
|
||||
|
||||
from data_class import Data
|
||||
|
||||
# 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) and self.is_atc_device(dev):
|
||||
data_obj = Data(self.parse_data(val))
|
||||
devices.append([dev, data_obj])
|
||||
|
||||
@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):
|
||||
global devices
|
||||
if 'A4:C1:38' not in dev.addr.upper(): return False
|
||||
device_found = False
|
||||
# print(devices.count(dev.addr.upper()))
|
||||
for device in devices:
|
||||
if str(device[0].addr) == str(dev.addr): return False
|
||||
print("Device %s (%s), RSSI=%d dB" % (dev.addr.upper(), dev.addrType, dev.rssi))
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def parse_data(val):
|
||||
bytes = [int(val[i:i + 2], 16) for i in range(0, len(val), 2)]
|
||||
if bytes[8] > 127:
|
||||
bytes[8] -= 256
|
||||
return {
|
||||
'timestamp': datetime.now().astimezone().replace(microsecond=0).isoformat(),
|
||||
'mac': ":".join(["{:02X}".format(bytes[i]) for i in range(2, 8)]),
|
||||
'temperature': (bytes[8] * 256 + bytes[9]) / 10,
|
||||
'humidity': bytes[10],
|
||||
'battery_percent': bytes[11],
|
||||
'battery_volt': (bytes[12] * 256 + bytes[13]) / 1000,
|
||||
'count': bytes[14],
|
||||
}
|
||||
|
||||
|
||||
def start_discovery(timeout=10.0):
|
||||
global devices
|
||||
print(f'Start discovery with timout {timeout}s...')
|
||||
|
||||
scanner = Scanner().withDelegate(ScanDelegate())
|
||||
scanner.scan(timeout=timeout, passive=True)
|
||||
|
||||
return devices
|
||||
19
python/src/main.py
Normal file
19
python/src/main.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from discovery import start_discovery
|
||||
from data_class import Data
|
||||
|
||||
devices = start_discovery()
|
||||
|
||||
|
||||
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')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user