<?php
	
	// https://www.aqcm.fr/v2/icone/all_sites_ICONE_execute_cron_script.php
	
	//-------------------------------------------------------------------------------------------
	$flag_debug_ASIECS = false;
	$flag_debug_ASIECS = true;

	$code_erreur_ASIECS = 0 ;
	$message		= "OK..." . "<br>\r\n" ;
	
	//-------------------------------------------------------------------------------------------
	$timestamp_now	= time();
	$annee_now		= date("Y", $timestamp_now);
	$mois_now			= date("m", $timestamp_now);
	$jour_now			= date("d", $timestamp_now);
	
	$timestamp_seek	= mktime(0, 0, 0, $mois_now, $jour_now, $annee_now);

	if ( date("w", $timestamp_seek) == 1 ) {
		
		$timestamp_lundi_avant_dernier = mktime(0, 0, 0, $mois_now, $jour_now - 7, $annee_now);
		
	}
	else {

		$jj = 0 ;
		while ( date("w", $timestamp_seek) != 1 ) {
			$jj++;
			$timestamp_seek	= mktime(0, 0, 0, $mois_now, $jour_now - $jj, $annee_now);
		}
		$timestamp_lundi_avant_dernier = mktime(0, 0, 0, $mois_now, $jour_now - $jj - 7, $annee_now);

	}
	
	if ( $flag_debug_ASIECS ) {
		echo( date("w - d/m/Y", $timestamp_lundi_avant_dernier) );
		echo("<br>\r\n");
	}

	$annee_lundi_avant_dernier		= date("Y", $timestamp_lundi_avant_dernier);
	$mois_lundi_avant_dernier		= date("m", $timestamp_lundi_avant_dernier);
	$jour_lundi_avant_dernier		= date("d", $timestamp_lundi_avant_dernier);

	//-----------------------------------------------------
	function hoursandmins($time, $format = '%02d:%02d') {
		$hours = floor($time / 60);
		$minutes = ($time % 60);
		return sprintf($format, $hours, $minutes);
	}

	//-------------------------------------------------------------------------------------------
	// recensement des sites
	//-------------------------------------------------------------------------------------------
	include("../M0/get_sites.php");
	
	if ( $flag_debug_ASIECS ) {
		echo("recensement des sites : ");
		print_r($aqcm_SITES_ID); 	
		echo("<br><br>\r\n");
	}

	
	for ($index_sites = 1; $index_sites < sizeof($aqcm_SITES_ID); $index_sites++) {

		//-------------------------------------------------------------------------------------------
		$code_erreur_ASIECS = 0;
		
		//-------------------------------------------------------------------------------------------
		// recensement des sondes
		//-------------------------------------------------------------------------------------------
		$p_aqcm_SITE_ID	= $aqcm_SITES_ID[$index_sites];
		
		include("../M0/get_sondes_of_one_site.php");
		$p_aqcm_MAC	= $aqcm_MAC ;
		$p_aqcm_NAME	= $aqcm_NAME ;
		
		if ( $get_sondes_of_one_site_erreur != 0 ) {
			if ( $flag_debug_ASIECS ) {
				$code_erreur_ASIECS = 1;
				print_r($p_aqcm_SITE_ID);echo("<br>\r\n");
				echo("anomalie !!!" );
				echo("<br><br>\r\n");
			}
		}
		else {
			if ( $flag_debug_ASIECS ) {
				print_r($p_aqcm_SITE_ID);echo("<br>\r\n");
				print_r($p_aqcm_NAME);echo("<br>\r\n");
				print_r($p_aqcm_MAC);echo("<br>\r\n");
				print_r(sizeof($p_aqcm_MAC));echo("<br>\r\n");
			}
		}


		//-------------------------------------------------------------------------------------------
		// bilans
		//-------------------------------------------------------------------------------------------
		if ( $code_erreur_ASIECS == 0 ) {
			
			include( "../_identifiants/sql_identification_sans_base.php") ;
			$mysql_base = "AQCM05_SITE_" . $p_aqcm_SITE_ID ;
			
			$lien = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
		
			if ( $lien ) {
				
				if ( $flag_debug_ASIECS ) {
					echo( "Connexion BDD OK..." . "</br>\r\n" );
				}
				
				//-----------------------------------------------------
				for ($index_sondes = 0; $index_sondes < sizeof($p_aqcm_MAC); $index_sondes++) {

					if ( $flag_debug_ASIECS ) {
						echo( "&nbsp;sonde : " . $p_aqcm_NAME[$index_sondes]); echo("<br>\r\n");
					}
					
					$p_MAC_SONDE = $p_aqcm_MAC[$index_sondes];
					
					include("../M0/get_parameters_of_one_sonde.php") ;
					$p_aqcm_SPL_TRIG_MODE_BIQUO	= $aqcm_SPL_TRIG_MODE_BIQUO ;
					$p_aqcm_SPL_TRIG_MOY 			= $aqcm_SPL_TRIG_MOY ;
					$p_aqcm_SPL_TRIG_MAX 			= $aqcm_SPL_TRIG_MAX ;
					$p_aqcm_CO2_TRIG_LOW 			= $aqcm_CO2_TRIG_LOW ;
					$p_aqcm_CO2_TRIG_HIGH			= $aqcm_CO2_TRIG_HIGH ;
					$p_aqcm_SCHEDULER 				= $aqcm_SCHEDULER ;

					
					//-----------------------------------------------------
					if ( $flag_debug_ASIECS ) {
						echo("&nbsp;<u>bilan hebdomadaire :</u>" ) ;echo("<br>\r\n");
					}
					
					include("./ICONE_one_week_routine_without_session.php") ;
					
					if ( $flag_debug_ASIECS ) {
						echo("=======================================<br>\r\n");
					}

				}

				mysqli_close($lien);
				
			}
			else {
				
				$code_erreur	= 31;
				$message		= "connexion à la base de données : erreur !!! " . "(#" . $strval($code_erreur) .  ")<br>\r\n" ;
				
			}

		}
		
		if ( $flag_debug_ASIECS ) {
			echo("<br><br>\r\n");
		}
	
	}

 ?>
