<?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';
if ($updateData || !file_exists($filename))
$this->doUpdate();
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)-220, 80, imagesx($image)-5, imagesy($image)-15-290, 2, 0, 0, 0, 130,0);
***********************************************************************/
$text = $current_temp."°C";
imagettftextboxopt($image, 22, 0, imagesx($image)-300, 85, $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, 12, 0, imagesx($image)-300, 130, $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 = 200+80*$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;
imagettftextboxopt($image, 13, 0, imagesx($image)-245, $offset, $style['textcolor'], $style['fontb'], $text, $opt);
}
}
}
// 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, 0, 700, 586, $style['textcolor'], $style['font'], $text, $opt);
$icon = ImageCreateFromPNG ( 'resources/icons/sunup.png' );
ImageCopy($image, $icon, 680, 582, 0, 0, imagesx($icon), imagesy($icon));
ImageDestroy($icon);
$icon = ImageCreateFromPNG ( 'resources/icons/sundown.png' );
ImageCopy($image, $icon, 742, 582, 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;
}
}
?>