From 01175129287d7514d014e5b0b70fca49c986dfb3 Mon Sep 17 00:00:00 2001 From: yohan <783b8c87@scimetis.net> Date: Wed, 5 Jun 2024 07:54:35 +0200 Subject: [PATCH] Add debug. --- thermostat.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/thermostat.py b/thermostat.py index 3150b01..755c9d7 100644 --- a/thermostat.py +++ b/thermostat.py @@ -273,25 +273,22 @@ def thermostat_loop(): if new_forced_mode is not None: with app.app_context(): data = Set_mode(value=new_forced_mode) + logging.debug("Update mode in DB") try: - logging.debug("Insert mode in DB") - db.session.add(data) - db.session.commit() - except sqlalchemy.exc.IntegrityError as e: - try: - logging.debug("Row already exists in DB.") - db.session.rollback() - logging.debug("Update mode in DB") - cur_mode = db.session.execute(db.select(Set_mode).filter_by(name="mode")).scalar_one() - logging.debug("Current mode: "+str(cur_mode.value)) - cur_mode = data - db.session.commit() - except Exception as e: - db.session.rollback() - logging.error(e) + cur_mode = db.session.execute(db.select(Set_mode).filter_by(name="mode")).scalar_one() + logging.debug("Remove current mode: "+str(cur_mode.value)) + db.session.delete(cur_mode) except Exception as e: - db.session.rollback() - logging.error(e) + db.session.rollback() + logging.debug(e) + try: + logging.debug("Insert mode in DB") + db.session.add(data) + db.session.commit() + except Exception as e: + db.session.rollback() + db.session.commit() + logging.error(e) #cursor.execute("INSERT OR REPLACE INTO set_mode (value) VALUES ('"+new_forced_mode+"')") #dbconn.commit()