<?php
	
	/*
	https://www.aqcm.fr/v2/authorize/sonde_2_base_iaqm_to_AQCM05.php?
	SITE_ID=05130_002&
	MAC=192168721&
	PERIODE=10_MN&
	TIMESTAMP=1698091200&
	NB=5&
	T=20.80|20.80|20.80&
	HR=54.10|54.10|54.10&
	CO2=1987.00|1989.80|1992.00
	*/

	//-----------------------------------------------------------------------------
	$sent_SITE_ID		= $_GET["SITE_ID"];

	$mac				= $_GET["MAC"];
	
	$periode			= $_GET["PERIODE"];
	$marqueur_temps	= $_GET["TIMESTAMP"];
	$nb					= $_GET["NB"];
	
	$temp		= isset($_GET["T"])			? $_GET["T"]		: "20.0|20.0|20.0" ;
	$humi		= isset($_GET["HR"])		? $_GET["HR"]		: "50.0|50.0|50.0" ;
	$co2		= isset($_GET["CO2"])		? $_GET["CO2"]	: "400|400|400" ;

	//-----------------------------------------------------------------------------
	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_only.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
				) 
			VALUES 
				(
					'$periode', '$marqueur_temps','$year','$month','$day','$heure','$nb',
					'$temp','$humi','$co2'
				)
			";

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

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

?>
