fix up some mess

This commit is contained in:
DasMoorhuhn 2024-07-03 00:27:48 +02:00
parent 4e76c7db1a
commit 285a2503d7
9 changed files with 67 additions and 47 deletions

BIN
.media/demo.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

View File

@ -39,12 +39,12 @@ WORKDIR /src
COPY ./python/src/ .
COPY ./python/docker_entrypoint.sh /
RUN mkdir data
RUN touch DOCKER
VOLUME /src/data
RUN apk add --no-cache sudo bluez tzdata
ENV TZ=Europe/Berlin
ENV DOCKER=TRUE
ENV DOCKER=true
ENV API=false
# Copy pips from the pip build stage
COPY --from=pip_build_stage /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages

View File

@ -1,5 +1,6 @@
<!-- TOC -->
* [ATC_MiThermometer_Gateway](#atc_mithermometer_gateway)
* [Roadmap](#roadmap)
* [Getting started](#getting-started)
* [Run Gateway](#run-gateway)
* [Shell Scripts](#shell-scripts)
@ -14,15 +15,16 @@ Python gateway for the [custom firmware](https://github.com/atc1441/ATC_MiThermo
![](.media/41N1IH9jwoL._AC_SL1024_.jpg)
**Features:**
- WIP
## Roadmap
**Done:**
- Make in runnable in a docker container (because only cool people are using docker)
- Make docker image smaller. I mean shiiit 1GB D: should be possible to be under 500MB (It's now around 100MB)
- Implement a loop for fetching the data every X seconds
**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
- [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
@ -31,11 +33,10 @@ 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**
![](.media/demo_001.gif)
![](.media/demo.gif)
## Getting started

View File

@ -7,4 +7,4 @@ if [ "$API" = true ]; then
sleep 1
fi
sudo python3.12 main.py
python3.12 main.py

View File

@ -1,3 +1,4 @@
bluepy
pyyaml
bs4
requests

View File

@ -1,16 +0,0 @@
# This is a quick and dirty hack since the ENVs from the docker run command won't show up in the main.py
# I echo the output from the env command of a .env file, read and load it here into the os.environ.
# https://stackoverflow.com/questions/78684481/python-wont-find-the-env-in-my-docker-container
import os
def load_env():
if 'DOCKER' not in os.listdir('.'): return False
with open(file='.env', mode='r') as file: ENV = file.readlines()
for env in ENV:
env = env.strip()
key, value = env.split('=')
os.environ[key] = value
return True

View File

@ -4,6 +4,8 @@ import json
from data_class import Data
from devices import Device
DEBUG = True if os.getenv('DEBUG') == 'true' else False
def log_to_json(devices):
workdir, filename = os.path.split(os.path.abspath(__file__))
@ -13,7 +15,7 @@ def log_to_json(devices):
data_obj: Data
from_config: Device
file_name = f'{workdir}/data/{str(data_obj.mac).replace(":", "-")}.json'
print(file_name)
print(file_name) if DEBUG else {}
try:
with open(file_name, 'r') as file: data = json.load(file)

View File

@ -2,16 +2,25 @@ import os
from discovery import start_discovery
from log_data import log_to_json
from loop import start_loop
from load_env import load_env
DOCKER = load_env()
INTERVAL = 40
TIMEOUT = 20
DOCKER = True if os.getenv('DOCKER') == 'true' else False
DEBUG = True if os.getenv('DEBUG') == 'true' else False
interval = os.getenv('LOOP')
timeout = os.getenv('TIMEOUT')
if DEBUG:
print(f"INTERVAL: {INTERVAL}")
print(f"TIMEOUT: {TIMEOUT}")
print(f"interval: {interval}")
print(f"timeout: {timeout}")
print(f"DOCKER: {DOCKER}")
print(f"DEBUG: {DEBUG}")
print("")
if DOCKER:
print("Running in docker")
interval = os.getenv('LOOP')
timeout = os.getenv('TIMEOUT')
try:INTERVAL = int(interval)
except:pass
@ -19,7 +28,8 @@ if DOCKER:
try:TIMEOUT = int(timeout)
except:pass
start_loop(INTERVAL, TIMEOUT)
if interval is None: log_to_json(start_discovery(timeout=TIMEOUT))
else:start_loop(INTERVAL, TIMEOUT)
else:
start_loop(interval=40)

View File

@ -1,13 +1,14 @@
TAG=develop
CONTAINER=dasmoorhuhn/atc-mithermometer-gateway:$TAG
CONTAINER_NAME=ATC_MiThermometer_Gateway
TAG="develop"
CONTAINER="dasmoorhuhn/atc-mithermometer-gateway"
CONTAINER_NAME="ATC_MiThermometer_Gateway"
VOLUME=YOUR_VOLUME
D=""
TIME_ZONE="Europe/Berlin"
BACKGROUND=""
TIME_ZONE=""
INTERACTIVE=false
BUILD=false
API=false
DEBUG=false
LOOP="0"
TIMEOUT="0"
@ -19,20 +20,19 @@ HELP="USAGE: sh run_docker.sh [OPTIONS] \n
[ -a | --api ] Start with the API \n
[ -tz | --timezone ] Set the timezone. Default is Europe/Berlin \n
[ -to | --timeout ] Set the timeout for the bluetooth scan. default is 20s \n
[ -h | --help ] Get this dialog"
[ -h | --help ] Get this dialog \n
[ --debug ] Set into debug mode"
docker_run() {
sudo killall -9 bluetoothd > /dev/null 2>&1
echo Killing old container...
docker stop $CONTAINER_NAME
docker stop $CONTAINER_NAME > /dev/null 2>&1
docker container rm $CONTAINER_NAME > /dev/null 2>&1
COMMAND="docker run $D"
COMMAND="docker run $BACKGROUND"
COMMAND="$COMMAND --cap-add=SYS_ADMIN"
COMMAND="$COMMAND --cap-add=NET_ADMIN"
COMMAND="$COMMAND --net=host"
COMMAND="$COMMAND --env TZ=$TIME_ZONE"
COMMAND="$COMMAND --env API=$API"
COMMAND="$COMMAND --name=$CONTAINER_NAME"
COMMAND="$COMMAND --restart=on-failure"
COMMAND="$COMMAND --volume=/var/run/dbus/:/var/run/dbus/"
@ -57,10 +57,28 @@ docker_run() {
sh build_docker.sh --tag $TAG
fi
if [ "$TIME_ZONE" != "" ]; then
COMMAND="$COMMAND --env TZ=$TIME_ZONE"
fi
if [ "$API" != false ]; then
COMMAND="$COMMAND --env API=$API"
fi
if [ "$DEBUG" = true ]; then
COMMAND="$COMMAND --env DEBUG=$DEBUG"
COMMAND="$COMMAND $CONTAINER:$TAG"
echo
echo $COMMAND
echo
echo DEBUG MODE
else
COMMAND="$COMMAND $CONTAINER:$TAG"
fi
echo Start container...
echo
$COMMAND $CONTAINER
$COMMAND
docker container rm $CONTAINER_NAME > /dev/null 2>&1
}
@ -68,7 +86,11 @@ docker_run() {
while [ "$1" != "" ]; do
case $1 in
-d )
D="-d"
BACKGROUND="-d"
shift
;;
--debug )
DEBUG=true
shift
;;
-a | --api)