fix: create missing devices during peer sync

- bulk_update_devices now creates devices if they don't exist locally
  but have useful metadata (floor, label, or favorite status)
- Sync trigger endpoint now reports both pulled and pushed device counts
- Enables full bidirectional sync of device metadata between peers

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
User
2026-02-01 04:15:36 +01:00
parent 6a3e3e8448
commit 827830b043
2 changed files with 23 additions and 6 deletions

View File

@@ -1025,9 +1025,24 @@ class DeviceDatabase:
if cursor.rowcount > 0: if cursor.rowcount > 0:
updated_count += 1 updated_count += 1
else: else:
# Device doesn't exist locally - we can only sync metadata for # Device doesn't exist locally - create it if it has useful metadata
# devices we've seen, so skip unknown devices if dev.get("assigned_floor") is not None or dev.get("custom_label") or dev.get("is_favorite"):
pass device_type = dev.get("device_type", "bluetooth")
name = dev.get("name") or dev.get("ssid") or device_id
now = datetime.now().isoformat()
cursor.execute("""
INSERT INTO devices (device_id, device_type, name, ssid, manufacturer,
custom_label, assigned_floor, is_favorite, notes,
first_seen, last_seen, total_observations, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?)
""", (
device_id, device_type, name, dev.get("ssid"), dev.get("manufacturer"),
dev.get("custom_label"), dev.get("assigned_floor"),
1 if dev.get("is_favorite") else 0, dev.get("notes"),
now, now, dev.get("updated_at", now)
))
updated_count += 1
conn.commit() conn.commit()
return updated_count return updated_count

View File

@@ -1389,12 +1389,14 @@ def create_app(config: Config | None = None) -> Flask:
peer_id = peer["scanner_id"] peer_id = peer["scanner_id"]
peer_url = peer["url"] peer_url = peer["url"]
try: try:
updated = peer_sync.sync_devices_from_peer(peer_url) pulled = peer_sync.sync_devices_from_peer(peer_url)
peer_sync.push_devices_to_peer(peer_url) push_result = peer_sync.push_devices_to_peer(peer_url)
pushed = push_result.get("updated", 0) if push_result else 0
results.append({ results.append({
"peer_id": peer_id, "peer_id": peer_id,
"status": "success", "status": "success",
"devices_updated": updated "devices_pulled": pulled,
"devices_pushed": pushed
}) })
except Exception as e: except Exception as e:
results.append({ results.append({