Codice PHP:
<?php require_once('Connections/LocalConn.php'); ?>
<?php
//PATH
$PATH_TO = "/var/www/TOSEND/";
// Definizione variabili
$file_to_send = "";
$Massimo = "0";
$to_send = 0;
// Definizione array contenente coppia CHIAVE=>VALORE corrispondenti a
// NOME_TABELLA=>FILE_RELATIVO_DATETIME
$tables = array(
"archive"=>$PATH_TO."datetime_files/archive_datetime.txt",
"baromPressure"=>$PATH_TO."datetime_files/baromPressure_datetime.txt",
"dewPoint"=>$PATH_TO."datetime_files/dewPoint_datetime.txt",
"ET"=>$PATH_TO."datetime_files/ET_datetime.txt",
"hail"=>$PATH_TO."datetime_files/hail_datetime.txt",
"hailRate"=>$PATH_TO."datetime_files/hailRate_datetime.txt",
"heatIndex"=>$PATH_TO."datetime_files/heatIndex_datetime.txt",
"inHumidity"=>$PATH_TO."datetime_files/inHumidity_datetime.txt",
"inTemp"=>$PATH_TO."datetime_files/inTemp_datetime.txt",
"noaahistory"=>$PATH_TO."datetime_files/noaahistory_datetime.txt",
"outHumidity"=>$PATH_TO."datetime_files/outHumidity_datetime.txt",
"outTemp"=>$PATH_TO."datetime_files/outTemp_datetime.txt",
"rain"=>$PATH_TO."datetime_files/rain_datetime.txt",
"rainRate"=>$PATH_TO."datetime_files/rainRate_datetime.txt",
"solarRadiation"=>$PATH_TO."datetime_files/solarRadiation_datetime.txt",
"UV"=>$PATH_TO."datetime_files/UV_datetime.txt",
"windChill"=>$PATH_TO."datetime_files/windChill_datetime.txt",
"windDir"=>$PATH_TO."datetime_files/windDir_datetime.txt",
"windGust"=>$PATH_TO."datetime_files/windGust_datetime.txt",
"windSpeed"=>$PATH_TO."datetime_files/windSpeed_datetime.txt"
);
// Key è il nome della tabella
// Value è il nome del file che contiene il datetime massimo
foreach($tables as $key=>$value){
/*********************************************************************************
**********************************************************************************
**
** 1) Lettura datetime da file
** 2) Esecuzione query
** 3) Memorizzazione mysqldump in file sql
**
**********************************************************************************
*********************************************************************************/
// Lettura e memorizzazione ora
$fp = fopen($value,"r");
$last_hour = fread($fp,filesize($value));
fclose($fp);
mysql_select_db($database_LocalConn, $LocalConn);
$query_Table = "SELECT ".$key.".*
FROM ".$key."
WHERE ".$key.".dateTime>'".$last_hour."'";
$Table = mysql_query($query_Table, $LocalConn) or die(mysql_error());
$row_Table = mysql_fetch_assoc($Table);
$totalRows_Table = mysql_num_rows($Table);
//Nome file da spedire
$file_name = "".$PATH_TO.""."file_".str_replace(":","_",$row_Table['id_station']).".sql";
$file_to_send = $file_name;
if( $totalRows_Table > 0 ){
//$fp_send = fopen("".$file_to_send."","a");
$stringa = "mysqldump --skip-triggers --compact --no-create-info --insert-ignore -u cbaldinu wview_db ".$key." --where='dateTime>".$last_hour."'";
//fwrite($fp_send,$stringa);
exec($stringa." >> ".$file_to_send);
$to_send = 1;
//fclose($fp_send);
}
}//Fine foreach
/**********************************************************************************************
***********************************************************************************************
**
** TEST PER CONTROLLARE OGNI QUALVOLTA VIENE SPEDITO IL FILE
**
***********************************************************************************************
**********************************************************************************************/
$fp_test = fopen("".$PATH_TO."logs/log_".date('d-m-Y H:i').".txt","a");
fwrite($fp_test,"CIAO, SONO LE ORE ".date('d-m-Y H:i:s')."");
if($to_send == 1){
// Calcolo il valore md5 del file
// Questo valore viene spedito insieme al file con il metodo post
$md5 = md5_file($file_to_send);
//$md5 = exec("md5sum ".$file_to_send."");
fwrite($fp_test,"\n\nFile creato alle ore ".date('d-m-Y H:i:s')." e md5 calcolato = ".$md5."\n");
/**********************************************************************************************
***********************************************************************************************
**
** 1) TRASFERIMENTO FILE SQL AD OPENSHIFT
** 2) SE IL TRASFERIMENTO ED IL CARICAMENTO SUL DATABASE DI OPENSHIFT è ANDATO A BUON FINE
** POSSO ESEGUIRE L'UPDATE DEL DATETIME NEI FILE DELLE TABELLE
** 3) ALTRIMENTI CANCELLO IL FILE SQL GENERATO IN QUANTO VERRA' RIGENERATO AL PROSSIMO CICLO
**
***********************************************************************************************
**********************************************************************************************/
$dati_post = array(
"file_dati"=>"@".$file_to_send,
"md5" => $md5
);
fwrite($fp_test,"Eseguo curl_init\n");
$ch = curl_init();
fwrite($fp_test,"Contatto la pagina ricevi.php sul cloud\n");
curl_setopt($ch, CURLOPT_URL, "http://PAGINA CHE STA SUL CLOUD.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dati_post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//json contiene il valore true o false a seconda che il caricamento sia andato a buon fine oppure no
$json = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
/*********************************************************************************
**********************************************************************************
**
** SE JSON CONTIENE TRUE ALLORA IL CARICAMENTO FILE è ANDATO A BUON FINE
** E POSSO AGGIORNARE I FILE CON I DATETIME
**
** ALTRIMENTI CANCELLO IL FILE SQL SENZA AGGIORNARE I DATETIME
**
**********************************************************************************
*********************************************************************************/
if( $json ){
fwrite($fp_test,"Il cloud ha risposto con un TRUE alle ore ".date('d-m-Y H:i:s')."\n");
// Key è il nome della tabella
// Value è il nome del file che contiene il datetime massimo
foreach($tables as $key=>$value){
//Cancello il vecchio file ed aggiorno il datetime
//exec("rm ".$value);
unlink($value);
mysql_select_db($database_LocalConn, $LocalConn);
$query_MaxDate = "SELECT MAX(".$key.".dateTime) AS MaxDate
FROM ".$key."";
$MaxDate = mysql_query($query_MaxDate, $LocalConn) or die(mysql_error());
$row_MaxDate = mysql_fetch_assoc($MaxDate);
$totalRows_MaxDate = mysql_num_rows($MaxDate);
$fp = fopen($value,"w");
if( isset($row_MaxDate['MaxDate']) ){
$Massimo = $row_MaxDate['MaxDate'];
fwrite($fp,$row_MaxDate['MaxDate']);
fwrite($fp_test,"Sto aggiornando il file del DateTime relativo a ".$key." con il valore ".$Massimo."\n");
}else{
$Massimo = "0";
fwrite($fp,$Massimo);
}
fclose($fp);
}//Fine foreach
fwrite($fp_test,"Cancello il file ".$file_to_send."\n");
//exec("rm ".$file_to_send."");
unlink($file_to_send);
}//Fine if JSON
else{
//Cancello semplicemente il file sql creato che andrà ricreato al cron successivo
fwrite($fp_test,"Il cloud ha risposto con un FALSE alle ore ".date('d-m-Y H:i:s')."\n");
fwrite($fp_test,"Cancello il file ".$file_to_send."\n");
//exec("rm ".$file_to_send);
unlink($file_to_send);
}
if (curl_errno($ch))
{
fwrite($fp_test,"Errore in CURL, codice = ".curl_error($ch)."\n");
unlink($file_to_send);
}
fwrite($fp_test,"Chiudo la connessione curl, ci rivediamo NEXT TIME\n");
curl_close($ch);
fwrite($fp_test,"Il CLOUD RISPONDE : \n".$json."\n");
}//Fine if to_send
else{
fwrite($fp_test,"Il file non è stato creato perchè non c'erano record nelle tabelle\n\n");
}
fclose($fp_test);
?>