<?php
	
	//--------------------------------------------------------------------------------------------------------
	// initialisation des variables
	//--------------------------------------------------------------------------------------------------------
	$Flag_debug_1 = 1;
	$Flag_debug_1 = 0;

	$code_erreur = 0;
	$NB_sondes   = 0 ;
	
	//--------------------------------------------------------------------------------------------------------
	// connexion à la base de données
	//--------------------------------------------------------------------------------------------------------
	$mysql_base ='AQCM05_SITE_' . $_SESSION['SITE_ID'] ;
	$link = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
	
	if ( ! $link) {
		
		$code_erreur = 100;
		
		if ($Flag_debug_1 == 1) {
			echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo '</br>';
			echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; echo '</br>';
		}

		echo "Connexion impossible à la base de donnée MySQL !" . PHP_EOL; echo '</br>';
		exit($code_erreur);
	}

	//--------------------------------------------------------------------------------------------------------
	// recherche des sondes du site demandé
	//--------------------------------------------------------------------------------------------------------
	if ( $_SESSION['aqcm_BAT'] == 'B99' ) {

		if ( $_SESSION['aqcm_NIVEAU'] == 'N99' ) {
			$query = "
				SELECT aqcm_MAC,aqcm_NAME,aqcm_IP_SONDE,aqcm_MESURES,aqcm_BAT,aqcm_NIVEAU,aqcm_PWD,aqcm_HW,aqcm_MAIL,aqcm_T_MAX,aqcm_RX_WARNING_EN 
				FROM AQCM05_SONDES_MESURES 
				WHERE (aqcm_SITE_ID='" . $_SESSION['SITE_ID'] . "') ORDER BY  aqcm_NAME" ;
		} else {
			$query = "
				SELECT aqcm_MAC,aqcm_NAME,aqcm_IP_SONDE,aqcm_MESURES,aqcm_BAT,aqcm_NIVEAU,aqcm_PWD,aqcm_HW,aqcm_MAIL,aqcm_T_MAX,aqcm_RX_WARNING_EN 
				FROM AQCM05_SONDES_MESURES 
				WHERE (aqcm_SITE_ID='" . $_SESSION['SITE_ID'] . "' AND aqcm_NIVEAU='" . $_SESSION['aqcm_NIVEAU'] . "') ORDER BY  aqcm_NAME" ;
		}

	} 
	else {
		if ( $_SESSION['aqcm_NIVEAU'] == 'N99' ) {
			$query = "
				SELECT aqcm_MAC,aqcm_NAME,aqcm_IP_SONDE,aqcm_MESURES,aqcm_BAT,aqcm_NIVEAU,aqcm_PWD,aqcm_HW,aqcm_MAIL,aqcm_T_MAX,aqcm_RX_WARNING_EN 
				FROM AQCM05_SONDES_MESURES 
				WHERE (aqcm_SITE_ID='" . $_SESSION['SITE_ID'] . "' AND aqcm_BAT='" . $_SESSION['aqcm_BAT'] . "') ORDER BY  aqcm_NAME";
		} else {
			$query = "
				SELECT aqcm_MAC,aqcm_NAME,aqcm_IP_SONDE,aqcm_MESURES,aqcm_BAT,aqcm_NIVEAU,aqcm_PWD,aqcm_HW,aqcm_MAIL,aqcm_T_MAX,aqcm_RX_WARNING_EN 
				FROM AQCM05_SONDES_MESURES 
				WHERE (aqcm_SITE_ID='" . $_SESSION['SITE_ID'] . "' AND aqcm_BAT='" . $_SESSION['aqcm_BAT'] . "' AND aqcm_NIVEAU='" . $_SESSION['aqcm_NIVEAU'] . "') ORDER BY  aqcm_NAME";
		}
	}		

	//echo $query . "</br>" ;

	$code_erreur = 0;

	$result_configuration = mysqli_query($link, $query); 
	
	if (!$result_configuration) {
		
		$code_erreur = 11;
		
	} else {
		
		if (mysqli_num_rows($result_configuration) == 0) {	
			$code_erreur = 12;
			
		} else {
			
			$i=0;
			while ($row = mysqli_fetch_array($result_configuration, MYSQLI_BOTH)) {

				$aqcm_one_site_MAC[$i]				= $row["aqcm_MAC"] ;
				$aqcm_one_site_NAME[$i]				= $row["aqcm_NAME"] ;
				$aqcm_one_site_IP_SONDE	[$i]		= $row["aqcm_IP_SONDE"] ;
				$aqcm_one_site_MESURES[$i]			= $row["aqcm_MESURES"] ;
				$aqcm_one_site_BAT[$i]				= $row["aqcm_BAT"] ;
				$aqcm_one_site_NIVEAU[$i]			= $row["aqcm_NIVEAU"] ;
				$aqcm_one_site_PWD[$i]				= $row["aqcm_PWD"] ;
				$aqcm_one_site_HW[$i]				= $row["aqcm_HW"] ; 
				$aqcm_one_site_MAIL[$i]				= $row["aqcm_MAIL"] ; 
				$aqcm_one_site_T_MAX[$i]			= date("d/m/Y", $row["aqcm_T_MAX"]) ; 
				$aqcm_one_site_RX_WARNING_EN[$i]	= $row["aqcm_RX_WARNING_EN"] ; 
				
				if ($Flag_debug_1 == 1) {
					echo "i = " . $i . " ; " . $aqcm_one_site_MAC[$i] . " ; " . $aqcm_one_site_NAME[$i] . "</br>";
				}
				
				$i++ ;
				
			}
			
			$NB_sondes = $i;
			
			if ($Flag_debug_1 == 1) {
				echo "NB_sondes = " . $NB_sondes . "</br>";
				
			}
		}
	}

	if ($Flag_debug_1 == 1) {
		if ( $code_erreur != 0 ) {
			echo "code erreur = " . strval($code_erreur) . "</br>";
		}
	}

	mysqli_close($link);

?>
