Add endpoint for getting weather data

This commit is contained in:
Samer Albahra
2019-02-28 09:16:23 -06:00
parent 3ec31ae6e3
commit 7b5939739f
3 changed files with 35 additions and 12 deletions

View File

@@ -114,7 +114,7 @@ function getOWMWeatherData( location, callback ) {
weather.humidity = weather.humidity / maxCount;
weather.wind = weather.wind / maxCount;
weather.precip = weather.precip / maxCount;
weather.icon = data.list[0].weather[0].icon;
location = location.join( "," );
@@ -225,6 +225,33 @@ function checkRainStatus( weather ) {
return false;
}
exports.showWeatherData = function( req, res ) {
let location = req.query.loc;
if ( filters.gps.test( location ) ) {
// Handle GPS coordinates by storing each coordinate in an array
location = location.split( "," );
location = [ parseFloat( location[ 0 ] ), parseFloat( location[ 1 ] ) ];
// Continue with the weather request
getOWMWeatherData( location, ( data ) => res.json( data ) );
} else {
// Attempt to resolve provided location to GPS coordinates when it does not match
// a GPS coordinate or Weather Underground location using Weather Underground autocomplete
resolveCoordinates( location, function( result ) {
if ( result === false ) {
res.send( "Error: Unable to resolve location" );
return;
}
location = result;
getOWMWeatherData( location, ( data ) => res.json( data ) );
} );
}
}
// API Handler when using the weatherX.py where X represents the
// adjustment method which is encoded to also carry the watering
// restriction and therefore must be decoded
@@ -324,17 +351,10 @@ exports.getWeather = function( req, res ) {
}
// Parse location string
if ( weatherUndergroundKey ) {
if ( filters.pws.test( location ) ) {
// The current weather script uses Weather Underground and during the transition period
// both will be supported and users who provide a Weather Underground API key will continue
// using Weather Underground until The Weather Service becomes the default API
getWeatherUndergroundData( location, weatherUndergroundKey, finishRequest );
} else if ( filters.pws.test( location ) ) {
// If no key is provided for Weather Underground then the PWS or ICAO cannot be resolved
res.send( "Error: Weather Underground key required when using PWS or ICAO location." );
// Weather Underground is discontinued and PWS or ICAO cannot be resolved
res.send( "Error: Weather Underground is discontinued." );
return;
} else if ( filters.gps.test( location ) ) {