<?php 



function translate($text) {
    switch ($text) {
	case 'chanceflurries': 	$tt = 'Sanse fulgi'; break;
	case 'chancerain': 	$tt = 'Sanse ploaie'; break;
	case 'chancesleet': 	$tt = 'Sanse lapovita'; break;
	case 'chancesnow': 	$tt = 'Sanse zapada'; break;
	case 'chancetstorms': 	$tt = 'Sanse furtuna'; break;
	case 'clear': 		$tt = 'Senin'; break;
	case 'cloudy': 		$tt = 'Innorat '; break;
	case 'flurries': 	$tt = 'Fulgi '; break;
	case 'fog': 		$tt = 'Ceata'; break;
	case 'hazy': 		$tt = 'Inchis'; break;
	case 'mostlycloudy': 	$tt = 'Majorit. innorat'; break;
	case 'mostlysunny': 	$tt = 'Majorit. senin'; break;
	case 'partlycloudy': 	$tt = 'Partial innorat'; break;
	case 'partlysunny': 	$tt = 'Partial insorit'; break;
	case 'sleet': 		$tt = 'Lapovita'; break;
	case 'rain': 		$tt = 'Ploaie'; break;
	case 'snow': 		$tt = 'Zapada'; break;
	case 'sunny': 		$tt = 'Insorit'; break;
	case 'tstorms': 	$tt = 'Furtuna'; break;
	case 'Monday':		$tt = 'L';break;
	case 'Tuesday':		$tt = 'M';break;
	case 'Wednesday':	$tt = 'M';break;
	case 'Thursday':	$tt = 'J';break;
	case 'Friday':		$tt = 'V';break;
	case 'Saturday':	$tt = 'S';break;
	case 'Sunday':		$tt = 'D';break;
	default: $tt = $text;
    }
    return $tt;
}

function get_between($input, $start, $end) 
{ 
  $substr = substr($input, strlen($start)+strpos($input, $start), (strlen($input) - strpos($input, $end))*(-1)); 
  return $substr; 
} 


function get_fhem_temp() {

//print('in get_fhem_temp()');

$url = 'https://ajoni.no-ip.org:8083/fhem?cmd=jsonlist+KS300';
//print($url);
$credentials = 'fhemuser:fhempassword';
$header_array = array('Expect' => '',
                'From' => 'User A');
$ssl_array = array('version' => SSL_VERSION_SSLv3);
$options = array(headers => $header_array,
                httpauth => $credentials,
                httpauthtype => HTTP_AUTH_BASIC,
            protocol => HTTP_VERSION_1_1,
            ssl => $ssl_array);
//print_r($options);
//create the httprequest object               
$httpRequest_OBJ = new httpRequest($url, HTTP_METH_POST, $options);
//print($httpRequest_OBJ);
//add the content type
$httpRequest_OBJ->setContentType = 'Content-Type: text/xml';
//add the raw post data
//$httpRequest_OBJ->setRawPostData ($theData);
$httpRequest_OBJ->setBody ($theData);
//send the http request
$result = $httpRequest_OBJ->send();
//print out the result
//print($result);

$stripped_result = get_between($result,'<pre>','</pre>');
//print($stripped_result);
$stripped_result = str_replace('<a href="/fhem?detail="></a> ','',$stripped_result);
//print($stripped_result);
$parsed_json = json_decode($stripped_result);
//print('<pre>');
//print_r($parsed_json);

//$t = $parsed_json->{'Results'};
$t = $parsed_json->{'Results'};
//print_r($t[0]->{'Internals'}->{'STATE'});
// old code, fhem change? 
 $u= str_replace(' ','',$t[0]->{'state'});
//new code 2017.12.29
//$u= str_replace(' ','',$t[0]->{'Internals'}->{'STATE'});
//print("--------------");
//print($u);
//print("--------------");
$a = strpos($u,'R');
$b = strpos($u,"IR");

//taiem partea cu cantitatea de ploaie momentana   si ce vine dupa
//$uu = substr($u,$a,strlen($u)-$b+1);
$uu = substr($u,0,$a+1);
//print("uu".$uu);
//$uv = str_replace($uu,'',$u);
$uv = str_replace('T:','',$uu);
//print("uv".$uv.'|');
$uv = str_replace('H:','C ',$uv);

$uv = str_replace('W:','% ',$uv);

$uv = str_replace('R','km ',$uv);
//print($uv.'|');

//print('</pre>');

return $uv;
}


function getWeather($city) {
	

	$requestAddress = "http://api.wunderground.com/api/6cdd163e7846d123/conditions/q/".str_replace(' ','%20',$city).".json";
	$json_string = file_get_contents($requestAddress);
//	echo $json_string;
	$parsed_json = json_decode($json_string);
	$tt = $parsed_json->{'current_observation'}->{'temp_c'}.'C ';
	$tt .= $parsed_json->{'current_observation'}->{'relative_humidity'}.' ';
	$tt .= translate($parsed_json->{'current_observation'}->{'icon'});
//	echo substr($tt,0,16).chr(10);
	$tt2 = get_fhem_temp();
	echo substr($tt2,0,16).chr(10);
//	echo $tt2.chr(10);
	
//	foreach($xml->weather as $item) {
//		foreach($item->current_conditions as $new) {
//		    echo $new->temp_c['data'].'C ';
//		    echo $new->humidity['data'];
//		    echo chr(10);
//		}

	
	$requestAddress = "http://api.wunderground.com/api/6cdd163e7846d123/forecast/q/".str_replace(' ','%20',$city).".json";
	$json_string = file_get_contents($requestAddress);
//	echo $json_string;
	$parsed_json = json_decode($json_string);

//	print_r($parsed_json);

	$zi = 0;
	for ($day = 0; $day < 8; $day +=2) {
	    $tt = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[$day];
	    $ts = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}[$zi];
	    $zi += 1;
	    $temps = '{'.$ts->{'low'}->{'celsius'}.'..'.$ts->{'high'}->{'celsius'}.'C} ';
//	    echo $tt->{'title'}.': '.$tt->{'fcttext_metric'}.chr(10);
	    echo translate($tt->{'title'}).': '.$temps.translate($tt->{'icon'}).chr(10);
	}

/*
		foreach($item->forecast_conditions as $new) {
			switch ($new->day_of_week['data']) {
			    case 'Sun':
				echo 'D';
				break;
			    case 'Mon':
				echo 'L';
				break;
			    case 'Tue':
				echo 'M';
				break;
			    case 'Wed':
				echo 'M';
				break;
			    case 'Thu':
				echo 'J';
				break;
			    case 'Fri':
				echo 'V';
				break;
			    case 'Sat':
				echo 'S';
				break;
			}
			echo ''.round(($new->low['data']-32)*5/9); //min()
			echo '..'.round(($new->high['data']-32)*5/9);//max()
			echo 'C:';
			translate($new->condition['data']);
			echo chr(10);
		
		}
	}
*/
  }
  

// Popescu@wunderground
//  $weatheradd = "http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=ITIMISUN2&format=1";
  $weatheradd = "http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=ITIMISTI6&format=1";
  $lines = file($weatheradd);  
  $i = count($lines);
  $line = $lines[$i-3];
  //echo $line;
  $ar = explode(",", $line);

//daca se trece pe wunderground (cand mere iarasi popescu...)
//  echo $ar[1]."C ".$ar[6]."km/h ".$ar[8]."%";
//  echo chr(10);
  getWeather($_GET['city']);?>
