From 5d7b5651a57a8099a92a445c78097b41a895d7cc Mon Sep 17 00:00:00 2001 From: Samer Albahra Date: Tue, 28 Aug 2018 00:14:27 -0500 Subject: [PATCH] Average out the incoming data from OpenWeatherMaps --- routes/weather.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/routes/weather.js b/routes/weather.js index f25d820..6207aaa 100755 --- a/routes/weather.js +++ b/routes/weather.js @@ -97,11 +97,25 @@ function getOWMWeatherData( location, callback ) { return; } - weather.temp = parseInt( data.list[ 0 ].main.temp ); - weather.humidity = parseInt( data.list[ 0 ].main.humidity ); - weather.wind = parseInt( data.list[ 0 ].wind.speed ); - weather.precip = data.list[ 0 ].rain ? parseFloat( data.list[ 0 ].rain[ "3h" ] || 0 ) : 0; + const maxCount = 10; + weather.temp = 0; + weather.humidity = 0; + weather.wind = 0; + weather.precip = 0; + for ( var index = 0; index < maxCount; index++ ) { + weather.temp += parseInt( data.list[ index ].main.temp ); + weather.humidity += parseInt( data.list[ index ].main.humidity ); + weather.wind += parseInt( data.list[ index ].wind.speed ); + weather.precip += data.list[ index ].rain ? parseFloat( data.list[ index ].rain[ "3h" ] || 0 ) : 0; + } + + weather.temp = weather.temp / maxCount; + weather.humidity = weather.humidity / maxCount; + weather.wind = weather.wind / maxCount; + weather.precip = weather.precip / maxCount; + + location = location.join( "," ); callback( weather );