diff --git a/package-lock.json b/package-lock.json index 827b553..6ef5b02 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "os-weather-service", - "version": "0.0.1", + "version": "1.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/routes/weather.js b/routes/weather.js index b38a7d5..9ddcb5a 100755 --- a/routes/weather.js +++ b/routes/weather.js @@ -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 ) ) { diff --git a/server.js b/server.js index dd5d39a..3ea2e4b 100755 --- a/server.js +++ b/server.js @@ -16,6 +16,9 @@ if ( !process.env.HOST || !process.env.PORT ) { app.get( /weather(\d+)\.py/, weather.getWeather ); app.get( /(\d+)/, weather.getWeather ); +// Handle requests matching /weatherData +app.get( /weatherData/, weather.showWeatherData ); + app.get( "/", function( req, res ) { res.send( "OpenSprinkler Weather Service" ); } );