<?php

	// https://www.aqcm.fr/v2/dashboard/JSON_get_date_IAQ_sonde.php

	// On prolonge la session
	include("../M0/session_start.php") ;
	
?>

<?php

	header("Content-type: text/json");

	//-------------------------------------------------------------------------
	$code_erreur = 0;
	date_default_timezone_set('Europe/Paris');

	//-------------------------------------------------------------------------
	include ("../_identifiants/sql_identification_sans_base.php");
	$mysql_base ='AQCM05_SITE_' . $_SESSION['SITE_ID'] ;
	

	$link = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
	if (!$link) {
		echo "Connexion impossible à la base de donnée MySQL !" . PHP_EOL; echo '</br>';
		echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo '</br>';
		echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; echo '</br>';
		exit;
	}

	$query = "SELECT temps,T,HR,CO2,P_mer,COV_IAQ,PM25,HCHO,LUX,SPL,NOX_IAQ FROM " . $_SESSION['aqcm_ONE_SONDE_MAC'] . "_mesures" . " WHERE ( periode = 'LIVE' )";

	$result = mysqli_query($link, $query); 
	if (!$result) {	
		$code_erreur = 1;
		echo "MyQL = " . $code_erreur . "<br>";
		echo "</br>"; 
	}
	else {
		if (mysqli_num_rows($result) != 1) {	
			$code_erreur = 2;
			echo "MyQL = " . $code_erreur . "<br>";
			echo "</br>"; 
		}
		else {
			
			$row = mysqli_fetch_array($result, MYSQLI_BOTH);
			
			$time_now = $row["temps"];
			$date_sonde = date("d/m/Y", $time_now);
			$time_sonde = date("H:i:s", $time_now);
			$date_time_sonde = $date_sonde . "&nbsp;&nbsp;&nbsp;~&nbsp;&nbsp;&nbsp;" . $time_sonde;

			$T				= $row["T"] ;
			$HR				= $row["HR"] ;
			$CO2			= $row["CO2"] ;
			$P_mer			= $row["P_mer"] ;
			$COV_IAQ		= $row["COV_IAQ"] ;
			$PM25			= $row["PM25"] ;
			$HCHO			= $row["HCHO"] ;
			$LUX			= $row["LUX"] ;
			$SPL			= $row["SPL"] ;
			$NOX_IAQ		= $row["NOX_IAQ"] ;
			
		}
	}

	$retour = array($code_erreur,$date_time_sonde);

	echo json_encode($retour);

?>