signalk-tides
Version:
Tidal predictions for the vessel's position from various online sources.
54 lines (53 loc) • 2.16 kB
JavaScript
import moment from 'moment';
export default function (app) {
return {
id: 'WorldTides.info',
title: 'worldtides.info',
properties: {
worldtidesApiKey: {
type: 'string',
title: 'worldtides.info API key'
}
},
start({ worldtidesApiKey = '' } = {}) {
app.debug("Using WorldTides API");
return async (params) => {
const { position, date = moment().subtract(1, "days") } = params;
const endPoint = new URL("https://www.worldtides.info/api/v3");
endPoint.search = new URLSearchParams({
date: moment(date).format("YYYY-MM-DD"),
datum: "CD",
days: "7",
extremes: "true",
key: worldtidesApiKey,
lat: position.latitude.toString(),
lon: position.longitude.toString(),
}).toString();
app.debug("Fetching worldtides: " + endPoint.toString());
const res = await fetch(endPoint);
if (!res.ok)
throw new Error('Failed to fetch worldtides: ' + res.statusText);
const data = await res.json();
app.debug(JSON.stringify(data, null, 2));
if (data.status != 200)
throw new Error("worldtides data: " + data.error ? data.error : "none");
return {
station: {
name: `${data.station} (${data.atlas})`,
position: {
latitude: data.responseLat,
longitude: data.responseLon,
},
},
extremes: data.extremes.map(({ type, dt, height }) => {
return {
type,
value: height,
time: new Date(dt * 1000).toISOString(),
};
}),
};
};
}
};
}