diff options
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/main.py | 63 |
1 files changed, 38 insertions, 25 deletions
diff --git a/backend/main.py b/backend/main.py index 1f82d08..720a422 100644 --- a/backend/main.py +++ b/backend/main.py @@ -49,7 +49,7 @@ CREATE TABLE IF NOT EXISTS timers ( timer_id INTEGER PRIMARY KEY, user_id VARCHAR(64) NOT NULL, start_time TEXT NOT NULL, - duration INT NOT NULL, + end_time TEXT NOT NULL, block INT NOT NULL, machine INT NOT NULL, status TEXT NOT NULL CHECK(status IN ('RUNNING', 'FINISHED')), @@ -57,6 +57,17 @@ CREATE TABLE IF NOT EXISTS timers ( );""") # block is either 1 or 2, machine (1-4), odd is dryer, even is machine. +class RowIndices(Enum): + TIMER_ID = 0, + USER_ID = 1, + START_TIME = 2, + END_TIME = 3, + BLOCK = 4, + MACHINE = 5, + STATUS = 6, + SUBSCRIPTION_ID = 7 + + # ## yaml configuration initialisation qr_uri = {} @@ -136,8 +147,8 @@ machine_status = [[Status.EMPTY.name, Status.EMPTY.name, Status.EMPTY.name, Stat [Status.EMPTY.name, Status.EMPTY.name, Status.EMPTY.name, Status.EMPTY.name]] machine_times = [[None, None, None, None], [None, None, None, None]] -machine_durations = [[None, None, None, None], - [None, None, None, None]] +machine_endings = [[None, None, None, None], + [None, None, None, None]] # ## some non-api endpoint method definitions @@ -166,17 +177,19 @@ def restart_terminated_schedules(): scheduler.add_job(reminder_timer_finished, 'date', run_date=end_date, id=str(row[0]), args=[row[0]]) print("setting internal array information") - machine_status[row[4] - 1][row[5] - 1] = Status.RUNNING.name - machine_times[row[4] - 1][row[5] - 1] = row[2] - machine_durations[row[4] - 1][row[5] - 1] = row[3] + machine_status[row[RowIndices.BLOCK] - 1][row[RowIndices.MACHINE] - 1] = Status.RUNNING.name + machine_times[row[RowIndices.BLOCK] - 1][row[RowIndices.MACHINE] - 1] = row[2] + machine_endings[row[RowIndices.BLOCK] - 1][row[RowIndices.MACHINE] - 1] = row[3] - print(machine_status, machine_times, machine_durations) + print(machine_status, machine_times, machine_endings) def reminder_timer_finished(timer_id): print("timer almost finished", timer_id) - end_date = datetime.datetime.now() + datetime.timedelta(minutes=5) - scheduler.add_job(final_timer_finished, 'date', run_date=end_date, id=str(timer_id), args=[timer_id]) + cursor.execute(f"SELECT * FROM timers WHERE timer_id = '{timer_id}'") + out = cursor.fetchall() + + scheduler.add_job(final_timer_finished, 'date', run_date=out[RowIndices.END_TIME], id=str(timer_id), args=[timer_id]) notification = Notification(app_id=ONESIGNAL_APP_ID, included_segments=['All'], contents={'en': 'get ready to get your bloody laundry'}, headings={'en': 'laundry almost finished'}) @@ -199,7 +212,7 @@ def final_timer_finished(timer_id): print(e) for row in out: - machine_status[row[4] - 1][row[5] - 1] = Status.FINISHED.name + machine_status[row[RowIndices.BLOCK] - 1][row[RowIndices.MACHINE] - 1] = Status.FINISHED.name def create_session(response: fastapi.Response): @@ -260,9 +273,10 @@ def start_new_timer(data: RequestData, response: fastapi.Response, session_key: try: print("session key valid", session_key) + end_date = now + datetime.timedelta(minutes=(data.duration * 30)) cursor.execute(f""" - INSERT INTO timers (user_id, start_time, duration, block, machine, status) - VALUES ('{session_key}', '{now.isoformat()}', {data.duration * 30}, {block}, {machine}, 'RUNNING') + INSERT INTO timers (user_id, start_time, end_time, block, machine, status) + VALUES ('{session_key}', '{now.isoformat()}', {end_date}, {block}, {machine}, 'RUNNING') """) conn.commit() cursor.execute("SELECT * FROM timers;") @@ -271,7 +285,6 @@ def start_new_timer(data: RequestData, response: fastapi.Response, session_key: for row in out: print(row) - end_date = now + datetime.timedelta(minutes=(data.duration * 30) - 5) timer_id = str(out[len(out) - 1][0]) print("timer id", timer_id) @@ -281,7 +294,7 @@ def start_new_timer(data: RequestData, response: fastapi.Response, session_key: return "something went wrong during sql stuff" try: - scheduler.add_job(reminder_timer_finished, 'date', run_date=end_date, id=timer_id, args=[timer_id]) + scheduler.add_job(reminder_timer_finished, 'date', run_date=end_date - datetime.timedelta(minutes=5), id=timer_id, args=[timer_id]) except Exception as exception: print("err @ scheduler //", exception) response.status_code = fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR @@ -290,7 +303,7 @@ def start_new_timer(data: RequestData, response: fastapi.Response, session_key: try: machine_status[block - 1][machine - 1] = Status.RUNNING.name machine_times[block - 1][machine - 1] = now.isoformat() - machine_durations[block - 1][machine - 1] = data.duration * 30 + machine_endings[block - 1][machine - 1] = end_date except Exception as exception: print("err @ machine_status //", exception) response.status_code = fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR @@ -324,7 +337,7 @@ def check_status(response: fastapi.Response, session_key: Annotated[str | None, def get_machine_status(response: fastapi.Response, auth_block: Annotated[str | None, fastapi.Cookie()] = None): if auth_block: block = int(auth_block) - 1 - return [machine_status[block], machine_times[block], machine_durations[block]] + return [machine_status[block], machine_times[block], machine_endings[block]] else: response.status_code = fastapi.status.HTTP_401_UNAUTHORIZED return "block cookie needed. unauthorised" @@ -341,17 +354,17 @@ def get_laundry_info(response: fastapi.Response, session_key: Annotated[str | No for row in out: curr_timer = { - "duration": 0, + "end_time": 0, "start_time": 0, "machine": 0, "status": "RUNNING", "id": -1, } - curr_timer["duration"] = row[3] - curr_timer["start_time"] = row[2] - curr_timer["machine"] = row[5] - curr_timer["status"] = row[6] - curr_timer["id"] = row[0] + curr_timer["end_time"] = row[RowIndices.END_TIME] + curr_timer["start_time"] = row[RowIndices.START_TIME] + curr_timer["machine"] = row[RowIndices.MACHINE] + curr_timer["status"] = row[RowIndices.STATUS] + curr_timer["id"] = row[RowIndices.TIMER_ID] result.append(curr_timer) @@ -368,9 +381,9 @@ def finish_laundry(data: FinishRequestData, response: fastapi.Response, session_ cursor.execute(f"SELECT * FROM timers WHERE timer_id = '{data.id}'") row = cursor.fetchall()[0] - machine_status[row[4] - 1][row[5] - 1] = Status.EMPTY.name - machine_times[row[4] - 1][row[5] - 1] = None - machine_durations[row[4] - 1][row[5] - 1] = None + machine_status[row[RowIndices.BLOCK] - 1][row[RowIndices.MACHINE] - 1] = Status.EMPTY.name + machine_times[row[RowIndices.BLOCK] - 1][row[RowIndices.MACHINE] - 1] = None + machine_endings[row[RowIndices.BLOCK] - 1][row[RowIndices.MACHINE] - 1] = None cursor.execute(f"DELETE FROM timers WHERE timer_id = {row[0]}") conn.commit() |
