From 7439ee7e5954bf8fbfbeb5b33a8a54b3bbc71686 Mon Sep 17 00:00:00 2001 From: Sebastian Andersson Date: Fri, 15 Mar 2024 19:54:57 +0100 Subject: [PATCH] add: error handling to write_tags.py --- write_tags.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/write_tags.py b/write_tags.py index 5593694..3545e01 100755 --- a/write_tags.py +++ b/write_tags.py @@ -15,8 +15,6 @@ import requests -# TODO: Documentation - SPOOL = "SPOOL" FILAMENT = "FILAMENT" NDEF_TEXT_TYPE = "urn:nfc:wkt:T" @@ -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): @@ -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={ @@ -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):