Cache weatherdata between two updates, only change of location or explicit click on reload-button triggers a reload of weatherdata. Reduces server load on openweathermap.org .

openweathermap_3.6
Jens Lody 11 years ago
parent bc65d7e44f
commit 142ca04700
  1. 167
      src/extension.js

@ -284,8 +284,12 @@ const WeatherMenuButton = new Lang.Class({
this._settings = Convenience.getSettings(WEATHER_SETTINGS_SCHEMA);
this._settingsC = this._settings.connect("changed", function() {
that.rebuildFutureWeatherUi();
that.refreshWeatherCurrent(false);
that.refreshWeatherForecast(false);
if (that.locationChanged()) {
that.currentWeatherCache = 'undefined';
that.forecastWeatherCache = 'undefined';
}
that.parseWeatherCurrent();
that.parseWeatherForecast();
});
},
@ -298,11 +302,23 @@ const WeatherMenuButton = new Lang.Class({
schema: schemaInterface
});
this._settingsInterfaceC = this._settingsInterface.connect("changed", function() {
that.refreshWeatherCurrent(false);
that.refreshWeatherForecast(false);
if (that.locationChanged()) {
that.currentWeatherCache = 'undefined';
that.forecastWeatherCache = 'undefined';
}
that.parseWeatherCurrent();
that.parseWeatherForecast();
});
},
locationChanged: function() {
let location = this.extractCity(this._city);
if (this.oldLocation != location) {
return true;
}
return false;
},
get _clockFormat() {
if (!this._settingsInterface)
this.loadConfigInterface();
@ -1088,27 +1104,11 @@ weather-storm.png = weather-storm-symbolic.svg
return 0;
},
refreshWeatherCurrent: function(recurse) {
if (!this.extractId(this._city)) {
this.updateCities();
return 0;
parseWeatherCurrent: function() {
if (this.currentWeatherCache == 'undefined') {
this.refreshWeatherCurrent();
return;
}
let params = {
q: this.extractCity(this._city),
units: 'metric'
};
if(this._appid)
params['APPID'] = this._appid;
this.load_json_async(WEATHER_URL_CURRENT, params, function(json) {
if (!json)
return 0;
if (Number(json.cod) != 200)
return 0;
this.rebuildSelectCityItem();
if (this._old_position_in_panel != this._position_in_panel) {
switch (this._old_position_in_panel) {
@ -1141,9 +1141,10 @@ weather-storm.png = weather-storm-symbolic.svg
this._old_position_in_panel = this._position_in_panel;
}
let json = this.currentWeatherCache;
// Refresh current weather
let location = this.extractLocation(this._city);
// Refresh current weather
let comment = json.weather[0].description;
if (this._translate_condition)
comment = this.get_weather_condition(json.weather[0].id);
@ -1162,18 +1163,16 @@ weather-storm.png = weather-storm-symbolic.svg
let sunrise = json.sys.sunrise;
let sunset = json.sys.sunset;
if (typeof this.lastBuildId == "undefined")
if (typeof this.lastBuildId == 'undefined')
this.lastBuildId = 0;
if (typeof this.lastBuildDate == "undefined")
if (typeof this.lastBuildDate == 'undefined')
this.lastBuildDate = 0;
if (this.lastBuildId != json.dt || !this.lastBuildDate) {
this.lastBuildId = json.dt;
this.lastBuildDate = new Date();
this.lastBuildDate = new Date(this.lastBuildId * 1000);
}
let actualDate = new Date();
let d = Math.floor((actualDate.getTime() - this.lastBuildDate.getTime()) / 86400000);
switch (this._pressure_units) {
case WeatherPressureUnits.inHg:
@ -1268,10 +1267,12 @@ weather-storm.png = weather-storm-symbolic.svg
lastBuild = this.lastBuildDate.toLocaleFormat("%I:%M %p");
}
if (d >= 1) {
let beginOfDay = new Date(new Date().setHours(0, 0, 0, 0));
let d = Math.floor((beginOfDay.getTime() - this.lastBuildDate.getTime()) / 86400000);
if (d < 0) {
lastBuild = _("Yesterday");
if (d > 1)
lastBuild = _("%s days ago").replace("%s", d);
if (d < -1)
lastBuild = _("%s days ago").replace("%s", -1 * d);
}
this._currentWeatherIcon.icon_name = this._weatherIcon.icon_name = iconname;
@ -1334,45 +1335,62 @@ weather-storm.png = weather-storm-symbolic.svg
else // i.e. wind > 0 && wind_direction
this._currentWeatherWind.text = wind_direction + ' ' + parseFloat(wind).toLocaleString() + ' ' + wind_unit;
return 0;
});
// Repeatedly refresh weather if recurse is set
if (recurse) {
this._timeoutCurrent = Mainloop.timeout_add_seconds(this._refresh_interval_current, Lang.bind(this, function() {
this.refreshWeatherCurrent(true);
}));
}
return 0;
},
refreshWeatherForecast: function(recurse) {
refreshWeatherCurrent: function(recurse) {
if (!this.extractId(this._city)) {
this.updateCities();
return 0;
return;
}
this.oldLocation = this.extractCity(this._city);
let params = {
q: this.extractCity(this._city),
units: 'metric',
cnt : '10'
q: this.oldLocation,
units: 'metric'
};
if(this._appid)
if (this._appid)
params['APPID'] = this._appid;
this.load_json_async(WEATHER_URL_FORECAST, params, function(json) {
this.load_json_async(WEATHER_URL_CURRENT, params, function(json) {
if (!json)
return 0;
return;
if (Number(json.cod) != 200)
return 0;
return;
if (this.currentWeatherCache != json)
this.currentWeatherCache = json;
this.rebuildSelectCityItem();
this.parseWeatherCurrent();
this.parseWeatherForecast();
});
// Repeatedly refresh weather if recurse is set
if (recurse) {
this._timeoutCurrent = Mainloop.timeout_add_seconds(this._refresh_interval_current, Lang.bind(this, function() {
this.refreshWeatherCurrent(true);
}));
}
},
parseWeatherForecast: function() {
if (this.forecastWeatherCache == 'undefined') {
this.refreshWeatherForecast();
return;
}
let forecast = json.list;
let forecast = this.forecastWeatherCache;
let beginOfDay = new Date(new Date().setHours(0, 0, 0, 0));
// Refresh forecast
for (let i = 0; i < this._days_forecast; i++) {
let forecastUi = this._forecast[i];
let forecastData = forecast[i];
if (forecastData == 'undefined')
continue;
let t_low = forecastData.temp.min;
let t_high = forecastData.temp.max;
@ -1424,25 +1442,52 @@ weather-storm.png = weather-storm-symbolic.svg
comment = this.get_weather_condition(forecastData.weather[0].id);
let forecastDate = new Date(forecastData.dt * 1000);
let actualDate = new Date();
let dayLeft = Math.floor((actualDate.getTime() - forecastDate.getTime()) / 1000 / 60 / 60 / 24);
let dayLeft = Math.floor((forecastDate.getTime() - beginOfDay.getTime()) / 86400000);
let date_string = _("Today");
if (dayLeft == -1)
if (dayLeft == 1)
date_string = _("Tomorrow");
else if (dayLeft < -1)
date_string = _("In %s days").replace("%s", -1 * dayLeft);
else if (dayLeft == 1)
date_string = _("Yesterday");
else if (dayLeft > 1)
date_string = _("%s days ago").replace("%s", dayLeft);
date_string = _("In %s days").replace("%s", dayLeft);
else if (dayLeft == -1)
date_string = _("Yesterday");
else if (dayLeft < -1)
date_string = _("%s days ago").replace("%s", -1 * dayLeft);
forecastUi.Day.text = date_string + ' (' + this.get_locale_day(forecastDate.getDay()) + ')';
forecastUi.Temperature.text = '\u2193 ' + parseFloat(t_low).toLocaleString() + ' ' + this.unit_to_unicode() + ' \u2191 ' + parseFloat(t_high).toLocaleString() + ' ' + this.unit_to_unicode();
forecastUi.Summary.text = comment;
forecastUi.Icon.icon_name = this.get_weather_icon_safely(forecastData.weather[0].id);
}
},
refreshWeatherForecast: function(recurse) {
if (!this.extractId(this._city)) {
this.updateCities();
return 0;
}
this.oldLocation = this.extractCity(this._city);
let params = {
q: this.oldLocation,
units: 'metric',
cnt: '13'
};
if(this._appid)
params['APPID'] = this._appid;
this.load_json_async(WEATHER_URL_FORECAST, params, function(json) {
if (!json)
return;
if (Number(json.cod) != 200)
return;
if (this.forecastWeatherCache != json.list)
this.forecastWeatherCache = json.list;
this.parseWeatherForecast();
});
// Repeatedly refresh weather if recurse is set

Loading…
Cancel
Save