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)
|
// Device source scanner info - loaded from database (for synced devices)
|
||||||
let deviceSources = {}; // { deviceId: { scanner_id, lat, lon } }
|
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
|
// Auto-scan state
|
||||||
let autoScanEnabled = false;
|
let autoScanEnabled = false;
|
||||||
let autoScanPollInterval = null;
|
let autoScanPollInterval = null;
|
||||||
@@ -1062,9 +1065,10 @@ async function stopAutoScan() {
|
|||||||
|
|
||||||
// ========== Device Position Functions ==========
|
// ========== 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() {
|
async function loadDevicePositions() {
|
||||||
try {
|
try {
|
||||||
|
// Load device data
|
||||||
const response = await fetch('/api/device/floors');
|
const response = await fetch('/api/device/floors');
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
@@ -1078,6 +1082,22 @@ async function loadDevicePositions() {
|
|||||||
console.log('[Sources] Loaded', Object.keys(deviceSources).length, 'device sources');
|
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) {
|
} catch (error) {
|
||||||
console.error('Error loading device positions:', error);
|
console.error('Error loading device positions:', error);
|
||||||
}
|
}
|
||||||
@@ -1091,15 +1111,26 @@ function getDevicePosition(device, scannerLat, scannerLon, minDistanceM) {
|
|||||||
const sourceInfo = deviceSources[deviceId];
|
const sourceInfo = deviceSources[deviceId];
|
||||||
|
|
||||||
// Determine which scanner position to use for this device
|
// 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 baseLat = scannerLat;
|
||||||
let baseLon = scannerLon;
|
let baseLon = scannerLon;
|
||||||
let isFromRemoteScanner = false;
|
let isFromRemoteScanner = false;
|
||||||
|
let sourceScannerId = null;
|
||||||
|
|
||||||
if (sourceInfo && sourceInfo.lat != null && sourceInfo.lon != null) {
|
if (sourceInfo && sourceInfo.scanner_id) {
|
||||||
baseLat = sourceInfo.lat;
|
sourceScannerId = sourceInfo.scanner_id;
|
||||||
baseLon = sourceInfo.lon;
|
// Use peer's CURRENT position from peerScanners (not stored position)
|
||||||
isFromRemoteScanner = true;
|
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)
|
// 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,
|
lon: baseLon + customPos.lon_offset,
|
||||||
isManual: true,
|
isManual: true,
|
||||||
isRemoteSource: isFromRemoteScanner,
|
isRemoteSource: isFromRemoteScanner,
|
||||||
sourceScanner: sourceInfo?.scanner_id || null
|
sourceScanner: sourceScannerId
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1126,7 +1157,7 @@ function getDevicePosition(device, scannerLat, scannerLon, minDistanceM) {
|
|||||||
lon: baseLon + lonOffset,
|
lon: baseLon + lonOffset,
|
||||||
isManual: false,
|
isManual: false,
|
||||||
isRemoteSource: isFromRemoteScanner,
|
isRemoteSource: isFromRemoteScanner,
|
||||||
sourceScanner: sourceInfo?.scanner_id || null
|
sourceScanner: sourceScannerId
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1232,9 +1263,16 @@ async function onMarkerDragEnd(marker, deviceId, scannerLat, scannerLon) {
|
|||||||
let baseLat = scannerLat;
|
let baseLat = scannerLat;
|
||||||
let baseLon = scannerLon;
|
let baseLon = scannerLon;
|
||||||
|
|
||||||
if (sourceInfo && sourceInfo.lat != null && sourceInfo.lon != null) {
|
if (sourceInfo && sourceInfo.scanner_id) {
|
||||||
baseLat = sourceInfo.lat;
|
// Use peer's CURRENT position
|
||||||
baseLon = sourceInfo.lon;
|
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;
|
const latOffset = lngLat.lat - baseLat;
|
||||||
|
|||||||
Reference in New Issue
Block a user