<?php

	$new_RX_MESURES	= [];
	$new_RX_STATUS	= [];
	$new_RX_ALERT		= [];

	echo("</br>\r\n");
	echo("détection de l'état des sondes :");
	echo("</br>\r\n");


	if ( $link_AQCM05_OK && ( $NB_sondes > 0 ) ) {

		for ( $index_sonde = 0; $index_sonde < $NB_sondes; $index_sonde++ ) {
			
			$mysql_table_mesures[$index_sonde]	= $aqcm_MAC[$index_sonde] . "_mesures" ;
			$nom_sonde[$index_sonde] 		   		= $aqcm_NAME[$index_sonde] ;

			$query = "
				SELECT CO2 
				FROM " . $mysql_table_mesures[$index_sonde] . "
				WHERE ( temps = '" . $marqueur_temps_10_MN . "' ) 	
			";
			
			//echo($query . "</br>\r\n");
			
			$result_mesures = mysqli_query($link_AQCM05, $query) ;
			
			if ( ! $result_mesures ) {
				
				$FLAG_RX = true ;

			} 
			else {
				
				if ( mysqli_num_rows( $result_mesures ) == 1 ) {
					
					$FLAG_RX = false ;
					
				} 
				else {

					$FLAG_RX = true ;
					
				}
			}

			echo("sonde : " . $aqcm_NAME[$index_sonde] . " ; " . $aqcm_MAC[$index_sonde] . " ; " );
			if ( $FLAG_RX ) {
				echo("not OK !!! ; ");
				$string_mesures = "0";
			}
			else {
				echo("    OK ... ; ");
				$string_mesures = "1";
			}
			
			$new_RX_MESURES[$index_sonde] = substr( $aqcm_RX_MESURES[$index_sonde] . $string_mesures, 1 ) ;
			echo( $new_RX_MESURES[$index_sonde] . "</br>\r\n" );
			
			if     ( $new_RX_MESURES[$index_sonde] == "000" ) {
				$new_RX_STATUS[$index_sonde] = "OFF" ;
			}
			elseif ( $new_RX_MESURES[$index_sonde] == "111" ) {
				$new_RX_STATUS[$index_sonde] = "ON" ;
			}
			else {
				$new_RX_STATUS[$index_sonde] = $aqcm_RX_STATUS[$index_sonde] ;
			}
			
			if ( $new_RX_STATUS[$index_sonde] != $aqcm_RX_STATUS[$index_sonde] ) {
				$new_RX_ALERT[$index_sonde] = "ON" ;
			}
			else {
				$new_RX_ALERT[$index_sonde] = $aqcm_RX_ALERT[$index_sonde] ;
			}

			$query_2 = "
				UPDATE IGNORE AQCM05_SONDES_MESURES 
				SET 
					aqcm_RX_MESURES='"	. $new_RX_MESURES[$index_sonde]	. "',
					aqcm_RX_STATUS='"	. $new_RX_STATUS[$index_sonde]	. "',
					aqcm_RX_ALERT='"		. $new_RX_ALERT[$index_sonde]		. "' 
				WHERE ( aqcm_MAC = '".$aqcm_MAC[$index_sonde]."' )
			";
					
			$result_2 = mysqli_query($link_AQCM05, $query_2) ;

		}
		
	}
	else {
		
		//----------------------------------------------------------------------------------------------------
		// aucune sonde répertoriée en mode RX_WARNING
		//----------------------------------------------------------------------------------------------------
		echo("aucune sonde répertoriée en mode RX_WARNING ! </br>\r\n");
		
		//----------------------------------------------------------------------------------------------------
		
	}



?>
