Improvement: Better communication with user while using NFC

Improvement: Skip updating SpoolMan when there is no change in active tray
This commit is contained in:
Filip Bednárik 2024-12-13 11:44:00 +01:00
parent 3a6ecf8e9f
commit 6d7eada993
2 changed files with 23 additions and 12 deletions

11
app.py
View File

@ -1,4 +1,5 @@
import json
import traceback
import uuid
from flask import Flask, request, render_template_string
@ -120,6 +121,7 @@ def tray_load():
<p>Updated Spool ID {spool_id} with TAG id {tag_id} to AMS {ams_id}, Tray {tray_id}.</p>
"""
except Exception as e:
traceback.print_exc()
return f"<h1>Error</h1><p>{str(e)}</p>"
@ -162,11 +164,12 @@ def home():
<ul>
"""
for spool in spools:
if not spool.get("extra", {}).get("tag"):
if not spool.get("extra", {}).get("tag") or spool.get("extra", {}).get("tag") == json.dumps(""):
html += f"<li><a href='/assign_tag?spool_id={spool.get('id')}'>Spool {spool.get('filament').get('vendor').get('name')} - {spool.get('filament').get('name')}</a></li>"
html += "</ul>"
return html
except Exception as e:
traceback.print_exc()
return f"<h1>Error</h1><p>{str(e)}</p>"
@ -189,12 +192,13 @@ def assign_tag():
<script type="text/javascript">
function writeNFC(){{
const ndef = new NDEFReader();
document.getElementById("message").textContent="Bring NFC Tag closer to the phone.";
ndef.write({{
records: [{{ recordType: "url", data: "{BASE_URL}/spool_info?tag_id={myuuid}" }}],
}}).then(() => {{
alert("Message written.");
document.getElementById("message").textContent="Message written.";
}}).catch(error => {{
alert(`Write failed :-( try again: ${{error}}.`);
document.getElementById("message").textContent="Write failed :-( try again: " + error + ".";
}});
}};
</script>
@ -202,6 +206,7 @@ def assign_tag():
<body>
NFC Write
<button id="write" onclick="writeNFC()">Write</button>
<h1 id="message"></h1>
</body>
</html>
"""

View File

@ -31,7 +31,7 @@ def on_message(client, userdata, msg):
# TODO: Consume spool
try:
data = json.loads(msg.payload.decode())
print(data)
#print(data)
if "print" in data and "vt_tray" in data["print"]:
print(data)
LAST_AMS_CONFIG["vt_tray"] = data["print"]["vt_tray"]
@ -79,15 +79,21 @@ def on_connect(client, userdata, flags, rc):
def setActiveTray(spool_id, spool_extra, ams_id, tray_id):
patchExtraTags(spool_id, spool_extra, {
"active_tray": json.dumps(f"{PRINTER_ID}_{ams_id}_{tray_id}"),
})
if spool_extra == None:
spool_extra = {}
# Remove active tray from inactive spools
for old_spool in SPOOLS:
if spool_id != old_spool["id"] and old_spool["extra"]["active_tray"] == json.dumps(
f"{PRINTER_ID}_{ams_id}_{tray_id}"):
patchExtraTags(old_spool["id"], old_spool["extra"], {"active_tray": json.dumps("")})
if not spool_extra.get("active_tray") or spool_extra.get("active_tray") != json.dumps(f"{PRINTER_ID}_{ams_id}_{tray_id}"):
patchExtraTags(spool_id, spool_extra, {
"active_tray": json.dumps(f"{PRINTER_ID}_{ams_id}_{tray_id}"),
})
# Remove active tray from inactive spools
for old_spool in SPOOLS:
if spool_id != old_spool["id"] and old_spool["extra"]["active_tray"] == json.dumps(
f"{PRINTER_ID}_{ams_id}_{tray_id}"):
patchExtraTags(old_spool["id"], old_spool["extra"], {"active_tray": json.dumps("")})
else:
print("Skipping set active tray")
# Fetch spools from spoolman