<?php
/***************************************************************************
* InfoFrame (image generator for digital picture frames)
* Copyright (C) 2009 Tobias Kolb
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
***************************************************************************/
class WeatherPlugin implements IPlugin
{
private $dbconn = NULL;
private $config = NULL;
public function __construct($dbconn, $config) {
$this->dbconn = $dbconn;
$this->config = $config;
}
public function doUpdate() {
// download weather XML into local file for caching
$city = urlencode( $this->config['city'] );
$api_key = $this->config['api_key'];
$curl = curl_init();
$file = fopen("cache/weather.xml", "w");
if ($file)
{
curl_setopt($curl, CURLOPT_URL, utf8_encode("http://api.wunderground.com/api/$api_key/geolookup/conditions/forecast/lang:DL/q/Germany/$city.xml"));
curl_setopt($curl, CURLOPT_FILE, $file);
curl_setopt($curl, CURLOPT_USERAGENT, utf8_encode("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"));
curl_exec($curl);
fclose($file);
}
curl_close($curl);
}
public function doOutput($image, $style, $updateData, &$yoffset) {
$filename = 'cache/weather.xml';
//doUpdate nur ausführen, wenn die Datei cache/weather.xml älter als eine halbe Stunde
$diff_seconds_weather = (time() - filectime($filename));
if (!file_exists($filename) || ($diff_seconds_weather > ($this->config['update_weather']*60))){
$this->doUpdate();
}
// XML-Datei auslesen
if(file_exists($filename) && (filesize($filename) > 0)) {
$xml = simplexml_load_file($filename);
if($xml) {
// parse weather data
// ===================
// current conditions
$current_condition = $xml->current_observation->weather;
$current_temp = $xml->current_observation->temp_c;
$current_humidity = $xml->current_observation->relative_humidity;
$current_wind_condition = $xml->current_observation->wind_kph;
$current_wind_dir = $xml->current_observation->wind_dir;
$current_pressure = $xml->current_observation->pressure_mb;
$night = !isDaylight();
$current_icon = $this->getLocalWeatherImage($xml->current_observation->icon, $night);
$wicon = ImageCreateFromPNG ( $current_icon );
ImageCopy($image, $wicon, imagesx($image)-190, 5, 0, 0, imagesx($wicon), imagesy($wicon));
//ImageCopy($image, $wicon, 50, 5, 0, 0, imagesx($wicon), imagesy($wicon));
ImageDestroy($wicon);
$opt = array(
'width' => 280,
'align' => ALIGN_RIGHT
);
}
}
$text = $current_temp."°C";
imagettftextboxopt($image, 24, 0, imagesx($image)-300, 105, $style['textcolor'], $style['font'], $text, $opt);
$text = "Aktuell: $current_condition\nLuftfeuchte: $current_humidity\nWind: $current_wind_condition $current_wind_dir\nLuftdruck: $current_pressure mbar";
imagettftextboxopt($image, 15, 0, imagesx($image)-300, 145, $style['textcolor'], $style['font'], $text, $opt);
// forecast for today and next 3 days
for ($i = 0; $i <= 3; $i++) {
// pixel offset for placing day 0-3 in different rows from top to bottom
if (imagesy($image) <= 500) {
$offset = 225+(85*$i); // smaller spacing for low resolution displays (vertical=480px)
} else {
$offset = 255+83*$i; // normal spacing for high resolution diplays (vertical=600px)
}
if ($offset > (imagesy($image)-80))
break; // offset out of range, skip output of further weather forecast days
// format data
$day = $xml->forecast->simpleforecast->forecastdays->forecastday[$i]->date->weekday_short;
if ($i == 0)
$day = 'Heute';
if ($i == 1)
$day = 'Morgen';
$low = $xml->forecast->simpleforecast->forecastdays->forecastday[$i]->low->celsius;
$high = $xml->forecast->simpleforecast->forecastdays->forecastday[$i]->high->celsius;
$condition = $xml->forecast->simpleforecast->forecastdays->forecastday[$i]->conditions;
$icon = $this->getLocalWeatherImage($xml->forecast->simpleforecast->forecastdays->forecastday[$i]->icon, false);
// output
$wicon = ImageCreateFromPNG ( $icon );
ImageCopyResampled($image, $wicon, imagesx($image)-95, $offset+5, 0, 0, imagesx($wicon)/2, imagesy($wicon)/2, imagesx($wicon), imagesy($wicon));
ImageDestroy($wicon);
$opt = array(
'width' => 150,
'align' => ALIGN_RIGHT
);
$text = $day."\n".$high."° | ".$low."°\n".$condition; // ."ICON:".$xml->weather->forecast_conditions[$i]->icon['data'];
imagettftextboxopt($image, 15, 0, imagesx($image)-240, $offset, $style['textcolor'], $style['font'], $text, $opt);
}
//ENDE XML-Datei auslesen
}
private function getLocalWeatherImage($googleWeatherImage, $night) {
$localImagePath = 'resources/weather/'.$googleWeatherImage.".png";
$localImagePathNight = 'resources/weather/'.$googleWeatherImage."_night.png";
//if parameter $night is true and night image exist use it
if ($night && file_exists($localImagePathNight))
$localImagePath = $localImagePathNight;
else if (!file_exists($localImagePath))
// if daylight image doesn't exist display N/A image
$localImagePath = 'resources/weather/na.png';
return $localImagePath;
}
}