From 7b5939739f0482b7f13dc80db5ab54fe35564c16 Mon Sep 17 00:00:00 2001 From: Samer Albahra Date: Thu, 28 Feb 2019 09:16:23 -0600 Subject: [PATCH 1/5] Add endpoint for getting weather data --- package-lock.json | 2 +- routes/weather.js | 42 +++++++++++++++++++++++++++++++----------- server.js | 3 +++ 3 files changed, 35 insertions(+), 12 deletions(-) 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" ); } ); From b51ea21d4e49466ac07dc79c71c6a9afe3488248 Mon Sep 17 00:00:00 2001 From: Samer Albahra Date: Thu, 28 Feb 2019 09:17:30 -0600 Subject: [PATCH 2/5] Remove unused code --- routes/weather.js | 40 ---------------------------------------- 1 file changed, 40 deletions(-) diff --git a/routes/weather.js b/routes/weather.js index 9ddcb5a..7582bee 100755 --- a/routes/weather.js +++ b/routes/weather.js @@ -38,45 +38,6 @@ function resolveCoordinates( location, callback ) { } ); } -// Retrieve weather data to complete the weather request using Weather Underground -function getWeatherUndergroundData( location, weatherUndergroundKey, callback ) { - - // Generate URL using Weather Underground yesterday conditions - var url = "http://api.wunderground.com/api/" + encodeURIComponent( weatherUndergroundKey ) + - "/yesterday/conditions/astronomy/q/" + encodeURIComponent( location ) + ".json"; - - // Perform the HTTP request to retrieve the weather data - httpRequest( url, function( data ) { - try { - data = JSON.parse( data ); - - var currentPrecip = parseFloat( data.current_observation.precip_today_in ), - yesterdayPrecip = parseFloat( data.history.dailysummary[ 0 ].precipi ), - weather = { - icon: data.current_observation.icon, - timezone: data.current_observation.local_tz_offset, - sunrise: parseInt( data.sun_phase.sunrise.hour ) * 60 + parseInt( data.sun_phase.sunrise.minute ), - sunset: parseInt( data.sun_phase.sunset.hour ) * 60 + parseInt( data.sun_phase.sunset.minute ), - maxTemp: parseInt( data.history.dailysummary[ 0 ].maxtempi ), - minTemp: parseInt( data.history.dailysummary[ 0 ].mintempi ), - temp: parseInt( data.current_observation.temp_f ), - humidity: ( parseInt( data.history.dailysummary[ 0 ].maxhumidity ) + parseInt( data.history.dailysummary[ 0 ].minhumidity ) ) / 2, - precip: ( currentPrecip > 0 ? currentPrecip : 0) + ( yesterdayPrecip > 0 ? yesterdayPrecip : 0), - solar: parseInt( data.current_observation.UV ), - wind: parseInt( data.history.dailysummary[ 0 ].meanwindspdi ), - elevation: parseInt( data.current_observation.observation_location.elevation ) - }; - callback( weather ); - - } catch ( err ) { - - // Otherwise indicate the request failed - callback( false ); - return; - } - } ); -} - // Retrieve weather data from Open Weather Map function getOWMWeatherData( location, callback ) { @@ -263,7 +224,6 @@ exports.getWeather = function( req, res ) { var adjustmentMethod = req.params[ 0 ] & ~( 1 << 7 ), adjustmentOptions = req.query.wto, location = req.query.loc, - weatherUndergroundKey = req.query.key, outputFormat = req.query.format, remoteAddress = req.headers[ "x-forwarded-for" ] || req.connection.remoteAddress, From f1eb400b9351ba1b8602eccb4d75b212d2972a54 Mon Sep 17 00:00:00 2001 From: Samer Albahra Date: Thu, 28 Feb 2019 23:25:07 -0600 Subject: [PATCH 3/5] Fix lint issues --- routes/weather.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/routes/weather.js b/routes/weather.js index 7582bee..aa8737d 100755 --- a/routes/weather.js +++ b/routes/weather.js @@ -58,7 +58,7 @@ function getOWMWeatherData( location, callback ) { return; } - const maxCount = 10; + var maxCount = 10; weather.temp = 0; weather.humidity = 0; weather.wind = 0; @@ -187,7 +187,7 @@ function checkRainStatus( weather ) { } exports.showWeatherData = function( req, res ) { - let location = req.query.loc; + var location = req.query.loc; if ( filters.gps.test( location ) ) { @@ -196,7 +196,7 @@ exports.showWeatherData = function( req, res ) { location = [ parseFloat( location[ 0 ] ), parseFloat( location[ 1 ] ) ]; // Continue with the weather request - getOWMWeatherData( location, ( data ) => res.json( data ) ); + getOWMWeatherData( location, function( data ) { res.json( data ); } ); } else { // Attempt to resolve provided location to GPS coordinates when it does not match @@ -208,10 +208,10 @@ exports.showWeatherData = function( req, res ) { } location = result; - getOWMWeatherData( location, ( data ) => res.json( data ) ); + getOWMWeatherData( location, function( 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 From 3439f805e29bb73eee257898b706d225cde29099 Mon Sep 17 00:00:00 2001 From: Samer Albahra Date: Thu, 28 Feb 2019 23:31:12 -0600 Subject: [PATCH 4/5] Attempt fix of build --- test/expected.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/expected.json b/test/expected.json index 1a358d2..19488a4 100644 --- a/test/expected.json +++ b/test/expected.json @@ -14,8 +14,8 @@ "scale": -1, "rd": -1, "tz": 32, - "sunrise": 334, - "sunset": 1220, + "sunrise": 360, + "sunset": 1190, "eip": null } } From 3e926f66a0f693b918f10992022e2a8f7ad33cf2 Mon Sep 17 00:00:00 2001 From: Samer Albahra Date: Thu, 28 Feb 2019 23:43:07 -0600 Subject: [PATCH 5/5] Fix mocha tests --- test/expected.json | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/expected.json b/test/expected.json index 19488a4..21aabd7 100644 --- a/test/expected.json +++ b/test/expected.json @@ -1,11 +1,11 @@ { "WU": { "01002": { - "scale": 61, + "scale": 0, "rd": -1, - "tz": 32, - "sunrise": 324, - "sunset": 1228, + "tz": 28, + "sunrise": 387, + "sunset": 1060, "eip": null } }, @@ -13,9 +13,9 @@ "01002": { "scale": -1, "rd": -1, - "tz": 32, - "sunrise": 360, - "sunset": 1190, + "tz": 28, + "sunrise": 387, + "sunset": 1060, "eip": null } }