Skip to content

Commit

Permalink
add: error handling to write_tags.py
Browse files Browse the repository at this point in the history
  • Loading branch information
bofh69 committed Mar 15, 2024
1 parent df26f8a commit 7439ee7
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions write_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import requests


# TODO: Documentation

SPOOL = "SPOOL"
FILAMENT = "FILAMENT"
NDEF_TEXT_TYPE = "urn:nfc:wkt:T"
Expand Down Expand Up @@ -94,12 +92,22 @@ def exit_app(self):
class TagWritingApp(npyscreen.NPSAppManaged):
"""The npyscreen's main class for the application"""

def __init__(self):
super().__init__()
self.status = ""

def on_nfc_connect(self, tag, spool: int, filament: int) -> bool:
"""Write given spool/filament ids to the tag"""
if tag.ndef:
tag.ndef.records = [
ndef.TextRecord(f"SPOOL:{spool}\nFILAMENT:{filament}\n")
]
try:
if tag.ndef and tag.ndef.is_writeable:
tag.ndef.records = [
ndef.TextRecord(f"SPOOL:{spool}\nFILAMENT:{filament}\n")
]
else:
self.status = "Tag is write protected"
except Exception as ex: # pylint: disable=W0718
print(ex)
self.status = "Got error while writing"
return False

def write_tag(self, record):
Expand All @@ -112,6 +120,8 @@ def write_tag(self, record):
spool = record["id"]
filament = record["filament"]["id"]

self.status = "Written"

clf = nfc.ContactlessFrontend(args.nfc_device)
clf.connect(
rdwr={
Expand All @@ -121,7 +131,7 @@ def write_tag(self, record):
}
)
clf.close()
npyscreen.notify("Written", title="Writing to tag")
npyscreen.notify(self.status, title="Writing to tag")
time.sleep(1)

def onStart(self):
Expand Down

0 comments on commit 7439ee7

Please sign in to comment.