<?php
session_start();
error_reporting(1);


// while(true){
//     fetchFtpData();
//     //sleep(300);
// }

fetchFtpData();
function fetchFtpData() {
    include('includes/config.php');
    $selectTable = SELECT_TABLE;
	//$ftp_server = "ftp.erave.in";
// 	$ftp_server = "serrax.io";

	 $local_file = "/home/serraxio/serrax.io/serraxuser/Mitrask/gandhidham.csv";

	if (($file = fopen($local_file, "r")) !== FALSE) {
	  echo "file name $file\n";
	  while (($line = fgetcsv($file, 1000, ",")) !== FALSE){ 
	   	//print_r($line);dom
	   	
	   	if ($line[0] !== NULL && $line[1] !== NULL && $line[2] !== NULL 
	   	    && $line[3] !== NULL && $line[4] !== NULL && $line[5] !== NULL && $line[6] !== NULL && $line[7] !== NULL 
	   	    && $line[8] !== NULL && $line[9] !== NULL && $line[10] !== NULL && $line[11] !== NULL && $line[12] !== NULL && $line[13] !== NULL && $line[14] !== NULL && $line[15] !== NULL) {
    	   	$id = rand(pow(10, 16-1), pow(10, 16)-1);	
    	   	//echo "id: ", $id;
    	   	$sql="INSERT INTO  $selectTable(id, TMPmax, TMPavg, TMPmin, RHmax, RHavg, RHmin, WSmax, WSmin, WSavg, RAINavg, Battery, WD, SOLARavg, BPavg, CreationDate) VALUES(:id, :TMPmax, :TMPavg, :TMPmin, :RHmax, :RHavg, :RHmin, :WSmax, :WSmin, :WSavg, :RAINavg, :Battery, :WD, :SOLARavg, :BPavg, :CreationDate)";
    	    //$sql="INSERT INTO  aws(id, TMPmax, TMPavg, TMPmin, RHmax, RHavg, RHmin, WSmax, WSmin, WSavg, BPavg, SOLARavg, RAINavg, Battery, WD) VALUES(:id, :TMPmax, :TMPavg, :TMPmin, :RHmax, :RHavg, :RHmin, :WSmax, :WSmin, :WSavg, :BPavg, :SOLARavg, :RAINavg, :Battery, :WD)";
    
    	   	$dateStr = $line[0]." ".$line[1];
    	   	$dateTime = date("Y-m-d H:i:s", strtotime($dateStr));
    	
    	   	
            $sqlNew = "select * from $selectTable where CreationDate = '$dateTime' ORDER BY $selectTable.`CreationDate` DESC limit 1";
            $query = $dbh->prepare($sqlNew);
            $query->execute();
            $results = $query->fetchAll(PDO::FETCH_OBJ);
            $PrevTime = $results[0]->CreationDate;

    	   
    	    
    	   // if ( strtotime($PrevTime) == strtotime($dateStr) ) {
    	   //     die("error::same time entry\n");
    	   // }
    	    
    	    if ( $PrevTime != "") {
    	        die("error::same time entry\n");
    	    }
    	    
    	    $query = $dbh->prepare($sql);
    	    
    	   	$query->bindParam(':id', $id, PDO::PARAM_INT);
    	   	$query->bindParam(':TMPmax',$line[5], PDO::PARAM_STR);
    	   	$query->bindParam(':TMPavg',$line[6], PDO::PARAM_STR);
    	   	$query->bindParam(':TMPmin',$line[7], PDO::PARAM_STR);
    	   	$query->bindParam(':RHmax',$line[8], PDO::PARAM_STR);
    	   	
    	   	$query->bindParam(':RHavg', $line[9], PDO::PARAM_STR);
    	   	$query->bindParam(':RHmin', $line[10], PDO::PARAM_STR);
    	   	$query->bindParam(':WSmax', $line[2], PDO::PARAM_STR);
    	   	$query->bindParam(':WSmin', $line[4], PDO::PARAM_STR);
    	   	$query->bindParam(':WSavg', $line[3], PDO::PARAM_STR);
    	   	$query->bindParam(':BPavg', $line[13], PDO::PARAM_STR);
    	   	$query->bindParam(':SOLARavg', $line[11], PDO::PARAM_STR);
    	   	$query->bindParam(':RAINavg', $line[14], PDO::PARAM_STR); 
    	   
    	   	$query->bindParam(':Battery', $line[15], PDO::PARAM_STR);
    	   	$query->bindParam(':WD', $line[12], PDO::PARAM_STR);
    	   	$query->bindParam(':CreationDate', $dateTime, PDO::PARAM_STR);
    	   	$res = $query->execute();
    	   	//echo $res;
    	   	if($res)
    	   	{
    	   		$msg = "New entry added successfully $PrevTime, $dateStr";
    	   		echo json_encode(array("success" => $msg));
    	   	}
    	   	else 
    	   	{
    	   		$msg = "Failed to add new entry ";
    	   		echo json_encode(array("error" => $msg));
    	   	}
	   	}
	  }
	  fclose($file);
	} else {
	   die("Unable to read file:: $local_file");
	}
    
}

?>
