xdata.csv * * Exemple d'exécution : php ecodevice-to-jeedom-suivi-conso.php xdata.csv | mysql -u -p jeedom * */ $file = $argv[1]; $csv = array_map('str_getcsv', file($file)); for ($i = 2; $i < count($csv); $i++) { $line = $csv[$i]; $timestamp = "$line[0]-$line[1]-$line[2] 23:59:59"; $rec_date = "$line[0]-$line[1]-$line[2]"; $previous_rec_date = new DateTime("{$csv[$i - 1][0]}-{$csv[$i - 1][1]}-{$csv[$i - 1][2]}", new DateTimeZone('Europe/Paris')); $next_day_previous_date = $previous_rec_date->add(new DateInterval('P1D')); if ($next_day_previous_date != new DateTime($rec_date, new DateTimeZone('Europe/Paris'))) { file_put_contents('php://stderr', "Ligne " . ($i+1) . " ignorée car écart supérieur à une journée avec la ligne précédente\n", FILE_APPEND); continue; } $date = new DateTime($rec_date, new DateTimeZone('Europe/Paris')); $periode = $date->format('D j y'); $idx_max_hp = $line[4]; $previous_idx_max_hp = $csv[$i - 1][4]; if ($idx_max_hp < $previous_idx_max_hp) { file_put_contents('php://stderr', "Ligne " . ($i+1) . " ignorée car index invalides (idx_max_hp = $idx_max_hp, previous_idx_max_hp = $previous_idx_max_hp)\n", FILE_APPEND); continue; } $idx_min_hp = $previous_idx_max_hp; $idx_max_hc = $line[5]; $previous_idx_max_hc = $csv[$i - 1][5]; $idx_min_hc = $previous_idx_max_hc; if ($idx_max_hc < $previous_idx_max_hc) { file_put_contents('php://stderr', "Ligne " . ($i+1) . " ignorée car index invalides (idx_max_hc = $idx_max_hc, previous_idx_max_hc = $previous_idx_max_hc)\n", FILE_APPEND); continue; } $id_eq = 1999; $temp_max = 0; $temp_min = 0; $temp_moy = 0; $dju = 0; $dju_clim = 0; $hp = ($idx_max_hp - $idx_min_hp) / 1000; $hc = ($idx_max_hc - $idx_min_hc) / 1000; if ($idx_max_hp > 0 && $idx_min_hp > 0 && $idx_max_hc > 0 && $idx_min_hc > 0 && $hp >= 0 && $hc >= 0) { echo "INSERT INTO conso_jour VALUES (unix_timestamp('$timestamp'),'$rec_date','$periode',$hp,$hc,$idx_max_hp,$idx_min_hp,$idx_max_hc,$idx_min_hc,$id_eq,$temp_max,$temp_min,$temp_moy,$dju,$dju_clim);\n"; } else { file_put_contents('php://stderr', "Ligne " . ($i+1) . " ignorée car données invalides (hp = $hp, hc = $hc, idx_max_hp = $idx_max_hp, idx_min_hp = $idx_min_hp, idx_max_hc = $idx_max_hc, idx_min_hc = $idx_min_hc)\n", FILE_APPEND); } } ?>