<?php
	
	// http://www.aqcm.fr/v2/M0/get_sondes_of_one_site.php
	// http://192.168.1.70/inscriptions/inscriptions_get_users_of_1_site.php
	
	/*
	$p_aqcm_SITE_ID = "05230_002";
	$p_aqcm_SITE_ID = "05000_001";
	*/
	
	//--------------------------------------------------------------------------------------------------------
	// initialisations des variables
	//--------------------------------------------------------------------------------------------------------
	$Flag_debug_A = true ;
	$Flag_debug_A = false ;

	$NB_sondes    = 0 ;
	$link_AQCM05_OK = false ;
	$get_sondes_of_one_site_erreur  = 0 ;
	$get_sondes_of_one_site_message = "" ;
	
	$marqueur_temps_maintenant = time();
	$marqueur_temps_maintenant = mktime(0, 0, 0, date("m", $marqueur_temps_maintenant)  , date("d", $marqueur_temps_maintenant) + 1, date("Y", $marqueur_temps_maintenant) );

	//--------------------------------------------------------------------------------------------------------
	// connexion à la base de données du site
	//--------------------------------------------------------------------------------------------------------
	include ("../_identifiants/sql_identification_sans_base.php");

	$conn_AQCM05	= new mysqli( $mysql_host, $mysql_user, $mysql_password );
	$mysql_base	= "AQCM05_SITE_" . $_SESSION['SITE_ID'] ;
	
	if ( empty ( mysqli_fetch_array( mysqli_query( $conn_AQCM05, "SHOW DATABASES LIKE '$mysql_base'" ) ) ) ) {
		$get_sondes_of_one_site_erreur  = 1 ;
		$get_sondes_of_one_site_message = "MySQL : le site " . $_SESSION['SITE_ID'] . " n'existe pas !!!" ;
	}

	if ( $get_sondes_of_one_site_erreur == 0 ) {
		
		$link_AQCM05 = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
		
		if ( ! $link_AQCM05 ) {
			
			$get_sondes_of_one_site_erreur  = 2 ;
			$get_sondes_of_one_site_message = "MySQL : connexion au site " . $_SESSION['SITE_ID'] . " : échec !!!" ;
			
			if ( $Flag_debug_A ) {
				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>';
			}

		}
		else {
			$link_AQCM05_OK = true ;
		}
	}

	//--------------------------------------------------------------------------------------------------------
	// recherche des sondes du site demandé
	//--------------------------------------------------------------------------------------------------------
	if ( $link_AQCM05_OK ) {

		if ( $_SESSION['aqcm_BAT'] == 'B99' ) {

			if ( $_SESSION['aqcm_NIVEAU'] == 'N99' ) {
				$query = "
					SELECT aqcm_MAC,aqcm_NAME,aqcm_FW_UPDATE_FF 
					FROM AQCM05_SONDES_FIRMWARE 
					WHERE (aqcm_SITE_ID='" . $_SESSION['SITE_ID'] . "') ORDER BY  aqcm_NAME" ;
			} else {
				$query = "
					SELECT aqcm_MAC,aqcm_NAME,aqcm_FW_UPDATE_FF 
					FROM AQCM05_SONDES_FIRMWARE 
					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_FW_UPDATE_FF 
					FROM AQCM05_SONDES_FIRMWARE 
					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_FW_UPDATE_FF 
					FROM AQCM05_SONDES_FIRMWARE 
					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>" ;
		

		$result = mysqli_query($link_AQCM05, $query) ; 
		
		if ( $result ) {
			
			if (mysqli_num_rows($result) > 0) {	
				
				$i = 0 ;
				
				while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {

					$aqcm_MAC[$i]				= $row["aqcm_MAC"];
					$aqcm_NAME[$i]			= $row["aqcm_NAME"];
					$aqcm_FW_UPDATE_FF[$i]	= $row["aqcm_FW_UPDATE_FF"];
					
					if ($Flag_debug_A) {
						echo("i=" . $i . " ; " . $aqcm_MAC[$i] . " ; " . $aqcm_NAME[$i] . "<br>" );
					}
					
					$i++ ;
				}
				
				$NB_sondes = $i ;

			}
			else {
				
				$get_sondes_of_one_site_erreur  = 3 ;
				//$get_sondes_of_one_site_message = "résultat de la requête sur $p_aqcm_SITE_ID : vide !!!" ;
				$get_sondes_of_one_site_message = "aucune sonde ne correspond aux critères de sélection !!!" ;
			}


		}
		else {
			
			$get_sondes_of_one_site_erreur  = 4 ;
			$get_sondes_of_one_site_message = "MySQL : requête sur le site $p_aqcm_SITE_ID : échec !!!" ;

		}

	}
	
	//--------------------------------------------------------------------------------------------------------
	// clôture
	//--------------------------------------------------------------------------------------------------------
	if ($Flag_debug_A) {
		echo "NB_sondes = " . $NB_sondes . "<br>";
		echo( $get_sondes_of_one_site_erreur . " ; " . $get_sondes_of_one_site_message . "<br>") ;
	}
	
	if ( $link_AQCM05_OK ) {
		mysqli_close($link_AQCM05);
	}

	//--------------------------------------------------------------------------------------------------------
	// retour $NB_sondes, $get_sondes_of_one_site_erreur, $get_sondes_of_one_site_message
	//--------------------------------------------------------------------------------------------------------

?>
