Skip to content

Commit 1921c24

Browse files
committed
Attempt to fix 'stalling' of rain updates
1 parent a4c06a6 commit 1921c24

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

dutch-weather.js

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ module.exports = function(RED) {
4545
}
4646

4747
var node = this;
48+
this.conf.weatherLogic.on('rain-error', function (msg) {
49+
node.warn(msg);
50+
});
51+
4852
this.conf.weatherLogic.on('rain-state', function (rainState) {
4953
node.send({ 'topic': 'rain-state', 'payload': rainState});
5054
});

lib/WeatherLogic.js

+29-15
Original file line numberDiff line numberDiff line change
@@ -171,23 +171,37 @@ class WeatherLogic extends EventEmitter {
171171
var atTime = new Date(after.getTime() + (inMinutes * MINUTE));
172172

173173
// Get forecasts for both providers
174+
var buienalarmPrecipitation = -1;
174175
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;
189178
} 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;
191205
}
192206
}
193207

0 commit comments

Comments
 (0)