Continue Flask migration.

This commit is contained in:
yohan 2024-06-02 14:12:46 +02:00
parent 31dfdaf64d
commit c998e291f9

View File

@ -14,6 +14,7 @@ from datetime import datetime
import time import time
import requests import requests
import subprocess import subprocess
from threading import Thread
# This code has been written for # This code has been written for
# python3 3.11.2-1+b1 # python3 3.11.2-1+b1
@ -217,113 +218,115 @@ first_loop = True
# print(row) # print(row)
#sys.stdout.flush() #sys.stdout.flush()
#while True: def thermostat_loop():
## if stop.is_set(): while True:
## httpd.shutdown() # # if stop.is_set():
## httpd.server_close() # # httpd.shutdown()
## dbconn.close() # # httpd.server_close()
## break # # dbconn.close()
# # # break
# if new_forced_mode is not None: #
# with app.app_context(): # if new_forced_mode is not None:
# data = Set_mode({"value": new_forced_mode}) # with app.app_context():
# db.session.add(data) # data = Set_mode({"value": new_forced_mode})
# db.session.commit() # db.session.add(data)
# #cursor.execute("INSERT OR REPLACE INTO set_mode (value) VALUES ('"+new_forced_mode+"')") # db.session.commit()
# #dbconn.commit() # #cursor.execute("INSERT OR REPLACE INTO set_mode (value) VALUES ('"+new_forced_mode+"')")
# logging.info("Switch to "+new_forced_mode) # #dbconn.commit()
# target_name = new_forced_mode # logging.info("Switch to "+new_forced_mode)
# new_forced_mode = None # target_name = new_forced_mode
# # Force immediate action: # new_forced_mode = None
# last_control_time = None # # Force immediate action:
# current_time = time.time() # last_control_time = None
# current_date = datetime.now() # current_time = time.time()
# today_awake_time = current_date.replace(hour=int(awake_hour.split(':')[0]), minute=int(awake_hour.split(':')[1]), second=0, microsecond=0) # current_date = datetime.now()
# today_sleep_time = current_date.replace(hour=int(sleep_hour.split(':')[0]), minute=int(sleep_hour.split(':')[1]), second=0, microsecond=0) # today_awake_time = current_date.replace(hour=int(awake_hour.split(':')[0]), minute=int(awake_hour.split(':')[1]), second=0, microsecond=0)
# forced_mode = get_forced_mode() # today_sleep_time = current_date.replace(hour=int(sleep_hour.split(':')[0]), minute=int(sleep_hour.split(':')[1]), second=0, microsecond=0)
# if forced_mode is not None and forced_mode in targets: # forced_mode = get_forced_mode()
# if target_name != forced_mode: # if forced_mode is not None and forced_mode in targets:
# target_name = forced_mode # if target_name != forced_mode:
# logging.info("Switch to "+forced_mode) # target_name = forced_mode
# else: # logging.info("Switch to "+forced_mode)
# if forced_mode == "long_absence": # else:
# if target_name != "target_frost_protection" or first_loop: # if forced_mode == "long_absence":
# target_name = "target_frost_protection" # if target_name != "target_frost_protection" or first_loop:
# logging.info("Switch to "+target_name) # target_name = "target_frost_protection"
# elif forced_mode == "short_absence" or first_loop: # logging.info("Switch to "+target_name)
# if target_name != "target_sleep_temperature": # elif forced_mode == "short_absence" or first_loop:
# target_name = "target_sleep_temperature" # if target_name != "target_sleep_temperature":
# logging.info("Switch to "+target_name) # target_name = "target_sleep_temperature"
# elif current_date > today_awake_time and current_date < today_sleep_time: # logging.info("Switch to "+target_name)
# if target_name != "target_unconfirmed_awake_temperature" and target_name != "target_awake_temperature": # elif current_date > today_awake_time and current_date < today_sleep_time:
# target_name = "target_unconfirmed_awake_temperature" # if target_name != "target_unconfirmed_awake_temperature" and target_name != "target_awake_temperature":
# logging.info("Switch to unconfirmed awake mode.") # target_name = "target_unconfirmed_awake_temperature"
# elif current_date < today_awake_time or current_date > today_sleep_time: # logging.info("Switch to unconfirmed awake mode.")
# if target_name != "target_unconfirmed_sleep_temperature" and target_name != "target_sleep_temperature": # elif current_date < today_awake_time or current_date > today_sleep_time:
# target_name = "target_unconfirmed_sleep_temperature" # if target_name != "target_unconfirmed_sleep_temperature" and target_name != "target_sleep_temperature":
# logging.info("Switch to unconfirmed sleep mode.") # target_name = "target_unconfirmed_sleep_temperature"
# # logging.info("Switch to unconfirmed sleep mode.")
# first_loop = False #
# # first_loop = False
# # Load shedder #
# current_load = get_metric("Modane_elec_main_power", current_time, load_shedder_interval) # # Load shedder
# if current_load is None: # current_load = get_metric("Modane_elec_main_power", current_time, load_shedder_interval)
# time.sleep(load_shedder_interval) # if current_load is None:
# continue # time.sleep(load_shedder_interval)
# elif max_load - current_load < load_margin: # continue
# logging.warning("Load too high: "+str(current_load)+"VA") # elif max_load - current_load < load_margin:
# total_shedded = 0 # logging.warning("Load too high: "+str(current_load)+"VA")
# for room in shedding_order: # total_shedded = 0
# current_state = relay_state(rooms_settings[room]["relays"]) # for room in shedding_order:
# if current_state != "Failed": # current_state = relay_state(rooms_settings[room]["relays"])
# logging.debug("Got relay_state: '"+current_state+"'") # if current_state != "Failed":
# if current_state == "1": # logging.debug("Got relay_state: '"+current_state+"'")
# result = set_relay(rooms_settings[room]["relays"], "off")
# if result == "OK":
# total_shedded += relays_load[rooms_settings[room]["relays"]]
# if max_load - current_load - total_shedded < load_margin:
# logging.info("Load should be back to normal.")
# break
#
# # Thermostat
# if last_control_time is None or current_time - last_control_time > relay_control_interval:
# last_control_time = current_time
# for room in rooms_settings:
# if not rooms_settings[room]["enabled"]:
# continue
# target = rooms_settings[room][target_name]
# logging.debug("Target: "+str(target))
# temperature = get_metric(rooms_settings[room]["metric"], current_time, relay_control_interval)
# if temperature is None:
# continue
# logging.debug(room+": "+str(temperature))
# current_state = relay_state(rooms_settings[room]["relays"])
# if current_state != "Failed":
# logging.debug("Got relay_state: '"+current_state+"'")
# if temperature < target - hysteresis:
# if current_state == "0":
# logging.info(room+": Target temperature is "+str(target))
# logging.info(room+": Current temperature is "+str(temperature))
# if current_load + relays_load[rooms_settings[room]["relays"]] + load_margin > max_load:
# logging.warning(room+": Load too high cannot start heaters.")
# else:
# logging.info(room+": Starting heaters.")
# set_relay(rooms_settings[room]["relays"], "on")
# sys.stdout.flush()
# else:
# logging.debug("Relay already on.")
#
# elif temperature > target + hysteresis:
# if current_state == "1": # if current_state == "1":
# logging.info(room+": Target temperature is "+str(target)) # result = set_relay(rooms_settings[room]["relays"], "off")
# logging.info(room+": Current temperature is "+str(temperature)) # if result == "OK":
# logging.info(room+": Stopping heaters.") # total_shedded += relays_load[rooms_settings[room]["relays"]]
# sys.stdout.flush() # if max_load - current_load - total_shedded < load_margin:
# set_relay(rooms_settings[room]["relays"], "off") # logging.info("Load should be back to normal.")
# else: # break
# logging.debug("Relay already off.") #
# time.sleep(load_shedder_interval) # # Thermostat
# if last_control_time is None or current_time - last_control_time > relay_control_interval:
# last_control_time = current_time
# for room in rooms_settings:
# if not rooms_settings[room]["enabled"]:
# continue
# target = rooms_settings[room][target_name]
# logging.debug("Target: "+str(target))
# temperature = get_metric(rooms_settings[room]["metric"], current_time, relay_control_interval)
# if temperature is None:
# continue
# logging.debug(room+": "+str(temperature))
# current_state = relay_state(rooms_settings[room]["relays"])
# if current_state != "Failed":
# logging.debug("Got relay_state: '"+current_state+"'")
# if temperature < target - hysteresis:
# if current_state == "0":
# logging.info(room+": Target temperature is "+str(target))
# logging.info(room+": Current temperature is "+str(temperature))
# if current_load + relays_load[rooms_settings[room]["relays"]] + load_margin > max_load:
# logging.warning(room+": Load too high cannot start heaters.")
# else:
# logging.info(room+": Starting heaters.")
# set_relay(rooms_settings[room]["relays"], "on")
# sys.stdout.flush()
# else:
# logging.debug("Relay already on.")
#
# elif temperature > target + hysteresis:
# if current_state == "1":
# logging.info(room+": Target temperature is "+str(target))
# logging.info(room+": Current temperature is "+str(temperature))
# logging.info(room+": Stopping heaters.")
# sys.stdout.flush()
# set_relay(rooms_settings[room]["relays"], "off")
# else:
# logging.debug("Relay already off.")
time.sleep(load_shedder_interval)
t1 = Thread(thermostat_loop)
logging.info("====== Ended successfully ======") logging.info("====== Ended successfully ======")
#import argparse #import argparse