UNPKG

signalk-tides

Version:

Tidal predictions for the vessel's position from various online sources.

51 lines (50 loc) 2.13 kB
import moment from 'moment'; export default function (app) { return { id: 'stormglass', title: 'StormGlass.io', properties: { stormglassApiKey: { type: 'string', title: 'StormGlass.io API key' } }, start({ stormglassApiKey } = {}) { app.debug("Using StormGlass.io API"); return async ({ position, date = moment().subtract(1, "days").toISOString() }) => { const endPoint = new URL("https://api.stormglass.io/v2/tide/extremes/point"); endPoint.search = new URLSearchParams({ start: moment(date).format("YYYY-MM-DD"), end: moment(date).add(7, "days").format("YYYY-MM-DD"), // datum: "CD", lat: position.latitude.toString(), lng: position.longitude.toString() }).toString(); app.debug("Fetching StormGlass.io: " + endPoint.toString()); const res = await fetch(endPoint, { headers: { Authorization: stormglassApiKey ?? '' }, }); if (!res.ok) throw new Error('Failed to fetch StormGlass.io: ' + res.statusText); const data = await res.json(); app.debug(JSON.stringify(data, null, 2)); return { station: { name: `${data.meta.station.name} (${data.meta.station.source})`, position: { latitude: data.meta.station.lat, longitude: data.meta.station.lng, }, }, extremes: data.data.map(({ type, time, height }) => { return { type: type === "high" ? "High" : "Low", value: height, time: new Date(time).toISOString(), }; }), }; }; } }; }