unify register and main py worker

This commit is contained in:
Martin Quarda 2025-04-01 09:35:20 +02:00
parent 2e9c11d73b
commit 586bd3dc84
2 changed files with 27 additions and 25 deletions

View File

@ -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)

View File

@ -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)