matekasse_NFC-Reader/NFC_Reader.py

43 lines
1.2 KiB
Python
Raw Permalink Normal View History

import time
import nfc
2024-02-16 21:16:05 +01:00
import subprocess
import requests
2024-04-12 22:01:57 +02:00
import oled
2024-04-17 20:36:03 +02:00
from multiprocessing import Process
2024-02-16 21:16:05 +01:00
url="http://172.16.1.105/api/"
def get_reader():
devices = subprocess.check_output("lsusb")
for i in devices.split(b'\n'):
if str(i).__contains__("Advanced Card Systems, Ltd ACR122U"):
bus = str(i).split(":")[0].split(" Device ")[0].split("Bus ")[1]
device = str(i).split(":")[0].split("Device ")[1]
return("usb:{}:{}".format(bus, device))
raise Exception("\n\n Error: NFC reader not found")
def onTagSense(tag):
2024-02-16 21:16:05 +01:00
id = int.from_bytes(tag.identifier, "big")
response = requests.post(f"{url}tag_id", data={"id":id})
2024-04-17 20:17:13 +02:00
2024-04-17 20:58:54 +02:00
try:
if response.json()["mode"] == "balance" or response.json()["mode"] == "message":
#oled.balance(response.json()["username"], response.json()["balance"])
return True
2024-04-17 20:57:02 +02:00
except:
None
2024-02-16 21:16:05 +01:00
return False
def start_nfc_normal():
2024-05-08 18:03:19 +02:00
device = get_reader()
clf = nfc.ContactlessFrontend(device)
clf.open(device)
2024-05-08 20:29:47 +02:00
while True:
2024-07-03 17:50:45 +02:00
if clf.connect(rdwr={'on-connect': onTagSense, 'beep-on-connect': True }) == False:
raise Exception("NFC Reader failed")
2024-05-08 20:29:47 +02:00
time.sleep(0.75)
if __name__ == "__main__":
start_nfc_normal()