Fix: GPS de "De Camino" solo se enviaba una vez, ahora se envía cada 25s
This commit is contained in:
@@ -481,6 +481,12 @@
|
|||||||
let selectedDateStr = "";
|
let selectedDateStr = "";
|
||||||
let activeNotifications = []; // Variable global para guardar IDs con mensajes
|
let activeNotifications = []; // Variable global para guardar IDs con mensajes
|
||||||
|
|
||||||
|
// 🛰️ SEGUIMIENTO GPS EN VIVO MIENTRAS EL AVISO ESTÁ "DE CAMINO"
|
||||||
|
let gpsPingIntervalId = null;
|
||||||
|
const GPS_PING_KEY = 'active_camino_service';
|
||||||
|
const GPS_PING_INTERVAL_MS = 25000; // cada 25s, mientras la página siga abierta
|
||||||
|
const GPS_PING_MAX_MS = 3 * 60 * 60 * 1000; // corte de seguridad: 3h sin cambiar de estado
|
||||||
|
|
||||||
async function applyTheme() {
|
async function applyTheme() {
|
||||||
try {
|
try {
|
||||||
let theme = JSON.parse(localStorage.getItem('app_theme'));
|
let theme = JSON.parse(localStorage.getItem('app_theme'));
|
||||||
@@ -540,6 +546,15 @@
|
|||||||
// Comprobar notificaciones al cargar y cada 30 segundos
|
// Comprobar notificaciones al cargar y cada 30 segundos
|
||||||
checkNotifications();
|
checkNotifications();
|
||||||
setInterval(checkNotifications, 30000);
|
setInterval(checkNotifications, 30000);
|
||||||
|
|
||||||
|
// Si veníamos de marcar "De Camino" y se recargó la página (o se reabrió la app),
|
||||||
|
// retomamos el envío periódico de posición para ese mismo aviso.
|
||||||
|
try {
|
||||||
|
const tracked = JSON.parse(localStorage.getItem(GPS_PING_KEY) || 'null');
|
||||||
|
if (tracked && tracked.id && (Date.now() - tracked.startedAt) < GPS_PING_MAX_MS) {
|
||||||
|
startGpsTracking(tracked.id);
|
||||||
|
}
|
||||||
|
} catch (e) { /* localStorage corrupto o no disponible: seguimos sin reanudar */ }
|
||||||
});
|
});
|
||||||
|
|
||||||
let lastUnreadCount = 0; // Para saber si el número ha subido y sonar
|
let lastUnreadCount = 0; // Para saber si el número ha subido y sonar
|
||||||
@@ -1506,14 +1521,13 @@
|
|||||||
if(res.ok) {
|
if(res.ok) {
|
||||||
showToast("¡Actualizado!");
|
showToast("¡Actualizado!");
|
||||||
|
|
||||||
if (action === 'camino' && navigator.geolocation) {
|
if (action === 'camino') {
|
||||||
navigator.geolocation.getCurrentPosition(async (pos) => {
|
// Arranca (o retoma) el envío periódico de posición mientras dure el desplazamiento
|
||||||
await fetch(`${API_URL}/services/${currentServiceId}/location`, {
|
startGpsTracking(currentServiceId);
|
||||||
method: 'POST',
|
} else {
|
||||||
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
|
// Cualquier otro cambio de estado (p.ej. "Trabajando") significa que ya ha llegado:
|
||||||
body: JSON.stringify({ lat: pos.coords.latitude, lng: pos.coords.longitude })
|
// paramos el seguimiento GPS de este aviso si estaba activo.
|
||||||
});
|
stopGpsTrackingIfMatches(currentServiceId);
|
||||||
}, () => {}, { enableHighAccuracy: false, timeout: 10000 });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
closeModal();
|
closeModal();
|
||||||
@@ -1522,6 +1536,55 @@
|
|||||||
} catch (e) { alert("Error al actualizar"); }
|
} catch (e) { alert("Error al actualizar"); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Envía una posición puntual del operario para el aviso indicado.
|
||||||
|
async function sendLocationPing(serviceId) {
|
||||||
|
if (!navigator.geolocation) return;
|
||||||
|
navigator.geolocation.getCurrentPosition(async (pos) => {
|
||||||
|
try {
|
||||||
|
await fetch(`${API_URL}/services/${serviceId}/location`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
|
||||||
|
body: JSON.stringify({ lat: pos.coords.latitude, lng: pos.coords.longitude })
|
||||||
|
});
|
||||||
|
} catch (e) { /* fallo puntual de red: lo reintentamos en el siguiente ciclo */ }
|
||||||
|
}, () => { /* permiso denegado o GPS lento: lo reintentamos en el siguiente ciclo */ }, { enableHighAccuracy: false, timeout: 10000, maximumAge: 15000 });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Arranca el ciclo de posición en vivo para un aviso "De Camino" (o lo retoma si ya estaba en curso).
|
||||||
|
function startGpsTracking(serviceId) {
|
||||||
|
if (gpsPingIntervalId) clearInterval(gpsPingIntervalId);
|
||||||
|
|
||||||
|
let tracked = null;
|
||||||
|
try { tracked = JSON.parse(localStorage.getItem(GPS_PING_KEY) || 'null'); } catch (e) {}
|
||||||
|
const startedAt = (tracked && tracked.id === serviceId) ? tracked.startedAt : Date.now();
|
||||||
|
localStorage.setItem(GPS_PING_KEY, JSON.stringify({ id: serviceId, startedAt }));
|
||||||
|
|
||||||
|
sendLocationPing(serviceId); // envío inmediato, sin esperar al primer ciclo
|
||||||
|
|
||||||
|
gpsPingIntervalId = setInterval(() => {
|
||||||
|
let cur = null;
|
||||||
|
try { cur = JSON.parse(localStorage.getItem(GPS_PING_KEY) || 'null'); } catch (e) {}
|
||||||
|
if (!cur || cur.id !== serviceId || (Date.now() - cur.startedAt) > GPS_PING_MAX_MS) {
|
||||||
|
stopGpsTracking();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sendLocationPing(serviceId);
|
||||||
|
}, GPS_PING_INTERVAL_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopGpsTracking() {
|
||||||
|
if (gpsPingIntervalId) { clearInterval(gpsPingIntervalId); gpsPingIntervalId = null; }
|
||||||
|
localStorage.removeItem(GPS_PING_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Para el seguimiento solo si el aviso que se está rastreando es este mismo (evita cortar
|
||||||
|
// el rastreo de OTRO aviso "de camino" si el operario cambia el estado de uno distinto).
|
||||||
|
function stopGpsTrackingIfMatches(serviceId) {
|
||||||
|
let cur = null;
|
||||||
|
try { cur = JSON.parse(localStorage.getItem(GPS_PING_KEY) || 'null'); } catch (e) {}
|
||||||
|
if (cur && cur.id === serviceId) stopGpsTracking();
|
||||||
|
}
|
||||||
|
|
||||||
async function askToFinish() {
|
async function askToFinish() {
|
||||||
if(!currentServiceId) return;
|
if(!currentServiceId) return;
|
||||||
|
|
||||||
@@ -1542,6 +1605,7 @@
|
|||||||
|
|
||||||
if(res.ok) {
|
if(res.ok) {
|
||||||
showToast(sendSurvey ? "Expediente finalizado y encuesta enviada." : "Expediente finalizado.");
|
showToast(sendSurvey ? "Expediente finalizado y encuesta enviada." : "Expediente finalizado.");
|
||||||
|
stopGpsTrackingIfMatches(currentServiceId);
|
||||||
closeModal();
|
closeModal();
|
||||||
refreshData();
|
refreshData();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user