Add printer scanning functionality and enhance UI for address input
This commit is contained in:
@@ -35,6 +35,7 @@ from fichero.printer import (
|
||||
PrinterNotFound,
|
||||
PrinterTimeout,
|
||||
connect,
|
||||
find_printer,
|
||||
)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -217,7 +218,10 @@ def _ui_html() -> str:
|
||||
<div class="card">
|
||||
<h2>Connection</h2>
|
||||
<label for="address">Printer address</label>
|
||||
<input id="address" value="{default_address}" placeholder="C9:48:8A:69:D5:C0">
|
||||
<div style="display:flex;gap:8px;align-items:center">
|
||||
<input id="address" value="{default_address}" placeholder="C9:48:8A:69:D5:C0" style="flex:1;width:auto">
|
||||
<button type="button" id="scan-btn" class="alt" style="width:auto;white-space:nowrap" onclick="scanAddress()">🔌 Scan</button>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div>
|
||||
@@ -352,6 +356,29 @@ def _ui_html() -> str:
|
||||
await showResponse(response);
|
||||
}}
|
||||
|
||||
async function scanAddress() {{
|
||||
const btn = document.getElementById("scan-btn");
|
||||
const output = document.getElementById("output");
|
||||
btn.disabled = true;
|
||||
btn.textContent = "Scanning…";
|
||||
output.textContent = "Scanning for printer (up to 8 s)…";
|
||||
try {{
|
||||
const response = await fetch("scan");
|
||||
const data = await response.json();
|
||||
if (response.ok && data.address) {{
|
||||
document.getElementById("address").value = data.address;
|
||||
output.textContent = JSON.stringify({{ status: response.status, ok: true, data }}, null, 2);
|
||||
}} else {{
|
||||
output.textContent = JSON.stringify({{ status: response.status, ok: false, data }}, null, 2);
|
||||
}}
|
||||
}} catch (e) {{
|
||||
output.textContent = "Scan failed: " + e;
|
||||
}} finally {{
|
||||
btn.disabled = false;
|
||||
btn.innerHTML = "🔌 Scan";
|
||||
}}
|
||||
}}
|
||||
|
||||
async function printText() {{
|
||||
const form = new FormData();
|
||||
form.set("text", document.getElementById("text").value);
|
||||
@@ -410,6 +437,22 @@ async def docs():
|
||||
)
|
||||
|
||||
|
||||
@app.get(
|
||||
"/scan",
|
||||
summary="Scan for printer",
|
||||
response_description="BLE address of the discovered printer",
|
||||
)
|
||||
async def scan_printer():
|
||||
"""Scan for a Fichero/D11s printer via BLE and return its address."""
|
||||
try:
|
||||
address = await find_printer()
|
||||
except PrinterNotFound as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=502, detail=str(exc)) from exc
|
||||
return {"address": address}
|
||||
|
||||
|
||||
@app.get(
|
||||
"/status",
|
||||
summary="Get printer status",
|
||||
|
||||
Reference in New Issue
Block a user