From aa69c98fc72c8f5ba38d324b5ee9c263cf5fa23d Mon Sep 17 00:00:00 2001 From: yohan <783b8c87@scimetis.net> Date: Mon, 3 Jun 2024 00:32:44 +0200 Subject: [PATCH] Continue Flask migration. --- thermostat.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/thermostat.py b/thermostat.py index 7a511ca..7a41c9b 100644 --- a/thermostat.py +++ b/thermostat.py @@ -15,6 +15,7 @@ import time import requests import subprocess from threading import Thread +from threading import Lock import sqlalchemy.exc from sqlalchemy.dialects.sqlite import insert as sqlite_upsert @@ -32,8 +33,15 @@ from sqlalchemy.dialects.sqlite import insert as sqlite_upsert # Flask-SQLAlchemy documentation: https://flask-sqlalchemy.palletsprojects.com/en/3.0.x/quickstart/ # We use SQLAlchemy ORM style 2.x: https://docs.sqlalchemy.org/en/20/tutorial/data_select.html +xprint_lock = Lock() logging.basicConfig(level=logging.WARNING) +def xprint(*args, **kwargs): + """Thread safe print function""" + with xprint_lock: + print(*args, **kwargs) + sys.stdout.flush() + authorizations = { 'apikey': { 'type': 'apiKey', @@ -150,7 +158,7 @@ def get_forced_mode(): return None #cur.execute("SELECT value, timestamp FROM set_mode WHERE name='mode'") #row = cur.fetchone() - print(row) + xprint(row) data = dict(zip(['value', 'timestamp'], row)) timestamp = data['timestamp'].replace(tzinfo=timezone.utc).timestamp() # We ignore old targets but never ignore absence modes @@ -217,9 +225,14 @@ Set_verbosity_model = api.model('Set_verbosity_Model', Set_verbosity_resource_fi class Set_verbosity_thermostat(Resource): @auth_required @api.expect(Set_verbosity_model, validate=True) - @api.marshal_with(Set_verbosity_model) def put(self): - print(api.payload) + xprint(api.payload.value) + if api.payload.value == 'DEBUG': + logging.getLogger().setLevel(logging.DEBUG) + elif api.payload.value == 'INFO': + logging.getLogger().setLevel(logging.INFO) + elif api.payload.value == 'WARNING': + logging.getLogger().setLevel(logging.WARNING) return "OK", 201 api.add_namespace(ns_thermostat) @@ -232,7 +245,7 @@ migrate = Migrate(app, db, compare_type=True) #cursor.execute("SELECT * FROM set_mode") #rows = cursor.fetchall() #for row in rows: -# print(row) +# xprint(row) #sys.stdout.flush() target_name = default_target @@ -387,16 +400,7 @@ logging.info("====== Ended successfully ======") #import threading #import socketserver -#from threading import Lock -# -#xprint_lock = Lock() -# -#def xprint(*args, **kwargs): -# """Thread safe print function""" -# with xprint_lock: -# print(*args, **kwargs) -# sys.stdout.flush() -# + #p = argparse.ArgumentParser(description='Thermostat and load shedder.') #p.add_argument("-v", "--verbosity", help="Increase output verbosity", # type=str, choices=['DEBUG', 'INFO', 'WARNING'], default='INFO')