diff --git a/app/__init__.py b/app/__init__.py index 12d4361..b64a251 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -36,6 +36,7 @@ def create_app(debug=False): app.config.update( SECRET_KEY='bosco', CORS_HEADERS='Content-Type', + BASE_PATH=BASE_PATH, RECIPES_PATH=BASE_PATH.joinpath('app/recipes'), SESSIONS_PATH=BASE_PATH.joinpath('app/sessions') ) diff --git a/app/main/config.py b/app/main/config.py index e824a25..cadf3f2 100644 --- a/app/main/config.py +++ b/app/main/config.py @@ -1,6 +1,11 @@ from flask import current_app +# base path +def base_path(): + return current_app.config['BASE_PATH'] + + # recipe paths def zymatic_recipe_path(): return current_app.config['RECIPES_PATH'].joinpath('zymatic') diff --git a/app/main/routes_frontend.py b/app/main/routes_frontend.py index f3fa044..dabb8eb 100644 --- a/app/main/routes_frontend.py +++ b/app/main/routes_frontend.py @@ -1,12 +1,16 @@ import json +import os import requests +import sys import uuid -from flask import render_template, request +from flask import render_template, request, redirect +from threading import Thread +from time import sleep from . import main from .recipe_parser import PicoBrewRecipe, PicoBrewRecipeImport, ZymaticRecipe, ZymaticRecipeImport, ZSeriesRecipe from .session_parser import load_ferm_session, get_ferm_graph_data, get_brew_graph_data, load_brew_session, active_brew_sessions, active_ferm_sessions -from .config import zymatic_recipe_path, zseries_recipe_path, pico_recipe_path, ferm_archive_sessions_path, brew_archive_sessions_path +from .config import base_path, zymatic_recipe_path, zseries_recipe_path, pico_recipe_path, ferm_archive_sessions_path, brew_archive_sessions_path # -------- Routes -------- @@ -16,6 +20,34 @@ def index(): ferm_sessions=load_active_ferm_sessions()) +@main.route('/restart_server') +def restart_server(): + # git pull & install any updated requirements + os.system('cd {0};git pull;pip3 install -r requirements.txt'.format(base_path())) + # TODO: Close file handles for open sessions? + + def restart(): + sleep(2) + os.execl(sys.executable, *([sys.executable]+sys.argv)) + thread = Thread(target=restart, daemon=True) + thread.start() + return redirect('/') + + +@main.route('/restart_system') +def restart_system(): + os.system('shutdown -r now') + # TODO: redirect to a page with alert of restart + return redirect('/') + + +@main.route('/shutdown_system') +def shutdown_system(): + os.system('shutdown -h now') + # TODO: redirect to a page with alert of shutdown + return redirect('/') + + @main.route('/brew_history') def brew_history(): return render_template('brew_history.html', sessions=load_brew_sessions()) diff --git a/app/main/session_parser.py b/app/main/session_parser.py index faf4240..0152089 100644 --- a/app/main/session_parser.py +++ b/app/main/session_parser.py @@ -42,8 +42,8 @@ def load_brew_session(file): 'data': json_data, 'graph': get_brew_graph_data(chart_id, name, step, json_data) } - if len(json_data) > 0: - session['recovery'] = json_data[-1]['recovery'] + if len(json_data) > 0 and 'recovery' in json_data[-1]: + session.update({'recovery': json_data[-1]['recovery']}) return (session) diff --git a/app/templates/navbar.html b/app/templates/navbar.html index ee3cac0..4fa280b 100644 --- a/app/templates/navbar.html +++ b/app/templates/navbar.html @@ -26,5 +26,13 @@