From 453ef0f53c61eb86be0dfbd5e558bbb5b46dff60 Mon Sep 17 00:00:00 2001 From: yohan <783b8c87@scimetis.net> Date: Mon, 3 Jun 2024 01:46:50 +0200 Subject: [PATCH] Continue Flask migration. --- thermostat.py | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/thermostat.py b/thermostat.py index ae263ed..99ef01b 100644 --- a/thermostat.py +++ b/thermostat.py @@ -270,30 +270,27 @@ def thermostat_loop(): # dbconn.close() # break - xprint(new_forced_mode) if new_forced_mode is not None: with app.app_context(): data = Set_mode(value=new_forced_mode) - db.session.add(data) - db.session.commit() -# try: -# db.session.add(data) -# db.session.commit() -# return "OK", 201 -# except sqlalchemy.exc.IntegrityError as e: -# try: -# db.session.rollback() -# cur_mode = db.session.execute(db.select(Set_mode).filter_by(name="mode")).scalar_one() -# cur_mode = data -# db.session.commit() -# except Exception as e: -# db.session.rollback() -# logging.error(e) -# return "K0", 400 -# except Exception as e: -# db.session.rollback() -# logging.error(e) -# return "K0", 400 + try: + db.session.add(data) + db.session.commit() + return "OK", 201 + except sqlalchemy.exc.IntegrityError as e: + try: + db.session.rollback() + cur_mode = db.session.execute(db.select(Set_mode).filter_by(name="mode")).scalar_one() + cur_mode = data + db.session.commit() + except Exception as e: + db.session.rollback() + logging.error(e) + return "K0", 400 + except Exception as e: + db.session.rollback() + logging.error(e) + return "K0", 400 #cursor.execute("INSERT OR REPLACE INTO set_mode (value) VALUES ('"+new_forced_mode+"')") #dbconn.commit()