api.php (4994B) download
1<?php
2
3$api_windchill = function(&$vars){
4 $headers = getallheaders();
5 $token = $headers["X-Token"];
6 $type = "windchill";
7 if($token != ""){
8 if(isset($vars["since"])){
9 header('Content-Type: application/json');
10 echo api($token, $vars["since"], $type);
11 update_last_request($token);
12 }else{
13 header('Content-Type: application/json');
14 echo api($token, "curdate()" , $type);
15 update_last_request($token);
16 }
17 }else{
18 return json_encode(["error" => "Token is null"]);
19 }
20};
21
22$api_graph = function(&$vars){
23 $headers = getallheaders();
24 $token = $headers["X-Token"];
25 $type = "graph";
26 if($token != ""){
27 if(isset($vars["since"])){
28 header('Content-Type: application/json');
29 echo api($token, $vars["since"], $type);
30 update_last_request($token);
31 }else{
32 header('Content-Type: application/json');
33 echo api($token, "curdate()" , $type);
34 update_last_request($token);
35 }
36 }else{
37 return json_encode(["error" => "Token is null"]);
38 }
39};
40function api(string $token, string $since, $type):string{
41 $result = query($token, $since);
42 if ($result == null) {
43 return '';
44 }
45 if ($type == "windchill") {
46 if (check_request($token, 15)) {
47 return json_windchill($result);
48 } else {
49 return '';
50 }
51 }elseif ($type == "graph") {
52 if (check_request($token, 1440)) {
53 return json_graph($result);
54 } else {
55 return '';
56 }
57 }
58 return '';
59}
60
61function query($token, $since){
62 $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2");
63 $weather_data = [];
64 $query = " SELECT distinct data_id, temperature, wind_speed, dew_point, date_time, DATE(date_time) as date, nl.name as city, co.country as country
65 FROM weather_data wd
66 JOIN station s ON wd.station_name = s.name
67 JOIN contract_station cs ON cs.station_name = s.name
68 JOIN nearestlocation nl ON nl.station_name = s.name
69 JOIN country co ON co.country_code = nl.country_code
70 JOIN geolocation geo ON geo.country_code = co.country_code
71 JOIN contract c ON cs.contract_id = c.contract_id
72 WHERE c.token = ?
73 AND date(date_time) >= ?
74 ORDER BY date DESC, temperature, wind_speed
75
76 ";
77 $stmt = $db->prepare($query);
78 $stmt->execute([0 => $token, 1 => $since]);
79 return $stmt->get_result();
80}
81function check_request($token, $cooldown): bool
82{
83 $datetime_format = 'YmdHis';
84 $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2");
85 $query = "select last_request+1 as last_request, current_timestamp+1 as now from contract where token = ?";
86 $stmt = $db->prepare($query);
87 $stmt->execute([0 => $token]);
88 $row = $stmt->get_result()->fetch_assoc();
89 $time = DateTime::createFromFormat($datetime_format, $row['last_request']);
90 $timenow = DateTime::createFromFormat($datetime_format, $row['now']);
91 $diff = $timenow->diff($time);
92 $diff_sec = ($diff->days * 24 * 60 * 60) + ($diff->h * 60 * 60) + ($diff->i * 60) + $diff->s;
93 if ($diff_sec > $cooldown * 60){
94 return true;
95 }
96 else{
97 return false;
98 }
99}
100
101function update_last_request($token){
102 $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2");
103 $query = "UPDATE contract SET `last_request` = CURRENT_TIMESTAMP WHERE `token` = ?";
104 $stmt = $db->prepare($query);
105 $stmt->execute([0 => $token]);
106}
107
108function json_windchill($result):string{
109 $weather_data = [];
110 while ($row = $result->fetch_assoc()) {
111 $wind_chill = windchill($row["temperature"], $row["wind_speed"]);
112 $weather_data[] = [
113 'data_id' => $row['data_id'],
114 'city' => $row['city'],
115 'country' => $row['country'],
116 'date' => $row['date'],
117 'windchill' => $wind_chill
118 ];
119 }
120 return json_encode($weather_data);
121}
122function json_graph($result):string{
123 $weather_data = [];
124 while ($row = $result->fetch_assoc()) {
125 $humid = humid($row['temperature'], $row['dew_point']);
126 $weather_data[] = [
127 'data_id' => $row['data_id'],
128 'city' => $row['city'],
129 'date_time' => $row['date_time'],
130 'humidity' => $humid
131 ];
132 }
133 return json_encode($weather_data);
134}
135function windchill($temp, $wind): float
136{
137 $result = 13.12 + 0.6215 * $temp - 11.37 * pow($wind, 0.16) + 0.3965 * $temp * pow($wind, 0.16);
138 return round($result, 2);
139}
140
141function humid($temp, $dewp): float|int
142{
143 $specific_humidity = exp((17.625 * $dewp) / (243.04 + $dewp));
144 $saturation_point = exp((17.625 * $temp) / (243.04 + $temp));
145
146 return round(($specific_humidity / $saturation_point) * 100, 2);
147}
148?>