@@ -85,24 +85,24 @@ var init = function (apiKey, language, locationGeo, l, d) {
8585 } ) ;
8686} ;
8787
88- var update = function ( callback ) {
88+ var update = function ( callback , requestedForecastDays ) {
8989 let weather = { } ;
9090 weather . forecasts = [ ] ;
9191 let that = this ;
9292
93- updateCache ( this . darkskyTimeMachine , function ( ) {
93+ updateCache ( this . darkskyTimeMachine , requestedForecastDays , function ( ) {
9494 debug ( "Updating weather with dark sky" ) ;
9595 that . darksky . get ( )
9696 . then ( function ( response ) {
9797
9898 // Current weather report
99- response [ ' currently' ] [ ' rainDay' ] = cache . report . rainDay ; // Load rainDay from cache
100- weather . report = parseReport ( response [ ' currently' ] , response [ ' timezone' ] ) ;
99+ response . currently . rainDay = cache . report . rainDay ; // Load rainDay from cache
100+ weather . report = parseReport ( response . currently , response . timezone ) ;
101101
102102 // Forecasts for today and next 6 days
103103 for ( let i = 0 ; i <= 6 ; i ++ ) {
104- response [ ' daily' ] [ ' data' ] [ i ] [ ' rainDay' ] = cache . forecast [ 'day' + i ] . rainDay ; // Load rainDay from cache
105- weather . forecasts . push ( parseForecast ( response [ ' daily' ] [ ' data' ] [ i ] , response [ ' timezone' ] ) ) ;
104+ response . daily . data [ i ] . rainDay = cache . forecast [ 'day' + i ] . rainDay ; // Load rainDay from cache
105+ weather . forecasts . push ( parseForecast ( response . daily . data [ i ] , response . timezone ) ) ;
106106 }
107107
108108 callback ( null , weather ) ;
@@ -118,57 +118,57 @@ var update = function (callback) {
118118var parseReport = function ( values , timezone ) {
119119 let report = { } ;
120120
121- report . AirPressure = parseInt ( values [ ' pressure' ] ) ;
122- report . CloudCover = parseInt ( values [ ' cloudCover' ] * 100 ) ;
123- report . Condition = values [ ' summary' ] ;
124- report . ConditionCategory = converter . getConditionCategory ( values [ ' icon' ] ) ;
125- report . DewPoint = parseInt ( values [ ' dewPoint' ] ) ;
126- report . Humidity = parseInt ( values [ ' humidity' ] * 100 ) ;
127- report . ObservationTime = moment . unix ( values [ ' time' ] ) . tz ( timezone ) . format ( 'HH:mm:ss' ) ;
128- report . Ozone = parseInt ( values [ ' ozone' ] ) ;
129- report . Rain1h = isNaN ( parseInt ( values [ ' precipIntensity' ] ) ) ? 0 : parseInt ( values [ ' precipIntensity' ] ) ;
130- report . RainDay = values [ ' rainDay' ]
131- report . Temperature = values [ ' temperature' ] ;
132- report . UVIndex = isNaN ( parseInt ( values [ ' uvIndex' ] ) ) ? 0 : parseInt ( values [ ' uvIndex' ] ) ;
133- report . Visibility = isNaN ( parseInt ( values [ ' visibility' ] ) ) ? 0 : parseInt ( values [ ' visibility' ] ) ;
134- report . WindDirection = converter . getWindDirection ( values [ ' windBearing' ] ) ;
135- report . WindSpeed = parseFloat ( values [ ' windSpeed' ] ) ;
136- report . WindSpeedMax = parseFloat ( values [ ' windGust' ] ) ;
121+ report . AirPressure = parseInt ( values . pressure ) ;
122+ report . CloudCover = parseInt ( values . cloudCover * 100 ) ;
123+ report . Condition = values . summary ;
124+ report . ConditionCategory = converter . getConditionCategory ( values . icon ) ;
125+ report . DewPoint = parseInt ( values . dewPoint ) ;
126+ report . Humidity = parseInt ( values . humidity * 100 ) ;
127+ report . ObservationTime = moment . unix ( values . time ) . tz ( timezone ) . format ( 'HH:mm:ss' ) ;
128+ report . Ozone = parseInt ( values . ozone ) ;
129+ report . Rain1h = isNaN ( parseInt ( values . precipIntensity ) ) ? 0 : parseInt ( values . precipIntensity ) ;
130+ report . RainDay = values . rainDay ;
131+ report . Temperature = values . temperature ;
132+ report . UVIndex = isNaN ( parseInt ( values . uvIndex ) ) ? 0 : parseInt ( values . uvIndex ) ;
133+ report . Visibility = isNaN ( parseInt ( values . visibility ) ) ? 0 : parseInt ( values . visibility ) ;
134+ report . WindDirection = converter . getWindDirection ( values . windBearing ) ;
135+ report . WindSpeed = parseFloat ( values . windSpeed ) ;
136+ report . WindSpeedMax = parseFloat ( values . windGust ) ;
137137
138138 return report ;
139139} ;
140140
141141var parseForecast = function ( values , timezone , i ) {
142142 let forecast = { } ;
143143
144- forecast . AirPressure = parseInt ( values [ ' pressure' ] ) ;
145- forecast . CloudCover = parseInt ( values [ ' cloudCover' ] * 100 ) ;
146- forecast . Condition = values [ ' summary' ] ;
147- forecast . ConditionCategory = converter . getConditionCategory ( values [ ' icon' ] ) ;
148- forecast . DewPoint = parseInt ( values [ ' dewPoint' ] ) ;
149- forecast . ForecastDay = moment . unix ( values [ ' time' ] ) . tz ( timezone ) . format ( 'dddd' ) ;
150- forecast . Humidity = parseInt ( values [ ' humidity' ] * 100 ) ;
151- forecast . Ozone = parseInt ( values [ ' ozone' ] ) ;
152- forecast . RainChance = parseInt ( values [ ' precipProbability' ] * 100 ) ;
153- forecast . RainDay = values [ ' rainDay' ]
154- forecast . Temperature = values [ ' temperatureHigh' ] ;
155- forecast . TemperatureMin = values [ ' temperatureLow' ] ;
156- forecast . UVIndex = isNaN ( parseInt ( values [ ' uvIndex' ] ) ) ? 0 : parseInt ( values [ ' uvIndex' ] ) ;
157- forecast . Visibility = isNaN ( parseInt ( values [ ' visibility' ] ) ) ? 0 : parseInt ( values [ ' visibility' ] ) ;
158- forecast . WindDirection = converter . getWindDirection ( values [ ' windBearing' ] ) ;
159- forecast . WindSpeed = parseFloat ( values [ ' windSpeed' ] ) ;
160- forecast . WindSpeedMax = parseFloat ( values [ ' windGust' ] ) ;
144+ forecast . AirPressure = parseInt ( values . pressure ) ;
145+ forecast . CloudCover = parseInt ( values . cloudCover * 100 ) ;
146+ forecast . Condition = values . summary ;
147+ forecast . ConditionCategory = converter . getConditionCategory ( values . icon ) ;
148+ forecast . DewPoint = parseInt ( values . dewPoint ) ;
149+ forecast . ForecastDay = moment . unix ( values . time ) . tz ( timezone ) . format ( 'dddd' ) ;
150+ forecast . Humidity = parseInt ( values . humidity * 100 ) ;
151+ forecast . Ozone = parseInt ( values . ozone ) ;
152+ forecast . RainChance = parseInt ( values . precipProbability * 100 ) ;
153+ forecast . RainDay = values . rainDay ;
154+ forecast . Temperature = values . temperatureHigh ;
155+ forecast . TemperatureMin = values . temperatureLow ;
156+ forecast . UVIndex = isNaN ( parseInt ( values . uvIndex ) ) ? 0 : parseInt ( values . uvIndex ) ;
157+ forecast . Visibility = isNaN ( parseInt ( values . visibility ) ) ? 0 : parseInt ( values . visibility ) ;
158+ forecast . WindDirection = converter . getWindDirection ( values . windBearing ) ;
159+ forecast . WindSpeed = parseFloat ( values . windSpeed ) ;
160+ forecast . WindSpeedMax = parseFloat ( values . windGust ) ;
161161
162162 return forecast ;
163163} ;
164164
165- var updateCache = function ( api , callback ) {
165+ var updateCache = function ( api , requestedForecastDays , callback ) {
166166 if ( typeof cache . lastUpdate === 'undefined' || new Date ( ) - cache . lastUpdate > 3600000 ) {
167167 debug ( "Called hourly update of rain data" ) ;
168168 cache . lastUpdate = new Date ( ) ;
169169
170170 let now = moment ( ) ;
171- let callbacks = 8 ;
171+ let callbacks = requestedForecastDays + 1 ;
172172
173173 doTimeMachineRequest ( api , now , function ( result ) {
174174 cache . report . rainDay = result ;
@@ -178,7 +178,7 @@ var updateCache = function (api, callback) {
178178 }
179179 } , true ) ;
180180
181- for ( let i = 0 ; i <= 6 ; i ++ ) {
181+ for ( let i = 0 ; i < requestedForecastDays ; i ++ ) {
182182 doTimeMachineRequest ( api , now . clone ( ) . add ( i , 'd' ) , function ( result ) {
183183 cache . forecast [ 'day' + i ] . rainDay = result ;
184184 callbacks -- ;
@@ -201,12 +201,12 @@ var doTimeMachineRequest = function (api, now, callback, limit = false) {
201201 // Current hour in time zone of weather location
202202 let hour = 24 ;
203203 if ( limit ) {
204- hour = now . tz ( response [ ' timezone' ] ) . format ( 'H' ) ;
204+ hour = now . tz ( response . timezone ) . format ( 'H' ) ;
205205 }
206206
207207 // Sum all values for the requested day
208- debug ( "Accumulate rain for " + now . tz ( response [ ' timezone' ] ) . format ( 'dddd' ) + ( limit ? ( ' until ' + hour + ':00' ) : '' ) ) ;
209- let result = converter . getRainAccumulated ( response [ ' hourly' ] [ ' data' ] . slice ( 0 , hour ) , 'precipIntensity' ) ;
208+ debug ( "Accumulate rain for " + now . tz ( response . timezone ) . format ( 'dddd' ) + ( limit ? ( ' until ' + hour + ':00' ) : '' ) ) ;
209+ let result = converter . getRainAccumulated ( response . hourly . data . slice ( 0 , hour ) , 'precipIntensity' ) ;
210210 debug ( "Accumulated rain: " + result ) ;
211211 callback ( result ) ;
212212 } )
0 commit comments