-
Notifications
You must be signed in to change notification settings - Fork 2
/
nfc2klipper.py
executable file
·170 lines (134 loc) · 4.76 KB
/
nfc2klipper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2024 Sebastian Andersson <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
"""Program to set current filament & spool in klipper."""
import argparse
import nfc
import requests
SPOOL = "SPOOL"
FILAMENT = "FILAMENT"
NDEF_TEXT_TYPE = "urn:nfc:wkt:T"
parser = argparse.ArgumentParser()
# description="Fetches filaments from Spoolman and creates SuperSlicer filament configs.",
parser.add_argument("--version", action="version", version="%(prog)s 0.0.1")
parser.add_argument(
"-c",
"--clear",
action="store_true",
help="Clears the spool & filamnet when no tag is present",
)
parser.add_argument(
"-d",
"--nfc-device",
metavar="device",
default="ttyAMA0",
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",
help="URL for the moonraker installation",
)
def set_spool_and_filament(url: str, spool: int, filament: int):
"""Calls moonraker with the current spool & filament"""
if "old_spool" not in set_spool_and_filament.__dict__:
set_spool_and_filament.old_spool = None
set_spool_and_filament.old_filament = None
if (
set_spool_and_filament.old_spool == spool
and set_spool_and_filament.old_filament == filament
):
print("Read same spool & filament", flush=True)
return
print(f"Sending spool #{spool}, filament #{filament} to klipper", flush=True)
commands = {
"commands": [
f"SET_ACTIVE_SPOOL ID={spool}",
f"SET_ACTIVE_FILAMENT ID={filament}",
]
}
# In case the post fails, we might not know if the server has received
# it or not, so set them to None:
set_spool_and_filament.old_spool = None
set_spool_and_filament.old_filament = None
try:
response = requests.post(
url + "/api/printer/command", timeout=10, json=commands
)
if response.status_code != 200:
raise ValueError(f"Request to moonraker failed: {response}")
except Exception as ex: # pylint: disable=W0718
print(ex)
set_spool_and_filament.old_spool = spool
set_spool_and_filament.old_filament = filament
def get_data_from_ndef_records(records):
"""Find wanted data from the NDEF records.
>>> import ndef
>>> record0 = ndef.TextRecord("")
>>> record1 = ndef.TextRecord("SPOOL:23\\n")
>>> record2 = ndef.TextRecord("FILAMENT:14\\n")
>>> record3 = ndef.TextRecord("SPOOL:23\\nFILAMENT:14\\n")
>>> get_data_from_ndef_records([record0])
(None, None)
>>> get_data_from_ndef_records([record3])
('23', '14')
>>> get_data_from_ndef_records([record1])
('23', None)
>>> get_data_from_ndef_records([record2])
(None, '14')
>>> get_data_from_ndef_records([record0, record3])
('23', '14')
>>> get_data_from_ndef_records([record3, record0])
('23', '14')
>>> get_data_from_ndef_records([record1, record2])
('23', '14')
>>> get_data_from_ndef_records([record2, record1])
('23', '14')
"""
spool = None
filament = None
for record in records:
if record.type == NDEF_TEXT_TYPE:
for line in record.text.splitlines():
line = line.split(":")
if len(line) == 2:
if line[0] == SPOOL:
spool = line[1]
if line[0] == FILAMENT:
filament = line[1]
else:
print(f"Read other record: {record}", flush=True)
return spool, filament
def on_nfc_connect(tag):
"""Handles a read tag"""
if tag.ndef is None:
print("The tag doesn't have NDEF records", flush=True)
return True
spool, filament = get_data_from_ndef_records(tag.ndef.records)
if not args.clear:
if not (spool and filament):
print("Did not find spool and filament records in tag", flush=True)
if args.clear or (spool and filament):
if not spool:
spool = 0
if not filament:
filament = 0
set_spool_and_filament(args.url, spool, filament)
# Don't let connect return until the tag is removed:
return True
if __name__ == "__main__":
args = parser.parse_args()
# Open NFC reader. Will throw an exception if it fails.
clf = nfc.ContactlessFrontend(args.nfc_device)
if args.clear:
# Start by unsetting current spool & filament:
set_spool_and_filament(args.url, 0, 0)
while True:
clf.connect(rdwr={"on-connect": on_nfc_connect})
# No tag connected anymore.
if args.clear:
set_spool_and_filament(args.url, 0, 0)