self.onmessage = function(event) {
    const { deviceId, latitude, longitude } = event.data;

    if (!deviceId || !latitude || !longitude) {
        postMessage({ status: "Invalid location data received." });
        return;
    }

    fetch('/api/location', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ deviceId, latitude, longitude })
    })
    .then(response => response.json())
    .then(data => {
        postMessage({ status: "Location sent successfully: " + new Date().toLocaleTimeString() });
    })
    .catch(error => {
        postMessage({ status: "Failed to send location." });
    });
};
