2023-06-23 17:25:41 +00:00
|
|
|
import logging
|
|
|
|
|
import sys
|
2023-05-16 17:39:30 +00:00
|
|
|
import time
|
2023-06-23 17:25:41 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import serial
|
2023-05-16 17:39:30 +00:00
|
|
|
import ndef
|
|
|
|
|
import nfc
|
|
|
|
|
from nfc.clf import RemoteTarget
|
|
|
|
|
|
2023-06-23 17:25:41 +00:00
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
format="{asctime}:{name}:{levelname}:{message}",
|
|
|
|
|
datefmt="%Y-%m-%dT%H-%M-%S%z",
|
|
|
|
|
style="{",
|
|
|
|
|
level="INFO",
|
|
|
|
|
handlers=[
|
|
|
|
|
logging.StreamHandler(stream=sys.stderr),
|
|
|
|
|
logging.FileHandler(
|
|
|
|
|
Path("data/logs") / f"{datetime.now().isoformat().replace(':', '_')}.log",
|
|
|
|
|
mode="a",
|
|
|
|
|
encoding="utf-8",
|
|
|
|
|
delay=False,
|
|
|
|
|
**({"errors": "surrogateescape"} if sys.version_info.major >= 3 and sys.version_info.minor >= 10 else {}),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
force=True,
|
|
|
|
|
**({"encoding": "utf-8"} if sys.version_info.major >= 3 and sys.version_info.minor >= 10 else {}),
|
|
|
|
|
**({"errors": "surrogateescape"} if sys.version_info.major >= 3 and sys.version_info.minor >= 10 else {}),
|
|
|
|
|
)
|
|
|
|
|
|
2023-05-18 08:40:16 +00:00
|
|
|
connected=False
|
2023-05-16 17:39:30 +00:00
|
|
|
while True:
|
2023-05-18 08:40:16 +00:00
|
|
|
try:
|
|
|
|
|
if not connected:
|
2023-06-23 08:20:26 +00:00
|
|
|
clf = nfc.ContactlessFrontend()
|
2023-05-18 08:40:16 +00:00
|
|
|
connected=clf.open('tty:USB0:pn532')
|
2023-06-23 08:20:26 +00:00
|
|
|
if connected:
|
|
|
|
|
print("CONNECTED TO TTYUSB0")
|
|
|
|
|
else:
|
|
|
|
|
connected = clf.open('tty:USB1:pn532')
|
|
|
|
|
if connected:
|
|
|
|
|
print("CONNECTED TO TTYUSB1")
|
|
|
|
|
|
2023-05-18 08:40:16 +00:00
|
|
|
if connected:
|
|
|
|
|
target = clf.sense(RemoteTarget('106A'), RemoteTarget('106B'), RemoteTarget('212F'))
|
|
|
|
|
if target is not None:
|
|
|
|
|
tag = nfc.tag.activate(clf, target)
|
|
|
|
|
if tag is not None:
|
|
|
|
|
print(tag)
|
|
|
|
|
if tag.ndef is not None:
|
|
|
|
|
if not len(tag.ndef.records):
|
|
|
|
|
print("EMPTY TAG - WRITING...")
|
|
|
|
|
if not tag.ndef.is_writeable:
|
|
|
|
|
print("This Tag is not writeable.")
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
tag.format()
|
|
|
|
|
#tag.ndef.records = [ndef.TextRecord("Errecinque"),ndef.TextRecord("5812345678")]
|
|
|
|
|
record=ndef.TextRecord("Errecinque,fixture,5812345678")
|
|
|
|
|
tag.ndef.records = [record]
|
|
|
|
|
if tag.ndef.has_changed:
|
|
|
|
|
print("WRITE ERROR")
|
|
|
|
|
else:
|
|
|
|
|
print("WRITE OK")
|
2023-05-16 17:39:30 +00:00
|
|
|
|
2023-05-18 08:40:16 +00:00
|
|
|
for record in tag.ndef.records:
|
|
|
|
|
print(record)
|
|
|
|
|
else:
|
|
|
|
|
print("ERROR NOT NDEF")
|
2023-05-16 17:39:30 +00:00
|
|
|
else:
|
2023-05-18 08:40:16 +00:00
|
|
|
print("NO TARGET")
|
2023-06-23 08:20:26 +00:00
|
|
|
else:
|
|
|
|
|
print("NOT CONNECTED")
|
2023-05-18 08:40:16 +00:00
|
|
|
except Exception as e:
|
|
|
|
|
print(f"EXCEPTION {e}")
|
|
|
|
|
connected=False
|
|
|
|
|
time.sleep(0.3)
|
2023-05-16 17:39:30 +00:00
|
|
|
|
|
|
|
|
print("EXITING")
|
|
|
|
|
clf.close()
|