cache OWM data

This commit is contained in:
Tobias Diekershoff 2015-07-12 08:18:24 +02:00
parent c1f26a22c3
commit 1c720a6824
2 changed files with 60 additions and 4 deletions

View file

@ -9,6 +9,7 @@
* *
*/ */
use Cmfcmf\OpenWeatherMap; use Cmfcmf\OpenWeatherMap;
use Cmfcmf\OpenWeatherMap\AbstractCache;
use Cmfcmf\OpenWeatherMap\Exception as OWMException; use Cmfcmf\OpenWeatherMap\Exception as OWMException;
// Must point to composer's autoload file. // Must point to composer's autoload file.
@ -28,6 +29,51 @@ function curweather_uninstall() {
} }
// The caching mechanism is taken from the cache example of the
// OpenWeatherMap-PHP-API library and a bit customized to allow admins to set
// the caching time depending on the plans they got from openweathermap.org
class ExampleCache extends AbstractCache
{
private function urlToPath($url)
{
$tmp = sys_get_temp_dir();
$dir = $tmp . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI";
if (!is_dir($dir)) {
mkdir($dir);
}
$path = $dir . DIRECTORY_SEPARATOR . md5($url);
return $path;
}
/**
* @inheritdoc
*/
public function isCached($url)
{
$path = $this->urlToPath($url);
if (!file_exists($path) || filectime($path) + $this->seconds < time()) {
return false;
}
return true;
}
/**
* @inheritdoc
*/
public function getCached($url)
{
return file_get_contents($this->urlToPath($url));
}
/**
* @inheritdoc
*/
public function setCached($url, $content)
{
file_put_contents($this->urlToPath($url), $content);
}
}
function curweather_network_mod_init(&$fk_app,&$b) { function curweather_network_mod_init(&$fk_app,&$b) {
@ -36,9 +82,14 @@ function curweather_network_mod_init(&$fk_app,&$b) {
$fk_app->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $fk_app->get_baseurl() . '/addon/curweather/curweather.css' . '" media="all" />' . "\r\n"; $fk_app->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $fk_app->get_baseurl() . '/addon/curweather/curweather.css' . '" media="all" />' . "\r\n";
// the getweather file does all the work here // the OpenWeatherMap-PHP-APIlib does all the work here
// the $rpt value is needed for location // the $rpt value is needed for location
// which getweather uses to fetch the weather data for weather and temp // $lang will be taken from the browser session to honour user settings
// $units can be set in the settings by the user
// $appid is configured by the admin in the admin panel
// those parameters will be used to get: cloud status, temperature, preassure
// and relative humidity for display, also the relevent area of the map is
// linked from lat/log of the reply of OWMp
$rpt = get_pconfig(local_user(), 'curweather', 'curweather_loc'); $rpt = get_pconfig(local_user(), 'curweather', 'curweather_loc');
@ -46,11 +97,13 @@ function curweather_network_mod_init(&$fk_app,&$b) {
$lang = $_SESSION['language']; $lang = $_SESSION['language'];
$units = get_pconfig( local_user(), 'curweather', 'curweather_units'); $units = get_pconfig( local_user(), 'curweather', 'curweather_units');
$appid = get_config('curweather','appid'); $appid = get_config('curweather','appid');
$cachetime = intval(get_config('curweather','cachetime'));
if ($units==="") if ($units==="")
$units = 'metric'; $units = 'metric';
// Get OpenWeatherMap object. Don't use caching (take a look into // Get OpenWeatherMap object. Don't use caching (take a look into
// Example_Cache.php to see how it works). // Example_Cache.php to see how it works).
$owm = new OpenWeatherMap(); //$owm = new OpenWeatherMap();
$owm = new OpenWeatherMap(null, new ExampleCache(), $cachetime);
try { try {
$weather = $owm->getWeather($rpt, $units, $lang, $appid); $weather = $owm->getWeather($rpt, $units, $lang, $appid);
@ -62,7 +115,7 @@ function curweather_network_mod_init(&$fk_app,&$b) {
}; };
$rhumid = $weather->humidity; $rhumid = $weather->humidity;
$pressure = $weather->pressure; $pressure = $weather->pressure;
$wind = $weather->wind->speed . " " . $weather->wind->direction; $wind = $weather->wind->speed->getDescription().', '.$weather->wind->speed . " " . $weather->wind->direction;
$description = $weather->clouds->getDescription(); $description = $weather->clouds->getDescription();
} catch(OWMException $e) { } catch(OWMException $e) {
alert ( 'OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'); alert ( 'OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').');
@ -137,6 +190,7 @@ function curweather_plugin_admin_post (&$a) {
return; return;
if ($_POST['curweather-submit']) { if ($_POST['curweather-submit']) {
set_config('curweather','appid',trim($_POST['appid'])); set_config('curweather','appid',trim($_POST['appid']));
set_config('curweather','cachetime',trim($_POST['cachetime']));
info( t('Curweather settings saved.'.EOL)); info( t('Curweather settings saved.'.EOL));
} }
} }
@ -147,6 +201,7 @@ function curweather_plugin_admin (&$a, &$o) {
$t = get_markup_template("admin.tpl", "addon/curweather/" ); $t = get_markup_template("admin.tpl", "addon/curweather/" );
$o = replace_macros ($t, array( $o = replace_macros ($t, array(
'$submit' => t('Save Settings'), '$submit' => t('Save Settings'),
'$cachetime' => array('cachetime', t('Caching Interval'), $cachetime, t('For how long should the weather data be cached? Choose according your OpenWeatherMap account type.'), array('0'=>t('no cache'), '300'=>'5 '.t('minutes'), '900'=>'15 '.t('minutes'), '1800'=>'30 '.t('minutes'), '3600'=>'60 '.t('minutes'))),
'$appid' => array('appid', t('Your APPID'), $appid, t('Your API key provided by OpenWeatherMap')) '$appid' => array('appid', t('Your APPID'), $appid, t('Your API key provided by OpenWeatherMap'))
)); ));
} }

View file

@ -1,2 +1,3 @@
{{include file="field_input.tpl" field=$appid}} {{include file="field_input.tpl" field=$appid}}
{{include file="field_select.tpl" field=$cachetime}}
<div class="submit"><input type="submit" name="curweather-submit" value="{{$submit}}" /></div> <div class="submit"><input type="submit" name="curweather-submit" value="{{$submit}}" /></div>