fix up some mess

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

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)