Fixed script by removing tidal data
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
b5238e238e
commit
0554841ee3
|
|
@ -11,37 +11,25 @@ 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}¤t=swell_wave_height,swell_wave_period,swell_wave_direction&hourly=sealevel`;
|
||||
|
||||
const marineUrl = `https://marine-api.open-meteo.com/v1/marine?latitude=${lat}&longitude=${lon}¤t=swell_wave_height,swell_wave_period,swell_wave_direction`;
|
||||
const weatherUrl = `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}¤t=wind_speed_10m,wind_direction_10m`;
|
||||
|
||||
async function fetchSurfData() {
|
||||
try {
|
||||
const [marineRes, weatherRes] = await Promise.all([
|
||||
console.log("🌊 Fetching latest surf conditions...");
|
||||
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 finalScore = getSurfScore(swell, wind);
|
||||
|
||||
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)
|
||||
|
|
@ -51,41 +39,40 @@ async function fetchSurfData() {
|
|||
|
||||
writeApi.writePoint(point);
|
||||
await writeApi.flush();
|
||||
console.log(`📡 Score: ${finalScore}/10 | Tide: ${tideDirection} | Period: ${swell.swell_wave_period}s`);
|
||||
|
||||
console.log(`✅ Success! Score: ${finalScore}/10 | Swell: ${swell.swell_wave_height}m @ ${swell.swell_wave_period}s | Wind: ${wind.wind_speed_10m}km/h`);
|
||||
} catch (error) {
|
||||
if (error.code === 'ECONNREFUSED') {
|
||||
console.error("❌ Database is not ready yet. Retrying in the next cycle...");
|
||||
} else if (error.response) {
|
||||
console.error("Data:", error.response.data);
|
||||
console.error("Status:", error.response.status);
|
||||
console.error("❌ API Error:", error.response.status, error.response.data);
|
||||
} else {
|
||||
console.error("Error:", error.message);
|
||||
console.error("❌ Error:", error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getSurfScore(swell, wind, isPushing){
|
||||
let score = 5;
|
||||
function getSurfScore(swell, wind) {
|
||||
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;
|
||||
if (swell.swell_wave_height < 0.5) return 1;
|
||||
|
||||
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 (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;
|
||||
|
||||
if (isOffshore) score += 3;
|
||||
else if (isOnshore && wind.wind_speed_10m > 12) score -= 4;
|
||||
|
||||
if (isPushing) score += 1;
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
Loading…
Reference in a new issue