diff --git a/fichero_printer/fichero/api.py b/fichero_printer/fichero/api.py index a96eb7b..0c860ce 100644 --- a/fichero_printer/fichero/api.py +++ b/fichero_printer/fichero/api.py @@ -35,6 +35,7 @@ from fichero.printer import ( PrinterNotFound, PrinterTimeout, connect, + find_printer, ) # --------------------------------------------------------------------------- @@ -217,7 +218,10 @@ def _ui_html() -> str:

Connection

- +
+ + +
@@ -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",