Add asset lookup from canteen DB — auto-match OCR machine IDs to asset records

This commit is contained in:
2026-05-25 00:43:44 -04:00
parent b079c9bf8f
commit 6adaf97958
6 changed files with 113 additions and 1 deletions
+56
View File
@@ -116,6 +116,11 @@ async def analyze_photo(file: UploadFile = File(...)):
exif_result = extract_exif(contents)
ocr_result = run_ocr(contents) if HAS_TESSERACT else {"available": False, "text": ""}
# Extract machine ID from OCR for auto-lookup
machine_id = None
if ocr_result.get("match_5dash6"):
machine_id = re.sub(r"\D", "", ocr_result["match_5dash6"])[-5:]
return {
"filename": file.filename,
"saved_as": fname,
@@ -123,9 +128,60 @@ async def analyze_photo(file: UploadFile = File(...)):
"file_size_kb": round(file_size / 1024, 1),
"exif": exif_result,
"ocr": ocr_result,
"machine_id": machine_id,
}
@app.get("/api/lookup")
async def lookup_asset(machine_id: str = ""):
"""Look up an asset by machine_id in the canteen assets database."""
if not machine_id or not machine_id.strip():
return {"found": False, "reason": "No machine_id provided"}
import sqlite3
from pathlib import Path as _Path
db_path = _Path(__file__).parent.parent / "canteen-asset-tracker" / "assets.db"
if not db_path.exists():
return {"found": False, "reason": f"Database not found at {db_path}"}
try:
conn = sqlite3.connect(str(db_path))
conn.row_factory = sqlite3.Row
row = conn.execute(
"SELECT id, machine_id, name, category, status, address, building_name, "
"floor, room, latitude, longitude, make, model, description, photo_path "
"FROM assets WHERE machine_id = ?",
(machine_id.strip(),),
).fetchone()
conn.close()
if row:
return {
"found": True,
"asset": {
"id": row["id"],
"machine_id": row["machine_id"],
"name": row["name"],
"category": row["category"],
"status": row["status"],
"address": row["address"],
"building_name": row["building_name"],
"floor": row["floor"],
"room": row["room"],
"latitude": row["latitude"],
"longitude": row["longitude"],
"make": row["make"],
"model": row["model"],
"description": row["description"],
"photo_path": row["photo_path"],
},
}
return {"found": False, "reason": f"No asset with machine_id '{machine_id}'"}
except Exception as e:
return {"found": False, "reason": str(e)}
@app.get("/api/uploads")
async def list_uploads():
"""List previously uploaded files."""