Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions public/downloadChargeLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,48 @@
"data imported_since_plugged" => ["header" => "Energie seit Anstecken", "type" => "energy"],
];

if (!preg_match("/[0-9]{4}/", $_GET["year"]) || !preg_match("/^((0?[1-9])|(1[0-2]))$/", $_GET["month"])) {
http_response_code(400);
die("invalid data");
// If the "year" parameter is missing or invalid, respond with HTTP 400 and return an error message.
if (!isset($_GET["year"]) || !preg_match("/^[0-9]{4}$/", $_GET["year"])) {
http_response_code(400);
echo json_encode(["error" => "Invalid 'year' parameter. A four-digit year, e.g., 2023, is expected."]);
exit;
}

$file_name = sprintf('%04d%02d', $_GET["year"], $_GET["month"]);
$charge_log_file = $charge_log_path . $file_name . ".json";
$charge_log_data = json_decode(file_get_contents($charge_log_file), true);
// If the "month" parameter is provided and matches the regex for valid months (1-12), use it
if (isset($_GET["month"])) {
if (preg_match("/^((0?[1-9])|(1[0-2]))$/", $_GET["month"])) {
$month = $_GET["month"];
// Format the file name based on the year and month
$file_name = sprintf('%04d%02d', $year, $month);
$charge_log_file = $charge_log_path . $file_name . ".json";

// Attempt to load the charge log file for the specific month
if (file_exists($charge_log_file)) {
$charge_log_data = json_decode(file_get_contents($charge_log_file), true);
} else {
$charge_log_data = []; // Default to an empty array if the file does not exist
}
} else {
// If the "month" parameter is invalid, return HTTP 400 Bad Request
http_response_code(400);
echo json_encode(["error" => "Invalid 'month' parameter. Expected a value between 1 and 12."]);
exit;
}
} else {
// If no "month" is provided, process all months for the given year
$charge_log_data = [];
for ($month = 1; $month <= 12; $month++) {
// Format the file name based on the year and month
$file_name = sprintf('%04d%02d', $year, $month);
$charge_log_file = $charge_log_path . $file_name . ".json";

// Append data from each month's charge log file if it exists
if (file_exists($charge_log_file)) {
$monthly_data = json_decode(file_get_contents($charge_log_file), true);
$charge_log_data = array_merge($charge_log_data, $monthly_data);
}
}
}

function newRow()
{
Expand Down
27 changes: 21 additions & 6 deletions src/views/ChargeLog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@
</openwb-base-card>
</openwb-base-card>
<openwb-base-alert subtype="info">
Das komplette Ladeprotokoll kann automatisiert über folgende URL abgerufen werden:
<a :href="downloadUrl">{{ downloadUrl }}</a>
Das Ladeprotokoll kann monatsweise automatisiert über folgende URL abgerufen werden:
<a :href="downloadUrlMonth">{{ downloadUrlMonth }}</a> <br>
Das komplette Ladeprotokoll für das gesamte Jahr kann automatisiert über folgende URL abgerufen werden:
<a :href="downloadUrlYear">{{ downloadUrlYear }}</a>
</openwb-base-alert>
<openwb-base-alert
v-if="!chargeLogRead"
Expand Down Expand Up @@ -484,11 +486,24 @@ export default {
mqttClientId() {
return this.$root.mqttClientId;
},
downloadUrl() {
const port = parseInt(location.port) || (location.protocol == "https:" ? 443 : 80);
const baseUrl = `${location.protocol}//${location.hostname}:${port}/openWB/web/settings/downloadChargeLog.php`;
return baseUrl + `?year=${this.chargeLogRequestData.year}&month=${this.chargeLogRequestData.month}`;
baseUrl() {
const port = parseInt(location.port) || (location.protocol === "https:" ? 443 : 80);
return `${location.protocol}//${location.hostname}:${port}/openWB/web/settings/downloadChargeLog.php`;
},
downloadUrlMonth() {
if (!this.chargeLogRequestData.year || !this.chargeLogRequestData.month) {
console.error("Fehlende Parameter für Monat oder Jahr");
return null; // oder ein Standardwert, falls gewünscht
}
return `${this.baseUrl}?year=${this.chargeLogRequestData.year}&month=${this.chargeLogRequestData.month}`;
},
downloadUrlYear() {
if (!this.chargeLogRequestData.year) {
console.error("Fehlendes Jahr");
return null; // oder ein Standardwert
}
return `${this.baseUrl}?year=${this.chargeLogRequestData.year}`;
}
chargeLogDate: {
get() {
return this.chargeLogRequestData.year + "-" + this.chargeLogRequestData.month;
Expand Down
Loading