This commit is contained in:
parent
72accae032
commit
b1459fcbd0
|
|
@ -11,13 +11,8 @@ const writeApi = client.getWriteApi(org, bucket);
|
||||||
|
|
||||||
async function fetchSurfData() {
|
async function fetchSurfData() {
|
||||||
try {
|
try {
|
||||||
// Change these coordinates to your local beach!
|
|
||||||
// These are for Bondi Beach, Sydney as an example.
|
|
||||||
const lat = -33.89;
|
|
||||||
const lon = 151.27;
|
|
||||||
|
|
||||||
console.log("🌊 Fetching latest swell data...");
|
console.log("🌊 Fetching latest swell data...");
|
||||||
const response = await axios.get(`https://marine-api.open-meteo.com/v1/marine?latitude=${lat}&longitude=${lon}¤t=swell_wave_height,swell_wave_period`);
|
const response = await axios.get(`https://marine-api.open-meteo.com/v1/marine?latitude=-33.89&longitude=151.27¤t=swell_wave_height,swell_wave_period`);
|
||||||
|
|
||||||
const swellHeight = response.data.current.swell_wave_height;
|
const swellHeight = response.data.current.swell_wave_height;
|
||||||
const swellPeriod = response.data.current.swell_wave_period;
|
const swellPeriod = response.data.current.swell_wave_period;
|
||||||
|
|
@ -27,16 +22,26 @@ async function fetchSurfData() {
|
||||||
.floatField('swell_height', swellHeight)
|
.floatField('swell_height', swellHeight)
|
||||||
.floatField('swell_period', swellPeriod);
|
.floatField('swell_period', swellPeriod);
|
||||||
|
|
||||||
|
console.log(`📡 Sending to InfluxDB: ${swellHeight}m...`);
|
||||||
|
|
||||||
|
// Write and explicitly flush
|
||||||
writeApi.writePoint(point);
|
writeApi.writePoint(point);
|
||||||
// Important: Flush the buffer to ensure data is sent
|
|
||||||
await writeApi.flush();
|
await writeApi.flush();
|
||||||
|
|
||||||
console.log(`✅ Sent: ${swellHeight}m at ${swellPeriod}s`);
|
console.log(`✅ Success! Data persisted.`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// If it's a connection error, let's be descriptive
|
||||||
|
if (error.code === 'ECONNREFUSED') {
|
||||||
|
console.error("❌ Database is not ready yet. Retrying in the next cycle...");
|
||||||
|
} else {
|
||||||
console.error("❌ Fetch error:", error.message);
|
console.error("❌ Fetch error:", error.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Fetch every hour
|
// 10 second delay for the very first run to let InfluxDB boot up
|
||||||
|
console.log("⏳ Fetcher started. Waiting 10s for DB initialization...");
|
||||||
|
setTimeout(fetchSurfData, 10000);
|
||||||
|
|
||||||
|
// Then run every hour
|
||||||
setInterval(fetchSurfData, 1000 * 60 * 60);
|
setInterval(fetchSurfData, 1000 * 60 * 60);
|
||||||
fetchSurfData();
|
|
||||||
Loading…
Reference in a new issue