hanze/iwa-panda1

api (546b6cca49706ba0a9634bac68f3bb35f039ff58)
Repositories

commit 546b6cca49706ba0a9634bac68f3bb35f039ff58
parent b9fee0f3f854265ccd93ac91347c230b6ba69b0b
Author: LennartSchroot <[email protected]>
Date:   Thu, 29 Jun 2023 16:54:38 +0200

api

Diffstat:
Mapii/api.php53++++++++++++++++++++++++++++++++++-------------------
1 file changed, 34 insertions(+), 19 deletions(-)

diff --git a/apii/api.php b/apii/api.php @@ -13,10 +13,10 @@ $api_windchill = function(&$vars){ header('Content-Type: application/json'); echo api($token, "curdate()" , $type); update_last_request($token); - } + } }else{ return json_encode(["error" => "Token is null"]); - } + } }; $api_graph = function(&$vars){ @@ -32,23 +32,30 @@ $api_graph = function(&$vars){ header('Content-Type: application/json'); echo api($token, "curdate()" , $type); update_last_request($token); - } + } }else{ return json_encode(["error" => "Token is null"]); - } + } }; function api(string $token, string $since, $type):string{ $result = query($token, $since); - if($result == null){ - return json_encode(["error" => "result is null"]); + if ($result == null) { + return ''; } - if($type == "windchill"){ - return json_windchill($result); - }elseif($type == "graph"){ - return json_graph($result); - }else{ - return json_encode(["error" => "json function fcked up"]); + if ($type == "windchill") { + if (check_request($token, 15)) { + return json_windchill($result); + } else { + return ''; + } + }elseif ($type == "graph") { + if (check_request($token, 1440)) { + return json_graph($result); + } else { + return ''; + } } + return ''; } function query($token, $since){ @@ -71,16 +78,24 @@ function query($token, $since){ $stmt->execute([0 => $token, 1 => $since]); return $stmt->get_result(); } -function last_request($token, $minutes_to_add){ +function check_request($token, $cooldown): bool +{ + $datetime_format = 'YmdHis'; $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2"); - $query = "select last_request+1, current_timestamp+1 as now from contract where token = ?"; + $query = "select last_request+1 as last_request, current_timestamp+1 as now from contract where token = ?"; $stmt = $db->prepare($query); $stmt->execute([0 => $token]); $row = $stmt->get_result()->fetch_assoc(); - $time = new DateTime($row['last_request']); - $timenow = new DateTime($row['now']); - $diff = $time->diff($timenow); - return $diff; + $time = DateTime::createFromFormat($datetime_format, $row['last_request']); + $timenow = DateTime::createFromFormat($datetime_format, $row['now']); + $diff = $timenow->diff($time); + $diff_sec = ($diff->days * 24 * 60 * 60) + ($diff->h * 60 * 60) + ($diff->i * 60) + $diff->s; + if ($diff_sec > $cooldown * 60){ + return true; + } + else{ + return false; + } } function update_last_request($token){ @@ -89,6 +104,7 @@ function update_last_request($token){ $stmt = $db->prepare($query); $stmt->execute([0 => $token]); } + function json_windchill($result):string{ $weather_data = []; while ($row = $result->fetch_assoc()) { @@ -127,5 +143,4 @@ function humid($temp, $dewp): float|int return round(($specific_humidity / $saturation_point) * 100, 2); } - ?> \ No newline at end of file