Skip to content

Commit eaa55ac

Browse files
authored
Merge pull request #113 from seehase/feature/sunny_multi_importer
Feature/sunny multi importer
2 parents 0ed4d15 + 22add01 commit eaa55ac

10 files changed

+292
-263
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
.DS_Store
1515
/ZonPHP/parameters_dev.php
1616
/.vscode/
17+
/ZonPHP/WR**/

ZonPHP/charts/months_chart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119

120120
// collect data for max bars
121121
for ($i = 1; $i <= 12; $i++) {
122-
$cnt_per_month[$i] = 0;
122+
$cnt_per_month[$i] = 1;
123123
if (!isset($maxPerMonth[$i][$inverter_name])) {
124124
$maxPerMonth[$i][$inverter_name] = 0;
125125
}

ZonPHP/charts/year_chart.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
$resultmax = mysqli_query($con, $sqlmax) or die("Query failed. jaar-max " . mysqli_error($con));
4848

4949
$maxPerMonth = array();
50+
51+
foreach ($params['PLANTS'] as $name => $plant) {
52+
for ($k = 1; $k <= 12; $k++) {
53+
$maxPerMonth[$k][$name] = 0.0;
54+
}
55+
}
5056
if (mysqli_num_rows($resultmax) > 0) {
5157
while ($row = mysqli_fetch_array($resultmax)) {
5258
$maxPerMonth[$row['maand']][$row['Name']] = $row['som'];

ZonPHP/importer/sunny_explorer_marcel.php

Lines changed: 0 additions & 259 deletions
This file was deleted.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)