@@ -171,23 +171,37 @@ class WeatherLogic extends EventEmitter {
171
171
var atTime = new Date ( after . getTime ( ) + ( inMinutes * MINUTE ) ) ;
172
172
173
173
// Get forecasts for both providers
174
+ var buienalarmPrecipitation = - 1 ;
174
175
try {
175
- var buienalarmPrecipitation = await this . Buienalarm . getForecast ( atTime ) ;
176
- var buienradarPrecipitation = await this . Buienradar . getForecast ( atTime ) ;
177
- if ( ( buienradarPrecipitation != null ) && ( buienalarmPrecipitation != null ) ) {
178
- var stateAtTime = this . rainStatePayload ( atTime , inMinutes , buienradarPrecipitation . mmh , buienalarmPrecipitation . mmh ) ;
179
-
180
- if ( ! tempState . hasOwnProperty ( 'now' ) ) {
181
- tempState . now = stateAtTime ;
182
- } else {
183
- if ( ! tempState . hasOwnProperty ( 'prediction' ) && ( tempState . now . state !== stateAtTime . state ) && ( stateAtTime . probability == 100 ) ) {
184
- tempState . prediction = stateAtTime ;
185
- break ;
186
- }
187
- }
188
- }
176
+ buienalarmPrecipitation = await this . Buienalarm . getForecast ( atTime ) ;
177
+ buienalarmPrecipitation = buienalarmPrecipitation . mmh ;
189
178
} catch ( e ) {
190
- console . log ( e ) ;
179
+ // In case this fails, we log it and use '-1' as value
180
+ this . emit ( 'rain-error' , 'Failed getting prediction for Buienalarm for time ' + atTime . toISOString ( ) + ': ' + JSON . stringify ( e ) ) ;
181
+ buienalarmPrecipitation = - 1 ;
182
+ }
183
+
184
+ var buienradarPrecipitation = - 1 ;
185
+ try {
186
+ buienradarPrecipitation = await this . Buienradar . getForecast ( atTime ) ;
187
+ buienradarPrecipitation = buienradarPrecipitation . mmh ;
188
+ } catch ( e ) {
189
+ // In case this fails, we log it and use '-1' as value
190
+ this . emit ( 'rain-error' , 'Failed getting prediction for Buienradar for time ' + atTime . toISOString ( ) + ': ' + JSON . stringify ( e ) ) ;
191
+ buienradarPrecipitation = - 1 ;
192
+ }
193
+
194
+ var stateAtTime = this . rainStatePayload ( atTime , inMinutes , buienradarPrecipitation , buienalarmPrecipitation ) ;
195
+
196
+ // First loop we set the 'current' state
197
+ if ( ! tempState . hasOwnProperty ( 'now' ) ) {
198
+ tempState . now = stateAtTime ;
199
+ }
200
+
201
+ // Subsequent checks we set the prediction if the state changes
202
+ if ( ! tempState . hasOwnProperty ( 'prediction' ) && ( tempState . now . state !== stateAtTime . state ) && ( stateAtTime . probability == 100 ) ) {
203
+ tempState . prediction = stateAtTime ;
204
+ break ;
191
205
}
192
206
}
193
207
0 commit comments