commit b9fee0f3f854265ccd93ac91347c230b6ba69b0b
parent 5608e5fe322f05e1379fa457ee9b463c9b453ae6
Author: Gerco van Woudenbergh <[email protected]>
Date: Wed, 28 Jun 2023 22:29:12 +0200
api
Diffstat:
M | apii/api.php | 103 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- |
M | index.php | 5 | +++++ |
2 files changed, 100 insertions(+), 8 deletions(-)
diff --git a/apii/api.php b/apii/api.php
@@ -3,10 +3,58 @@
$api_windchill = function(&$vars){
$headers = getallheaders();
$token = $headers["X-Token"];
-
+ $type = "windchill";
+ if($token != ""){
+ if(isset($vars["since"])){
+ header('Content-Type: application/json');
+ echo api($token, $vars["since"], $type);
+ update_last_request($token);
+ }else{
+ 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){
+ $headers = getallheaders();
+ $token = $headers["X-Token"];
+ $type = "graph";
+ if($token != ""){
+ if(isset($vars["since"])){
+ header('Content-Type: application/json');
+ echo api($token, $vars["since"], $type);
+ update_last_request($token);
+ }else{
+ 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($type == "windchill"){
+ return json_windchill($result);
+ }elseif($type == "graph"){
+ return json_graph($result);
+ }else{
+ return json_encode(["error" => "json function fcked up"]);
+ }
+}
+
+function query($token, $since){
$db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2");
$weather_data = [];
- $query = " SELECT distinct temperature, wind_speed, dew_point, DATE(date_time) AS date, nl.name as city
+ $query = " SELECT distinct temperature, wind_speed, dew_point, date_time, DATE(date_time) as date, nl.name as city, co.country as country
FROM weather_data wd
JOIN station s ON wd.station_name = s.name
JOIN contract_station cs ON cs.station_name = s.name
@@ -15,30 +63,69 @@ $api_windchill = function(&$vars){
JOIN geolocation geo ON geo.country_code = co.country_code
JOIN contract c ON cs.contract_id = c.contract_id
WHERE c.token = ?
- AND date(date_time) = CURDATE()
+ AND date(date_time) >= ?
ORDER BY date DESC, temperature, wind_speed
";
+ $stmt = $db->prepare($query);
+ $stmt->execute([0 => $token, 1 => $since]);
+ return $stmt->get_result();
+}
+function last_request($token, $minutes_to_add){
+ $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2");
+ $query = "select last_request+1, 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;
+}
+function update_last_request($token){
+ $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2");
+ $query = "UPDATE contract SET `last_request` = CURRENT_TIMESTAMP WHERE `token` = ?";
$stmt = $db->prepare($query);
$stmt->execute([0 => $token]);
- $result = $stmt->get_result();
+}
+function json_windchill($result):string{
+ $weather_data = [];
while ($row = $result->fetch_assoc()) {
$wind_chill = windchill($row["temperature"], $row["wind_speed"]);
$weather_data[] = [
'city' => $row['city'],
+ 'country' => $row['country'],
'date' => $row['date'],
'windchill' => $wind_chill
];
}
-
- echo json_encode($weather_data);
-};
-
+ return json_encode($weather_data);
+}
+function json_graph($result):string{
+ $weather_data = [];
+ while ($row = $result->fetch_assoc()) {
+ $humid = humid($row['temperature'], $row['dew_point']);
+ $weather_data[] = [
+ 'city' => $row['city'],
+ 'date_time' => $row['date_time'],
+ 'humidity' => $humid
+ ];
+ }
+ return json_encode($weather_data);
+}
function windchill($temp, $wind): float
{
$result = 13.12 + 0.6215 * $temp - 11.37 * pow($wind, 0.16) + 0.3965 * $temp * pow($wind, 0.16);
return round($result, 2);
}
+function humid($temp, $dewp): float|int
+{
+ $specific_humidity = exp((17.625 * $dewp) / (243.04 + $dewp));
+ $saturation_point = exp((17.625 * $temp) / (243.04 + $temp));
+
+ return round(($specific_humidity / $saturation_point) * 100, 2);
+}
+
?>
\ No newline at end of file
diff --git a/index.php b/index.php
@@ -12,6 +12,11 @@ $router->addRoute(['GET'], '/', 'views/homepage.php');
//all data
$router->addRoute(['GET', 'POST'], '/api/windchill', $api_windchill);
+$router->addRoute(['GET', 'POST'], '/api/windchill/:since', $api_windchill);
+
+$router->addRoute(['GET', 'POST'], '/api/graph/:since', $api_graph);
+
+$router->addRoute(['GET', 'POST'], '/api/graph/:since', $api_graph);
//add contract
$router->addRoute(['GET', 'POST'], '/add_contract', 'views/add_contract.php');