diff --git a/.media/demo.gif b/.media/demo.gif new file mode 100644 index 0000000..5a550bc Binary files /dev/null and b/.media/demo.gif differ diff --git a/Dockerfile b/Dockerfile index 3fb8851..bf958e5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/README.md b/README.md index 602cfbf..a5e2f66 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ * [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 diff --git a/python/docker_entrypoint.sh b/python/docker_entrypoint.sh index 9e4f582..aeb780b 100644 --- a/python/docker_entrypoint.sh +++ b/python/docker_entrypoint.sh @@ -3,8 +3,8 @@ env > .env if [ "$API" = true ]; then - python3.12 api_endpoints.py & - sleep 1 + python3.12 api_endpoints.py & + sleep 1 fi -sudo python3.12 main.py \ No newline at end of file +python3.12 main.py \ No newline at end of file diff --git a/python/requierements.txt b/python/requierements.txt index bfe7725..b7ed443 100644 --- a/python/requierements.txt +++ b/python/requierements.txt @@ -1,3 +1,4 @@ +bluepy pyyaml bs4 requests diff --git a/python/src/load_env.py b/python/src/load_env.py deleted file mode 100644 index 641c4fa..0000000 --- a/python/src/load_env.py +++ /dev/null @@ -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 diff --git a/python/src/log_data.py b/python/src/log_data.py index 54d541a..a157481 100644 --- a/python/src/log_data.py +++ b/python/src/log_data.py @@ -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) diff --git a/python/src/main.py b/python/src/main.py index 934f877..2fd502a 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -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) diff --git a/run_docker.sh b/run_docker.sh index c6e4b24..81ef62b 100644 --- a/run_docker.sh +++ b/run_docker.sh @@ -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 - echo $COMMAND + 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)