<?php  
$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);

 $sumber = 'https://bwssumatera4:hidrologi2018@prinus.net/api/sensor';
 $konten = file_get_contents($sumber);
 $data = json_decode($konten, true);
 for($a=0; $a < count($data); $a++)
   {
   	 $sensor=$data[$a]['sn'];
	 $sumber2 = 'https://bwssumatera4:hidrologi2018@prinus.net/api/sensor/'.$sensor;
	 "<br>==============================================================<br>";
	 $konten2 = file_get_contents($sumber2);
	 $data2 = json_decode($konten2, true);
	 for($b=0; $b < count($data2); $b++)
	   {
		if($sensor=="1908-1"){$tbl="R_nongsa";}
		else if($sensor=="1908-2"){$tbl="R_sekanak2";}
		else if($sensor=="1908-3"){$tbl="R_sribintan";}
		else if($sensor=="1908-4"){$tbl="R_wadukjago";}
		else if($sensor=="1908-9"){$tbl="R_batubi";}
		else if($sensor=="1908-5"){$tbl="WL_sekanak2";}
		else if($sensor=="1908-6"){$tbl="WL_waduk_sei_jago";}
		else if($sensor=="1908-7"){$tbl="WL_sei_jeram";}
		else if($sensor=="1908-8"){$tbl="WL_kolong_enam";}
		

       $cektgl=mysql_query("select * from ".$tbl." WHERE 1=1 ORDER BY ReceivedDate DESC LIMIT 1");
       $jml=mysql_num_rows($cektgl);
       if($jml>=1){
       $insertToSda="insert into ".$tbl." (SELECT DISTINCT
   	                                    '',
                                        b.ReceivedDate,
                                        b.ReceivedTime,
                                        b.SamplingDate,
                                        b.SamplingTime,
                                        b.Rain,
                                        b.WLevel,
                                        b.ML
                                        FROM
                                        ".$tbl." AS a
                                        RIGHT JOIN ".$tbl."_temp AS b ON a.ML = b.ML AND a.SamplingTime = b.SamplingTime AND a.SamplingDate = b.SamplingDate
                                        WHERE 1=1 AND b.ReceivedDate>=(SELECT ReceivedDate FROM ".$tbl." ORDER BY ReceivedDate DESC,ReceivedTime DESC LIMIT 1)
                                        )";
       } else {                               
       $insertToSda="insert into ".$tbl." (SELECT DISTINCT
   	                                    '',
                                        b.ReceivedDate,
                                        b.ReceivedTime,
                                        b.SamplingDate,
                                        b.SamplingTime,
                                        b.Rain,
                                        b.WLevel,
                                        b.ML
                                        FROM
                                        ".$tbl." AS a
                                        RIGHT JOIN ".$tbl."_temp AS b ON a.ML = b.ML AND a.SamplingTime = b.SamplingTime AND a.SamplingDate = b.SamplingDate
                                        WHERE 1=1 AND b.ReceivedDate='".date('Y-m-d')."')";
       }
       echo $insertToSda;
       echo "<br>";
		$queryUp=mysql_query($insertToSda);
	    if($queryUp) { 
	        //echo "berhasil insert<br>";
			mysql_query("DELETE FROM ".$tbl."_temp");
	        //echo "sukses";	
	        
	    }
	    	  //echo "<br>";
	   }
   }
  ?>
