UNPKG

@yachteye/signalk-weather-plugin

Version:
79 lines (78 loc) 3.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Weather Controller, to serve the weather API. */ class WeatherController { constructor(app, ow, updater) { /** * Get the current weather for the specified location, and add it to the graph. * @param req * @param res * @param next */ this.currentWeatherAt = async (req, res, next) => { try { const geonameId = parseInt(req.query.geonameId, 10); const lat = parseFloat(req.query.latitude); const lon = parseFloat(req.query.longitude); this.app.debug(`[WeatherController] currentWeatherAt() lat=${lat} lon=${lon} geonameId=${geonameId} `); if (Number.isNaN(lat) || Number.isNaN(lon)) { this.app.debug(`[WeatherController] currentWeatherAt(): No latitude or longitude: ${req.query.latitude} ${req.query.longitude}`); res.status(400).json({ message: 'Incorrect latitude or longitude' }); return; } if (Number.isNaN(geonameId)) { this.app.debug(`[WeatherController] currentWeatherAt(): No Geoname ID: ${req.query.geonameId}`); res.status(400).json({ message: 'Invalid geonameId' }); return; } const currentWeather = await this.openWeather.currentWeatherForPosition(lat, lon); this.weatherUpdater.handleCurrentWeatherResult(currentWeather, `resources.weather.${geonameId}`); res.sendStatus(200); } catch (error) { next(error); } }; /** * Get the forecast weather for the specified location, and add it to the graph. * For now we get the 3-hour forecast data for the next 48 hours, and the daily forecaswt for the requested number of days (default 2). * @param req * @param res * @param next */ this.forecastWeatherAt = async (req, res, next) => { try { const geonameId = parseInt(req.query.geonameId, 10); const lat = parseFloat(req.query.latitude); const lon = parseFloat(req.query.longitude); const days = req.query.days === undefined ? 2 : parseInt(req.query.days, 10); this.app.debug(`[WeatherController] forecastWeatherAt() lat=${lat} lon=${lon} geonameId=${geonameId} days=${days} .`); if (Number.isNaN(lat) || Number.isNaN(lon)) { this.app.debug(`[WeatherController] forecastWeatherAt(): No latitude or longitude: ${req.query.latitude} ${req.query.longitude}`); res.status(400).json({ message: 'Incorrect latitude or longitude' }); return; } if (Number.isNaN(geonameId)) { this.app.debug(`[WeatherController] forecastWeatherAt(): No Geoname ID: ${req.query.geonameId}`); res.status(400).json({ message: 'Invalid geonameId' }); return; } const forecast = await this.openWeather.dailyForecastForPosition(lat, lon, days); const threeHourForecasts = await this.openWeather.threeHourlyForecastForPosition(lat, lon, 16); this.weatherUpdater.handleDailyForecastResult(forecast, `resources.weather.${geonameId}.forecast`); this.weatherUpdater.handleThreeHourForecastResult(threeHourForecasts, `resources.weather.${geonameId}.forecast`, false); res.sendStatus(200); } catch (error) { next(error); } }; console.log('[WeatherController] constructor()'); this.app = app; this.openWeather = ow; this.weatherUpdater = updater; } } exports.default = WeatherController;