73 lines
2.0 KiB
Python
73 lines
2.0 KiB
Python
import time
|
|
import pyperclip
|
|
|
|
from smartcard.CardRequest import CardRequest
|
|
from smartcard.Exceptions import CardConnectionException
|
|
from smartcard.CardConnection import CardConnection
|
|
from smartcard.util import toHexString
|
|
|
|
from card_reader import read_card
|
|
|
|
CLIPBOARD_MESSAGE_PREFIX = "ezgglan-clipboard-message:"
|
|
|
|
|
|
class ClipboardReaderService:
|
|
def __init__(self) -> None:
|
|
self._is_running = True
|
|
self._last_uid = None
|
|
|
|
def run(self) -> None:
|
|
print("Reader ready.")
|
|
|
|
while self._is_running:
|
|
try:
|
|
card_request = CardRequest(timeout=None)
|
|
card_service = card_request.waitforcard()
|
|
connection = card_service.connection
|
|
|
|
try:
|
|
connection.connect(CardConnection.T1_protocol)
|
|
|
|
GET_UID = [0xFF, 0xCA, 0x00, 0x00, 0x00]
|
|
uid, sw1, sw2 = connection.transmit(GET_UID)
|
|
|
|
if sw1 != 0x90:
|
|
raise CardConnectionException("UID read failed")
|
|
|
|
uid_hex = toHexString(uid)
|
|
|
|
if uid_hex == self._last_uid:
|
|
connection.disconnect()
|
|
time.sleep(0.5)
|
|
continue
|
|
|
|
self._last_uid = uid_hex
|
|
|
|
user_id = read_card(connection)
|
|
|
|
if user_id is not None:
|
|
pyperclip.copy(CLIPBOARD_MESSAGE_PREFIX + str(user_id))
|
|
else:
|
|
pyperclip.copy(CLIPBOARD_MESSAGE_PREFIX + "Fehler: Erneut scannen")
|
|
|
|
except CardConnectionException:
|
|
pass
|
|
|
|
finally:
|
|
try:
|
|
connection.disconnect()
|
|
except:
|
|
pass
|
|
|
|
time.sleep(0.5)
|
|
|
|
except (KeyboardInterrupt, SystemExit):
|
|
self.stop()
|
|
|
|
def stop(self) -> None:
|
|
self._is_running = False
|
|
|
|
if __name__ == "__main__":
|
|
service = ClipboardReaderService()
|
|
service.run()
|