summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraltaf-creator <dev@altafcreator.com>2025-11-29 16:31:45 +0700
committeraltaf-creator <dev@altafcreator.com>2025-11-29 16:31:45 +0700
commit89fecb11b1cae5baa80e482256291487e036d437 (patch)
tree619d890b083c0012714d102ab815727bbca41f30
parentb107add5afb23a710e3297249e314eeee6bf0563 (diff)
fix backend ordering, some changes
-rw-r--r--.gitignore2
-rw-r--r--backend/__pycache__/main.cpython-314.pycbin11779 -> 13768 bytes
-rw-r--r--backend/db.dbbin0 -> 8192 bytes
-rw-r--r--backend/main.py58
-rw-r--r--frontend/main.js4
5 files changed, 53 insertions, 11 deletions
diff --git a/.gitignore b/.gitignore
index a867d6a..27a2b91 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1 @@
-./backend/serviceAccountKey.json
+./backend/db.db
diff --git a/backend/__pycache__/main.cpython-314.pyc b/backend/__pycache__/main.cpython-314.pyc
index 34bd8f7..803bbdc 100644
--- a/backend/__pycache__/main.cpython-314.pyc
+++ b/backend/__pycache__/main.cpython-314.pyc
Binary files differ
diff --git a/backend/db.db b/backend/db.db
new file mode 100644
index 0000000..097d291
--- /dev/null
+++ b/backend/db.db
Binary files differ
diff --git a/backend/main.py b/backend/main.py
index 7593f23..0042dba 100644
--- a/backend/main.py
+++ b/backend/main.py
@@ -72,24 +72,51 @@ machine_durations = [[None, None, None, None],
[None, None, None, None]]
+# this method checks for any entry, and starts the previously-terminated schedules
+# useful if you're restarting the server
+def restart_terminated_schedules():
+ cursor.execute("SELECT * FROM timers;")
+ out = cursor.fetchall()
+ print("unfinished timers: " + str(len(out)))
+
+ for row in out:
+ print(row)
+ end_date = datetime.datetime.fromisoformat(row[2]) + datetime.timedelta(minutes=row[3])
+ now = datetime.datetime.now()
+
+ if now > end_date:
+ print("unfinished timer was long gone")
+ scheduler.add_job(final_timer_finished, 'date', run_date=(now + datetime.timedelta(seconds=1)), id=str(row[0]), args=[row[0]])
+ elif now + datetime.timedelta(minutes=5) > end_date:
+ print("unfinished timer ends in less than five mins")
+ scheduler.add_job(final_timer_finished, 'date', run_date=end_date, id=str(row[0]), args=[row[0]])
+ else:
+ print("unfinished timer scheduler started")
+ print(row[0])
+ scheduler.add_job(reminder_timer_finished, 'date', run_date=end_date, id=str(row[0]), args=[row[0]])
+
+
def reminder_timer_finished(timer_id):
print("timer almost finished", timer_id)
end_date = datetime.datetime.now() + datetime.timedelta(seconds=5)
scheduler.add_job(final_timer_finished, 'date', run_date=end_date, id=timer_id, args=[timer_id])
cursor.execute(f"SELECT * FROM timers WHERE timer_id = '{timer_id}'")
out = cursor.fetchall()
+
for row in out:
- machine_status[row[3] - 1][row[4] - 1] = Status.FINISHED.name
+ machine_status[row[4] - 1][row[5] - 1] = Status.FINISHED.name
def final_timer_finished(timer_id):
print("timer finished!1", timer_id)
cursor.execute(f"SELECT * FROM timers WHERE timer_id = '{timer_id}'")
out = cursor.fetchall()
+
for row in out:
- machine_status[row[3] - 1][row[4] - 1] = Status.EMPTY.name
- machine_times[row[3] - 1][row[4] - 1] = None
- machine_durations[row[3] - 1][row[4] - 1] = None
+ 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
+
cursor.execute(f"DELETE FROM timers WHERE timer_id = {timer_id}")
conn.commit()
@@ -100,6 +127,14 @@ def create_session(response: fastapi.Response):
return cookie
+# ## beginning
+
+print("Hello, world!")
+restart_terminated_schedules()
+
+
+# ## api endpoints
+
# --- starting new timer
@app.post("/start", response_class=PlainTextResponse)
async def start_new_timer(data: RequestData, response: fastapi.Response, session_key: Annotated[str | None, fastapi.Cookie()] = None):
@@ -116,11 +151,12 @@ async def start_new_timer(data: RequestData, response: fastapi.Response, session
print("session key valid", session_key)
cursor.execute(f"""
INSERT INTO timers (user_id, start_time, duration, block, machine, status)
- VALUES ('{session_key}', '{now.isoformat()}', {data.block}, {data.machine}, {data.duration * 30}, 'RUNNING')
+ VALUES ('{session_key}', '{now.isoformat()}', {data.duration * 30}, {data.block}, {data.machine}, 'RUNNING')
""")
conn.commit()
cursor.execute("SELECT * FROM timers;")
out = cursor.fetchall()
+
for row in out:
print(row)
@@ -151,9 +187,14 @@ async def start_new_timer(data: RequestData, response: fastapi.Response, session
# --- check whether user has laundry or not
@app.post("/check", response_class=PlainTextResponse)
-def check_status(session_key: Annotated[str | None, fastapi.Cookie()] = None):
+def check_status(response: fastapi.Response, session_key: Annotated[str | None, fastapi.Cookie()] = None):
+ if not session_key:
+ print("no session key, creating.")
+ session_key = create_session(response)
+
cursor.execute(f"SELECT * FROM timers WHERE user_id = '{session_key}'")
out = cursor.fetchall()
+
for row in out:
print(row)
@@ -178,6 +219,7 @@ def get_laundry_info(session_key: Annotated[str | None, fastapi.Cookie()] = None
cursor.execute(f"SELECT * FROM timers WHERE user_id = '{session_key}'")
out = cursor.fetchall()
+
for row in out:
curr_timer = {
"duration": 0,
@@ -185,9 +227,9 @@ def get_laundry_info(session_key: Annotated[str | None, fastapi.Cookie()] = None
"machine": 0,
"status": "RUNNING",
}
- curr_timer["duration"] = row[5]
+ curr_timer["duration"] = row[3]
curr_timer["start_time"] = row[2]
- curr_timer["machine"] = row[4]
+ curr_timer["machine"] = row[5]
curr_timer["status"] = row[6]
result.append(curr_timer)
diff --git a/frontend/main.js b/frontend/main.js
index 4f47473..6f953ce 100644
--- a/frontend/main.js
+++ b/frontend/main.js
@@ -5,7 +5,7 @@ const data = {
machine: 2,
}
-const API_URL = "http://localhost:8000"
+const API_URL = "https://backend.laundryweb.altafcreator.com"
// --- permission request
const btn = document.getElementById("notbtn")
@@ -90,7 +90,7 @@ async function startUpdateMachines() {
updateMachines();
while (true) {
- await delay(60 * 1000);
+ await delay(30 * 1000);
await updateMachines();
}