import db from '../lib/db'; import * as satellite from 'satellite.js'; const MY_LAT = 55.6683; const MY_LON = 12.5333; const MY_ALT = 0; // Altitude in kilometers async function updateISSData() { console.log("🛰️ Fetching TLE data..."); const response = await fetch('https://celestrak.org/NORAD/elements/gp.php?CATNR=25544&FORMAT=tle'); const data = await response.text(); const lines = data.split('\n').filter(line => line.trim().length > 0); const line1 = lines[1]; const line2 = lines[2]; // 1. Initialize satellite record from TLE const satrec = satellite.twoline2satrec(line1, line2); // 2. Observer coordinates in radians const observerGd = { longitude: satellite.degreesToRadians(MY_LON), latitude: satellite.degreesToRadians(MY_LAT), height: MY_ALT }; let nextPassDate: Date | null = null; const now = new Date(); // 3. Look ahead for the next 24 hours (1440 minutes) for (let i = 0; i < 1440; i++) { const checkTime = new Date(now.getTime() + i * 60000); const positionAndVelocity = satellite.propagate(satrec, checkTime); const gmst = satellite.gstime(checkTime); // Calculate Look Angles (Azimuth, Elevation, Range) if (typeof positionAndVelocity?.position !== 'boolean') { const positionEcf = satellite.eciToEcf(positionAndVelocity!.position, gmst); const lookAngles = satellite.ecfToLookAngles(observerGd, positionEcf); const elevation = satellite.radiansToDegrees(lookAngles.elevation); // If elevation > 0, the ISS is above your horizon! if (elevation > 0) { nextPassDate = checkTime; break; } } } if (nextPassDate) { const upsert = db.prepare('INSERT INTO iss_passes (id, pass_time) VALUES (1, ?) ON CONFLICT(id) DO UPDATE SET pass_time=excluded.pass_time'); upsert.run(nextPassDate.toISOString()); console.log(`✅ Real orbital pass found: ${nextPassDate.toISOString()}`); } else { console.log("❌ No pass found in the next 24 hours (check your TLE or coordinates)."); } } updateISSData().catch(console.error);