surf-hub/fetcher/index.js
2026-01-27 20:25:52 +01:00

42 lines
1.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);
async function fetchSurfData() {
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...");
const response = await axios.get(`https://marine-api.open-meteo.com/v1/marine?latitude=${lat}&longitude=${lon}&current=swell_wave_height,swell_wave_period`);
const swellHeight = response.data.current.swell_wave_height;
const swellPeriod = response.data.current.swell_wave_period;
const point = new Point('surf_conditions')
.tag('location', 'local_break')
.floatField('swell_height', swellHeight)
.floatField('swell_period', swellPeriod);
writeApi.writePoint(point);
// Important: Flush the buffer to ensure data is sent
await writeApi.flush();
console.log(`✅ Sent: ${swellHeight}m at ${swellPeriod}s`);
} catch (error) {
console.error("❌ Fetch error:", error.message);
}
}
// Fetch every hour
setInterval(fetchSurfData, 1000 * 60 * 60);
fetchSurfData();