From 586bd3dc84114c82a669fe9a49f5fe90b814d6ef Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Tue, 1 Apr 2025 09:35:20 +0200 Subject: [PATCH] unify register and main py worker --- SW/PC/Stopwatch/main.py | 38 ++++++++++++++++++----------------- SW/PC/Stopwatch/registrace.py | 14 ++++++------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/SW/PC/Stopwatch/main.py b/SW/PC/Stopwatch/main.py index 37be99a..e8697b8 100644 --- a/SW/PC/Stopwatch/main.py +++ b/SW/PC/Stopwatch/main.py @@ -9,6 +9,7 @@ import requests import json gi.require_version('Gtk', '4.0') +from queue import Queue from gi.repository import Gtk, Gio, Gdk from requests.adapters import HTTPAdapter, Retry from usbcardreader import UsbCardReader @@ -63,26 +64,27 @@ def refreshDbEvery3mins(): threading.Thread(target=refreshDbEvery3mins).start() +log = [] -def sendQueueToSend(): - global queue_to_send + +with open('log.json', 'r') as f: + log = json.loads(f.read()) + + +queue = Queue() + + +def worker(): while True: - if len(queue_to_send) > 0: - to_send = queue_to_send.pop() - - response = session.post(config['host'] + '/api/station/register', - json=to_send - ) - if response.status_code != 200: - if response.status_code == 400: - continue - queue_to_send.append(to_send) #try again later - time.sleep(1) - else: - time.sleep(1) + item = queue.get() + log.append(item) + with open('log.json', 'w') as f: + f.write(json.dumps(log)) + response = session.post(item['url'], json=item['json']) + print(item, response) -threading.Thread(target=sendQueueToSend).start() +threading.Thread(target=worker).start() def callbackOnCard(card_id): @@ -131,11 +133,11 @@ class GridRow(): countdown=threading.Thread(target=self.updateTime) countdown.start() - queue_to_send.append({ + queue.put({'url': config['host'] + '/api/station/register', 'json': { 'card_id': racer['card_id'], 'station_id': config['station_id'], 'time': datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S.%f') - }) + }}) def printIndex(self, button): print(self.index) diff --git a/SW/PC/Stopwatch/registrace.py b/SW/PC/Stopwatch/registrace.py index c91509b..c7f386c 100644 --- a/SW/PC/Stopwatch/registrace.py +++ b/SW/PC/Stopwatch/registrace.py @@ -21,7 +21,7 @@ session = requests.Session() session.post(config['host'] + '/api/login', {'email':config['login'], 'password': config['password']}) -retries = Retry(total=3, +retries = Retry(total=10, backoff_factor=1, status_forcelist=[ 500, 502, 503, 504 ],) @@ -36,11 +36,11 @@ response = session.get(config['host'] + '/api/racers') racers = response.json() -db = [] +log = [] -with open('db.json', 'r') as f: - db = json.loads(f.read()) +with open('log.json', 'r') as f: + log = json.loads(f.read()) queue = Queue() @@ -49,9 +49,9 @@ queue = Queue() def worker(): while True: item = queue.get() - db.append(item) - with open('db.json', 'w') as f: - f.write(json.dumps(db)) + log.append(item) + with open('log.json', 'w') as f: + f.write(json.dumps(log)) response = session.post(item['url'], json=item['json']) print(item, response)