From 9deda461ee40ad8993b0b179876c66d314aac1ba Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Thu, 27 Mar 2025 17:56:04 +0100 Subject: [PATCH] added error handling in registrace.py --- SW/PC/Stopwatch/config.json | 2 +- SW/PC/Stopwatch/main.py | 10 ++++++++++ SW/PC/Stopwatch/registrace.py | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/SW/PC/Stopwatch/config.json b/SW/PC/Stopwatch/config.json index 59673ed..d05fbd3 100644 --- a/SW/PC/Stopwatch/config.json +++ b/SW/PC/Stopwatch/config.json @@ -1,6 +1,6 @@ { "station_id": "1", - "host": "https://beta.quarda.cz", + "host": "https://beta.alkator.cz", "login": "station_register@alkator.cz", "password": "password_heslo" } \ No newline at end of file diff --git a/SW/PC/Stopwatch/main.py b/SW/PC/Stopwatch/main.py index 986e951..01145db 100644 --- a/SW/PC/Stopwatch/main.py +++ b/SW/PC/Stopwatch/main.py @@ -10,6 +10,7 @@ import json gi.require_version('Gtk', '4.0') from gi.repository import Gtk, Gio, Gdk +from requests.adapters import HTTPAdapter, Retry activeCards = [] @@ -28,6 +29,15 @@ with open('config.json', 'r') as f: config = json.load(f) session = requests.Session() + + +retries = Retry(total=10, + backoff_factor=1, + status_forcelist=[ 500, 502, 503, 504 ]) + +session.mount('https://', HTTPAdapter(max_retries=retries)) + + session.post(config['host'] + '/api/login', {'email':config['login'], 'password': config['password']}) def refreshDb(): diff --git a/SW/PC/Stopwatch/registrace.py b/SW/PC/Stopwatch/registrace.py index ef8dcc1..b758318 100644 --- a/SW/PC/Stopwatch/registrace.py +++ b/SW/PC/Stopwatch/registrace.py @@ -6,6 +6,8 @@ import usb.core import usb.util from PyQt6 import QtWidgets, uic, QtGui +from requests.adapters import HTTPAdapter, Retry + app = QtWidgets.QApplication(sys.argv) TIME = 600 @@ -22,6 +24,13 @@ session = requests.Session() session.post(config['host'] + '/api/login', {'email':config['login'], 'password': config['password']}) +retries = Retry(total=3, + backoff_factor=1, + status_forcelist=[ 500, 502, 503, 504 ],) + +session.mount('https://', HTTPAdapter(max_retries=retries)) + + last_card = 0 last_time = datetime.datetime.now() @@ -41,13 +50,21 @@ def register_racer(): raise response = session.post(config['host'] + '/api/card/register', json={'racer_id': racer_id, 'starting_number': starting_number, 'card_id': card_id}) + if response.status_code == 400: + mb = QtWidgets.QMessageBox(text="Zaregistrování selhalo! Je špatně vyplněné startovní číslo nebo karta už je přiřazená k jinému závodníku.") + mb.exec() + return if response.status_code != 200: - raise Exception(response.status_code, response.content) + mb = QtWidgets.QMessageBox(text="Neznámá chyba!") + mb.exec() + return racer = list(filter(lambda x: x['racer_id'] == racer_id, racers))[0] racer['card_id'] = card_id racer['starting_number'] = starting_number updateRacers() + mb = QtWidgets.QMessageBox(text="Úspěšné zaregistrování závodníka!") + mb.exec() def unregister_racer(): @@ -55,7 +72,10 @@ def unregister_racer(): time = last_time response = session.post(config['host'] + '/api/station/register', json={'card_id': card_id, 'time': time.strftime('%d.%m.%Y %H:%M:%S.%f'), 'station_id': config['station_id']}) - assert response.status_code == 200 + if response != 200: + mb = QtWidgets.QMessageBox(text="Selhalo zapsání na Stanici!") + mb.exec() + return racer = list(filter(lambda x: x['card_id'] == card_id, racers))[0] starting_number = racer['starting_number'] @@ -64,7 +84,16 @@ def unregister_racer(): response = session.post(config['host'] + '/api/card/unregister', json={'racer_id': racer_id, 'starting_number': starting_number, 'card_id': card_id}) - assert response.status_code == 200 + if response.status_code != 200: + mb = QtWidgets.QMessageBox(text="Registrace na stanici proběhla úspěšně, ale karta nebyla odhlášena!") + mb.exec() + return + + json = response.json() + + mb = QtWidgets.QMessageBox(text=f"Úspěšné odhlášení závodníka! Finální čas je: {json['time']}") + mb.exec() + window = uic.loadUi("registrace.ui")