<?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);
// roundbox_trans_neu($image, imagesx($image)-220, 80, imagesx($image)-5, imagesy($image)-395-100, 2, 0, 0, 0, 10,0);
$wicon = ImageCreateFromPNG ( $current_icon );
ImageCopy($image, $wicon, imagesx($image)-190, 0, 0, 0, imagesx($wicon), imagesy($wicon));
ImageDestroy($wicon);
$opt = array(
'width' => 280,
'align' => ALIGN_RIGHT
);
}
}
/************************* Halbtransparentbox **************************
* roundbox_trans_neu($image, imagesx($image)-250, 55, imagesx($image)-5, imagesy($image)-15-290, 2, 0, 0, 0, 130,0);
***********************************************************************/
$text = $current_temp."°C";
imagettftextboxopt($image,58, 0, imagesx($image)-470, 75, $style['textcolor'], $style['fontb'], $text, $opt);
$text = "Aktuell: $current_condition\nLuftfeuchte: $current_humidity\nWind: $current_wind_condition km/h $current_wind_dir \nLuftdruck: $current_pressure mbar";
imagettftextboxopt($image, 14, 0, imagesx($image)-300, 150, $style['textcolor'], $style['fontb'], $text, $opt);
/*********************** Halbtransparentbox *************************
* roundbox_trans_neu($image, imagesx($image)-230, 0, imagesx($image)-5, imagesy($image)-15-330, 2, 0, 0, 0, 230,0);
*********************************************************************/
// 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 = 235+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+15, 0, 0, imagesx($wicon)/2, imagesy($wicon)/2, imagesx($wicon), imagesy($wicon));
ImageDestroy($wicon);
$opt = array(
'width' => 130,
'align' => ALIGN_RIGHT
);
$text = $day."\n".$high."° | ".$low."°\n".$condition; // ."ICON:".$xml->weather->forecast_conditions[$i]->icon['data'];
imagettftextboxopt($image, 15, 0, imagesx($image)-245, $offset, $style['textcolor'], $style['font'], $text, $opt);
}
//ENDE XML-Datei auslesen
// display sunrise and sunset
date_default_timezone_set('Europe/Berlin');
$longitude = 48.562728; // longitude
$latitude = 10.424322; // latitude
if (date("I") == 1) $dst = 2; // Sommerzeit
if (date("I") == 0) $dst = 1; // Winterzeit
$sunrise = date_sunrise(time(), SUNFUNCS_RET_STRING, $longitude, $latitude, 90, $dst);
$sunset = date_sunset(time(), SUNFUNCS_RET_STRING, $longitude, $latitude, 90, $dst);
$text = $sunrise." ".$sunset;
$opt = array(
'width' => 370,
'align' => ALIGN_LEFT
);
imagettftextboxopt($image, 10, 00, 660, 569, $style['textcolor'], $style['font'], $text, $opt);
$icon = ImageCreateFromPNG ( 'resources/icons/sunup.png' );
ImageCopy($image, $icon, 640, 568, 0, 0, imagesx($icon), imagesy($icon));
ImageDestroy($icon);
$icon = ImageCreateFromPNG ( 'resources/icons/sundown.png' );
ImageCopy($image, $icon, 702, 568, 0, 0, imagesx($icon), imagesy($icon));
ImageDestroy($icon);
// display sunrise and sunset ende
}
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;
}
}