import { DumonGeolocation } from 'dumon-geolocation'; const logArea = document.getElementById('logArea'); let posListenerHandle = null; let gnssListenerHandle = null; function appendLog(title, data) { const timestamp = new Date().toLocaleTimeString(); const formatted = `[${timestamp}] ${title}\n${JSON.stringify(data, null, 2)}\n\n`; logArea.textContent = formatted + logArea.textContent; } function clearLog() { logArea.textContent = ''; } function readNumber(id) { const el = document.getElementById(id); if (!el) return undefined; const v = el.value.trim(); if (v === '') return undefined; const n = Number(v); return Number.isFinite(n) ? n : undefined; } function readBool(id) { const el = document.getElementById(id); return !!el?.checked; } async function applyOptionsFromForm() { const options = {}; // Booleans if (document.getElementById('optEnableForwardPrediction')) options.enableForwardPrediction = readBool('optEnableForwardPrediction'); if (document.getElementById('optEmitGnssStatus')) options.emitGnssStatus = readBool('optEmitGnssStatus'); if (document.getElementById('optEnableWifiRtt')) options.enableWifiRtt = readBool('optEnableWifiRtt'); if (document.getElementById('optEnableLogging')) options.enableLogging = readBool('optEnableLogging'); if (document.getElementById('optSuppressMocked')) options.suppressMockedUpdates = readBool('optSuppressMocked'); if (document.getElementById('optKeepScreenOn')) options.keepScreenOn = readBool('optKeepScreenOn'); // Numbers const emitDebounceMs = readNumber('optEmitDebounceMs'); const drivingEmitIntervalMs = readNumber('optDrivingEmitIntervalMs'); const wifiScanIntervalMs = readNumber('optWifiScanIntervalMs'); const distanceThresholdMeters = readNumber('optDistanceThreshold'); const speedChangeThreshold = readNumber('optSpeedThreshold'); const directionChangeThreshold = readNumber('optDirectionThreshold'); const maxPredictionSeconds = readNumber('optMaxPrediction'); if (emitDebounceMs !== undefined) options.emitDebounceMs = emitDebounceMs; if (drivingEmitIntervalMs !== undefined) options.drivingEmitIntervalMs = drivingEmitIntervalMs; if (wifiScanIntervalMs !== undefined) options.wifiScanIntervalMs = wifiScanIntervalMs; if (distanceThresholdMeters !== undefined) options.distanceThresholdMeters = distanceThresholdMeters; if (speedChangeThreshold !== undefined) options.speedChangeThreshold = speedChangeThreshold; if (directionChangeThreshold !== undefined) options.directionChangeThreshold = directionChangeThreshold; if (maxPredictionSeconds !== undefined) options.maxPredictionSeconds = maxPredictionSeconds; await DumonGeolocation.setOptions(options); appendLog('setOptions', options); // Manage GNSS status listener dynamically const wantGnss = !!options.emitGnssStatus; if (wantGnss && !gnssListenerHandle) { gnssListenerHandle = DumonGeolocation.addListener('onGnssStatus', (data) => { appendLog('onGnssStatus', data); }); } else if (!wantGnss && gnssListenerHandle) { await gnssListenerHandle.remove(); gnssListenerHandle = null; } } async function requestPermissions() { try { const perm = await DumonGeolocation.checkAndRequestPermissions(); appendLog('permissions', perm); } catch (err) { appendLog('permissions', { error: err.message }); } } async function startGeolocation() { if (!posListenerHandle) { posListenerHandle = DumonGeolocation.addListener('onPositionUpdate', (data) => { appendLog('onPositionUpdate', data); }); } try { await DumonGeolocation.startPositioning(); appendLog('startPositioning', { success: true }); } catch (err) { appendLog('startPositioning', { error: err.message }); } } async function stopGeolocation() { try { await DumonGeolocation.stopPositioning(); appendLog('stopPositioning', { success: true }); } catch (err) { appendLog('stopPositioning', { error: err.message }); } } async function getLatestPosition() { try { const data = await DumonGeolocation.getLatestPosition(); appendLog('getLatestPosition', data); } catch (err) { appendLog('getLatestPosition', { error: err.message }); } } async function setDrivingMode() { try { await DumonGeolocation.setGpsMode({ mode: 'driving' }); appendLog('setGpsMode', { mode: 'driving' }); } catch (err) { appendLog('setGpsMode', { error: err.message }); } } async function setNormalMode() { try { await DumonGeolocation.setGpsMode({ mode: 'normal' }); appendLog('setGpsMode', { mode: 'normal' }); } catch (err) { appendLog('setGpsMode', { error: err.message }); } } async function getGnssStatus() { try { const data = await DumonGeolocation.getGnssStatus(); appendLog('getGnssStatus', data); } catch (err) { appendLog('getGnssStatus', { error: err.message }); } } async function getLocationServicesStatus() { try { const data = await DumonGeolocation.getLocationServicesStatus(); appendLog('getLocationServicesStatus', data); } catch (err) { appendLog('getLocationServicesStatus', { error: err.message }); } } window.addEventListener('DOMContentLoaded', async () => { document.getElementById('startButton').addEventListener('click', startGeolocation); document.getElementById('stopButton').addEventListener('click', stopGeolocation); document.getElementById('getLatestButton').addEventListener('click', getLatestPosition); document.getElementById('permButton').addEventListener('click', requestPermissions); document.getElementById('drivingBtn').addEventListener('click', setDrivingMode); document.getElementById('normalBtn').addEventListener('click', setNormalMode); document.getElementById('applyOptionsButton').addEventListener('click', applyOptionsFromForm); document.getElementById('getGnssStatusButton').addEventListener('click', getGnssStatus); document.getElementById('getLocationServicesStatusButton').addEventListener('click', getLocationServicesStatus); document.getElementById('clearLogButton').addEventListener('click', clearLog); // Apply a reasonable default UI config for E2E await DumonGeolocation.configureEdgeToEdge({ bgColor: '#ffffff', style: 'DARK', overlay: false, }); });