<?php
	
	//-----------------------------------------------------------------------------
	$sent_SITE_ID  	 = $_GET["SITE_ID"];
	$sent_SERVER_PWD = $_GET["SITE_PW"];
	$mac			 = $_GET["MAC"];
	
	$periode	 	 = $_GET["PERIODE"];
	$marqueur_temps	 = $_GET["TIMESTAMP"];
	$nb				 = $_GET["NB"];
	
	$temp	= isset($_GET["T"])		? $_GET["T"]	: "0.0" ;
	$humi	= isset($_GET["HR"])	? $_GET["HR"]	: "0.0" ;
	$co2	= isset($_GET["CO2"])	? $_GET["CO2"]	: "400" ;
	$cov	= isset($_GET["COV"])	? $_GET["COV"]	: "0.0" ;
	$pm25	= isset($_GET["PM25"])	? $_GET["PM25"]	: "0.0" ;
	$pm10	= isset($_GET["PM10"])	? $_GET["PM10"]	: "0.0" ;
	$hcho	= isset($_GET["HCHO"])	? $_GET["HCHO"]	: "0.0" ;
	$lux	= isset($_GET["LUX"])	? $_GET["LUX"]	: "0.0" ;
	$spl	= isset($_GET["SPL"])	? $_GET["SPL"]	: "0.0" ;
	$nox	= isset($_GET["NOX"])	? $_GET["NOX"]	: "0.0" ;

	//-----------------------------------------------------------------------------
	include ("../_identifiants/sql_identification_sans_base.php");
	$mysql_base ='AQCM05_SITE_' . $sent_SITE_ID;

	$link = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
	
	if (!$link) {
		$code_erreur = 1;
		//echo( "connexion à la base " . $mysql_base . " impossible !");
		echo "NOK";
		exit($code_erreur);
	} 
	
	//-----------------------------------------------------------------------------
	include ("./get_sonde_exists_and_ok_hash.php");
	
	if ( $sonde_exists_erreur == 0 ) {
		
		$table_cible = $mac . "_mesures";

		date_default_timezone_set('Europe/Paris');
		$year  = date('Y', $marqueur_temps);
		$month = date('m', $marqueur_temps);
		$day   = date('d', $marqueur_temps);
		$heure = date('H:i:s', $marqueur_temps);

		$query = "
			INSERT INTO " . $table_cible . " 
				(
					periode,temps,annee,mois,jour,heure,Nb,
					T, HR, CO2, COV_IAQ, PM25, PM10, HCHO, LUX, SPL, NOX_IAQ
				) 
			VALUES 
				(
					'$periode','$marqueur_temps','$year','$month','$day','00:00:00','$nb',
					'$temp','$humi','$co2','$cov','$pm25','$pm10','$hcho','$lux','$spl','$nox'
				)
			";

		
		$result = mysqli_query($link, $query);
		if (!$result) 
		{
			//echo($query . "</br>") ;
			echo ("NOK");
		}
		else
		{	
			echo ("OK");
		}   

		mysqli_close($link);
		
	}
	else {
		//echo "AQCM_get_sonde_exists" . "</br>" ;
		echo "NOK";
	}

?>
