mission-control/scripts/update-space.ts
2026-01-28 20:11:44 +01:00

119 lines
3.9 KiB
TypeScript

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;
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];
const satrec = satellite.twoline2satrec(line1, line2);
const observerGd = {
longitude: satellite.degreesToRadians(MY_LON),
latitude: satellite.degreesToRadians(MY_LAT),
height: MY_ALT
};
const passesFound: { start: Date, end: Date }[] = [];
const now = new Date();
const searchTime = new Date(now.getTime());
const maxSearchMinutes = 2880;
let minutesSearched = 0;
while (passesFound.length < 2 && minutesSearched < maxSearchMinutes) {
const checkTime = new Date(searchTime.getTime() + (minutesSearched * 60000));
const positionAndVelocity = satellite.propagate(satrec, checkTime);
const gmst = satellite.gstime(checkTime);
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) {
const aos = checkTime;
let los = new Date(aos.getTime() + 5 * 60000);
for (let j = 1; j < 20; j++) {
const exitTime = new Date(aos.getTime() + j * 60000);
const pos = satellite.propagate(satrec, exitTime);
const gmstExit = satellite.gstime(exitTime);
if (typeof pos?.position !== 'boolean') {
const lookExit = satellite.ecfToLookAngles(observerGd, satellite.eciToEcf(pos!.position, gmstExit));
if (satellite.radiansToDegrees(lookExit.elevation) < 0) {
los = exitTime;
break;
}
}
}
passesFound.push({ start: aos, end: los });
minutesSearched += 90;
continue;
}
}
minutesSearched++;
}
db.prepare('DELETE FROM iss_passes').run();
const insert = db.prepare('INSERT INTO iss_passes (pass_time, end_time) VALUES (?, ?)');
for (const pass of passesFound) {
insert.run(pass.start.toISOString(), pass.end.toISOString());
}
console.log(`✅ Stored ${passesFound.length} future passes with start and end times.`);
}
// Add this to your worker script
function getNextMoonPhase() {
const LUNAR_CYCLE = 29.53059; // Days
const KNOWN_NEW_MOON = new Date('2024-01-11T11:57:00Z'); // A reference New Moon
const now = new Date();
const msSinceReference = now.getTime() - KNOWN_NEW_MOON.getTime();
const daysSinceReference = msSinceReference / (1000 * 60 * 60 * 24);
const currentCycleProgress = daysSinceReference % LUNAR_CYCLE;
const daysToFullMoon = (LUNAR_CYCLE / 2) - currentCycleProgress;
const daysToNewMoon = LUNAR_CYCLE - currentCycleProgress;
// If we've passed the Full Moon in this cycle, count to New Moon
let targetDate, title;
if (daysToFullMoon > 0) {
targetDate = new Date(now.getTime() + daysToFullMoon * 86400000);
title = "Next Full Moon";
} else {
targetDate = new Date(now.getTime() + daysToNewMoon * 86400000);
title = "Next New Moon";
}
return { title, targetDate };
}
async function updateAllData() {
await updateISSData();
const moon = getNextMoonPhase();
const upsertMoon = db.prepare(`
INSERT INTO global_events (id, title, event_time)
VALUES ('moon_phase', ?, ?)
ON CONFLICT(id) DO UPDATE SET title=excluded.title, event_time=excluded.event_time
`);
upsertMoon.run(moon.title, moon.targetDate.toISOString());
console.log(`🌙 Updated Moon Phase: ${moon.title}`);
}
updateAllData().catch(console.error);