<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE); 
if(function_exists('date_default_timezone_set')) date_default_timezone_set('Asia/Jakarta');

$server="localhost"; $user="sdatelem_icuadm"; $pass="Icupu2015"; $db="sdatelem_icu";
$con_project=mysql_connect($server, $user, $pass);
$db_project=mysql_select_db($db,$con_project);

$tgl=date('Y-m-d');
$hr1=date('Y-m-d', strtotime('-1 days', strtotime($tgl)));
$hr2=date('Y-m-d', strtotime('-2 days', strtotime($tgl)));
$hr3=date('Y-m-d', strtotime('-3 days', strtotime($tgl)));

$jamini=date('H:00:00');
//echo "<br />";
$jamdualalu=date('H:00:00')-5;
//echo "<br />";
$duajam="0".$jamdualalu.":00:00";
$query_lokasi=mysql_query("select * from m_lokasi where 1=1 AND ReceivedDate!='0000-00-00'",$con_project);
while($rl=mysql_fetch_array($query_lokasi)){
	$tglhrini=$rl['ReceivedDate'];
	$dataduajam=$rl['ReceivedTime'];
	//echo "select * from ".$rl['nama_lokasi']." WHERE 1=1 AND ML!='50' ORDER BY ReceivedDate DESC,ReceivedTime DESC LIMIT 0,1";
	$sqlcek=mysql_query("select * from ".$rl['nama_lokasi']." WHERE 1=1 AND ML!='50' ORDER BY ReceivedDate DESC,ReceivedTime DESC LIMIT 0,1");
	
	/*echo "select * from ".$rl['nama_lokasi']." WHERE 1=1 AND ML!='50' ORDER BY ReceivedDate DESC,ReceivedTime DESC LIMIT 0,1";
	echo "<br>";
	*/
	$rowcek=mysql_fetch_array($sqlcek);
	$rain=$rowcek['Rain'];
	$wlevel=$rowcek['WLevel']+$rl['kalibrasi'];
	$ML=$rowcek['ML'];

//echo $rl['siaga4'];
//echo $rl['id_tipe'];
//	echo "<br>";
	//if($rl['id_merk']=="RADIO" || $rl['id_merk']=="FEWS" || $rl['id_merk']=="ICU") {
       if($rl['id_tipe']=="PCH" && $tglhrini==$tgl && $duajam>=$duajam){
		if($rain==0 || $rain=="") { $iconNew="cerah"; }
		  else if($rain>=$rl['siaga4'] && $rain<=$rl['siaga3']) { $iconNew="ringan"; }
		  else if($rain>=$rl['siaga3'] && $rain<=$rl['siaga2']) { $iconNew="sedang"; }
		  else if($rain>=$rl['siaga2'] && $rain<=$rl['siaga1']) { $iconNew="lebat"; }
		  else if($rain>$rl['siaga1']) { $iconNew="sangatlebat"; }		
	   } else if($rl['id_tipe']=="PDA" && $tglhrini==$tgl  && $duajam>=$duajam){
		if($wlevel==0 || $wlevel<=$rl['siaga4']) { $iconNew="normal"; }
		  else if($wlevel>$rl['siaga4'] && $wlevel<=$rl['siaga3']) { $iconNew="normal"; }
		  else if($wlevel>$rl['siaga3'] && $wlevel<=$rl['siaga2']) { $iconNew="siaga3"; }
		  else if($wlevel>$rl['siaga2'] && $wlevel<=$rl['siaga1']) { $iconNew="siaga2"; }
		  else if($wlevel>$rl['siaga1']) { $iconNew="siaga1"; }		
 	    } else if($tglhrini==$hr1 || $tglhrini==$hr2 || $tglhrini==$hr3){
		$iconNew="Delay";	
	    } else {
	        $iconNew="Offline";
	    }
        //echo $iconNew."<br>";
	$update="update m_lokasi set ReceivedDate='".$rowcek['ReceivedDate']."',ReceivedTime='".$rowcek['ReceivedTime']."',SamplingDate='".$rowcek['SamplingDate']."',SamplingTime='".$rowcek['SamplingTime']."',Rain='".$rowcek['Rain']."',WLevel='".$rowcek['WLevel']."',icon='".$iconNew."',top='".$ML."' where nama_lokasi='".$rl['nama_lokasi']."'";
	//echo "<br>";
	$queryUp=mysql_query($update);
}
?>