Skip to content

Commit

Permalink
WIP: Program to write spool data to tags
Browse files Browse the repository at this point in the history
  • Loading branch information
bofh69 committed Mar 5, 2024
1 parent ec9d3c1 commit 1dc0e80
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nfcpy==1.0.4
requests==2.31.0
npyscreen==4.10.5
97 changes: 97 additions & 0 deletions write_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env python3

""" A program to write NFC tags from Spoolman's data """

import argparse

import time
import npyscreen


# TODO: Documentation
# TODO: Fetch spools' data from Spoolman
# TODO: NFC writing

SPOOL = "SPOOL"
FILAMENT = "FILAMENT"
NDEF_TEXT_TYPE = "urn:nfc:wkt:T"

parser = argparse.ArgumentParser(
description="Fetches spools from Spoolman and allows writing info about them to RFID tags.",
)

parser.add_argument("--version", action="version", version="%(prog)s 0.0.1")

parser.add_argument(
"-d",
"--nfc-device",
metavar="device",
default="ttyS0",
help="Which NFC reader to use, see "
+ "https://nfcpy.readthedocs.io/en/latest/topics/get-started.html#open-a-local-device"
+ " for format",
)

parser.add_argument(
"-u",
"--url",
metavar="URL",
default="http://mainsailos.local:7912",
help="URL for the Spoolman installation",
)


class PostList(npyscreen.MultiLineAction):
"""A wrapper for MultiLineAction to call the write_tag function"""

def actionHighlighted(self, act_on_this, _key_press):
"""Called when a line is chosen"""
self.parent.parentApp.write_tag(act_on_this)


class PostSelectForm(npyscreen.FormBaseNew):
"""Simple form for showing the spools"""

def create(self):
"""Create the forms widgets"""
self.add(
npyscreen.ButtonPress,
name="Exit",
when_pressed_function=self.exit_app,
)

self.posts = self.add(
PostList,
values=["Data 1", "Data 2", "Data 3", "Data 4"] * 25,
name="Choose spools to write",
scroll_exit=True,
)

def exit_app(self):
"""Called when exit is choosen"""
self.parentApp.switchForm(None)


class TagWritingApp(npyscreen.NPSAppManaged):
"""The npyscreen's main class for the application"""

def write_tag(self, post):
"""Write the chosen post's data to the tag"""
npyscreen.notify("Writing " + post, title="Writing to tag")
time.sleep(0.5)
npyscreen.notify("Written", title="Writing to tag")
time.sleep(1)

def onStart(self):
"""Called when application starts, just add the form"""
form = self.addForm(
"MAIN",
PostSelectForm,
name="Choose spool, press enter to write to tag",
)
form.set_editing(form.posts)


if __name__ == "__main__":
app = TagWritingApp()
app.run()

0 comments on commit 1dc0e80

Please sign in to comment.