surf-hub/fetcher/index.js
GeorgeWebberley 0804c0aa74
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Fixed script for tidal data
2026-01-28 12:22:12 +01:00

88 lines
3.5 KiB
JavaScript

const { InfluxDB, Point } = require('@influxdata/influxdb-client');
const axios = require('axios');
const token = process.env.INFLUX_TOKEN;
const url = process.env.INFLUX_URL;
const org = process.env.DOCKER_INFLUXDB_INIT_ORG;
const bucket = process.env.DOCKER_INFLUXDB_INIT_BUCKET;
const client = new InfluxDB({ url, token });
const writeApi = client.getWriteApi(org, bucket);
const lat = 51.58;
const lon = -4.29;
const marineUrl = `https://marine-api.open-meteo.com/v1/marine?latitude=${lat}&longitude=${lon}&current=swell_wave_height,swell_wave_period,swell_wave_direction&hourly=sealevel`;
const weatherUrl = `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}&current=wind_speed_10m,wind_direction_10m`;
async function fetchSurfData() {
try {
const [marineRes, weatherRes] = await Promise.all([
axios.get(marineUrl),
axios.get(weatherUrl),
]);
const beachTime = new Intl.DateTimeFormat('en-GB', {
hour: 'numeric',
hour12: false,
timeZone: 'Europe/London'
}).format(new Date());
const currentHour = parseInt(beachTime);
const hourlyTides = marineRes.data.hourly.sealevel;
const currentTideHeight = hourlyTides[currentHour];
const nextHour = (currentHour + 1) % hourlyTides.length;
const nextTideHeight = hourlyTides[nextHour];
const isPushing = nextTideHeight > currentTideHeight;
const tideDirection = isPushing ? "Pushing" : "Pulling";
const swell = marineRes.data.current;
const wind = weatherRes.data.current;
const finalScore = getSurfScore(swell, wind, isPushing);
const point = new Point('surf_conditions')
.tag('location', 'llangennith_beach')
.tag('tide_direction', tideDirection)
.floatField('tide_height', currentTideHeight)
.floatField('swell_height', swell.swell_wave_height)
.floatField('swell_period', swell.swell_wave_period)
.floatField('swell_direction', swell.swell_wave_direction)
.floatField('wind_speed', wind.wind_speed_10m)
.floatField('wind_direction', wind.wind_direction_10m)
.intField('surf_score', finalScore);
writeApi.writePoint(point);
await writeApi.flush();
console.log(`📡 Score: ${finalScore}/10 | Tide: ${tideDirection} | Period: ${swell.swell_wave_period}s`);
} catch (error) {
if (error.code === 'ECONNREFUSED') {
console.error("❌ Database is not ready yet. Retrying in the next cycle...");
} else {
console.error("❌ Fetch error:", error.message);
}
}
}
function getSurfScore(swell, wind, isPushing){
let score = 5;
if (swell.swell_wave_height < 0.5) {
score = 1;
} else {
if (swell.swell_wave_period >= 9 && swell.swell_wave_period <= 13) score += 2;
else if (swell.swell_wave_period > 13) score += 1;
else if (swell.swell_wave_period < 7) score -= 2;
const isOffshore = wind.wind_direction_10m >= 70 && wind.wind_direction_10m <= 110;
const isOnshore = wind.wind_direction_10m > 190 || wind.wind_direction_10m < 20;
if (isOffshore) score += 3;
else if (isOnshore && wind.wind_speed_10m > 12) score -= 4;
if (isPushing) score += 1;
}
return Math.max(1, Math.min(10, score));
}
console.log("⏳ Fetcher started. Waiting 10s for DB initialization...");
setTimeout(fetchSurfData, 10000);
setInterval(fetchSurfData, 1000 * 60 * 60);