Compare commits
18 Commits
5653972b8d
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80653dfe11 | ||
|
|
b737934f05 | ||
|
|
9227a8bfdb | ||
|
|
5a34e9e4e5 | ||
|
|
ef915110a7 | ||
|
|
19ead426fb | ||
|
|
2c9ea8e379 | ||
|
|
8104096c0b | ||
|
|
c4d7df8b5d | ||
|
|
03d8045990 | ||
|
|
c59b24a561 | ||
|
|
586bd3dc84 | ||
|
|
2e9c11d73b | ||
|
|
ca8d008e77 | ||
|
|
bc61d8a0bd | ||
|
|
2221f5b39d | ||
|
|
9deda461ee | ||
|
|
52d81c127d |
@@ -1,10 +1,7 @@
|
||||
{
|
||||
"url": "https://beta.alkator.cz/api/station/register",
|
||||
"station_id": "1",
|
||||
"login_url": "https://beta.alkator.cz/api/login",
|
||||
"racers_url": "https://beta.alkator.cz/api/racers",
|
||||
"card_register": "https://beta.alkator.cz/api/card/register",
|
||||
"card_unregister": "https://beta.alkator.cz/api/card/card_unregister",
|
||||
"station_id": 2,
|
||||
"host": "https://beta.alkator.cz",
|
||||
"login": "station_register@alkator.cz",
|
||||
"password": "password_heslo"
|
||||
"password": "password_heslo",
|
||||
"countdown_seconds": 60
|
||||
}
|
||||
1
SW/PC/Stopwatch/log.json
Normal file
1
SW/PC/Stopwatch/log.json
Normal file
@@ -0,0 +1 @@
|
||||
[]
|
||||
@@ -2,37 +2,44 @@ import gi
|
||||
import threading
|
||||
import time
|
||||
import datetime
|
||||
import collections
|
||||
import usb.core
|
||||
import usb.util
|
||||
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
|
||||
|
||||
|
||||
with open('config.json', 'r') as f:
|
||||
config = json.load(f)
|
||||
|
||||
|
||||
activeCards = []
|
||||
|
||||
TIME = 600
|
||||
|
||||
USB_IF = 0 # Interface
|
||||
USB_TIMEOUT = 5 # Timeout in ms
|
||||
USB_VENDOR = 0xffff # Vendor-ID:
|
||||
USB_PRODUCT = 0x0035 # Product-ID
|
||||
TIME = config['countdown_seconds']
|
||||
|
||||
db = {}
|
||||
|
||||
queue_to_send = []
|
||||
|
||||
with open('config.json', 'r') as f:
|
||||
config = json.load(f)
|
||||
|
||||
session = requests.Session()
|
||||
session.post(config['login_url'], {'email':config['login'], 'password': config['password']})
|
||||
|
||||
|
||||
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():
|
||||
global db
|
||||
resp = session.get(config['racers_url'])
|
||||
resp = session.get(config['host'] + '/api/racers')
|
||||
if resp.status_code == 200:
|
||||
json = resp.json()
|
||||
db = {}
|
||||
@@ -47,82 +54,46 @@ def refreshDbEvery3mins():
|
||||
time.sleep(3 * 60)
|
||||
|
||||
|
||||
Thread(target=refreshDbEvery3mins).start()
|
||||
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()
|
||||
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)
|
||||
|
||||
response = session.post(config['url'],
|
||||
to_send, 'application/json'
|
||||
)
|
||||
if response.status_code != 200:
|
||||
if response.status_code == 400:
|
||||
continue
|
||||
queue_to_send.append(to_send) #try again later
|
||||
time.sleep(1)
|
||||
|
||||
threading.Thread(target=worker).start()
|
||||
|
||||
|
||||
def callbackOnCard(card_id):
|
||||
global win, activeCards, db
|
||||
if card_id not in db:
|
||||
refreshDb()
|
||||
if card_id in db:
|
||||
if card_id not in activeCards:
|
||||
activeCards.append(card_id)
|
||||
win.arb(db[card_id])
|
||||
print('Card is active now')
|
||||
else:
|
||||
time.sleep(1)
|
||||
print('Card is already active')
|
||||
else:
|
||||
print(f'Card {card_id} not found in db')
|
||||
|
||||
|
||||
Thread(target=sendQueueToSend).start()
|
||||
|
||||
|
||||
class UsbCardReader():
|
||||
def __init__(self, **kargs):
|
||||
# Find the HID device by vendor/product ID
|
||||
self.dev = usb.core.find(idVendor=USB_VENDOR, idProduct=USB_PRODUCT)
|
||||
# Get and store the endpoint
|
||||
self.endpoint = self.dev[0][(0,0)][0]
|
||||
if self.dev.is_kernel_driver_active(USB_IF) is True:
|
||||
self.dev.detach_kernel_driver(USB_IF)
|
||||
# Claim the device
|
||||
usb.util.claim_interface(self.dev, USB_IF)
|
||||
self.receivedNumber = 0
|
||||
|
||||
cardread=threading.Thread(target=self.read)
|
||||
cardread.start()
|
||||
|
||||
def read(self):
|
||||
global win, activeCards, db
|
||||
|
||||
print("Waiting for card")
|
||||
receivedNumber = 0
|
||||
|
||||
while True:
|
||||
control = None
|
||||
try:
|
||||
control = self.dev.read(self.endpoint.bEndpointAddress, self.endpoint.wMaxPacketSize, USB_TIMEOUT)
|
||||
if (control[2] != 40) & (control[2] != 0):
|
||||
receivedDigit = control[2] - 29
|
||||
if receivedDigit == 10:
|
||||
receivedDigit = 0
|
||||
receivedNumber = 10 * receivedNumber + receivedDigit
|
||||
|
||||
if (( control[0] == 0 )) & (( control[2] == 40 )) & (( not receivedNumber == 0 )):
|
||||
if receivedNumber not in db:
|
||||
refreshDb()
|
||||
if receivedNumber in db:
|
||||
if receivedNumber not in activeCards:
|
||||
activeCards.append(receivedNumber)
|
||||
win.arb(db[receivedNumber])
|
||||
print('Card is active now')
|
||||
else:
|
||||
print('Card is already active')
|
||||
else:
|
||||
print(f'Card {receivedNumber} not found in db')
|
||||
receivedNumber = 0
|
||||
|
||||
except KeyboardInterrupt:
|
||||
exit()
|
||||
except:
|
||||
pass
|
||||
|
||||
time.sleep(0.001)
|
||||
|
||||
class GridRow():
|
||||
def __init__(self, parent, racer, index):
|
||||
global queue_to_send
|
||||
@@ -151,11 +122,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)
|
||||
@@ -227,7 +198,7 @@ def on_activate(app):
|
||||
win = GridWindow(application=app)
|
||||
win.present()
|
||||
|
||||
UsbCardReader()
|
||||
UsbCardReader(callbackOnCard)
|
||||
app = Gtk.Application(application_id='com.example.App')
|
||||
app.connect('activate', on_activate)
|
||||
|
||||
|
||||
@@ -1,72 +1,112 @@
|
||||
import sys
|
||||
import datetime
|
||||
import requests
|
||||
import threading
|
||||
import json
|
||||
import usb.core
|
||||
import usb.util
|
||||
from PyQt6 import QtWidgets, uic, QtGui
|
||||
from PyQt6 import QtWidgets, uic, QtGui, QtCore
|
||||
from PyQt6.QtCore import pyqtSignal as Signal, pyqtSlot as Slot
|
||||
from queue import Queue
|
||||
|
||||
from requests.adapters import HTTPAdapter, Retry
|
||||
from usbcardreader import UsbCardReader
|
||||
|
||||
app = QtWidgets.QApplication(sys.argv)
|
||||
|
||||
TIME = 600
|
||||
|
||||
USB_IF = 0 # Interface
|
||||
USB_TIMEOUT = 5 # Timeout in ms
|
||||
USB_VENDOR = 0xffff # Vendor-ID:
|
||||
USB_PRODUCT = 0x0035 # Product-ID
|
||||
|
||||
with open('config.json', 'r') as f:
|
||||
config = json.load(f)
|
||||
|
||||
session = requests.Session()
|
||||
session.post(config['login_url'], {'email':config['login'], 'password': config['password']})
|
||||
session.post(config['host'] + '/api/login', {'email':config['login'], 'password': config['password']})
|
||||
|
||||
|
||||
last_card = 1111
|
||||
retries = Retry(total=10,
|
||||
backoff_factor=1,
|
||||
status_forcelist=[ 500, 502, 503, 504 ],)
|
||||
|
||||
session.mount('https://', HTTPAdapter(max_retries=retries))
|
||||
|
||||
|
||||
last_card = 0
|
||||
last_time = datetime.datetime.now()
|
||||
|
||||
racers = []
|
||||
|
||||
response = session.get(config['racers_url'])
|
||||
log = []
|
||||
|
||||
racers = response.json()
|
||||
|
||||
with open('log.json', 'r') as f:
|
||||
log = json.loads(f.read())
|
||||
|
||||
|
||||
queue = Queue()
|
||||
|
||||
|
||||
class Worker(QtCore.QThread):
|
||||
returned_state = Signal(str)
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.returned_state.connect(self.handle_returned_state)
|
||||
self.worker_thread = threading.Thread(target=self.worker)
|
||||
self.worker_thread.start()
|
||||
|
||||
def worker(self):
|
||||
while True:
|
||||
item = queue.get()
|
||||
log.append({
|
||||
'url': item['url'],
|
||||
'json': item['json'],
|
||||
})
|
||||
with open('log.json', 'w') as f:
|
||||
f.write(json.dumps(log))
|
||||
response = session.post(item['url'], json=item['json'])
|
||||
if response.status_code == 200:
|
||||
if item.get('on_success'):
|
||||
item['on_success']()
|
||||
if item.get('message'):
|
||||
self.returned_state.emit(f'{item['message']}')
|
||||
else:
|
||||
if item.get('failed'):
|
||||
self.returned_state.emit(f'{item['failed']}')
|
||||
|
||||
print(item, response)
|
||||
def handle_returned_state(self, state):
|
||||
mb = QtWidgets.QMessageBox(text=f"{state}")
|
||||
mb.exec()
|
||||
|
||||
|
||||
worker = Worker()
|
||||
|
||||
|
||||
def register_racer():
|
||||
card_id = last_card
|
||||
index = window.racers.currentIndex()
|
||||
if not index.data():
|
||||
mb = QtWidgets.QMessageBox(text='Prosím vyberte závodníka!')
|
||||
mb.exec()
|
||||
return
|
||||
racer_id = int(index.data().split(' ')[-1])
|
||||
try:
|
||||
starting_number = int(window.starting_number.text())
|
||||
except Exception:
|
||||
raise
|
||||
|
||||
response = session.post(config['card_register'], json={'racer_id': racer_id, 'starting_number': starting_number, 'card_id': last_card})
|
||||
if response.status_code != 200:
|
||||
raise Exception(response.status_code, response.content)
|
||||
|
||||
starting_number = max(filter(lambda r: r['starting_number'], racers), key=lambda racer: racer['starting_number'])['starting_number'] + 1
|
||||
except ValueError:
|
||||
starting_number = 1
|
||||
racer = list(filter(lambda x: x['racer_id'] == racer_id, racers))[0]
|
||||
racer['card_id'] = card_id
|
||||
racer['starting_number'] = starting_number
|
||||
updateRacers()
|
||||
|
||||
def register_update_racer():
|
||||
racer['card_id'] = card_id
|
||||
racer['starting_number'] = starting_number
|
||||
racer['started'] = False
|
||||
updateRacers()
|
||||
|
||||
def unregister_racer():
|
||||
card_id = last_card
|
||||
time = last_time
|
||||
|
||||
response = session.post(config['url'], json={'card_id': card_id, 'time': time.strftime('%d.%m.%Y %H:%M:%S.%f'), 'station_id': config['station_id']})
|
||||
|
||||
|
||||
|
||||
|
||||
window = uic.loadUi("registrace.ui")
|
||||
window.register_racer.clicked.connect(register_racer)
|
||||
window.unregister_racer.clicked.connect(unregister_racer)
|
||||
window.show()
|
||||
queue.put({
|
||||
'url': config['host'] + '/api/card/register',
|
||||
'json': {'racer_id': racer_id, 'starting_number': starting_number, 'card_id': card_id, 'time': datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S.%f')},
|
||||
'message': f"Úspěšné zaregistrování závodníka {racer['first_name']} {racer['last_name']} se startovním číslem {starting_number}!",
|
||||
'failed': f"Neúspěšná registrace závodníka {racer['first_name']} {racer['last_name']}",
|
||||
'on_success': register_update_racer,
|
||||
})
|
||||
|
||||
|
||||
model = QtGui.QStandardItemModel()
|
||||
window.racers.setModel(model)
|
||||
|
||||
def updateRacers():
|
||||
model.clear()
|
||||
@@ -80,55 +120,65 @@ def updateRacers():
|
||||
updateRacers()
|
||||
|
||||
|
||||
def reload_racers():
|
||||
global racers
|
||||
response = session.get(config['host'] + '/api/racers')
|
||||
|
||||
racers = response.json()
|
||||
updateRacers()
|
||||
|
||||
|
||||
reload_racers()
|
||||
|
||||
|
||||
def findByCard(card_id):
|
||||
for racer in racers:
|
||||
if racer['card_id'] == card_id:
|
||||
return racer
|
||||
|
||||
|
||||
def updateLastCard(card_id):
|
||||
global last_card, last_time
|
||||
if card_id == last_card and datetime.datetime.now() - last_time < datetime.timedelta(seconds=15):
|
||||
return
|
||||
time = last_time = datetime.datetime.now()
|
||||
last_card = card_id
|
||||
window.lastCard.setText(str(card_id))
|
||||
|
||||
racer = findByCard(card_id)
|
||||
if racer:
|
||||
if racer['started']:
|
||||
def finish_update_racer():
|
||||
racer['card_id'] = None
|
||||
queue.put({
|
||||
'url': config['host'] + '/api/card/unregister',
|
||||
'json': {'racer_id': racer['racer_id'], 'starting_number': racer['starting_number'], 'card_id': card_id, 'time': time.strftime('%d.%m.%Y %H:%M:%S.%f')},
|
||||
'message': f"Úspěšné odhlášení závodníka {racer['starting_number']}!",
|
||||
'failed': f"Neúspěšné odhlášení karty závodníka {racer['starting_number']}",
|
||||
'on_success': finish_update_racer,
|
||||
})
|
||||
else:
|
||||
def start_update_racer():
|
||||
racer['started'] = True
|
||||
queue.put({
|
||||
'url': config['host'] + '/api/station/register',
|
||||
'json': {'card_id': card_id, 'time': time.strftime('%d.%m.%Y %H:%M:%S.%f'), 'station_id': 1},
|
||||
'message': f"Úspěšné odstartování závodníka {racer['starting_number']}!",
|
||||
'failed': f"Núspěšné odstartování závodníka {racer['starting_number']}, prosím registraci opakujte!",
|
||||
'on_success': start_update_racer,
|
||||
})
|
||||
|
||||
class UsbCardReader():
|
||||
def __init__(self, **kargs):
|
||||
# Find the HID device by vendor/product ID
|
||||
self.dev = usb.core.find(idVendor=USB_VENDOR, idProduct=USB_PRODUCT)
|
||||
# Get and store the endpoint
|
||||
self.endpoint = self.dev[0][(0,0)][0]
|
||||
if self.dev.is_kernel_driver_active(USB_IF) is True:
|
||||
self.dev.detach_kernel_driver(USB_IF)
|
||||
# Claim the device
|
||||
usb.util.claim_interface(self.dev, USB_IF)
|
||||
self.receivedNumber = 0
|
||||
|
||||
cardread=threading.Thread(target=self.read)
|
||||
cardread.start()
|
||||
UsbCardReader(updateLastCard)
|
||||
|
||||
def read(self):
|
||||
global last_card, last_time
|
||||
|
||||
print("Waiting for card")
|
||||
receivedNumber = 0
|
||||
|
||||
while True:
|
||||
control = None
|
||||
try:
|
||||
control = self.dev.read(self.endpoint.bEndpointAddress, self.endpoint.wMaxPacketSize, USB_TIMEOUT)
|
||||
if (control[2] != 40) & (control[2] != 0):
|
||||
receivedDigit = control[2] - 29
|
||||
if receivedDigit == 10:
|
||||
receivedDigit = 0
|
||||
receivedNumber = 10 * receivedNumber + receivedDigit
|
||||
|
||||
if (( control[0] == 0 )) & (( control[2] == 40 )) & (( not receivedNumber == 0 )):
|
||||
updateLastCard(receivedNumber)
|
||||
if last_card != receivedNumber:
|
||||
last_time = datetime.datetime.now()
|
||||
last_card = receivedNumber
|
||||
receivedNumber = 0
|
||||
window = uic.loadUi("registrace.ui")
|
||||
window.racers.setModel(model)
|
||||
window.register_racer.clicked.connect(register_racer)
|
||||
window.reload_racers.clicked.connect(reload_racers)
|
||||
window.show()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
exit()
|
||||
except:
|
||||
pass
|
||||
|
||||
time.sleep(0.001)
|
||||
|
||||
UsbCardReader()
|
||||
|
||||
app.exec()
|
||||
@@ -14,44 +14,10 @@
|
||||
<string>Registrace závodníků</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="5" column="0">
|
||||
<widget class="QLineEdit" name="lastCard"/>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QPushButton" name="register_racer">
|
||||
<property name="text">
|
||||
<string>Registrovat</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<widget class="QPushButton" name="unregister_racer">
|
||||
<property name="text">
|
||||
<string>Dokončit</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLineEdit" name="starting_number"/>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>startovní číslo</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="0">
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>číslo karty</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="6" column="0">
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
<enum>Qt::Orientation::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
@@ -61,9 +27,33 @@
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item row="0" column="1" rowspan="7">
|
||||
<item row="0" column="0">
|
||||
<widget class="QPushButton" name="register_racer">
|
||||
<property name="text">
|
||||
<string>Registrovat</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLineEdit" name="lastCard"/>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>číslo karty</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1" rowspan="5">
|
||||
<widget class="QListView" name="racers"/>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<widget class="QPushButton" name="reload_racers">
|
||||
<property name="text">
|
||||
<string>Obnovit Závodníky</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
|
||||
52
SW/PC/Stopwatch/usbcardreader.py
Normal file
52
SW/PC/Stopwatch/usbcardreader.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import usb.core
|
||||
import usb.util
|
||||
import threading
|
||||
import time
|
||||
|
||||
|
||||
USB_IF = 0 # Interface
|
||||
USB_TIMEOUT = 5 # Timeout in ms
|
||||
USB_VENDOR = 0xffff # Vendor-ID:
|
||||
USB_PRODUCT = 0x0035 # Product-ID
|
||||
|
||||
|
||||
class UsbCardReader:
|
||||
def __init__(self, callback):
|
||||
#fn called on found card
|
||||
self.callback = callback
|
||||
# Find the HID device by vendor/product ID
|
||||
self.dev = usb.core.find(idVendor=USB_VENDOR, idProduct=USB_PRODUCT)
|
||||
# Get and store the endpoint
|
||||
self.endpoint = self.dev[0][(0,0)][0]
|
||||
if self.dev.is_kernel_driver_active(USB_IF) is True:
|
||||
self.dev.detach_kernel_driver(USB_IF)
|
||||
# Claim the device
|
||||
usb.util.claim_interface(self.dev, USB_IF)
|
||||
self.receivedNumber = 0
|
||||
|
||||
cardread=threading.Thread(target=self.read)
|
||||
cardread.start()
|
||||
|
||||
def read(self):
|
||||
receivedNumber = 0
|
||||
|
||||
while True:
|
||||
control = None
|
||||
try:
|
||||
control = self.dev.read(self.endpoint.bEndpointAddress, self.endpoint.wMaxPacketSize, USB_TIMEOUT)
|
||||
if (control[2] != 40) and (control[2] != 0):
|
||||
receivedDigit = control[2] - 29
|
||||
if receivedDigit == 10:
|
||||
receivedDigit = 0
|
||||
receivedNumber = 10 * receivedNumber + receivedDigit
|
||||
|
||||
if ( control[0] == 0 ) and ( control[2] == 40 ) and ( not receivedNumber == 0 ):
|
||||
self.callback(receivedNumber)
|
||||
receivedNumber = 0
|
||||
except KeyboardInterrupt:
|
||||
exit()
|
||||
except:
|
||||
receivedNumber = 0
|
||||
pass
|
||||
|
||||
time.sleep(0.001)
|
||||
Reference in New Issue
Block a user