UNPKG

@yachteye/signalk-weather-plugin

Version:
102 lines (101 loc) 4.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Class to use the OpenWeather API. */ class OpenWeather { constructor(code) { this.baseUrlCurrent = 'https://api.openweathermap.org/data/2.5/weather/'; this.baseUrlDailyForecast = 'https://api.openweathermap.org/data/2.5/forecast/daily'; // this.baseUrlHourlyForecast = 'https://api.openweathermap.org/data/2.5/forecast/hourly'; this.baseUrlThreeHourlyForecast = 'https://api.openweathermap.org/data/2.5/forecast'; this.licenseCode = code; } /** * Get the current weather for the specified position, using metric units. * @param lat Latitude (degrees). * @param lon Longitude (degrees). * @returns Promise. */ currentWeatherForPosition(lat, lon) { // lat = 9.4; // lon = -84; const url = new URL(this.baseUrlCurrent); url.searchParams.append('appid', this.licenseCode); url.searchParams.append('units', 'metric'); url.searchParams.append('lang', 'en'); url.searchParams.append('lat', lat.toString(10)); url.searchParams.append('lon', lon.toString(10)); return this.queryForPosition(url); } /** * Get the Daily Forecast (up to 16 Days), using standard units. * @param lat Latitude (degrees). * @param lon Longitude (degrees). * @param dayCount Number of days (including today). * @returns A Promise. */ dailyForecastForPosition(lat, lon, dayCount = 8) { // lat = -31.9514; // Perth Australia. // lon= 115.8617; // Perth Australia. const url = new URL(this.baseUrlDailyForecast); url.searchParams.append('appid', this.licenseCode); url.searchParams.append('units', 'standard'); url.searchParams.append('lang', 'en'); url.searchParams.append('cnt', dayCount.toFixed(0)); // A number of days, which will be returned in the API response url.searchParams.append('lat', lat.toString(10)); url.searchParams.append('lon', lon.toString(10)); return this.queryForPosition(url); } /** * 5 day / 3 hour forecast data (i.e. data in 3 hour steps), using standard units. * @param lat Latitude (degrees). * @param lon Longitude (degrees). * @param count The number of forecasts to get (3-hour intervals). * @returns A Promise. */ threeHourlyForecastForPosition(lat, lon, count) { // lat = 77.62; // Greenland Sea. // lon = -2.11; // Greenland Sea. const url = new URL(this.baseUrlThreeHourlyForecast); url.searchParams.append('appid', this.licenseCode); url.searchParams.append('units', 'standard'); url.searchParams.append('lang', 'en'); url.searchParams.append('cnt', count.toFixed(0)); // Number of forecasts, which will be returned in the API response url.searchParams.append('lat', lat.toString(10)); url.searchParams.append('lon', lon.toString(10)); return this.queryForPosition(url); } queryForPosition(url) { const p = new Promise((resolve, reject) => { fetch(url.toString(), { method: 'GET', cache: 'no-cache', // not supported by node-fetch }) .then((response) => { if (response.ok && response.status === 200) { return response.json(); } else { throw new Error(`queryForPosition() HTTP fetch error, Status: ${response.status}`); } }) .then((json) => { resolve(json); }) .catch((err) => { console.log('queryForPosition() Unable to fetch', err); reject(err); }); }); return p; } } /** Weather condition values, see https://openweathermap.org/weather-conditions#Weather-Condition-Codes-2 . */ OpenWeather.thunderstormConditionValues = [200, 201, 202, 210, 211, 212, 221, 230, 231, 232]; OpenWeather.drizzleConditionValues = [300, 301, 302, 310, 311, 312, 313, 314, 321]; OpenWeather.rainConditionValues = [500, 501, 502, 503, 504, 511, 520, 521, 522, 531]; OpenWeather.snowConditionValues = [600, 601, 602, 611, 612, 613, 615, 616, 620, 621, 622]; OpenWeather.atmosphereConditionValues = [700, 711, 721, 731, 741, 751, 761, 762, 771, 781]; OpenWeather.clearAndCloudConditionValues = [800, 801, 802, 803, 804]; exports.default = OpenWeather;