@@ -15,6 +15,8 @@ const GPS_FRESHNESS_BUFFER_MS = 5000; // Buffer time for GPS freshness
1515const GPS_ACCURACY_THRESHOLD_M = 100 ; // Maximum acceptable GPS accuracy in meters
1616const MESHMAPPER_DELAY_MS = 7000 ; // Delay MeshMapper API call by 7 seconds
1717const COOLDOWN_MS = 7000 ; // Cooldown period for manual ping and auto toggle
18+ const STATUS_UPDATE_DELAY_MS = 100 ; // Brief delay to ensure "Ping sent" status is visible
19+ const MAP_REFRESH_DELAY_MS = 1000 ; // Delay after API post to ensure backend updated
1820const WARDROVE_KEY = new Uint8Array ( [
1921 0x40 , 0x76 , 0xC3 , 0x15 , 0xC1 , 0xEF , 0x38 , 0x5F ,
2022 0xA9 , 0x3F , 0x06 , 0x60 , 0x27 , 0x32 , 0x0F , 0xE5
@@ -110,7 +112,7 @@ function buildCoverageEmbedUrl(lat, lon) {
110112 return `${ base } &lat=${ encodeURIComponent ( lat ) } &lon=${ encodeURIComponent ( lon ) } ` ;
111113}
112114let coverageRefreshTimer = null ;
113- function scheduleCoverageRefresh ( lat , lon ) {
115+ function scheduleCoverageRefresh ( lat , lon , delayMs = 0 ) {
114116 if ( ! coverageFrameEl ) return ;
115117
116118 if ( coverageRefreshTimer ) clearTimeout ( coverageRefreshTimer ) ;
@@ -119,7 +121,7 @@ function scheduleCoverageRefresh(lat, lon) {
119121 const url = buildCoverageEmbedUrl ( lat , lon ) ;
120122 console . log ( "Coverage iframe URL:" , url ) ;
121123 coverageFrameEl . src = url ;
122- } , 5000 ) ;
124+ } , delayMs ) ;
123125}
124126function setConnectButton ( connected ) {
125127 if ( ! connectBtn ) return ;
@@ -464,23 +466,48 @@ async function sendPing(manual = false) {
464466 // Start cooldown period after successful ping
465467 startCooldown ( ) ;
466468
469+ setStatus ( manual ? "Ping sent" : "Auto ping sent" , "text-emerald-300" ) ;
470+
467471 // Schedule MeshMapper API call with 7-second delay (non-blocking)
468472 // Clear any existing timer first
469473 if ( state . meshMapperTimer ) {
470474 clearTimeout ( state . meshMapperTimer ) ;
471475 }
472- state . meshMapperTimer = setTimeout ( ( ) => {
473- postToMeshMapperAPI ( lat , lon ) ;
476+
477+ // Update status to show we're waiting to post to API
478+ setTimeout ( ( ) => {
479+ if ( state . connection ) {
480+ setStatus ( "Waiting to post to API" , "text-sky-300" ) ;
481+ }
482+ } , STATUS_UPDATE_DELAY_MS ) ;
483+
484+ state . meshMapperTimer = setTimeout ( async ( ) => {
485+ // Capture accuracy in closure to ensure it's available in nested callback
486+ const capturedAccuracy = accuracy ;
487+
488+ try {
489+ await postToMeshMapperAPI ( lat , lon ) ;
490+ } catch ( error ) {
491+ console . error ( "MeshMapper API post failed:" , error ) ;
492+ // Continue with map refresh and status update even if API fails
493+ }
494+
495+ // Update map after API post to ensure backend updated
496+ setTimeout ( ( ) => {
497+ if ( capturedAccuracy && capturedAccuracy < GPS_ACCURACY_THRESHOLD_M ) {
498+ scheduleCoverageRefresh ( lat , lon ) ;
499+ }
500+
501+ // Set status to idle after map update
502+ if ( state . connection ) {
503+ setStatus ( "Idle" , "text-slate-300" ) ;
504+ }
505+ } , MAP_REFRESH_DELAY_MS ) ;
506+
474507 state . meshMapperTimer = null ;
475508 } , MESHMAPPER_DELAY_MS ) ;
476-
477- // Only refresh coverage iframe if GPS accuracy is good
478- if ( accuracy && accuracy < GPS_ACCURACY_THRESHOLD_M ) {
479- scheduleCoverageRefresh ( lat , lon ) ;
480- }
481-
509+
482510 const nowStr = new Date ( ) . toLocaleString ( ) ;
483- setStatus ( manual ? "Ping sent" : "Auto ping sent" , "text-emerald-300" ) ;
484511 if ( lastPingEl ) lastPingEl . textContent = `${ nowStr } — ${ payload } ` ;
485512
486513 // Session log
0 commit comments