From e85080078e3f7752ef4b3e35ff95531554be3cc8 Mon Sep 17 00:00:00 2001 From: Christian Steinle Date: Fri, 17 May 2024 12:10:25 +0200 Subject: [PATCH] Add daily production values. --- functions/functions.php | 21 ++++++- jobs/aggregate.php | 2 +- public/css/layout.css | 49 +++++++++++++++ public/index.php | 131 +++++++++++----------------------------- public/production.php | 88 +++++++++++++++++++++++++++ 5 files changed, 194 insertions(+), 97 deletions(-) create mode 100644 public/css/layout.css create mode 100644 public/production.php diff --git a/functions/functions.php b/functions/functions.php index 01a1dd1..78278a1 100644 --- a/functions/functions.php +++ b/functions/functions.php @@ -24,15 +24,17 @@ function build_date_dropdown(mysqli $db, string $table): array function get_aggregation(mysqli $db, string $table, $date): array { - $value_query = 'SELECT date, meter_consumption, power_sensor, grid_feed FROM ' . $table . ' WHERE date LIKE "' . $date . '%" ORDER BY date ASC;'; + $value_query = 'SELECT date, meter_consumption, power_sensor, grid_feed, production_eg, production_og FROM ' . $table . ' WHERE date LIKE "' . $date . '%" ORDER BY date ASC;'; $value_result = $db->query($value_query); $values = []; while ($row = $value_result->fetch_assoc()) { + $production = ($row['production_eg'] + $row['production_og']); $values['date'][] = $row['date']; $values['meter_consumption'][] = $row['meter_consumption']; $values['power_sensor'][] = $row['power_sensor']; $values['grid_feed'][] = $row['grid_feed']; + $values['production'][] = $production; $values['min_meter'] = (isset($values['min_meter']) && $values['min_meter'] < $row['meter_consumption']) ? $values['min_meter'] : $row['meter_consumption']; $values['max_meter'] = (isset($values['max_meter']) && $values['max_meter'] > $row['meter_consumption']) ? $values['max_meter'] : $row['meter_consumption']; $values['avg_meter'] += $row['meter_consumption']; @@ -42,11 +44,15 @@ function get_aggregation(mysqli $db, string $table, $date): array $values['min_feed'] = (isset($values['min_feed']) && $values['min_feed'] < $row['grid_feed']) ? $values['min_feed'] : $row['grid_feed']; $values['max_feed'] = (isset($values['max_feed']) && $values['max_feed'] > $row['grid_feed']) ? $values['max_feed'] : $row['grid_feed']; $values['avg_feed'] += $row['grid_feed']; + $values['min_production'] = (isset($values['min_production']) && $values['min_production'] < $production) ? $values['min_production'] : $production; + $values['max_production'] = (isset($values['max_production']) && $values['max_production'] > $production) ? $values['max_production'] : $production; + $values['avg_production'] += $production; } $values['avg_meter'] /= count($values['meter_consumption']); $values['avg_power'] /= count($values['power_sensor']); $values['avg_feed'] /= count($values['grid_feed']); + $values['avg_production'] /= count($values['production']); return $values; } @@ -118,4 +124,17 @@ function get_last_value(mysqli $db, string $table, $date): float $data = $value_result->fetch_assoc(); return floatval($data['last_value']) / 1000; +} + +function get_production_values_for_month(mysqli $db, string $table, $date) +{ + $value_query = 'SELECT date, production_og, production_eg FROM ' . $table . ' WHERE date LIKE "' . $date . '%" ORDER BY date ASC;'; + $value_result = $db->query($value_query); + $production_values = []; + + while ($row = $value_result->fetch_assoc()) { + $production_values[] = $row; + } + + return $production_values; } \ No newline at end of file diff --git a/jobs/aggregate.php b/jobs/aggregate.php index b5bc4af..9028c56 100644 --- a/jobs/aggregate.php +++ b/jobs/aggregate.php @@ -58,7 +58,7 @@ for ($date_diff = $interval->days; $date_diff > 0; $date_diff--) { $grid_feed = 0; } - $aggregation_query = 'INSERT INTO ' . $local_table . ' (`date`, `meter_consumption`, `power_sensor`, `grid_feed`, `last_value`) VALUES ("' . $date->format('Y-m-d') . '", ' . $meter_consumption . ', ' . $power_sensor . ', ' . $grid_feed . ', ' . $consumption_end . ');'; + $aggregation_query = 'INSERT INTO ' . $local_table . ' (`date`, `meter_consumption`, `power_sensor`, `grid_feed`, `last_value`, `production_eg`, `production_og`) VALUES ("' . $date->format('Y-m-d') . '", ' . $meter_consumption . ', ' . $power_sensor . ', ' . $grid_feed . ', ' . $consumption_end . ', 0, 0);'; $local_db->query($aggregation_query); } diff --git a/public/css/layout.css b/public/css/layout.css new file mode 100644 index 0000000..32e3640 --- /dev/null +++ b/public/css/layout.css @@ -0,0 +1,49 @@ +body { + font-family: sans-serif; + display: flex; + flex-direction: column; +} + +div > span { + text-align: right; +} + +div.grid-container { + display: grid; + column-gap: 20px; +} + +span.color-1 { + background-color: #375BEB; +} + +span.color-2 { + background-color: #90EB36; +} + +span.color-3 { + background-color: #EB5F36; +} + +span.color-4 { + background-color: #DAE32D; +} + +form > div { + display: flex; + flex-direction: row; +} + +.button { + text-align: center; + background-color: rgb(239, 239, 239); + border: 2px outset rgb(0, 0 , 0); + text-decoration: none; + color: rgb(0, 0, 0); +} + +#chart-container { + position: relative; + width: 95vw; + height: 95vh; +} diff --git a/public/index.php b/public/index.php index 4eef287..509e31e 100644 --- a/public/index.php +++ b/public/index.php @@ -29,61 +29,17 @@ $month_production = get_month_production($local_db, $table_production, $chosen_d $year_production = get_year_production($local_db, $table_production, $chosen_date); $last_value = get_last_value($local_db, $table_aggregation, $chosen_date); -$colors = ['#375BEB', '#90EB36', '#EB5F36', '#DAE32D']; - ?> Consumption values - + -
- + + Werte setzen
-
- - - - - - - - - -
- -
- +
- Monat - Jahr - Min - Max - Durchschnitt -
+ + Monat + Jahr + Min + Max + Durchschnitt -
- + KWh KWh KWh KWh KWh -
-
- + KWh KWh KWh KWh KWh -
-
KWh @@ -143,46 +83,42 @@ $colors = ['#375BEB', '#90EB36', '#EB5F36', '#DAE32D']; KWh KWh KWh -
-
KWh KWh - kWh -
+ -
KWh KWh - - - kWh -
+ KWh + KWh + KWh -
-
+ + + -
0) ? number_format(100 - $month_values['grid_feed'] * 100 / ($month_production['eg'] + $month_production['og']), 2, ',', '.') : 0; ?> % % -
+ + + -
- - kWh + + kWh
@@ -201,17 +137,22 @@ $colors = ['#375BEB', '#90EB36', '#EB5F36', '#DAE32D']; { label: 'Verbrauch Zählerstand', data: , - backgroundColor: '', + backgroundColor: '#375BEB', }, { label: 'Verbrauch berechnet', data: , - backgroundColor: '', + backgroundColor: '#90EB36', }, { label: 'Einspeisung', data: , - backgroundColor: '', + backgroundColor: '#EB5F36', + }, + { + label: 'Erzeugung', + data: , + backgroundColor: '#DAE32D', } ] }, @@ -222,4 +163,4 @@ $colors = ['#375BEB', '#90EB36', '#EB5F36', '#DAE32D']; }); - + \ No newline at end of file diff --git a/public/production.php b/public/production.php new file mode 100644 index 0000000..5ad24eb --- /dev/null +++ b/public/production.php @@ -0,0 +1,88 @@ + $value) { + $update_query = 'UPDATE ' . $table_aggregation . ' SET production_eg = ' . $value['production_eg'] . ', production_og = ' . $value['production_og'] . ' WHERE `date` = "' . $key . '" LIMIT 1;'; + $local_db->query($update_query); + $sum_eg += $value['production_eg'] / 1000; + $sum_og += $value['production_og'] / 1000; + } + + $check_query = 'SELECT date FROM ' . $table_production . ' WHERE date = "' . $_POST['date'] . '-01";'; + $check_result = $local_db->query($check_query); + + if ($check_result->num_rows === 0) { + $query = 'INSERT INTO ' . $table_production . ' (date, eg, og, price) VALUES ("' . $_POST['date'] . '-01", ' . $sum_eg . ', ' . $sum_og . ', ' . $_POST['price'] . ');'; + } else { + $query = 'UPDATE ' . $table_production . ' SET eg = "' . $sum_eg . '", og = "' . $sum_og . '", price = "' . $_POST['price'] . '" WHERE date = "' . $_POST['date'] . '-01";'; + } + $local_db->query($query); +} + +$dates = build_date_dropdown($local_db, $table_aggregation); +$chosen_date = (isset($_POST['date'])) ? $_POST['date'] : $dates[0]; +$production_values = get_production_values_for_month($local_db, $table_aggregation, $chosen_date); +$month_production = get_month_production($local_db, $table_production, $chosen_date); + +?> + + + + Consumption values + + + +
+ + + + + Übersicht +
+ +
+ + + + + + + + Preis + + + + + + +
+ + + \ No newline at end of file