fix: use peer's current position for synced devices
- Load peer scanner positions from /api/peers - Use peer's CURRENT position instead of stored position - Ensures device positions update when peer scanner moves Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -21,6 +21,9 @@ let manualPositions = {}; // { deviceId: { lat_offset, lon_offset } }
|
||||
// Device source scanner info - loaded from database (for synced devices)
|
||||
let deviceSources = {}; // { deviceId: { scanner_id, lat, lon } }
|
||||
|
||||
// Peer scanner positions - loaded from /api/peers (live positions)
|
||||
let peerScanners = {}; // { scanner_id: { lat, lon, floor, name } }
|
||||
|
||||
// Auto-scan state
|
||||
let autoScanEnabled = false;
|
||||
let autoScanPollInterval = null;
|
||||
@@ -1062,9 +1065,10 @@ async function stopAutoScan() {
|
||||
|
||||
// ========== Device Position Functions ==========
|
||||
|
||||
// Load saved device positions and source scanner info from database
|
||||
// Load saved device positions, source scanner info, and peer positions
|
||||
async function loadDevicePositions() {
|
||||
try {
|
||||
// Load device data
|
||||
const response = await fetch('/api/device/floors');
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
@@ -1078,6 +1082,22 @@ async function loadDevicePositions() {
|
||||
console.log('[Sources] Loaded', Object.keys(deviceSources).length, 'device sources');
|
||||
}
|
||||
}
|
||||
|
||||
// Load peer scanner positions (live/current positions)
|
||||
const peersResponse = await fetch('/api/peers');
|
||||
if (peersResponse.ok) {
|
||||
const peersData = await peersResponse.json();
|
||||
peerScanners = {};
|
||||
(peersData.peers || []).forEach(peer => {
|
||||
peerScanners[peer.scanner_id] = {
|
||||
lat: peer.latitude,
|
||||
lon: peer.longitude,
|
||||
floor: peer.floor,
|
||||
name: peer.name
|
||||
};
|
||||
});
|
||||
console.log('[Peers] Loaded', Object.keys(peerScanners).length, 'peer positions');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading device positions:', error);
|
||||
}
|
||||
@@ -1091,15 +1111,26 @@ function getDevicePosition(device, scannerLat, scannerLon, minDistanceM) {
|
||||
const sourceInfo = deviceSources[deviceId];
|
||||
|
||||
// Determine which scanner position to use for this device
|
||||
// If device was synced from another scanner, use that scanner's position
|
||||
// If device was synced from another scanner, use that scanner's CURRENT position
|
||||
let baseLat = scannerLat;
|
||||
let baseLon = scannerLon;
|
||||
let isFromRemoteScanner = false;
|
||||
let sourceScannerId = null;
|
||||
|
||||
if (sourceInfo && sourceInfo.lat != null && sourceInfo.lon != null) {
|
||||
baseLat = sourceInfo.lat;
|
||||
baseLon = sourceInfo.lon;
|
||||
isFromRemoteScanner = true;
|
||||
if (sourceInfo && sourceInfo.scanner_id) {
|
||||
sourceScannerId = sourceInfo.scanner_id;
|
||||
// Use peer's CURRENT position from peerScanners (not stored position)
|
||||
const peerPos = peerScanners[sourceInfo.scanner_id];
|
||||
if (peerPos && peerPos.lat != null && peerPos.lon != null) {
|
||||
baseLat = peerPos.lat;
|
||||
baseLon = peerPos.lon;
|
||||
isFromRemoteScanner = true;
|
||||
} else if (sourceInfo.lat != null && sourceInfo.lon != null) {
|
||||
// Fallback to stored position if peer not in list
|
||||
baseLat = sourceInfo.lat;
|
||||
baseLon = sourceInfo.lon;
|
||||
isFromRemoteScanner = true;
|
||||
}
|
||||
}
|
||||
|
||||
// If device has manual position, use it (relative to source scanner)
|
||||
@@ -1109,7 +1140,7 @@ function getDevicePosition(device, scannerLat, scannerLon, minDistanceM) {
|
||||
lon: baseLon + customPos.lon_offset,
|
||||
isManual: true,
|
||||
isRemoteSource: isFromRemoteScanner,
|
||||
sourceScanner: sourceInfo?.scanner_id || null
|
||||
sourceScanner: sourceScannerId
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1126,7 +1157,7 @@ function getDevicePosition(device, scannerLat, scannerLon, minDistanceM) {
|
||||
lon: baseLon + lonOffset,
|
||||
isManual: false,
|
||||
isRemoteSource: isFromRemoteScanner,
|
||||
sourceScanner: sourceInfo?.scanner_id || null
|
||||
sourceScanner: sourceScannerId
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1232,9 +1263,16 @@ async function onMarkerDragEnd(marker, deviceId, scannerLat, scannerLon) {
|
||||
let baseLat = scannerLat;
|
||||
let baseLon = scannerLon;
|
||||
|
||||
if (sourceInfo && sourceInfo.lat != null && sourceInfo.lon != null) {
|
||||
baseLat = sourceInfo.lat;
|
||||
baseLon = sourceInfo.lon;
|
||||
if (sourceInfo && sourceInfo.scanner_id) {
|
||||
// Use peer's CURRENT position
|
||||
const peerPos = peerScanners[sourceInfo.scanner_id];
|
||||
if (peerPos && peerPos.lat != null && peerPos.lon != null) {
|
||||
baseLat = peerPos.lat;
|
||||
baseLon = peerPos.lon;
|
||||
} else if (sourceInfo.lat != null && sourceInfo.lon != null) {
|
||||
baseLat = sourceInfo.lat;
|
||||
baseLon = sourceInfo.lon;
|
||||
}
|
||||
}
|
||||
|
||||
const latOffset = lngLat.lat - baseLat;
|
||||
|
||||
Reference in New Issue
Block a user