Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #103 from stawen/unstable
Browse files Browse the repository at this point in the history
fix #37 - Imporved Cron Function
  • Loading branch information
stawen authored Nov 30, 2020
2 parents 09ce6b9 + 3cb5704 commit 320f360
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 165 deletions.
95 changes: 4 additions & 91 deletions _include/administration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public function saveInfoGenerale($s)
$r = [];
$r['response'] = true;

//$ok = file_put_contents(CONTEXT.'/config.json',json_encode($param, JSON_UNESCAPED_SLASHES));
$ok = file_put_contents(CONTEXT.'/config.json', json_encode($param));

if (!$ok) {
Expand Down Expand Up @@ -138,8 +137,8 @@ public function importFileFromChaudiere($s)

$oko = new okofen();

$status = $oko->getChaudiereData('onDemande', $s['url']);
//$status =true;
$status = $oko->getChaudiereData($s['url']);

if ($status) {
$import = $oko->csv2bdd();
} else {
Expand Down Expand Up @@ -333,7 +332,7 @@ public function getDayWithoutSynthese()
public function makeSyntheseByDay($day)
{
$oko = new okofen();
$r['response'] = $oko->makeSyntheseByDay('onDemande', $day);
$r['response'] = $oko->makeSyntheseByDay($day);
$this->sendResponse($r);
}

Expand Down Expand Up @@ -603,7 +602,7 @@ public function checkUpdate()
}

/**
* Function notify stawen's server for making a update.
* Function notify stawen's server for making an update.
*
* @return json
*/
Expand All @@ -629,7 +628,6 @@ public function addOkoStat()
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
//var_dump($resp);
curl_close($curl);
}

Expand All @@ -647,10 +645,8 @@ public function makeUpdate()

$result = $update->update(); //fait une simulation d'abord, si ok ça install
if (true === $result) {
//echo 'Update successful<br>';
$r['install'] = true;
} else { // si echec de la simulation d'install
//echo 'Update failed: ' . $result . '!<br>';
if ($result = AutoUpdate::ERROR_SIMULATE) {
$r['information'] = '<pre>'.var_dump($update->getSimulationResults()).'</pre>';
}
Expand All @@ -669,11 +665,9 @@ public function getFileFromTmp()
$files = scandir('_tmp');
$r = [];
foreach ($files as $f) {
//if ($f == '*.csv'){
if ('.' != $f && '..' != $f && 'matrice.csv' != $f && 'import.csv' != $f && 'readme.md' != $f && 'cookies_boiler.txt' != $f) {
$r[] = $f;
}
//}
}

return $this->sendResponse($r);
Expand All @@ -697,87 +691,6 @@ public function importFileFromTmp($file)
$this->importcsv();
}

// public function getDateForMigrate(){
// ini_set('max_execution_time', 120);
// /*
// SELECT a.jour as jour FROM oko_historique as a ".
// "LEFT OUTER JOIN oko_resume_day as b ON a.jour = b.jour ".
// "WHERE b.jour is NULL AND a.jour <> '".$now."'group by a.jour;";
// */
// $newTableHistorique = "CREATE TABLE IF NOT EXISTS `oko_historique_full` (`jour` DATE NOT NULL,`heure` TIME NOT NULL,`col_2` DECIMAL(6,2) NULL DEFAULT NULL,`col_3` DECIMAL(6,2) NULL DEFAULT NULL,`col_4` DECIMAL(6,2) NULL DEFAULT NULL,`col_5` DECIMAL(6,2) NULL DEFAULT NULL,`col_6` DECIMAL(6,2) NULL DEFAULT NULL,`col_7` DECIMAL(6,2) NULL DEFAULT NULL,`col_8` DECIMAL(6,2) NULL DEFAULT NULL,`col_9` DECIMAL(6,2) NULL DEFAULT NULL,`col_10` DECIMAL(6,2) NULL DEFAULT NULL,`col_11` DECIMAL(6,2) NULL DEFAULT NULL,`col_12` DECIMAL(6,2) NULL DEFAULT NULL,`col_13` DECIMAL(6,2) NULL DEFAULT NULL,`col_14` DECIMAL(6,2) NULL DEFAULT NULL,`col_15` DECIMAL(6,2) NULL DEFAULT NULL,`col_16` DECIMAL(6,2) NULL DEFAULT NULL,`col_17` DECIMAL(6,2) NULL DEFAULT NULL,`col_18` DECIMAL(6,2) NULL DEFAULT NULL,`col_19` DECIMAL(6,2) NULL DEFAULT NULL,`col_20` DECIMAL(6,2) NULL DEFAULT NULL,`col_21` DECIMAL(6,2) NULL DEFAULT NULL,`col_22` DECIMAL(6,2) NULL DEFAULT NULL,`col_23` DECIMAL(6,2) NULL DEFAULT NULL,`col_24` DECIMAL(6,2) NULL DEFAULT NULL,`col_25` DECIMAL(6,2) NULL DEFAULT NULL,`col_26` DECIMAL(6,2) NULL DEFAULT NULL,`col_27` DECIMAL(6,2) NULL DEFAULT NULL,`col_28` DECIMAL(6,2) NULL DEFAULT NULL,`col_29` DECIMAL(6,2) NULL DEFAULT NULL,`col_30` DECIMAL(6,2) NULL DEFAULT NULL,`col_31` DECIMAL(6,2) NULL DEFAULT NULL,`col_32` DECIMAL(6,2) NULL DEFAULT NULL,`col_33` DECIMAL(6,2) NULL DEFAULT NULL,`col_34` DECIMAL(6,2) NULL DEFAULT NULL,`col_35` DECIMAL(6,2) NULL DEFAULT NULL,`col_36` DECIMAL(6,2) NULL DEFAULT NULL,`col_37` DECIMAL(6,2) NULL DEFAULT NULL,`col_38` DECIMAL(6,2) NULL DEFAULT NULL,`col_39` DECIMAL(6,2) NULL DEFAULT NULL,`col_40` DECIMAL(6,2) NULL DEFAULT NULL,`col_41` DECIMAL(6,2) NULL DEFAULT NULL,`col_42` DECIMAL(6,2) NULL DEFAULT NULL,`col_43` DECIMAL(6,2) NULL DEFAULT NULL,`col_44` DECIMAL(6,2) NULL DEFAULT NULL,`col_45` DECIMAL(6,2) NULL DEFAULT NULL,`col_46` DECIMAL(6,2) NULL DEFAULT NULL,`col_47` DECIMAL(6,2) NULL DEFAULT NULL,`col_48` DECIMAL(6,2) NULL DEFAULT NULL,`col_49` DECIMAL(6,2) NULL DEFAULT NULL,`col_50` DECIMAL(6,2) NULL DEFAULT NULL,`col_51` DECIMAL(6,2) NULL DEFAULT NULL,`col_52` DECIMAL(6,2) NULL DEFAULT NULL,`col_53` DECIMAL(6,2) NULL DEFAULT NULL,`col_54` DECIMAL(6,2) NULL DEFAULT NULL,`col_55` DECIMAL(6,2) NULL DEFAULT NULL,`col_56` DECIMAL(6,2) NULL DEFAULT NULL,`col_57` DECIMAL(6,2) NULL DEFAULT NULL,`col_58` DECIMAL(6,2) NULL DEFAULT NULL,`col_59` DECIMAL(6,2) NULL DEFAULT NULL,`col_60` DECIMAL(6,2) NULL DEFAULT NULL,`col_61` DECIMAL(6,2) NULL DEFAULT NULL,`col_62` DECIMAL(6,2) NULL DEFAULT NULL,`col_63` DECIMAL(6,2) NULL DEFAULT NULL,`col_64` DECIMAL(6,2) NULL DEFAULT NULL,`col_65` DECIMAL(6,2) NULL DEFAULT NULL,`col_66` DECIMAL(6,2) NULL DEFAULT NULL,`col_67` DECIMAL(6,2) NULL DEFAULT NULL,`col_68` DECIMAL(6,2) NULL DEFAULT NULL,`col_69` DECIMAL(6,2) NULL DEFAULT NULL,`col_70` DECIMAL(6,2) NULL DEFAULT NULL,`col_71` DECIMAL(6,2) NULL DEFAULT NULL,`col_72` DECIMAL(6,2) NULL DEFAULT NULL,`col_73` DECIMAL(6,2) NULL DEFAULT NULL,`col_74` DECIMAL(6,2) NULL DEFAULT NULL,`col_75` DECIMAL(6,2) NULL DEFAULT NULL,`col_76` DECIMAL(6,2) NULL DEFAULT NULL,`col_77` DECIMAL(6,2) NULL DEFAULT NULL,`col_78` DECIMAL(6,2) NULL DEFAULT NULL,`col_79` DECIMAL(6,2) NULL DEFAULT NULL,`col_80` DECIMAL(6,2) NULL DEFAULT NULL,`col_81` DECIMAL(6,2) NULL DEFAULT NULL,`col_82` DECIMAL(6,2) NULL DEFAULT NULL,`col_83` DECIMAL(6,2) NULL DEFAULT NULL,`col_84` DECIMAL(6,2) NULL DEFAULT NULL,`col_85` DECIMAL(6,2) NULL DEFAULT NULL,`col_86` DECIMAL(6,2) NULL DEFAULT NULL,`col_87` DECIMAL(6,2) NULL DEFAULT NULL,`col_88` DECIMAL(6,2) NULL DEFAULT NULL,`col_89` DECIMAL(6,2) NULL DEFAULT NULL,`col_90` DECIMAL(6,2) NULL DEFAULT NULL,`col_91` DECIMAL(6,2) NULL DEFAULT NULL,`col_92` DECIMAL(6,2) NULL DEFAULT NULL,`col_93` DECIMAL(6,2) NULL DEFAULT NULL,`col_94` DECIMAL(6,2) NULL DEFAULT NULL,`col_95` DECIMAL(6,2) NULL DEFAULT NULL,`col_96` DECIMAL(6,2) NULL DEFAULT NULL,`col_97` DECIMAL(6,2) NULL DEFAULT NULL,`col_98` DECIMAL(6,2) NULL DEFAULT NULL,`col_99` DECIMAL(6,2) NULL DEFAULT NULL,PRIMARY KEY (`jour`, `heure`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
// $this->log->info("Class ".__CLASS__." | ".__FUNCTION__." | ".$newTableHistorique);
// $this->query($newTableHistorique);

// $q = "select distinct(jour) from oko_historique group by jour";

// $result = $this->query($q);
// $r = array();

// if($result){
// while($res = $result->fetch_object()){
// array_push($r,$res);
// }
// }

// $this->sendResponse($r);

// }

// public function migrateDataForDate($jour){
// $r['response']= false;
// $error = false;

// //on recupere la matrice des capteurs
// $ob_capteur = new capteur();
// $capteurs = $ob_capteur->getForImportCsv(); //l'index du tableau correspond a la colonne du capteur dans le fichier csv

// $insert = "INSERT IGNORE INTO oko_historique_full set jour = '".$jour."'";

// //puis apres le jour, les minutes present pour le jour en cours
// $qHeure = "select distinct(heure) from oko_historique where jour = '".$jour."' group by heure";
// $resHeure = $this->query($qHeure);

// while($rHeure = $resHeure->fetch_object()){

// $set = ", heure = '".$rHeure->heure."'";
// //puis pour chaque minutes on recupere pour chaque capteur
// foreach($capteurs as $positionCsv => $capteur){

// $qCapteur = "select value from oko_historique where jour ='".$jour."' AND heure ='".$rHeure->heure."' AND oko_capteur_id=".$capteur['id'];
// //$this->log->debug("_UPGRADE | ".$qCapteur);
// $resCapteur = $this->query($qCapteur);
// $rCapteur = $resCapteur->fetch_object();

// if ($rCapteur == null){
// $value = 'null';
// }else{
// $value = $rCapteur->value;
// }
// $set .= ", col_".$positionCsv."=".$value ;

// }

// //$this->log->debug("_UPGRADE | ".$insert.$set);

// if(!$this->query($insert.$set)){
// $this->log->debug("Class ".__CLASS__." | ".__FUNCTION__." | ".$insert.$set);
// $error = true;
// }

// }
// //on supprime la ligne dans oko_historique si pas d'erreur lors de la migration de la journéee
// if(!$error){
// $q = "delete from oko_historique where jour = '".$jour."'";

// if($this->query($q)){
// $r['response']= true;
// }
// }

// $this->sendResponse($r);

// }

/**
* Function login. Check if user/password is ok.
*
Expand Down
170 changes: 111 additions & 59 deletions _include/okofen.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,15 @@ public function __destruct()
parent::__destruct();
}

/*
* Fonction pour recuperer les fichiers csv present sur la chaudiere
* Cron / OnDemand :
* Cron -> en finction de l'heure d'appel il recupere le fichier du jour ou de la veille
* Ondemande -> recupere celui qui lui est precisé
*/
public function getChaudiereData($trigger = 'cron', $url = '')
// Fonction pour recuperer les fichiers csv present sur la chaudiere
public function getChaudiereData($url)
{
if ('onDemande' != $trigger) {
if ($this->newDay()) {
$today = date('Ymd', mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')));
} else {
$today = date('Ymd'); // 20010310
}

$link = PATH.$today.EXTENTION;
} else {
$link = $url;
}
$link = $url;

$this->log->info('Class '.__CLASS__.' | '.__FUNCTION__.' | Recuperation du fichier '.$link);
//on lance le dl
$result = $this->download($link, CSVFILE);
//$result = true;

if (!$result) {
//throw new Exception('Download error...');
$this->log->error('Class '.__CLASS__.' | '.__FUNCTION__.' | Données chaudiere non recupérées');
Expand All @@ -61,6 +46,51 @@ public function getChaudiereData($trigger = 'cron', $url = '')
return true;
}

/**
* Look into the DB to check if we have the data for the last minute of the data.
* If the minute is missing, then we need to download the file again.
*
* @param type $dataFilename
* @param mixed $dateChoosen
*/
public function isDayComplete($dateChoosen)
{
if (empty($dateChoosen)) {
return false;
}
$sql = "SELECT COUNT(*) FROM oko_historique_full WHERE jour = '{$dateChoosen}' AND heure = '23:59:00'";

$result = $this->query($sql);

if ($result) {
if ($res = $result->fetch_row()) {
return 1 == $res[0];
}
}

return false;
}

/**
* Converts a filename of the form 'touch_20161016.csv' to the corresponding
* date - ie : '2016-10-16'.
*
* @param type $dataFilename
*/
public function getDateFromFilename($dataFilename)
{
$matches = [];
if (preg_match('@touch_([0-9]{4})([0-9]{2})([0-9]{2})\.csv@', $dataFilename, $matches)) {
$year = $matches[1];
$month = $matches[2];
$day = $matches[3];

return "{$year}-{$month}-{$day}";
}

return false;
}

// integration du fichier csv dans okovision
//V1.3.0
public function csv2bdd()
Expand All @@ -80,7 +110,6 @@ public function csv2bdd()
$start_cycle = 0;
$nbColCsv = count($capteurs);

//$insert = "INSERT IGNORE INTO oko_historique (jour,heure,oko_capteur_id,value) VALUES ";
$insert = 'INSERT IGNORE INTO oko_historique_full SET ';
while (!feof($file)) {
$ligne = fgets($file);
Expand All @@ -91,8 +120,6 @@ public function csv2bdd()
$colCsv = explode(CSV_SEPARATEUR, $ligne);

if (isset($colCsv[1])) { //test si ligne non vide
//$nbColCsv = count($colCsv);

$jour = $colCsv[0];
$heure = $colCsv[1];

Expand Down Expand Up @@ -139,31 +166,32 @@ public function csv2bdd()
return true;
}

// Fonction lancant les requettes de synthèse du jour, elle ne s'active que si nous sommes dans le traitement de minuit. Elle fera la synthese
// des jours precedents.

public function makeSyntheseByDay($trigger = 'cron', $dayChossen = null)
/**
* Fonction lancant les requettes de synthèse du jour, elle ne s'active
* que si la date demandée est dans le passé.
*
* @param string $dateChoosen A date of the form '2015-10-25'
* @param bool $bForce If true, the synthese will be rebuilt even if it
* exists already
*
* @return bool
*/
public function makeSyntheseByDay($dateChoosen = null, $bForce = true)
{
if ($this->newDay() && 'cron' == $trigger) {
//Il est minuit nous lançons la synthèse, et prenons la date d'hier
$day = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')));
} elseif ('onDemande' == $trigger) {
//on ne fait rien si la date choisie est la date du jour
if ($dayChossen == date('Y-m-d', mktime(0, 0, 0, date('m'), date('d'), date('Y')))) {
return false;
}
//on ne fait rien si la date choisie est la date du jour
if ($dateChoosen == date('Y-m-d', mktime(0, 0, 0, date('m'), date('d'), date('Y')))) {
return false;
}

//si c'est une demande manuelle, nous prenons la date choisie
$day = $dayChossen;
if (!$this->deleteSyntheseDay($day)) {
return false;
}
} else {
//si cron, mais pas new day, ou si pas ondemande, nous ne faisons rien
return;
if (!$bForce && $this->isSyntheseDone($dateChoosen)) {
return true;
}
// On supprime les data éventuels
if (!$this->deleteSyntheseDay($dateChoosen)) {
return false;
}

return $this->insertSyntheseDay($day);
return $this->insertSyntheseDay($dateChoosen);
}

/**
Expand Down Expand Up @@ -204,6 +232,26 @@ public function boilerDisconnect()
return @unlink($this->_cookies);
}

/**
* Look at the boiler data repository, and returns a list of the data
* files that are available.
*/
public function getAvailableBoilerDataFiles()
{
$rh = fopen('http://'.CHAUDIERE.URL, 'rb');
while (!feof($rh)) {
$dirData = fread($rh, 4096);
}
fclose($rh);

$matches = [];
if (preg_match_all('@touch_[0-9]{8}\.csv@sm', $dirData, $matches)) {
return array_unique($matches[0]);
}

return $matches;
}

//fonction de telechargement de fichier sur internet
// download('http://xxx','/usr/var/tmp)');
private function download($file_source, $file_target)
Expand All @@ -226,22 +274,6 @@ private function download($file_source, $file_target)
return true;
}

//pour que cela marche, il faut le la tache cron s'execute toutes les 2 heure a partir de 00:15
//si nous sommes entre 00 et 02 heures du matin, nous recuperons le fichir de la veuille, sinon le fichier du jour courant
//le cron doit s'executer a 00:15 pour etre bien

private function newDay()
{
$hour = date('H'); //17
//$this->log->debug("hour::".$hour);
if ('00' == $hour || '01' == $hour) {
//$this->log->debug("Minuit");
return true;
}

return false;
}

//function de convertion du format decimal de l'import au format bdd
private function cvtDec($n)
{
Expand All @@ -256,6 +288,26 @@ private function deleteSyntheseDay($day)
return $this->query($q);
}

/**
* Checks if a synthese already exists for this date.
*
* @param type $day
*/
private function isSyntheseDone($day)
{
$sql = "SELECT COUNT(*) FROM oko_resume_day WHERE jour = '{$day}'";

$result = $this->query($sql);

if ($result) {
if ($res = $result->fetch_row()) {
return 1 == $res[0];
}
}

return false;
}

private function insertSyntheseDay($day)
{
$query = 'INSERT INTO oko_resume_day ( jour, tc_ext_max, tc_ext_min, conso_kg, dju, nb_cycle ) VALUE ';
Expand Down
2 changes: 1 addition & 1 deletion _include/version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8.4
1.8.5
Loading

0 comments on commit 320f360

Please sign in to comment.