fix: include saved floor assignments in scan API responses

The /api/scan, /api/scans/<filename>, and /api/latest endpoints
now look up saved floor assignments from the database and include
them in responses. Previously only /api/scan/bt did this.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
User
2026-02-01 03:46:12 +01:00
parent 7eb76498b0
commit ea0ee0c190

View File

@@ -436,8 +436,8 @@ def create_app(config: Config | None = None) -> Flask:
"bluetooth_devices": []
}
# Don't assign default floor to detected devices - their floor is unknown
# Only the scanner position has a known floor (from building config)
# Get saved floor assignments from database
saved_floors = db.get_all_device_floors() if db else {}
for net in wifi:
dist = estimate_distance(net.rssi)
@@ -451,7 +451,7 @@ def create_app(config: Config | None = None) -> Flask:
"manufacturer": net.manufacturer,
"estimated_distance_m": round(dist, 2),
"signal_quality": net.signal_quality,
"floor": net.floor, # None = unknown floor
"floor": saved_floors.get(net.bssid, net.floor),
"height_m": net.height_m
})
@@ -466,7 +466,7 @@ def create_app(config: Config | None = None) -> Flask:
"manufacturer": dev.manufacturer,
"estimated_distance_m": round(dist, 2),
"signal_quality": dev.signal_quality,
"floor": dev.floor, # None = unknown floor
"floor": saved_floors.get(dev.address, dev.floor),
"height_m": dev.height_m
})
@@ -507,18 +507,22 @@ def create_app(config: Config | None = None) -> Flask:
with open(filepath) as f:
scan = json.load(f)
# Enrich with distance estimates and ensure floor fields exist
# Get saved floor assignments from database
db = app.config.get("DATABASE")
saved_floors = db.get_all_device_floors() if db else {}
# Enrich with distance estimates and saved floor assignments
for net in scan.get("wifi_networks", []):
net["estimated_distance_m"] = round(estimate_distance(net["rssi"]), 2)
if "floor" not in net:
net["floor"] = None
bssid = net.get("bssid")
net["floor"] = saved_floors.get(bssid) if bssid else net.get("floor")
if "height_m" not in net:
net["height_m"] = None
for dev in scan.get("bluetooth_devices", []):
dev["estimated_distance_m"] = round(estimate_distance(dev["rssi"], tx_power=-65), 2)
if "floor" not in dev:
dev["floor"] = None
address = dev.get("address")
dev["floor"] = saved_floors.get(address) if address else dev.get("floor")
if "height_m" not in dev:
dev["height_m"] = None
@@ -536,18 +540,22 @@ def create_app(config: Config | None = None) -> Flask:
with open(scan_files[0]) as f:
scan = json.load(f)
# Enrich with distance estimates and ensure floor fields exist
# Get saved floor assignments from database
db = app.config.get("DATABASE")
saved_floors = db.get_all_device_floors() if db else {}
# Enrich with distance estimates and saved floor assignments
for net in scan.get("wifi_networks", []):
net["estimated_distance_m"] = round(estimate_distance(net["rssi"]), 2)
if "floor" not in net:
net["floor"] = None
bssid = net.get("bssid")
net["floor"] = saved_floors.get(bssid) if bssid else net.get("floor")
if "height_m" not in net:
net["height_m"] = None
for dev in scan.get("bluetooth_devices", []):
dev["estimated_distance_m"] = round(estimate_distance(dev["rssi"], tx_power=-65), 2)
if "floor" not in dev:
dev["floor"] = None
address = dev.get("address")
dev["floor"] = saved_floors.get(address) if address else dev.get("floor")
if "height_m" not in dev:
dev["height_m"] = None