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

Loading…
Cancel
Save