-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Program to write spool data to tags
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |