added stopwatch card reader code

This commit is contained in:
Angoosh Leviocki 2025-01-13 21:26:05 +01:00
parent 07473c8a20
commit 46df08600d
Signed by: angoosh
GPG Key ID: 2DAE446D291BD8D3
4 changed files with 227 additions and 12 deletions

View File

@ -1,12 +1,83 @@
import gi import gi
import threading import threading
import time import time
import usb.core
import usb.util
import requests
import json
gi.require_version('Gtk', '4.0') gi.require_version('Gtk', '4.0')
from gi.repository import Gtk, Gio, Gdk from gi.repository import Gtk, Gio, Gdk
index = 2 DEBUG = False
TIME = 10
if DEBUG == True:
index = 2
else:
index = 1
activeCards = []
TIME = 600
USB_IF = 0 # Interface
USB_TIMEOUT = 5 # Timeout in ms
USB_VENDOR = 0xffff # Vendor-ID:
USB_PRODUCT = 0x0035 # Product-ID
with open('people.json', 'r') as f:
db = json.load(f)
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 )):
try:
with open('people.json', 'r') as f:
db = json.load(f)
for i in db:
if db[i] == receivedNumber:
if i not in activeCards:
activeCards.append(i)
win.arb(i)
else:
print("Card is active now")
except Exception as e: print(e)
receivedNumber = 0
except KeyboardInterrupt:
exit()
except:
pass
time.sleep(0.001)
class GridRow(): class GridRow():
def __init__(self, parent, runner, index): def __init__(self, parent, runner, index):
@ -81,17 +152,20 @@ class GridWindow(Gtk.ApplicationWindow):
self.runners = [] self.runners = []
self.grid = Gtk.Grid() self.grid = Gtk.Grid()
self.grid.attach(button1, 0, 0, 1, 1) if DEBUG == True:
self.grid.attach(button2, 1, 0, 2, 1) self.grid.attach(button1, 0, 0, 1, 1)
self.grid.attach(runlabel, 0, 1, 1, 1) self.grid.attach(button2, 1, 0, 2, 1)
self.grid.attach(cntlabel, 1, 1, 2, 1) self.grid.attach(runlabel, 0, 1, 1, 1)
self.grid.attach(cntlabel, 1, 1, 2, 1)
else:
self.grid.attach(runlabel, 0, 0, 1, 1)
self.grid.attach(cntlabel, 1, 0, 2, 1)
self.set_child(self.grid) self.set_child(self.grid)
def arb(self, button): def arb(self, personId):
global index global index
self.runners.append(GridRow(self, index, index)) self.runners.append(GridRow(self, personId, index))
#self.addRow(index,index)
index += 1 index += 1
def rrb(self, button): def rrb(self, button):
@ -106,18 +180,20 @@ class GridWindow(Gtk.ApplicationWindow):
self.grid.attach(runb, 1, index, 1, 1) self.grid.attach(runb, 1, index, 1, 1)
def remRow(self): def remRow(self):
global index global index, activeCards
if index > 1: if index > 1:
self.grid.remove_row(1) self.grid.remove_row(1)
index -= 1 index -= 1
del activeCards[0]
def on_activate(app): def on_activate(app):
# Create window # Create window
global win
win = GridWindow(application=app) win = GridWindow(application=app)
win.present() win.present()
UsbCardReader()
app = Gtk.Application(application_id='com.example.App') app = Gtk.Application(application_id='com.example.App')
app.connect('activate', on_activate) app.connect('activate', on_activate)

View File

@ -0,0 +1,4 @@
{
"1": 3619716179,
"2": 3620062547
}

View File

@ -0,0 +1,51 @@
import usb.core
import usb.util
import time
import requests
import json
USB_IF = 0 # Interface
USB_TIMEOUT = 5 # Timeout in MS
USB_VENDOR = 0xffff # Vendor-ID:
USB_PRODUCT = 0x0035 # Product-ID
# Find the HID device by vender/product ID
dev = usb.core.find(idVendor=USB_VENDOR, idProduct=USB_PRODUCT)
# Get and store the endpoint
endpoint = dev[0][(0,0)][0]
if dev.is_kernel_driver_active(USB_IF) is True:
dev.detach_kernel_driver(USB_IF)
# Claim the device
usb.util.claim_interface(dev, USB_IF)
receivedNumber = 0
while True:
control = None
try:
# Read a character from the device
control = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, USB_TIMEOUT)
# Here you have to analyze what's coming in.
# In my case you had to check the first byte (command)
if (control[2] != 40) & (control[2] != 0):
# Convert ascii to a number, there's probably better ways to do so.
receivedDigit = control[2] - 29
if receivedDigit == 10:
receivedDigit = 0
# Append the digit to the number
receivedNumber = 10 * receivedNumber + receivedDigit
# Check if the received character is CRLF
if (( control[0] == 0 )) & (( control[2] == 40 )) & (( not receivedNumber == 0 )):
print('cardNumber:', receivedNumber)
except KeyboardInterrupt:
exit()
except:
pass
time.sleep(0.001) # Let CTRL+C actually exit

View File

@ -0,0 +1,84 @@
import time
import usb.core
import usb.util
import requests
import json
import threading
USB_IF = 0 # Interface
USB_TIMEOUT = 5 # Timeout in ms
USB_VENDOR = 0xffff # Vendor-ID:
USB_PRODUCT = 0x0035 # Product-ID
activeCards = {}
try:
with open('people.json', 'r') as f:
activeCards = json.load(f)
except:
print("No people.json found")
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
def read(self):
global activeCards
print("Priloz kartu")
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
self.receivedNumber = 10 * self.receivedNumber + receivedDigit
if (( control[0] == 0 )) & (( control[2] == 40 )) & (( not self.receivedNumber == 0 )):
try:
cardPresent = False
for i in activeCards:
if activeCards[i] == self.receivedNumber:
cardPresent = True
break
if cardPresent == False:
assign=threading.Thread(target=self.assign)
assign.start()
else:
print("Naramek jiz zaregistrovan")
self.receivedNumber = 0
print("Priloz kartu")
except Exception as e: print(e)
except KeyboardInterrupt:
exit()
except:
pass
time.sleep(0.001)
def assign(self):
global activeCards
n = input("Cislo zavodnika: ")
activeCards[str(n)] = self.receivedNumber
with open('people.json', 'w') as f:
f.write(json.dumps(activeCards, indent = 4))
self.receivedNumber = 0
print("Ulozeno, priloz kartu")
if __name__ == "__main__":
reader = UsbCardReader()
print("Zapis zavodniku ready")
reader.read()