diff --git a/.env b/.env deleted file mode 100644 index e69de29..0000000 diff --git a/.gitignore b/.gitignore index 5078187..7b4b1f4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ history.* data/ *.json *.iso -*.cow \ No newline at end of file +*.cow +.env \ No newline at end of file diff --git a/example.env b/example.env new file mode 100644 index 0000000..37d421d --- /dev/null +++ b/example.env @@ -0,0 +1,10 @@ +BACKGROUND= +TIME_ZONE= +NAME= +INTERACTIVE=true +BUILD=true +API=true +DEBUG=INFO +MODE=1 +LOOP=20 +TIMEOUT=20 \ No newline at end of file diff --git a/python/src/logger.py b/python/src/logger.py index 8dfc870..cd17c5e 100644 --- a/python/src/logger.py +++ b/python/src/logger.py @@ -4,9 +4,12 @@ import logging def get_logger(logger_name:str, log_file='gateway.log'): logger_name = logger_name.replace('__', '') - DEBUG = os.getenv('DEBUG') == 'true' + debug_level = os.getenv('DEBUG').upper() + if debug_level not in ('CRITICAL', 'ERROR', 'WARNING', 'WARN', 'INFO', 'DEBUG', 'NOTSET', 'FATAL'): + print(f'Loglevel "{debug_level}" is not supported.') + exit(0) logger = logging.getLogger(logger_name) - logger.setLevel(logging.DEBUG) if DEBUG else logger.setLevel(logging.INFO) + logger.setLevel(logging.getLevelName(debug_level)) handler = logging.FileHandler(filename=f'data/{log_file}', encoding='utf-8', mode='a') formatter = logging.Formatter('%(asctime)s|%(levelname)s|%(name)s|:%(message)s') handler.setFormatter(formatter) diff --git a/python/src/main.py b/python/src/main.py index 7b0cdf9..b9936d6 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -26,18 +26,20 @@ logger.debug(f"VERSION: {os.getenv('VERSION')}") update_state = check_for_update() print_state(update_state) +try: + if DOCKER: + logger.info('Running in Docker') -if DOCKER: - logger.info('Running in Docker') + try:INTERVAL = int(interval) + except:pass - try:INTERVAL = int(interval) - except:pass + try:TIMEOUT = int(timeout) + except:pass - try:TIMEOUT = int(timeout) - except:pass + if interval is None: log_to_json(start_discovery(timeout=TIMEOUT)) + else: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) + else: + start_loop(interval=40) +except Exception as err: + logger.error(err) diff --git a/run_gateway.sh b/run_gateway.sh index 0699743..c39f609 100644 --- a/run_gateway.sh +++ b/run_gateway.sh @@ -3,18 +3,8 @@ CONTAINER="dasmoorhuhn/atc-mithermometer-gateway" CONTAINER_NAME="ATC_MiThermometer_Gateway" VOLUME=$(pwd)/data -BACKGROUND="" -TIME_ZONE="" -NAME="" -INTERACTIVE=false -BUILD=false -API=false -DEBUG=false -MODE=1 -LOOP="0" -TIMEOUT="0" - -HELP="USAGE: sh run_docker.sh [OPTIONS] \n +HELP="Using any command line argument except -d bypasses the .env file\n\n +USAGE: sh run_docker.sh [OPTIONS] \n [ -d ] Run in Backgrund \n [ -t | --tag ] Set a docker tag. Default: latest \n [ -b | --build ] Build the image before running the container \n @@ -55,6 +45,43 @@ docker_run() { check_for_devices_config + if [ "$SKIP_ENV" = true ]; then + echo "Skip env file" + ENV_EXISTS=false + + BACKGROUND="" + TIME_ZONE="" + NAME="" + INTERACTIVE=true + BUILD=true + API=true + DEBUG="INFO" + MODE="1" + LOOP="40" + TIMEOUT="20" + else + if [ -e .env ] + then + echo Loading .env file + export $(cat .env | xargs) + ENV_EXISTS=true + else + echo No env file found + ENV_EXISTS=false + + BACKGROUND="" + TIME_ZONE="" + NAME="" + INTERACTIVE=true + BUILD=true + API=true + DEBUG="INFO" + MODE="1" + LOOP="40" + TIMEOUT="20" + fi + fi + COMMAND="docker run $BACKGROUND" COMMAND="$COMMAND --cap-add=SYS_ADMIN" COMMAND="$COMMAND --cap-add=NET_ADMIN" @@ -65,6 +92,10 @@ docker_run() { COMMAND="$COMMAND --volume=$VOLUME:/src/data" COMMAND="$COMMAND --volume=$PWD/devices.yml:/src/devices.yml" + if [ "$ENV_EXISTS" = true ]; then + COMMAND="$COMMAND --env-file .env" + fi + if [ "$INTERACTIVE" = true ]; then COMMAND="$COMMAND --interactive" COMMAND="$COMMAND --tty" @@ -100,7 +131,7 @@ docker_run() { COMMAND="$COMMAND --env NAME=$NAME" fi - if [ "$DEBUG" = true ]; then + if [ "$DEBUG" = "DEBUG" ]; then COMMAND="$COMMAND --env DEBUG=$DEBUG" COMMAND="$COMMAND $CONTAINER:$TAG" echo @@ -108,6 +139,7 @@ docker_run() { echo echo DEBUG MODE else + COMMAND="$COMMAND --env DEBUG=$DEBUG" COMMAND="$COMMAND $CONTAINER:$TAG" fi @@ -120,53 +152,68 @@ docker_run() { while [ "$1" != "" ]; do case $1 in + -se | --skip-env-file ) + SKIP_ENV=true + shift + ;; -d ) BACKGROUND="-d" shift ;; --debug ) - DEBUG=true + shift + DEBUG=$1 + SKIP_ENV=true shift ;; -a | --api) API=true + SKIP_ENV=true shift ;; -b | --build ) BUILD=true + SKIP_ENV=true shift ;; -v | --volume ) shift VOLUME=$1 + SKIP_ENV=true shift ;; -n | --name ) shift NAME=$1 + SKIP_ENV=true shift ;; -m2 | --mesh-gateway) MODE=2 + SKIP_ENV=true shift ;; -tz | --timezone ) shift TIME_ZONE=$1 + SKIP_ENV=true shift ;; -to | --timeout ) shift TIMEOUT=$1 + SKIP_ENV=true shift ;; -t | --tag ) shift TAG=$1 + SKIP_ENV=true shift ;; -l | --loop ) shift + SKIP_ENV=true firstchar=`echo $1 | cut -c1-1` if [ "$firstchar" = "-" ]; then LOOP=0 @@ -179,6 +226,7 @@ while [ "$1" != "" ]; do ;; -i | --interactive ) INTERACTIVE=true + SKIP_ENV=true shift ;; -h | --help )