From 961fba65bee565daa7ecc3b0516969f7b2f4b230 Mon Sep 17 00:00:00 2001 From: DasMoorhuhn Date: Sat, 22 Jun 2024 22:37:24 +0200 Subject: [PATCH] did stuff idk i'm cooked --- Dockerfile | 17 ++++++++++++++--- README.md | 7 ++++--- bluetooth tools/search_for_ble.py | 0 .../search_for_blc.py | 0 bluetooth_tools/search_for_ble.py | 8 ++++++++ log_to_json.py | 13 +++++++++++++ python/requierements.txt | 5 ++++- python/src/data_class.py | 6 ++---- python/src/devices.py | 5 ++--- python/src/discovery.py | 10 +++------- python/src/log_data.py | 1 + python/src/main.py | 4 +--- python/src/mqtt.py | 1 + 13 files changed, 53 insertions(+), 24 deletions(-) delete mode 100644 bluetooth tools/search_for_ble.py rename {bluetooth tools => bluetooth_tools}/search_for_blc.py (100%) create mode 100644 bluetooth_tools/search_for_ble.py create mode 100644 log_to_json.py create mode 100644 python/src/log_data.py create mode 100644 python/src/mqtt.py diff --git a/Dockerfile b/Dockerfile index a474eea..0a9db2a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.12 +FROM python:3.12-alpine3.20 WORKDIR = /src @@ -8,8 +8,19 @@ COPY python/docker_entrypoint.sh / RUN mkdir data -RUN apt-get update && \ - apt-get install -y bluez sudo +# RUN apt-get update && \ +# apt-get install -y bluez sudo + + +RUN apk add --no-cache \ + sudo \ + make \ + bluez \ + bluez-deprecated \ + alsa-utils \ + alsa-utils-doc \ + alsa-lib \ + alsaconf RUN pip3.12 install -r requierements.txt && rm -f requierements.txt diff --git a/README.md b/README.md index dd2f5bf..2675b4a 100644 --- a/README.md +++ b/README.md @@ -5,14 +5,14 @@ Python gateway for the [custom firmware](https://github.com/atc1441/ATC_MiThermo ![](.media/41N1IH9jwoL._AC_SL1024_.jpg) **Features:** -- [DONE] Make in runnable in a docker container (because only cool people are using docker) +- WIP **TODOs:** - [WIP] Can run on Raspberry Pi (3, 4, zero w) or any other Linux driven hardware which has BLE and WiFi support - [WIP] Storing temperature, humidity and battery state as json in a text file - [WIP] Implement a loop for fetching the data every x minute - [WIP] Make discoveries async -- [TODO] Make docker image smaller. I mean shiiit 1GB D: should be possible to be under 500MB +- [WIP] Make docker image smaller. I mean shiiit 1GB D: should be possible to be under 500MB - [TODO] Make a microPython version for using the raspberry pico w or any other microcontroller with BLE and WiFi support - [TODO] Collect data from multiple devices/gateways - [TODO] Command line tool for managing the devices @@ -21,6 +21,7 @@ Python gateway for the [custom firmware](https://github.com/atc1441/ATC_MiThermo - [TODO] MQTT publishing - [TODO] Maybe... a webinterface. But I suck at web stuff, so I don't know. - [TODO] Implement other BLE Sensors +- [BROK] Make in runnable in a docker container (because only cool people are using docker) **Current State** @@ -50,7 +51,7 @@ sudo python3 main.py ### Docker -Build docker container +Build docker container (Currently broken) ```bash docker-compose build ``` diff --git a/bluetooth tools/search_for_ble.py b/bluetooth tools/search_for_ble.py deleted file mode 100644 index e69de29..0000000 diff --git a/bluetooth tools/search_for_blc.py b/bluetooth_tools/search_for_blc.py similarity index 100% rename from bluetooth tools/search_for_blc.py rename to bluetooth_tools/search_for_blc.py diff --git a/bluetooth_tools/search_for_ble.py b/bluetooth_tools/search_for_ble.py new file mode 100644 index 0000000..8703b7a --- /dev/null +++ b/bluetooth_tools/search_for_ble.py @@ -0,0 +1,8 @@ +from bluepy.btle import Scanner +scanner = Scanner(0) +print('Start scan...') +devices = scanner.scan(10) +for device in devices: + print('address : %s' % device.addr.upper()) + print(device.getScanData()) + print('') diff --git a/log_to_json.py b/log_to_json.py new file mode 100644 index 0000000..7cef3ea --- /dev/null +++ b/log_to_json.py @@ -0,0 +1,13 @@ +import json + + +with open('history.txt', 'r') as file: + content = file.readlines() + +lines = [] +for line in content: + line = json.loads(line.strip()) + lines.append(line) + +with open('history.json', 'w') as file: + file.write(json.dumps(lines)) diff --git a/python/requierements.txt b/python/requierements.txt index d99d203..dd13488 100644 --- a/python/requierements.txt +++ b/python/requierements.txt @@ -1,2 +1,5 @@ bluepy -pyyaml \ No newline at end of file +pyyaml +bs4 +lxml +requests \ No newline at end of file diff --git a/python/src/data_class.py b/python/src/data_class.py index 9ddfcf8..79213e7 100644 --- a/python/src/data_class.py +++ b/python/src/data_class.py @@ -14,12 +14,10 @@ class Data: def parse_data(self, val): data_bytes = [int(val[i:i + 2], 16) for i in range(0, len(val), 2)] + if data_bytes[8] > 127: data_bytes[8] -= 256 - if data_bytes[8] > 127: - print(data_bytes) - data_bytes[8] -= 256 self.timestamp = datetime.now().astimezone().replace(microsecond=0).isoformat() - self.mac = ":".join(["{:02X}".format(data_bytes[i]) for i in range(2, 8)]) + self.mac = ":".join(["{:02X}".format(data_bytes[i]) for i in range(2, 8)]).upper() self.temperature = (data_bytes[8] * 256 + data_bytes[9]) / 10 self.humidity = data_bytes[10] self.battery_percent = data_bytes[11] diff --git a/python/src/devices.py b/python/src/devices.py index b5f480c..f0af17f 100644 --- a/python/src/devices.py +++ b/python/src/devices.py @@ -20,6 +20,5 @@ def get_devices(): return devices_list - - - +def get_device(dev): + return next((d for d in get_devices() if d.mac == dev.addr.upper()), None) diff --git a/python/src/discovery.py b/python/src/discovery.py index 2c965ff..88e1bbe 100644 --- a/python/src/discovery.py +++ b/python/src/discovery.py @@ -2,7 +2,7 @@ from bluepy.btle import DefaultDelegate from bluepy.btle import Scanner from data_class import Data -from devices import get_devices as get_device_from_config +from devices import get_device # This is the list, where the responses will be stored from the `handleDiscovery` devices = [] @@ -20,7 +20,7 @@ class ScanDelegate(DefaultDelegate): data_obj = Data(val) if self.is_atc_device(dev, data_obj): - device_from_config = self.get_device(dev) + device_from_config = get_device(dev) devices.append([dev, data_obj, device_from_config]) @staticmethod @@ -35,17 +35,13 @@ class ScanDelegate(DefaultDelegate): for device in devices: if str(device[0].addr) == str(dev.addr): return False - device_from_config = self.get_device(dev) + 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 - @staticmethod - def get_device(dev): - return next((d for d in get_device_from_config() if d.mac == dev.addr.upper()), None) - def cleanup(): global devices diff --git a/python/src/log_data.py b/python/src/log_data.py new file mode 100644 index 0000000..4640904 --- /dev/null +++ b/python/src/log_data.py @@ -0,0 +1 @@ +# TODO diff --git a/python/src/main.py b/python/src/main.py index 089dbe0..b2f60ab 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -1,7 +1,5 @@ from discovery import start_discovery -from data_class import Data from loop import start_loop -from time import sleep + devices = start_discovery() - diff --git a/python/src/mqtt.py b/python/src/mqtt.py new file mode 100644 index 0000000..4640904 --- /dev/null +++ b/python/src/mqtt.py @@ -0,0 +1 @@ +# TODO