pico-s0-powermeter/api.php
2023-12-21 18:43:10 +00:00

92 lines
3.2 KiB
PHP

<?php
$servername = "db-host";
$username = "username";
$password = "password";
$dbname = "power";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
$jsonres = array("Error"=>$conn->connect_error);
die("Connection failed: " . $conn->connect_error);
}
if (isset($_GET["action"]) && isset($_GET["total"]) && isset($_GET["current"])) {
$action = $_GET["action"];
$total = $_GET["total"];
$current = $_GET["current"];
if ($action == "send") {
$last_update = date("Y-m-d H:i:s");
$sql = "INSERT INTO powerdata (total, current, last_update) VALUES ('".$total."', '".$current."', '".$last_update."')";
$result = $conn->query($sql);
if ($result === FALSE) {
$jsonres = array("Error"=>"Not inserted into DB.");
} else {
$jsonres = array("OK"=>"Inserted into DB.");
}
} else if ($action == "stats") {
$sql = "SELECT Sub1.Date, (b.total - a.total) AS DailyConsumption FROM (
SELECT DATE_FORMAT(last_update, '%Y-%m-%d') AS Date, MIN(last_update) AS MinTimeStamp, MAX(last_update) AS MaxTimeStamp
FROM powerdata
GROUP BY Date) Sub1
INNER JOIN powerdata a ON Sub1.MinTimeStamp = a.last_update
INNER JOIN powerdata b ON Sub1.MaxTimeStamp = b.last_update";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$todayConsumption = 0;
$yesterdayConsumption = 0;
$weekConsumption = 0;
$lastWeekConsumption = 0;
$monthConsumption = 0;
$lastMonthConsumption = 0;
$yearConsumption = 0;
while($row = $result->fetch_assoc()) {
//returns Wh per day
if ($row["Date"] == date("Y-m-d")){
$todayConsumption = $row["DailyConsumption"];
}
if ($row["Date"] == date("Y-m-d", strtotime("-1 days"))){
$yesterdayConsumption = $row["DailyConsumption"];
}
if (date("W", strtotime($row["Date"])) == date("W")){
$weekConsumption += $row["DailyConsumption"];
}
if (date("W", strtotime($row["Date"])) == date("W", strtotime("-1 weeks"))){
$lastWeekConsumption += $row["DailyConsumption"];
}
if (date("m", strtotime($row["Date"])) == date("m")){
$monthConsumption += $row["DailyConsumption"];
}
if (date("m", strtotime($row["Date"])) == date("m", strtotime("-1 month"))){
$lastMonthConsumption += $row["DailyConsumption"];
}
if (date("y", strtotime($row["Date"])) == date("y")){
$yearConsumption += $row["DailyConsumption"];
}
$jsonres = array("todayConsumption"=>$todayConsumption, "yesterdayConsumption"=>$yesterdayConsumption, "weekConsumption"=>$weekConsumption, "lastWeekConsumption"=>$lastWeekConsumption, "monthConsumption"=>$monthConsumption, "lastMonthConsumption"=>$lastMonthConsumption, "yearConsumption"=>$yearConsumption);
}
} else {
$jsonres = array("Error"=>"No result from database.");
}
} else {
$jsonres = array("Error"=>"Invalid action.");
}
} else {
$sql = "SELECT * FROM powerdata ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$jsonres = array("total"=>($row["total"]/1000), "current"=>$row["current"], "last_update"=>$row["last_update"]);
}
} else {
$jsonres = array("Error"=>"No result from database.");
}
}
$conn->close();
echo json_encode($jsonres)
?>