Skip to content

Commit

Permalink
weather cache regards repeat param of widgets (connects issues #20 and
Browse files Browse the repository at this point in the history
  • Loading branch information
smaiLee committed Sep 9, 2016
1 parent 34658bd commit a81201b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
10 changes: 7 additions & 3 deletions lib/weather/service/wunderground.com.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,24 @@ public function run()
{
// api call
$cache = new class_cache('wunderground_'.$this->location.'.json');
error_log($this->cache_duration_minutes);

if ($cache->hit(10))
if ($cache->hit($this->cache_duration_minutes))
{
$content = $cache->read();
error_log('hit');
}
else
{
error_log('nohit');
$url = 'http://api.wunderground.com/api/'.config_weather_key.'/conditions/forecast/lang:'.trans('wunderground', 'lang').'/q/'.$this->location.'.json';
$content = file_get_contents($url);
$cache->write($content);
}

$parsed_json = json_decode($content);
if ($parsed_json->{'forecast'})
{
// write cache
$cache->write($content);
$this->debug($parsed_json);

// today
Expand Down
5 changes: 5 additions & 0 deletions lib/weather/weather.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class weather extends service
{
var $location = '';
var $icon_sm = 'sun_';
var $cache_duration_minutes = 0;

/**
* initialization of some parameters
Expand All @@ -29,6 +30,10 @@ public function init($request)

$this->location = $request['location'];

// reduce real cache duration by 2 seconds to avoid getting old weather on calling repeat on widget
if (isset($request['cache_duration_minutes']))
$this->cache_duration_minutes = $request['cache_duration_minutes'] - (2/60);

if (!isset($request['sunrise']))
$request['sunrise'] = 6;

Expand Down
13 changes: 8 additions & 5 deletions widgets/weather.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @param unique id for this widget
* @param location as string (optional)
* @param the refresh interval for this widget (using relative time format), default 1h
* @param the refresh interval for this widget (using relative time format), default 15i (15 minutes)
*
* @info based on jDigiClock by Radoslav Dimov
* @link http://www.radoslavdimov.com
Expand All @@ -26,7 +26,7 @@
{% endif %}

<div class="center">
<div id="{{ uid }}-weather" class="weather" data-widget="weather.current" data-repeat="{{ repeat|default('1h') }}">
<div id="{{ uid }}-weather" class="weather" data-widget="weather.current" data-repeat="{{ repeat|default('15i') }}">
<div class="humi"></div>
<div class="wind"></div>
<div class="temp"></div>
Expand All @@ -38,7 +38,8 @@
<script type="text/javascript">
$(document).delegate('div[data-widget="weather.current"]', {
'init' : function (event) {
$.getJSON('lib/weather/service/{{ config_weather_service }}.php?location={{ location|default(config_weather_location) }}', function (data) {
var repeatMinutes = (new Date().duration("{{ repeat|default('15i') }}") - 0) / 60000;
$.getJSON('lib/weather/service/{{ config_weather_service }}.php?location={{ location|default(config_weather_location) }}&cache_duration_minutes='+repeatMinutes, function (data) {
$('#{{ uid }}-weather').css('background', 'url(lib/weather/pics/' + data.current.icon + '.png) 50% 0 no-repeat')
$('#{{ uid }}-weather .city').html(data.city);
$('#{{ uid }}-weather .cond').html(data.current.conditions);
Expand Down Expand Up @@ -91,7 +92,8 @@
<script type="text/javascript">
$(document).delegate('div[data-widget="weather.forecast"]', {
'init' : function (event) {
$.getJSON('lib/weather/service/{{ config_weather_service }}.php?location={{ location|default(config_weather_location) }}', function (data) {
var repeatMinutes = (new Date().duration("{{ repeat|default('3h') }}") - 0) / 60000;
$.getJSON('lib/weather/service/{{ config_weather_service }}.php?location={{ location|default(config_weather_location) }}&cache_duration_minutes='+repeatMinutes, function (data) {
$('#{{ uid }}-forecast').css('background', 'url(lib/weather/pics/' + data.forecast[{{ day|default(1) }}].icon + '.png) 50% 0 no-repeat')
$('#{{ uid }}-forecast .city').html(data.city);
$('#{{ uid }}-forecast .cond').html(data.forecast[{{ day|default(1) }}].conditions);
Expand Down Expand Up @@ -136,7 +138,8 @@
<script type="text/javascript">
$(document).delegate('div[data-widget="weather.forecastweek"]', {
'init' : function (event) {
$.getJSON('lib/weather/service/{{ config_weather_service }}.php?location={{ location|default(config_weather_location) }}', function (data) {
var repeatMinutes = (new Date().duration("{{ repeat|default('3h') }}") - 0) / 60000;
$.getJSON('lib/weather/service/{{ config_weather_service }}.php?location={{ location|default(config_weather_location) }}&cache_duration_minutes='+repeatMinutes, function (data) {
var forecast = '';
for (var i in data.forecast) {
forecast += '<div class=\'day\'>'
Expand Down

0 comments on commit a81201b

Please sign in to comment.