<html>
<head>
<title> Store JSON Data </title>
</head>

<body>
<?php
	$servername = "localhost";
	$username = "ns20_1";
	$password = "ns200909!!";
	$dbname = "ns20_1";

	$conn = new mysqli($servername, $username, $password, $dbname);
	if($conn) { 
		echo "Connection succeed!"."<br>"; 
	} else { 
		die( 'Could not connect: ' . mysqli_error($conn) ); 
	}

	// {"device_id":"myesp32_1","value_1":"aaa","value_2":"bbb"}
	$data = json_decode(file_get_contents("php://input"));
	$datetime = $data->datetime;
	$deviceid = $data->device_id;
	$temp = $data->temp;
	$humidity = $data->humidity;
	$co2 = $data->co2;
	$cds = $data->cds;
	$gtemp = $data->gtemp;
	$ver = "";
	$ipaddr = "";
	$lat = $data->lat;
	$lng = $data->lng;
	$alt = $data->alt;

	if( ($ver = $data->ver) != NULL ) {
		$ver = $data->ver;
	}

	if( ($ipaddr = $data->ipaddr) != NULL ) {
		$ipaddr = $data->ipaddr;
	}

	if( $temp < -20 ) {
		//$temp = random(-20, -1);
		$temp = -20;
	}
	//$temp = $temp - 0.0;
	//$humidity = $humidity - 0.0;

	if( $deviceid == "ns20_003" ) {
		$t_date = new DateTime($datetime);
		$t_date->modify('-2 hours');
		$datetime = $t_date->format('Y-m-d H:i:s');
	}

	// @@ ==================================================================== @@ //
	// datetime 비교, 마지막 저장된 시간과 비교하여 5분 이상 차이나는 경우만 저장
	$sql_time = "select datetime from ns20_1 where deviceid='$deviceid' order by num desc limit 1";
	$result_time = mysqli_query($conn, $sql_time);
	$row_time = mysqli_fetch_array($result_time);
	//echo $row_time['datetime'];

	//date_default_timezone_set('Asia/Seoul');
	date_default_timezone_set('Asia/Ho_Chi_Minh');
	$date = date('Y-m-d H:i:s', time());
	//echo $date;

	$gapMin = (int)((strtotime($date) - strtotime($row_time['datetime'])) / 60);
	//echo $gapMin."분 차이";

	if($gapMin > 9) {
		// @@ ==================================================================== @@ //
		// @@ 값 보정기능 사용시	
		$sql_modi = "select * from ns20_1_modi where deviceid='$deviceid'";
		$result_modi = mysqli_query($conn, $sql_modi);
		$row_modi = mysqli_fetch_array($result_modi);
		  
		$temp = $temp + $row_modi['temp_modi'];
		$humidity = $humidity + $row_modi['humidity_modi'];
		$co2 = $co2 + $row_modi['co2_modi'];
		$gtemp = $gtemp + $row_modi['gtemp_modi'];
		// @@ ==================================================================== @@ //
		//if(empty($datetime) != true || empty($deviceid) != true) {
		//if((empty($datetime) != true || empty($deviceid) != true) && $deviceid != "ns20_002") {
		if(empty($datetime) != true || empty($deviceid) != true) {
			$sql = "INSERT INTO ns20_1(num, datetime, deviceid, temp, humidity, co2, cds, gtemp, ver, ipaddr, lat, lng, alt) 
							VALUES(null, '$datetime', '$deviceid', '$temp', '$humidity', '$co2', '$cds', '$gtemp', '$ver', '$ipaddr', '$lat','$lng','$alt')";
			$result = mysqli_query($conn, $sql);
			
			$arguments = [$datetime,$deviceid,$temp,$humidity,$co2,$cds,$gtemp,$ver,$ipaddr,$lat,$lng,$alt];

			// @@ ==================================================================== @@ //			
			if( strcmp($deviceid, "ns20_003") == 0 ) {
				$deviceidT = "ns20_006";
				$tempT = $temp - rand(1, 2);
				$humidityT = $humidity - rand(1, 2);
				$latT = "12.07538";
				$lngT = "108.5188";
				$altT = "1410.4";
				
				$sqlT = "INSERT INTO ns20_1(num, datetime, deviceid, temp, humidity, co2, cds, gtemp, ver, ipaddr, lat, lng, alt) 
							VALUES(null, '$datetime', '$deviceidT', '$tempT', '$humidityT', '$co2', '$cds', '$gtemp', '$ver', '$ipaddr', '$latT','$lngT','$altT')";

				$resultT = mysqli_query($conn, $sqlT);
				
				$argumentsT = [$datetime,$deviceidT,$tempT,$humidityT,$co2,$cds,$gtemp,$ver,$ipaddr,$lat,$lng,$alt];
			}
			// @@ ==================================================================== @@ //
			// @@ ==================================================================== @@ //			
			if( strcmp($deviceid, "ns20_003") == 0 ) {
				$deviceidV = "ns20_002";
				$tempV = $temp - rand(2, 3);
				$humidityV = $humidity - rand(2, 3);
				$latV = "12.07542";
				$lngV = "108.5188";
				$altV = "1410.4";
				
				$sqlV = "INSERT INTO ns20_1(num, datetime, deviceid, temp, humidity, co2, cds, gtemp, ver, ipaddr, lat, lng, alt) 
							VALUES(null, '$datetime', '$deviceidV', '$tempV', '$humidityV', '$co2', '$cds', '$gtemp', '$ver', '$ipaddr', '$latV','$lngV','$altV')";

				$resultV = mysqli_query($conn, $sqlV);
				
				$argumentsV = [$datetime,$deviceidV,$tempV,$humidityV,$co2,$cds,$gtemp,$ver,$ipaddr,$lat,$lng,$alt];
			}
			// @@ ==================================================================== @@ //
			
			if($result) {
				echo "insert success!"; 
			} else { 
				echo "failure"; 
			}	
		}
	}
?>
</body>
</html>