From a4eefa18b7858b54c5159ab3db7a2cbd2ca95a90 Mon Sep 17 00:00:00 2001 From: yohan <783b8c87@scimetis.net> Date: Sun, 2 Jun 2024 22:52:39 +0200 Subject: [PATCH] Continue Flask migration. --- thermostat.py | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/thermostat.py b/thermostat.py index cc5c879..9f46b6b 100644 --- a/thermostat.py +++ b/thermostat.py @@ -179,7 +179,7 @@ class Set_mode(db.Model): Set_mode_resource_fields = { 'name': fields.String(description='mode type'), - 'value': fields.String(required=True, description='value'), + 'value': fields.String(required=True, description='value', enum=targets+modes), 'time': fields.DateTime(dt_format='iso8601'), } @@ -195,24 +195,27 @@ class Set_mode_thermostat(Resource): except Exception as e: 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 + new_forced_mode = data.value + return "OK", 201 + +# 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 @ns_thermostat.route('/status') class Status_thermostat(Resource): @@ -238,14 +241,15 @@ migrate = Migrate(app, db, compare_type=True) target_name = default_target forced_mode = None current_time = time.time() +new_forced_mode = None def thermostat_loop(): start_time = time.time() last_control_time = None - new_forced_mode = None first_loop = True global target_name global forced_mode + global new_forced_mode global current_time while True: