diff options
| -rw-r--r-- | backend/main.py | 35 | ||||
| -rw-r--r-- | frontend/.DS_Store | bin | 6148 -> 6148 bytes | |||
| -rw-r--r-- | frontend/admin/admin.js | 33 | ||||
| -rw-r--r-- | frontend/admin/panel.html | 6 |
4 files changed, 65 insertions, 9 deletions
diff --git a/backend/main.py b/backend/main.py index 6d47fe3..e242389 100644 --- a/backend/main.py +++ b/backend/main.py @@ -492,6 +492,13 @@ def delete_cookie_scheduler(cookie): pass +def authenticate_admin_check(cookie): + cursor.execute("SELECT * FROM admin_cookies WHERE cookie = ?", (cookie,)) + rows = cursor.fetchall() + + return len(rows) > 0 + + # --- admin login @app.post("/admin_login", response_class=PlainTextResponse) def admin_login(data: PlaintextPasswordData, response: fastapi.Response): @@ -525,10 +532,8 @@ def admin_login(data: PlaintextPasswordData, response: fastapi.Response): @app.post("/admin_check", response_class=PlainTextResponse) def admin_check(response: fastapi.Response, admin_auth: Annotated[str | None, fastapi.Cookie()] = None): print("admin check request, ", admin_auth) - cursor.execute("SELECT * FROM admin_cookies WHERE cookie = ?", (admin_auth,)) - rows = cursor.fetchall() - if len(rows) > 0: + if authenticate_admin_check(admin_auth): response.status_code = fastapi.status.HTTP_202_ACCEPTED return "Authorised." else: @@ -543,10 +548,7 @@ def override_status(data: OverrideMachineData, response: fastapi.Response, admin response.status_code = fastapi.status.HTTP_401_UNAUTHORIZED return "Unauthorised." - cursor.execute("SELECT * FROM admin_cookies WHERE cookie = ?", (admin_auth,)) - rows = cursor.fetchall() - - if len(rows) > 0: + if authenticate_admin_check(admin_auth): if (data.disabled): machine_status[data.block - 1][data.machine_id - 1] = Status.OUTOFSERVICE.name else: @@ -574,4 +576,21 @@ def admin_change_password(data: PlaintextPasswordData, response: fastapi.Respons response.status_code = fastapi.status.HTTP_401_UNAUTHORIZED return "Unauthorised." - pass + if authenticate_admin_check(admin_auth): + pass + else: + pass + + +# --- get all blocks machine status for admin +@app.post("/admin_machine_status") +def admin_machine_status(response: fastapi.Response, admin_auth: Annotated[str | None, fastapi.Cookie()] = None): + if not admin_auth: + response.status_code = fastapi.status.HTTP_401_UNAUTHORIZED + return """{"reply": "Unauthorised."}""" + + if authenticate_admin_check(admin_auth): + return machine_status + else: + response.status_code = fastapi.status.HTTP_403_FORBIDDEN + return """{"reply": "Forbidden."}""" diff --git a/frontend/.DS_Store b/frontend/.DS_Store Binary files differindex 6e268de..ac6b678 100644 --- a/frontend/.DS_Store +++ b/frontend/.DS_Store diff --git a/frontend/admin/admin.js b/frontend/admin/admin.js index 6f1ebce..b78b1b3 100644 --- a/frontend/admin/admin.js +++ b/frontend/admin/admin.js @@ -38,9 +38,42 @@ async function panelLoginCheck() { if (await checkLoginStatus()) { msg.style.display = "none"; authDiv.style.display = "inherit"; + return true; } else { msg.style.display = "inherit"; authDiv.style.display = "none"; + return false; + } +} + +async function syncMachineStatus() { + const response = await fetch(`${API_URL}/admin_machine_status`, { + method: "POST", + credentials: "include", + }); + const data = await response.json(); + + for (let b = 1; b <= data.length; b++) { + for (let m = 1; m <= data[b].length; m++) { + const img = document.getElementById("h"+b.toString()+"m"+m.toString()+"img"); + const dropdown = document.getElementById("h"+b.toString()+"m"+m.toString()); + + if (data[b][m] != "OUTOFSERVICE") { + if (m % 2 == 0) { + img.src = "/assets/img/washer_down.png"; + } else { + img.src = "/assets/img/dryer_down.png"; + } + dropdown.selectedIndex = 1; + } else { + if (m % 2 == 0) { + img.src = "/assets/img/washer_off.png"; + } else { + img.src = "/assets/img/dryer_off.png"; + } + dropdown.selectedIndex = 0; + } + } } } diff --git a/frontend/admin/panel.html b/frontend/admin/panel.html index 1169c30..1b0f2cf 100644 --- a/frontend/admin/panel.html +++ b/frontend/admin/panel.html @@ -77,7 +77,11 @@ </div> <script src="admin.js"></script> <script> - panelLoginCheck(); + (async () => { + if (await panelLoginCheck()) { + syncMachineStatus(); + } + })(); </script> </body> </html> |
