|
| 1 | +<?php |
| 2 | +global $con, $params; |
| 3 | + |
| 4 | +// Loop over all plants only one folder should contain the CSV files with values for all converters |
| 5 | +foreach ($params['PLANTS'] as $name => $plant) { |
| 6 | + $lastImportDate = getLastImportDateForPlant($name, $con); |
| 7 | + $files_to_import = getFilesToImport($name, $lastImportDate, $plant['importPrefix']); |
| 8 | + $importDateFormat = $plant['importDateFormat']; |
| 9 | + addDebugInfo("sunny_explorer_multi: LastStartImportDate: $lastImportDate - ImportFilesCount: " . count($files_to_import)); |
| 10 | + |
| 11 | + foreach ($files_to_import as $import_filename) { |
| 12 | + addDebugInfo("sunny_explorer_multi: importFile: $import_filename"); |
| 13 | + $importData = readImportFile($import_filename, 0); |
| 14 | + $index = 0; |
| 15 | + foreach ($params['PLANTS'] as $importName => $plant) { |
| 16 | + addDebugInfo("sunny_explorer_multi: importFile: $import_filename -> plant: $importName"); |
| 17 | + $lastImportDate = getLastImportDateForPlant($importName, $con); |
| 18 | + $dbValues = mapLinesToDBValues($importData, $importName, $lastImportDate, $importDateFormat, $index); |
| 19 | + prepareAndInsertData($dbValues, $con); |
| 20 | + $index++; |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function mapLinesToDBValues(array $lines, string $name, $lastImportDate, $importDateFormat, $index): array |
| 26 | +{ |
| 27 | + global $params; |
| 28 | + $dbValues = array(); |
| 29 | + $minkWhCounter = 0.0; |
| 30 | + $lineCounter = 0; |
| 31 | + $isFirstValueLine = true; |
| 32 | + foreach ($lines as $line) { |
| 33 | + $lineCounter++; |
| 34 | + if ($lineCounter == 8) { |
| 35 | + $importDateFormat = parseImportDateTimeFormat($line, $importDateFormat); |
| 36 | + } |
| 37 | + if ($lineCounter > 8) { |
| 38 | + $lineValues = explode(";", $line); |
| 39 | + // all values are available and not null |
| 40 | + if (hasValidValues($lineValues, $index)) { |
| 41 | + // first data row get initial $minkWhCounter value from first line |
| 42 | + if ($isFirstValueLine) { |
| 43 | + $isFirstValueLine = false; |
| 44 | + $minkWhCounter = str_replace(',', '.', $lineValues[($index * 2) + 1]); |
| 45 | + } |
| 46 | + $dateFromDB = $lineValues[0]; |
| 47 | + |
| 48 | + // convert to UTC if parameter "importLocalDateAsUTC" is set to true otherwise it will remain localDate |
| 49 | + if ($params['importLocalDateAsUTC']) { |
| 50 | + $convertedDate = convertLocalDateTime($dateFromDB, $importDateFormat, true); // in UTC now |
| 51 | + } else { |
| 52 | + $convertedDate = $dateFromDB; // keep local time |
| 53 | + } |
| 54 | + $convertedTimeStamp = convertToUnixTimestamp($convertedDate); |
| 55 | + $currentTimeStamp = date("Y-m-d H:i:s", $convertedTimeStamp); |
| 56 | + $currentkWhCounter = str_replace(',', '.', $lineValues[($index * 2) + 1]); |
| 57 | + $cummulatedkWh = round($currentkWhCounter - $minkWhCounter, 3); |
| 58 | + $currentWatt = 0; |
| 59 | + $currentWattStr = trim(str_replace(',', '.', $lineValues[($index * 2) + 2])); |
| 60 | + if (strlen($currentWattStr) > 0 && is_numeric($currentWattStr)) { |
| 61 | + $currentWatt = $currentWattStr * 1000; |
| 62 | + } |
| 63 | + |
| 64 | + // insert only new data and value > 0 |
| 65 | + if ($currentWatt > 0 && ($currentTimeStamp != "") && (strtotime($currentTimeStamp) > strtotime($lastImportDate))) { |
| 66 | + $dbValues[] = array('name' => $name, 'timestamp' => $currentTimeStamp, 'watt' => $currentWatt, 'cummulatedkWh' => round($cummulatedkWh, 3)); |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + addDebugInfo("sunny_explorer: mapLinesToDBValues: ImportedLines: " . count($lines) . " - DataRows: " . count($dbValues)); |
| 73 | + return $dbValues; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Try to parse dateformat from CSV wich is normaly written in line 9 |
| 78 | + * dd.MM.yyyy HH:mm:ss;kWh;kW |
| 79 | + * --> "d.m.Y H:i:s" |
| 80 | + */ |
| 81 | +function parseImportDateTimeFormat(string $line, string $default): string |
| 82 | +{ |
| 83 | + $parts = explode(";", $line); |
| 84 | + if (count($parts) > 0) { |
| 85 | + $format = $parts[0]; |
| 86 | + $format = str_replace("dd", "d", $format); |
| 87 | + $format = str_replace("MM", "m", $format); |
| 88 | + $format = str_replace("yyyy", "Y", $format); |
| 89 | + $format = str_replace("HH", "H", $format); |
| 90 | + $format = str_replace("mm", "i", $format); |
| 91 | + $format = str_replace("ss", "s", $format); |
| 92 | + addDebugInfo("parseImportDateTimeFormat: parsed import format from CSV: $line -> $format"); |
| 93 | + return $format; |
| 94 | + } else { |
| 95 | + addDebugInfo("parseImportDateTimeFormat: cannot parse import format from CSV: $line using default: $default override in [plant][importDateFormat] if needed"); |
| 96 | + return $default; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments