const API_BASE_URL = 'https://fbbb261497e3.ngrok-free.app'; // Funzione per mostrare messaggi di status function showStatus(message, isSuccess = true) { const statusDiv = document.getElementById('status'); statusDiv.innerHTML = `
${message}
`; } // Funzione per mostrare i risultati function showResult(data) { const resultDiv = document.getElementById('result'); resultDiv.style.display = 'block'; resultDiv.textContent = JSON.stringify(data, null, 2); } // GET: Ottiene dati dal database async function getData() { try { console.log('๐Ÿ“ฅ Richiesta GET in corso...'); showStatus('๐Ÿ“ฅ Richiesta GET in corso...', true); const response = await fetch(`${API_BASE_URL}/api/data`); const data = await response.json(); if (response.ok) { showStatus('โœ… Dati ottenuti con successo!', true); showResult(data); } else { showStatus(`โŒ Errore: ${data.detail || 'Errore sconosciuto'}`, false); } } catch (error) { console.error('Errore GET:', error); showStatus(`โŒ Errore di connessione: ${error.message}`, false); } } // POST: Inserisce dati nel database async function createData() { try { console.log('๐Ÿ“ค Richiesta POST in corso...'); showStatus('๐Ÿ“ค Richiesta POST in corso...', true); const response = await fetch(`${API_BASE_URL}/api/data`, { method: 'POST', headers: { 'Content-Type': 'application/json', } }); const data = await response.json(); if (response.ok) { showStatus('โœ… Dato inserito con successo!', true); showResult(data); } else { showStatus(`โŒ Errore: ${data.detail || 'Errore sconosciuto'}`, false); } } catch (error) { console.error('Errore POST:', error); showStatus(`โŒ Errore di connessione: ${error.message}`, false); } } // POST: Inserisce coordinate nel database async function createCoordinates() { try { console.log('๐Ÿ“ Richiesta POST coordinate in corso...'); showStatus('๐Ÿ“ Richiesta POST coordinate in corso...', true); coordinates = { "coords": [123.12,456.78], } const response = await fetch(`${API_BASE_URL}/geo-access`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(coordinates) }); const data = await response.json(); if (response.ok) { showStatus('โœ… Coordinate inserite con successo!', true); showResult(data); } else { showStatus(`โŒ Errore: ${data.detail || 'Errore sconosciuto'}`, false); } } catch (error) { console.error('Errore POST coordinate:', error); showStatus(`โŒ Errore di connessione: ${error.message}`, false); } } // Check GPS Location async function checkGPSLocation() { try { console.log('๐ŸŒ Richiesta coordinate GPS...'); showStatus('๐ŸŒ Richiesta coordinate GPS...', true); // Ottieni coordinate GPS const position = await getCurrentPosition(); const { latitude, longitude } = position.coords; console.log(`๐Ÿ“ Coordinate GPS: ${latitude}, ${longitude}`); showStatus(`๐Ÿ“ Coordinate GPS ottenute: ${latitude.toFixed(6)}, ${longitude.toFixed(6)}`, true); // Chiama la route geo-access/real const coordinates = { "coords": [latitude, longitude] }; const response = await fetch(`${API_BASE_URL}/geo-access/real`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(coordinates) }); const data = await response.json(); if (response.ok) { const result = data.success ? 'โœ… DENTRO' : 'โŒ FUORI'; showStatus(`${result} - Sei ${data.success ? 'dentro' : 'fuori'} dal raggio di 2000 metri!`, data.success); showResult(data); } else { showStatus(`โŒ Errore: ${data.detail || 'Errore sconosciuto'}`, false); } } catch (error) { console.error('Errore GPS:', error); showStatus(`โŒ Errore GPS: ${error.message}`, false); } } // Funzione per ottenere la posizione GPS function getCurrentPosition() { return new Promise((resolve, reject) => { if (!navigator.geolocation) { reject(new Error('Geolocalizzazione non supportata')); return; } navigator.geolocation.getCurrentPosition( (position) => resolve(position), (error) => reject(new Error(`Errore GPS: ${error.message}`)), { enableHighAccuracy: true, timeout: 10000, maximumAge: 60000 } ); }); } // Send Molinella coordinates async function sendMolinellaCoordinates() { try { console.log('๐Ÿ˜๏ธ Invio coordinate Molinella...'); showStatus('๐Ÿ˜๏ธ Invio coordinate Molinella...', true); // Coordinate hardcoded di Molinella const coordinates = { "coords": [44.61758, 11.66719] }; console.log(`๐Ÿ“ Coordinate Molinella: ${coordinates.coords[0]}, ${coordinates.coords[1]}`); showStatus(`๐Ÿ“ Coordinate Molinella: ${coordinates.coords[0]}, ${coordinates.coords[1]}`, true); // Chiama la route geo-access/real const response = await fetch(`${API_BASE_URL}/geo-access/real`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(coordinates) }); const data = await response.json(); if (response.ok) { const result = data.success ? 'โœ… DENTRO' : 'โŒ FUORI'; showStatus(`${result} - Molinella รจ ${data.success ? 'dentro' : 'fuori'} dal raggio di 50 metri!`, data.success); showResult(data); } else { showStatus(`โŒ Errore: ${data.detail || 'Errore sconosciuto'}`, false); } } catch (error) { console.error('Errore Molinella:', error); showStatus(`โŒ Errore di connessione: ${error.message}`, false); } } // Test di connessione all'avvio window.addEventListener('load', async () => { try { const response = await fetch(`${API_BASE_URL}/`); if (response.ok) { console.log('โœ… Backend connesso!'); } else { console.log('โŒ Backend non raggiungibile'); } } catch (error) { console.log('โŒ Backend non raggiungibile:', error.message); } });