diff --git a/SW/PC/Stopwatch/main.py b/SW/PC/Stopwatch/main.py index 8248046..d19b42c 100644 --- a/SW/PC/Stopwatch/main.py +++ b/SW/PC/Stopwatch/main.py @@ -1,12 +1,83 @@ import gi import threading import time +import usb.core +import usb.util +import requests +import json gi.require_version('Gtk', '4.0') from gi.repository import Gtk, Gio, Gdk -index = 2 -TIME = 10 +DEBUG = False + +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(): def __init__(self, parent, runner, index): @@ -81,17 +152,20 @@ class GridWindow(Gtk.ApplicationWindow): self.runners = [] self.grid = Gtk.Grid() - self.grid.attach(button1, 0, 0, 1, 1) - self.grid.attach(button2, 1, 0, 2, 1) - self.grid.attach(runlabel, 0, 1, 1, 1) - self.grid.attach(cntlabel, 1, 1, 2, 1) + if DEBUG == True: + self.grid.attach(button1, 0, 0, 1, 1) + self.grid.attach(button2, 1, 0, 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) - def arb(self, button): + def arb(self, personId): global index - self.runners.append(GridRow(self, index, index)) - #self.addRow(index,index) + self.runners.append(GridRow(self, personId, index)) index += 1 def rrb(self, button): @@ -106,19 +180,21 @@ class GridWindow(Gtk.ApplicationWindow): self.grid.attach(runb, 1, index, 1, 1) def remRow(self): - global index + global index, activeCards if index > 1: self.grid.remove_row(1) index -= 1 + del activeCards[0] def on_activate(app): # Create window + global win win = GridWindow(application=app) win.present() - +UsbCardReader() app = Gtk.Application(application_id='com.example.App') app.connect('activate', on_activate) -app.run(None) \ No newline at end of file +app.run(None) diff --git a/SW/PC/Stopwatch/people.json b/SW/PC/Stopwatch/people.json new file mode 100644 index 0000000..ff37f79 --- /dev/null +++ b/SW/PC/Stopwatch/people.json @@ -0,0 +1,4 @@ +{ + "1": 3619716179, + "2": 3620062547 +} diff --git a/SW/PC/Stopwatch/rfid_read.py b/SW/PC/Stopwatch/rfid_read.py new file mode 100644 index 0000000..e3a6815 --- /dev/null +++ b/SW/PC/Stopwatch/rfid_read.py @@ -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 diff --git a/SW/PC/Stopwatch/zapis_zavodniku.py b/SW/PC/Stopwatch/zapis_zavodniku.py new file mode 100644 index 0000000..c00109c --- /dev/null +++ b/SW/PC/Stopwatch/zapis_zavodniku.py @@ -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() \ No newline at end of file