Continue Flask migration.
This commit is contained in:
parent
361c53ef74
commit
31dfdaf64d
212
thermostat.py
212
thermostat.py
@ -217,112 +217,112 @@ first_loop = True
|
|||||||
# print(row)
|
# print(row)
|
||||||
#sys.stdout.flush()
|
#sys.stdout.flush()
|
||||||
|
|
||||||
while True:
|
#while True:
|
||||||
# if stop.is_set():
|
## if stop.is_set():
|
||||||
# httpd.shutdown()
|
## httpd.shutdown()
|
||||||
# httpd.server_close()
|
## httpd.server_close()
|
||||||
# dbconn.close()
|
## dbconn.close()
|
||||||
# break
|
## break
|
||||||
|
#
|
||||||
if new_forced_mode is not None:
|
# if new_forced_mode is not None:
|
||||||
with app.app_context():
|
# with app.app_context():
|
||||||
data = Set_mode({"value": new_forced_mode})
|
# data = Set_mode({"value": new_forced_mode})
|
||||||
db.session.add(data)
|
# db.session.add(data)
|
||||||
db.session.commit()
|
# db.session.commit()
|
||||||
#cursor.execute("INSERT OR REPLACE INTO set_mode (value) VALUES ('"+new_forced_mode+"')")
|
# #cursor.execute("INSERT OR REPLACE INTO set_mode (value) VALUES ('"+new_forced_mode+"')")
|
||||||
#dbconn.commit()
|
# #dbconn.commit()
|
||||||
logging.info("Switch to "+new_forced_mode)
|
# logging.info("Switch to "+new_forced_mode)
|
||||||
target_name = new_forced_mode
|
# target_name = new_forced_mode
|
||||||
new_forced_mode = None
|
# new_forced_mode = None
|
||||||
# Force immediate action:
|
# # Force immediate action:
|
||||||
last_control_time = None
|
# last_control_time = None
|
||||||
current_time = time.time()
|
# current_time = time.time()
|
||||||
current_date = datetime.now()
|
# current_date = datetime.now()
|
||||||
today_awake_time = current_date.replace(hour=int(awake_hour.split(':')[0]), minute=int(awake_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)
|
||||||
today_sleep_time = current_date.replace(hour=int(sleep_hour.split(':')[0]), minute=int(sleep_hour.split(':')[1]), second=0, microsecond=0)
|
# today_sleep_time = current_date.replace(hour=int(sleep_hour.split(':')[0]), minute=int(sleep_hour.split(':')[1]), second=0, microsecond=0)
|
||||||
forced_mode = get_forced_mode()
|
# forced_mode = get_forced_mode()
|
||||||
if forced_mode is not None and forced_mode in targets:
|
# if forced_mode is not None and forced_mode in targets:
|
||||||
if target_name != forced_mode:
|
# if target_name != forced_mode:
|
||||||
target_name = forced_mode
|
# target_name = forced_mode
|
||||||
logging.info("Switch to "+forced_mode)
|
# logging.info("Switch to "+forced_mode)
|
||||||
else:
|
# else:
|
||||||
if forced_mode == "long_absence":
|
# if forced_mode == "long_absence":
|
||||||
if target_name != "target_frost_protection" or first_loop:
|
# if target_name != "target_frost_protection" or first_loop:
|
||||||
target_name = "target_frost_protection"
|
# target_name = "target_frost_protection"
|
||||||
logging.info("Switch to "+target_name)
|
# logging.info("Switch to "+target_name)
|
||||||
elif forced_mode == "short_absence" or first_loop:
|
# elif forced_mode == "short_absence" or first_loop:
|
||||||
if target_name != "target_sleep_temperature":
|
# if target_name != "target_sleep_temperature":
|
||||||
target_name = "target_sleep_temperature"
|
# target_name = "target_sleep_temperature"
|
||||||
logging.info("Switch to "+target_name)
|
# logging.info("Switch to "+target_name)
|
||||||
elif current_date > today_awake_time and current_date < today_sleep_time:
|
# elif current_date > today_awake_time and current_date < today_sleep_time:
|
||||||
if target_name != "target_unconfirmed_awake_temperature" and target_name != "target_awake_temperature":
|
# if target_name != "target_unconfirmed_awake_temperature" and target_name != "target_awake_temperature":
|
||||||
target_name = "target_unconfirmed_awake_temperature"
|
# target_name = "target_unconfirmed_awake_temperature"
|
||||||
logging.info("Switch to unconfirmed awake mode.")
|
# logging.info("Switch to unconfirmed awake mode.")
|
||||||
elif current_date < today_awake_time or current_date > today_sleep_time:
|
# elif current_date < today_awake_time or current_date > today_sleep_time:
|
||||||
if target_name != "target_unconfirmed_sleep_temperature" and target_name != "target_sleep_temperature":
|
# if target_name != "target_unconfirmed_sleep_temperature" and target_name != "target_sleep_temperature":
|
||||||
target_name = "target_unconfirmed_sleep_temperature"
|
# target_name = "target_unconfirmed_sleep_temperature"
|
||||||
logging.info("Switch to unconfirmed sleep mode.")
|
# logging.info("Switch to unconfirmed sleep mode.")
|
||||||
|
#
|
||||||
first_loop = False
|
# first_loop = False
|
||||||
|
#
|
||||||
# Load shedder
|
# # Load shedder
|
||||||
current_load = get_metric("Modane_elec_main_power", current_time, load_shedder_interval)
|
# current_load = get_metric("Modane_elec_main_power", current_time, load_shedder_interval)
|
||||||
if current_load is None:
|
# if current_load is None:
|
||||||
time.sleep(load_shedder_interval)
|
# time.sleep(load_shedder_interval)
|
||||||
continue
|
# continue
|
||||||
elif max_load - current_load < load_margin:
|
# elif max_load - current_load < load_margin:
|
||||||
logging.warning("Load too high: "+str(current_load)+"VA")
|
# logging.warning("Load too high: "+str(current_load)+"VA")
|
||||||
total_shedded = 0
|
# total_shedded = 0
|
||||||
for room in shedding_order:
|
# for room in shedding_order:
|
||||||
current_state = relay_state(rooms_settings[room]["relays"])
|
# current_state = relay_state(rooms_settings[room]["relays"])
|
||||||
if current_state != "Failed":
|
# if current_state != "Failed":
|
||||||
logging.debug("Got relay_state: '"+current_state+"'")
|
# logging.debug("Got relay_state: '"+current_state+"'")
|
||||||
if current_state == "1":
|
# if current_state == "1":
|
||||||
result = set_relay(rooms_settings[room]["relays"], "off")
|
# result = set_relay(rooms_settings[room]["relays"], "off")
|
||||||
if result == "OK":
|
# if result == "OK":
|
||||||
total_shedded += relays_load[rooms_settings[room]["relays"]]
|
# total_shedded += relays_load[rooms_settings[room]["relays"]]
|
||||||
if max_load - current_load - total_shedded < load_margin:
|
# if max_load - current_load - total_shedded < load_margin:
|
||||||
logging.info("Load should be back to normal.")
|
# logging.info("Load should be back to normal.")
|
||||||
break
|
# break
|
||||||
|
#
|
||||||
# Thermostat
|
# # Thermostat
|
||||||
if last_control_time is None or current_time - last_control_time > relay_control_interval:
|
# if last_control_time is None or current_time - last_control_time > relay_control_interval:
|
||||||
last_control_time = current_time
|
# last_control_time = current_time
|
||||||
for room in rooms_settings:
|
# for room in rooms_settings:
|
||||||
if not rooms_settings[room]["enabled"]:
|
# if not rooms_settings[room]["enabled"]:
|
||||||
continue
|
# continue
|
||||||
target = rooms_settings[room][target_name]
|
# target = rooms_settings[room][target_name]
|
||||||
logging.debug("Target: "+str(target))
|
# logging.debug("Target: "+str(target))
|
||||||
temperature = get_metric(rooms_settings[room]["metric"], current_time, relay_control_interval)
|
# temperature = get_metric(rooms_settings[room]["metric"], current_time, relay_control_interval)
|
||||||
if temperature is None:
|
# if temperature is None:
|
||||||
continue
|
# continue
|
||||||
logging.debug(room+": "+str(temperature))
|
# logging.debug(room+": "+str(temperature))
|
||||||
current_state = relay_state(rooms_settings[room]["relays"])
|
# current_state = relay_state(rooms_settings[room]["relays"])
|
||||||
if current_state != "Failed":
|
# if current_state != "Failed":
|
||||||
logging.debug("Got relay_state: '"+current_state+"'")
|
# logging.debug("Got relay_state: '"+current_state+"'")
|
||||||
if temperature < target - hysteresis:
|
# if temperature < target - hysteresis:
|
||||||
if current_state == "0":
|
# if current_state == "0":
|
||||||
logging.info(room+": Target temperature is "+str(target))
|
# logging.info(room+": Target temperature is "+str(target))
|
||||||
logging.info(room+": Current temperature is "+str(temperature))
|
# logging.info(room+": Current temperature is "+str(temperature))
|
||||||
if current_load + relays_load[rooms_settings[room]["relays"]] + load_margin > max_load:
|
# if current_load + relays_load[rooms_settings[room]["relays"]] + load_margin > max_load:
|
||||||
logging.warning(room+": Load too high cannot start heaters.")
|
# logging.warning(room+": Load too high cannot start heaters.")
|
||||||
else:
|
# else:
|
||||||
logging.info(room+": Starting heaters.")
|
# logging.info(room+": Starting heaters.")
|
||||||
set_relay(rooms_settings[room]["relays"], "on")
|
# set_relay(rooms_settings[room]["relays"], "on")
|
||||||
sys.stdout.flush()
|
# sys.stdout.flush()
|
||||||
else:
|
# else:
|
||||||
logging.debug("Relay already on.")
|
# logging.debug("Relay already on.")
|
||||||
|
#
|
||||||
elif temperature > target + hysteresis:
|
# elif temperature > target + hysteresis:
|
||||||
if current_state == "1":
|
# if current_state == "1":
|
||||||
logging.info(room+": Target temperature is "+str(target))
|
# logging.info(room+": Target temperature is "+str(target))
|
||||||
logging.info(room+": Current temperature is "+str(temperature))
|
# logging.info(room+": Current temperature is "+str(temperature))
|
||||||
logging.info(room+": Stopping heaters.")
|
# logging.info(room+": Stopping heaters.")
|
||||||
sys.stdout.flush()
|
# sys.stdout.flush()
|
||||||
set_relay(rooms_settings[room]["relays"], "off")
|
# set_relay(rooms_settings[room]["relays"], "off")
|
||||||
else:
|
# else:
|
||||||
logging.debug("Relay already off.")
|
# logging.debug("Relay already off.")
|
||||||
time.sleep(load_shedder_interval)
|
# time.sleep(load_shedder_interval)
|
||||||
|
|
||||||
logging.info("====== Ended successfully ======")
|
logging.info("====== Ended successfully ======")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user