0.1.31
This commit is contained in:
@@ -77,7 +77,7 @@ async def lifespan(app: FastAPI): # noqa: ARG001
|
||||
app = FastAPI(
|
||||
title="Fichero Printer API",
|
||||
description="REST API for the Fichero D11s (AiYin) thermal label printer.",
|
||||
version = "0.1.30",
|
||||
version = "0.1.31",
|
||||
lifespan=lifespan,
|
||||
docs_url=None,
|
||||
redoc_url=None,
|
||||
@@ -123,43 +123,74 @@ def _ui_html() -> str:
|
||||
|
||||
# Inject debug scan section and script
|
||||
scan_html = """
|
||||
<div class="section">
|
||||
<div class="card">
|
||||
<h2>Debug Scan</h2>
|
||||
<p>Scans for all nearby BLE devices to help with debugging connection issues.</p>
|
||||
<button type="button" onclick="scanForDevices()">Scan for BLE Devices (10s)</button>
|
||||
<pre id="scan-results" style="background-color: #f0f0f0; border: 1px solid #ccc; padding: 10px; margin-top: 10px; white-space: pre-wrap; word-wrap: break-word;"></pre>
|
||||
<p class="muted">Scans for all nearby BLE devices to help with debugging connection issues.</p>
|
||||
<button type="button" onclick="scanForDevices()" id="scan-button">
|
||||
<span class="loading" id="scan-loading" style="display: none;"></span>
|
||||
<span id="scan-text">Scan for BLE Devices (10s)</span>
|
||||
</button>
|
||||
<pre id="scan-results"></pre>
|
||||
</div>
|
||||
"""
|
||||
scan_script = r'''
|
||||
<script>
|
||||
async function scanForDevices() {
|
||||
const resultsEl = document.getElementById('scan-results');
|
||||
resultsEl.textContent = 'Scanning for 10 seconds...';
|
||||
const scanButton = document.getElementById('scan-button');
|
||||
const loadingEl = document.getElementById('scan-loading');
|
||||
const textEl = document.getElementById('scan-text');
|
||||
|
||||
// Show loading state
|
||||
scanButton.disabled = true;
|
||||
loadingEl.style.display = 'inline-block';
|
||||
textEl.textContent = 'Scanning...';
|
||||
resultsEl.textContent = '🔍 Searching for BLE devices (this may take up to 10 seconds)...';
|
||||
|
||||
try {
|
||||
const response = await fetch('/scan');
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.detail || `HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const devices = await response.json();
|
||||
|
||||
if (devices.length === 0) {
|
||||
resultsEl.textContent = 'No BLE devices found.';
|
||||
resultsEl.textContent = '📡 No BLE devices found.\n\nTroubleshooting tips:\n- Make sure your printer is powered on\n- Ensure Bluetooth is enabled on this device\n- Bring the printer closer (within 5 meters)\n- Try restarting the printer';
|
||||
} else {
|
||||
resultsEl.textContent = 'Found devices:\n\n' +
|
||||
devices.map(d => ` ${d.address} | RSSI: ${d.rssi} | ${d.name}`).join('\n');
|
||||
let resultText = '🎉 Found ' + devices.length + ' device(s):\n\n';
|
||||
devices.forEach((d, index) => {
|
||||
resultText += `${index + 1}. ${d.name || 'Unknown Device'}\n`;
|
||||
resultText += ` Address: ${d.address}\n`;
|
||||
resultText += ` Signal: ${d.rssi} dBm (${Math.abs(d.rssi) < 60 ? 'Strong' : Math.abs(d.rssi) < 80 ? 'Good' : 'Weak'})\n`;
|
||||
resultText += ` ${'='.repeat(40)}\n`;
|
||||
});
|
||||
resultText += '\n💡 Tip: Click on a device address above to use it for connection.';
|
||||
resultsEl.textContent = resultText;
|
||||
}
|
||||
} catch (e) {
|
||||
resultsEl.textContent = 'Error during scan: ' + e.message;
|
||||
resultsEl.textContent = '❌ Error during scan: ' + e.message + '\n\nPossible causes:\n- Bluetooth adapter not available\n- Missing permissions\n- Bluetooth service not running';
|
||||
console.error('Scan error:', e);
|
||||
} finally {
|
||||
// Reset button state
|
||||
scanButton.disabled = false;
|
||||
loadingEl.style.display = 'none';
|
||||
textEl.textContent = 'Scan for BLE Devices (10s)';
|
||||
}
|
||||
}
|
||||
</script>
|
||||
'''
|
||||
# Inject before the closing </body> tag
|
||||
if "</body>" in template:
|
||||
# Inject after the main content but before scripts
|
||||
if "</main>" in template:
|
||||
parts = template.split("</main>", 1)
|
||||
template = parts[0] + "</main>" + scan_html + parts[1]
|
||||
elif "</body>" in template:
|
||||
parts = template.split("</body>", 1)
|
||||
template = parts[0] + scan_html + scan_script + "</body>" + parts[1]
|
||||
else:
|
||||
# Fallback if no body tag
|
||||
# Fallback if no main or body tag
|
||||
template += scan_html + scan_script
|
||||
|
||||
return template
|
||||
|
||||
Reference in New Issue
Block a user