Continue Flask migration.

This commit is contained in:
yohan 2024-06-03 00:32:44 +02:00
parent ed602a8abc
commit aa69c98fc7

View File

@ -15,6 +15,7 @@ import time
import requests import requests
import subprocess import subprocess
from threading import Thread from threading import Thread
from threading import Lock
import sqlalchemy.exc import sqlalchemy.exc
from sqlalchemy.dialects.sqlite import insert as sqlite_upsert 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/ # 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 # 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) logging.basicConfig(level=logging.WARNING)
def xprint(*args, **kwargs):
"""Thread safe print function"""
with xprint_lock:
print(*args, **kwargs)
sys.stdout.flush()
authorizations = { authorizations = {
'apikey': { 'apikey': {
'type': 'apiKey', 'type': 'apiKey',
@ -150,7 +158,7 @@ def get_forced_mode():
return None return None
#cur.execute("SELECT value, timestamp FROM set_mode WHERE name='mode'") #cur.execute("SELECT value, timestamp FROM set_mode WHERE name='mode'")
#row = cur.fetchone() #row = cur.fetchone()
print(row) xprint(row)
data = dict(zip(['value', 'timestamp'], row)) data = dict(zip(['value', 'timestamp'], row))
timestamp = data['timestamp'].replace(tzinfo=timezone.utc).timestamp() timestamp = data['timestamp'].replace(tzinfo=timezone.utc).timestamp()
# We ignore old targets but never ignore absence modes # 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): class Set_verbosity_thermostat(Resource):
@auth_required @auth_required
@api.expect(Set_verbosity_model, validate=True) @api.expect(Set_verbosity_model, validate=True)
@api.marshal_with(Set_verbosity_model)
def put(self): 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 return "OK", 201
api.add_namespace(ns_thermostat) api.add_namespace(ns_thermostat)
@ -232,7 +245,7 @@ migrate = Migrate(app, db, compare_type=True)
#cursor.execute("SELECT * FROM set_mode") #cursor.execute("SELECT * FROM set_mode")
#rows = cursor.fetchall() #rows = cursor.fetchall()
#for row in rows: #for row in rows:
# print(row) # xprint(row)
#sys.stdout.flush() #sys.stdout.flush()
target_name = default_target target_name = default_target
@ -387,16 +400,7 @@ logging.info("====== Ended successfully ======")
#import threading #import threading
#import socketserver #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 = argparse.ArgumentParser(description='Thermostat and load shedder.')
#p.add_argument("-v", "--verbosity", help="Increase output verbosity", #p.add_argument("-v", "--verbosity", help="Increase output verbosity",
# type=str, choices=['DEBUG', 'INFO', 'WARNING'], default='INFO') # type=str, choices=['DEBUG', 'INFO', 'WARNING'], default='INFO')