public function csv2bdd() { ini_set('max_execution_time', 120); $t = new timeExec(); $ob_capteur = new capteur(); $capteurs = $ob_capteur->getForImportCsv(); //l'index du tableau correspond a la colonne du capteur dans le fichier csv $capteurStatus = $ob_capteur->getByType('status'); $startCycle = $ob_capteur->getByType('startCycle'); unset($ob_capteur); $file = fopen(CSVFILE, 'r'); $ln = 0; $old_status = 0; $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); //ne pas prendre en compte la derniere colonne vide $ligne = substr($ligne, 0, strlen($ligne) - 2); if ($ln != 0) { //pour ne pas lire la premiere ligne d'entete du fichier csv $colCsv = explode(CSV_SEPARATEUR, $ligne); if (isset($colCsv[1])) { //test si ligne non vide //$nbColCsv = count($colCsv); $jour = $colCsv[0]; $heure = $colCsv[1]; // Round to the minute, since in some cases it is possible to // import two files with the same data but not the same seconds // Case of an import on the same day of the web files and the USB files $heure = preg_replace('/:[0-9]{2}$/', ':00', $heure); $query = ""; $beginValue = "jour = STR_TO_DATE('" . $jour . "','%d.%m.%Y')," . "heure = '" . $heure . "'," . "timestamp = UNIX_TIMESTAMP(CONCAT(STR_TO_DATE('" . $jour . "','%d.%m.%Y'),' ','" . $heure . "'))"; //utc timestamp $query = $insert . $beginValue; //Detection demarrage d'un cycle //Statut 4 = Debut d'un cycle sur le front montant du statut if ($colCsv[$capteurStatus['position_column_csv']] == "4" && $colCsv[$capteurStatus['position_column_csv']] != $old_status) { $st = 1; //creation de la requette pour le comptage des cycle de la chaudiere //Enregistrement de 1 si nous commençons un cycle d'allumage $query .= ", col_" . $startCycle['column_oko'] . "=" . $st; } //creation de la requette sql pour les capteurs //on commence à la deuxieme colonne de la ligne du csv for ($i = 2; $i <= $nbColCsv; $i++) { $query .= ", col_" . $capteurs[$i]['column_oko'] . "=" . $this->cvtDec($colCsv[$i]); } $query .= ";"; //execution de la requette representant l'ensemble d'un ligne du csv $this->log->debug("Class " . __CLASS__ . " | " . __FUNCTION__ . " | " . $query); $this->query($query); $old_status = $colCsv[$capteurStatus['position_column_csv']]; } } $ln++; } fclose($file); $this->log->info("Class " . __CLASS__ . " | " . __FUNCTION__ . " | SUCCESS - import du CSV dans la BDD - " . $ln . " lignes en " . $t->getTime() . " sec "); return true; }
<?php ini_set('max_execution_time', 600); $this->log->info("UPGRADE | {$version} | begin"); $t = new timeExec(); $dico = json_decode(file_get_contents("_langs/fr.matrice.json"), true); $c = new capteur(); $res = $c->getAll(); foreach ($res as $key) { $okoSensor = $key['original_name']; $name = isset($dico[$okoSensor]['name']) ? $dico[$okoSensor]['name'] : $okoSensor; $q = "update oko_capteur set name='" . $name . "' where original_name='{$okoSensor}'"; $this->log->info("UPGRADE | {$version} | update {$okoSensor} :: " . $name); if (!$this->query($q)) { $this->log->info("UPGRADE | {$version} | Failed | " . $q); } } $q = "CREATE TABLE IF NOT EXISTS `oko_silo_events` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `event_date` date NOT NULL,\n `quantity` int(5) unsigned NOT NULL COMMENT 'in kg',\n `remaining` int(6) NOT NULL COMMENT 'in kg',\n `price` int(8) NOT NULL,\n `event_type` char(10) NOT NULL DEFAULT 'PELLET',\n PRIMARY KEY (`id`)\n ) ENGINE=MYISAM DEFAULT CHARSET=utf8"; $this->log->info("UPGRADE | {$version} | Adding table oko_silo_events"); if (!$this->query($q)) { $this->log->info("UPGRADE | {$version} | Failed | " . $q); } //ajouter dans config.php // Utilisation d'un silo $configFile = file_get_contents('config.php'); $configFile = str_replace("//NEWPARAMUPDATE", "// Utilisation d'un silo\nDEFINE('HAS_SILO', (\$" . "config['has_silo']==1)?true:false); // default -> true //json \nDEFINE('SILO_SIZE', \$" . "config['silo_size']); // kg \nDEFINE('ASHTRAY', (isset(\$" . "config['ashtray']))?\$" . "config['ashtray']:''); // kg \n//NEWPARAMUPDATE", $configFile); file_put_contents('config.php', $configFile); $this->log->info("UPGRADE | {$version} | end :" . $t->getTime());