Continue Flask migration.

This commit is contained in:
yohan 2024-06-02 08:39:31 +02:00
parent f85a1564b0
commit 29d1bb35c1
2 changed files with 92 additions and 36 deletions

View File

@ -10,3 +10,73 @@ flask_settings_dev:
flask_settings_prod:
DEBUG: False
SQLALCHEMY_TRACK_MODIFICATIONS: True
targets:
- target_awake_temperature
- target_sleep_temperature
- target_frost_protection
modes:
- short_absence
- long_absence
rooms_settings:
"double bedroom":
target_awake_temperature: 19
target_unconfirmed_awake_temperature: 18
target_sleep_temperature: 18
target_unconfirmed_sleep_temperature: 18
target_frost_protection: 6
metric: Modane_temperature_double_bedroom
relays: "1"
enabled: True
"single bedroom":
target_awake_temperature: 17
target_unconfirmed_awake_temperature: 16
target_sleep_temperature: 16
target_unconfirmed_sleep_temperature: 16
target_frost_protection: 6
metric: Modane_temperature_single_bedroom
relays: "3"
enabled: True
"living room":
target_awake_temperature: 21
target_unconfirmed_awake_temperature: 21
target_sleep_temperature: 18
target_unconfirmed_sleep_temperature: 18
target_frost_protection: 6
metric: Modane_temperature_living_room
relays: "4"
enabled: True
"bathroom":
target_awake_temperature: 21
target_unconfirmed_awake_temperature: 21
target_sleep_temperature: 19
target_unconfirmed_sleep_temperature: 19
target_frost_protection: 6
metric: Modane_temperature_bathroom
relays: ""
enabled: False
shedding_order:
- double bedroom
- single bedroom
- living room
default_target: target_frost_protection
# Load in VA
relays_load:
"1": 1500
"3": 1500
"4": 2000
awake_hour: "7:30"
sleep_hour: "23:30"
forced_mode_duration: 7200.0
load_shedder_interval: 10.0
relay_control_interval: 300.0
hysteresis: 0.5
max_load: 7800
load_margin: 100

View File

@ -8,6 +8,7 @@ import logging
import yaml
import os
import sys
import datetime
logging.basicConfig(level=logging.WARNING)
@ -62,6 +63,24 @@ def auth_required(func):
return func(*args, **kwargs)
return check_auth
##
def now():
return "["+datetime.datetime.now().strftime("%c")+"]"
def enabled_rooms():
rooms_list=[]
for room in rooms_settings:
if rooms_settings[room]["enabled"]:
rooms_list.append(room)
return rooms_list
def status_as_text():
return 'target: '+target_name+'\n'\
+'forced_mode: '+str(forced_mode)+'\n'\
+'\n'.join(['Target temperature for '+room+': '+str(rooms_settings[room][target_name])+'\n'+'Current temperature for '+room+': '+str(get_metric(rooms_settings[room]["metric"], current_time, relay_control_interval)) for room in enabled_rooms()])
##
app = Flask(__name__)
app.config.from_mapping(flask_settings)
app.config.from_mapping(flask_settings_env)
@ -104,27 +123,7 @@ class Set_mode_thermostat(Resource):
class Status_thermostat(Resource):
@auth_required
def get(self):
logging.debug(json.loads(request.args.get("filter")))
page = request.args.get('page', default = 1, type = int)
filters = json.loads(request.args.get("filter", default = '*', type = str))
record_query = Stock.query.filter()
for stock_filter in filters:
if 'name' in stock_filter.keys():
if stock_filter['name'] == 'metric':
record_query = record_query.filter(Stock.metric == stock_filter["val"])
if stock_filter['name'] in ['price', 'volume', 'time']:
if stock_filter['op'] == 'le':
record_query = record_query.filter(getattr(Stock, stock_filter['name']) <= stock_filter["val"])
if stock_filter['op'] == 'ge':
record_query = record_query.filter(getattr(Stock, stock_filter['name']) >= stock_filter["val"])
if stock_filter['op'] == 'eq':
record_query = record_query.filter(getattr(Stock, stock_filter['name']) == stock_filter["val"])
record_query = record_query.paginate(page=page, per_page=20)
result = dict(datas=record_query.items,
total_results=record_query.total,
current_page=record_query.page,
pages=record_query.pages)
result = dict(datas=status_as_text())
logging.debug(result)
return result
@ -136,7 +135,6 @@ migrate = Migrate(app, db, compare_type=True)
#import subprocess
#import time
#import datetime
#from datetime import timezone
#import requests
#import argparse
@ -176,8 +174,6 @@ migrate = Migrate(app, db, compare_type=True)
#elif verbosity == 'WARNING':
# logging.basicConfig(level=logging.WARNING)
#
#def now():
# return "["+datetime.datetime.now().strftime("%c")+"]"
#
#def api_help():
# return """
@ -193,18 +189,8 @@ migrate = Migrate(app, db, compare_type=True)
#+'Permanent forced modes:\n'\
#+'\n'.join(['# /'+mode+'\n' for mode in modes])
#
#def enabled_rooms():
# rooms_list=[]
# for room in rooms_settings:
# if rooms_settings[room]["enabled"]:
# rooms_list.append(room)
# return rooms_list
#
#def status_as_text():
# return 'target: '+target_name+'\n'\
# +'forced_mode: '+str(forced_mode)+'\n'\
# +'\n'.join(['Target temperature for '+room+': '+str(rooms_settings[room][target_name])+'\n'+'Current temperature for '+room+': '+str(get_metric(rooms_settings[room]["metric"], current_time, relay_control_interval)) for room in enabled_rooms()])
#
#def relay_state(relay):
# try:
# returned_output = subprocess.check_output(["./relay.py", relay, "status"])