UNPKG

signalk-tides

Version:

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

61 lines (60 loc) 2.58 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; const moment_1 = __importDefault(require("moment")); function default_1(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 (params = {}) => { const { date = (0, moment_1.default)().subtract(1, "days").toISOString() } = params; const endPoint = new URL("https://api.stormglass.io/v2/tide/extremes/point"); const position = app.getSelfPath("navigation.position.value"); if (!position) throw new Error("no position"); endPoint.search = new URLSearchParams({ start: (0, moment_1.default)(date).format("YYYY-MM-DD"), end: (0, moment_1.default)(date).add(7, "days").format("YYYY-MM-DD"), // datum: "CD", lat: position.latitude, lng: position.longitude, }).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, 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(), }; }), }; }; } }; }