Files
power-consumption/public/index.php

226 lines
8.7 KiB
PHP

<?php
$app = require(__DIR__ . '/../bootstrap.php');
$local_db = $app['local_db'];
$table_aggregation = $app['config']['local']['table_aggregation'];
$table_production = $app['config']['local']['table_production'];
require_once(__DIR__ . '/../jobs/aggregate.php');
require_once(__DIR__ . '/../functions/functions.php');
if (isset($_POST['action']) && $_POST['action'] == 'set_values') {
$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", ' . $_POST['eg'] . ', ' . $_POST['og'] . ', ' . $_POST['price'] . ');';
} else {
$query = 'UPDATE ' . $table_production . ' SET eg = "' . $_POST['eg'] . '", og = "' . $_POST['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];
$data = get_aggregation($local_db, $table_aggregation, $chosen_date);
$month_values = get_month_aggregation($local_db, $table_aggregation, $chosen_date);
$year_values = get_year_aggregation($local_db, $table_aggregation, $chosen_date);
$grid_feed_by_month = get_grid_feed_by_month($local_db, $table_aggregation, $table_production, $chosen_date);
$month_production = get_month_production($local_db, $table_production, $chosen_date);
$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'];
?>
<!DOCTYPE html>
<html lang="de">
<head>
<title>Consumption values</title>
<style>
body {
font-family: sans-serif;
}
label {
display: inline-block;
width: 200px;
}
span.color-1 {
background-color: <?php echo $colors[0]; ?>;
}
span.color-2 {
background-color: <?php echo $colors[1]; ?>;
}
span.color-3 {
background-color: <?php echo $colors[2]; ?>;
}
span.color-4 {
background-color: <?php echo $colors[3]; ?>;
}
span.color {
display: inline-block;
width: 40px;
height: 12px;
}
span {
display: inline-block;
width: 200px;
text-align: right;
}
#chart-container {
position: relative;
width: 80vw;
height: 70vh;
}
</style>
</head>
<body>
<form action="/index.php" method="post" style="display: inline-block;float: left; margin-right: 100px;">
<select name="date" onchange="submit();">
<?php
foreach ($dates as $date) {
$selected = ($date == $chosen_date) ? ' selected="selected"' : '';
echo '<option value="' . $date . '"' . $selected . '>' . $date . '</option>';
}
?>
</select>
</form>
<form action="/index.php" method="post">
<input type="hidden" name="date" value="<?php echo $chosen_date; ?>"/>
<input type="hidden" name="action" value="set_values"/>
<label style="text-align: right;" for="EG">EG</label>
<input type="number" step="0.001" name="eg" id="EG" value="<?php echo $month_production['eg']; ?>"/>
<label style="text-align: right;" for="OG">OG</label>
<input type="number" step="0.001" name="og" id="OG" value="<?php echo $month_production['og']; ?>"/>
<label style="text-align: right;" for="price">Preis</label>
<input type="number" step="0.01" name="price" id="price" value="<?php echo $month_production['price']; ?>"/>
<button type="submit">Werte setzen</button>
</form>
<div style="margin-top: 20px; font-weight: bold;">
<label>Messungen</label>
<span class="color"></span>
<span>Monat</span>
<span>Jahr</span>
<span>Min</span>
<span>Max</span>
<span>Durchschnitt</span>
</div>
<div>
<span class="color color-1"></span>
<label>Verbrauch Zählerstand</label>
<span><?php echo number_format($month_values['meter_consumption'], 3, ',', '.'); ?> KWh</span>
<span><?php echo number_format($year_values['meter_consumption'], 3, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['min_meter'] / 1000, 3, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['max_meter'] / 1000, 3, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['avg_meter'] / 1000, 3, ',', '.'); ?> KWh</span>
</div>
<div>
<span class="color color-2"></span>
<label>Verbrauch berechnet</label>
<span><?php echo number_format($month_values['power_sensor'], 3, ',', '.'); ?> KWh</span>
<span><?php echo number_format($year_values['power_sensor'], 3, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['min_power'] / 1000, 3, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['max_power'] / 1000, 3, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['avg_power'] / 1000, 3, ',', '.'); ?> KWh</span>
</div>
<div>
<span class="color color-3"></span>
<label>Einspeisung</label>
<span><?php echo number_format($month_values['grid_feed'], 3, ',', '.'); ?> KWh</span>
<span><?php echo number_format($year_values['grid_feed'], 3, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['min_feed'] / 1000, 3, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['max_feed'] / 1000, 3, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['avg_feed'] / 1000, 3, ',', '.'); ?> KWh</span>
</div>
<div>
<span class="color color-4"></span>
<label>Produktion EG</label>
<span><?php echo number_format($month_production['eg'], 3, ',', '.'); ?> KWh</span>
<span><?php echo number_format($year_production['eg'], 3, ',', '.'); ?> KWh</span>
<span></span>
<span></span>
<span><?php echo number_format($month_production['eg'] / count($data['date']), 3, ',', '.'); ?> kWh</span>
</div>
<div style="border-bottom: 1px solid black;">
<span class="color color-4"></span>
<label>Produktion OG</label>
<span><?php echo number_format($month_production['og'], 3, ',', '.'); ?> KWh</span>
<span><?php echo number_format($year_production['og'], 3, ',', '.'); ?> KWh</span>
<span></span>
<span></span>
<span><?php echo number_format($month_production['og'] / count($data['date']), 3, ',', '.'); ?> kWh</span>
</div>
<div>
<span class="color"></span>
<label><b>Ersparnis</b></label>
<span><b><?php echo number_format(($month_production['eg'] + $month_production['og'] - $month_values['grid_feed']) * $month_production['price'] / 100, 2, ',', '.'); ?> €</b></span>
<span><b><?php echo number_format(($year_production['eg_price'] + $year_production['og_price'] - array_sum($grid_feed_by_month)), 2, ',', '.'); ?> €</b></span>
</div>
<div>
<span class="color"></span>
<label><b>Eigenverbrauch</b></label>
<span><b><?php echo ($month_production['eg'] + $month_production['og'] > 0) ? number_format(100 - $month_values['grid_feed'] * 100 / ($month_production['eg'] + $month_production['og']), 2, ',', '.') : 0; ?> %</b></span>
<span><b><?php echo number_format(100 - $year_values['grid_feed'] * 100 / ($year_production['eg'] + $year_production['og']), 2, ',', '.'); ?> %</b></span>
</div>
<div>
<span class="color"></span>
<label><b>Letzter Zählerstand</b></label>
<span><b><?php echo number_format($last_value, 3, ',', '.'); ?> %</b></span>
</div>
<div id="chart-container">
<canvas id="chart"></canvas>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.1/chart.umd.js"></script>
<script>
const chart = document.getElementById('chart');
new Chart(chart, {
type: 'bar',
data: {
labels: <?php echo json_encode($data['date']); ?>,
datasets: [
{
label: 'Verbrauch Zählerstand',
data: <?php echo json_encode($data['meter_consumption']); ?>,
backgroundColor: '<?php echo $colors[0]; ?>',
},
{
label: 'Verbrauch berechnet',
data: <?php echo json_encode($data['power_sensor']); ?>,
backgroundColor: '<?php echo $colors[1]; ?>',
},
{
label: 'Einspeisung',
data: <?php echo json_encode($data['grid_feed']); ?>,
backgroundColor: '<?php echo $colors[2]; ?>',
}
]
},
options: {
aspectRatio: 2,
}
});
</script>
</body>
</html>