signalk-tides
Version:
Tidal predictions for the vessel's position from various online sources.
63 lines (62 loc) • 2.56 kB
JavaScript
;
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: '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 { date = (0, moment_1.default)().subtract(1, "days") } = params;
const endPoint = new URL("https://www.worldtides.info/api/v3");
const position = app.getSelfPath("navigation.position.value");
if (!position)
throw new Error("no position");
endPoint.search = new URLSearchParams({
date: (0, moment_1.default)(date).format("YYYY-MM-DD"),
datum: "CD",
days: "7",
extremes: "true",
key: worldtidesApiKey,
lat: position.latitude,
lon: position.longitude,
}).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: "WorldTides",
position: {
latitude: data.responseLat,
longitude: data.responseLon,
},
},
extremes: data.extremes.map(({ type, dt, height }) => {
return {
type,
value: height,
time: new Date(dt * 1000).toISOString(),
};
}),
};
};
}
};
}