commit d47fe86e841043e405d9e2ac36b92749b2d99329
parent 786a19f736fd1217db14790bb7aed2afccc9d1ca
Author: LennartSchroot <[email protected]>
Date: Thu, 29 Jun 2023 18:20:11 +0200
api
Diffstat:
5 files changed, 95 insertions(+), 73 deletions(-)
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
@@ -5,8 +5,10 @@
</component>
<component name="ChangeListManager">
<list default="true" id="f1ac0514-5d96-49e8-8c16-ecccecd1bbe1" name="Changes" comment="">
- <change afterPath="$PROJECT_DIR$/Controller/api/api.php" afterDir="false" />
+ <change afterPath="$PROJECT_DIR$/Model/Connect.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/Controller/api/api.php" beforeDir="false" afterPath="$PROJECT_DIR$/Controller/api/api.php" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/Model/Key.php" beforeDir="false" afterPath="$PROJECT_DIR$/Model/Key.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/index.php" beforeDir="false" afterPath="$PROJECT_DIR$/index.php" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@@ -53,6 +55,9 @@
<workItem from="1688048224856" duration="968000" />
<workItem from="1688049596887" duration="774000" />
<workItem from="1688050748885" duration="268000" />
+ <workItem from="1688051125887" duration="668000" />
+ <workItem from="1688051801098" duration="1872000" />
+ <workItem from="1688053996805" duration="1600000" />
</task>
<servers />
</component>
diff --git a/Controller/api/api.php b/Controller/api/api.php
@@ -1,64 +1,59 @@
<?php
function storeZooData($dataArray): void{
- if ($dataArray)
+ if ($dataArray != '') {
$db = new mysqli("86.92.67.21", "friedel", "hailiwa", "panda");
- $query = "INSERT INTO retrieve_zoos (city, wind_chill, date, country) VALUES (?, ?, ?, ?)";
- $stmt = mysqli_prepare($db, $query);
+ $query = "INSERT INTO retrieve_zoos (city, wind_chill, date, country) VALUES (?, ?, ?, ?)";
+ $stmt = mysqli_prepare($db, $query);
- foreach ($dataArray as $data) {
- $city = $data->city;
- $windchill = $data->windchill;
- $date = $data->date;
- $country = $data->country;
+ foreach ($dataArray as $data) {
+ $city = $data->city;
+ $windchill = $data->windchill;
+ $date = $data->date;
+ $country = $data->country;
- $stmt->bind_param("sdss", $city, $windchill, $date, $country);
- $stmt->execute();
- }
+ $stmt->bind_param("sdss", $city, $windchill, $date, $country);
+ $stmt->execute();
+ }
- $stmt->close();
- $db->close();
+ $stmt->close();
+ $db->close();
+ }
}
function storeFacData($dataArray): void{
- $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "panda");
- $query = "INSERT INTO retrieve_facilities (city, date_time, humidity) VALUES (?, ?, ?)";
- $stmt = mysqli_prepare($db, $query);
+ if ($dataArray != '') {
+ $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "panda");
+ $query = "INSERT INTO retrieve_facilities (city, date_time, humidity) VALUES (?, ?, ?)";
+ $stmt = mysqli_prepare($db, $query);
- foreach ($dataArray as $data) {
- $city = $data->city;
- $humidity = $data->humidity;
- $date_time = $data->date_time;
+ foreach ($dataArray as $data) {
+ $city = $data->city;
+ $humidity = $data->humidity;
+ $date_time = $data->date_time;
- $stmt->bind_param("ssd", $city, $date_time, $humidity);
- $stmt->execute();
- }
+ $stmt->bind_param("ssd", $city, $date_time, $humidity);
+ $stmt->execute();
+ }
- $stmt->close();
- $db->close();
+ $stmt->close();
+ $db->close();
+ }
}
function get_windchill(){
$token = '3jvl/yb?sRr80s6lTdeOyxV9VTQZkCPRp/bKOWKFWxfL2vhsU4Hhpgcmz9qe0zEk';
- if(isset($vars['since'])){
- $since = $vars['since'];
- }else{
- $since = "curdate()";
- }
+ $since = $vars['since'] ?? "''";
$url = 'http://86.92.67.21/api/windchill/' . $since;
$connect = new Model\Connect($url, $token);
$response = $connect->connect();
storeZooData($response);
-};
+}
function get_graph(){
$token = 'ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684';
- if(isset($vars['since'])){
- $since = $vars['since'];
- }else{
- $since = "curdate()";
- }
+ $since = $vars['since'] ?? "''";
$url = 'http://86.92.67.21/api/graph/' . $since;
$connect = new Model\Connect($url, $token);
$response = $connect->connect();
storeFacData($response);
-};
+}
diff --git a/Model/Connect.php b/Model/Connect.php
@@ -0,0 +1,39 @@
+<?php
+namespace Model{
+ class Connect{
+
+ protected string $url;
+ protected string $token;
+ protected string $json;
+
+ function __construct($url, $token){
+ $this->url = $url;
+ $this->token = $token;
+ }
+
+ function connect(){
+ $ch = curl_init();
+ if ($ch === false) {
+ throw new Exception('failed to initialize');
+ }
+
+ $headers = array(
+ "X-Token: $this->token", // Your token value
+ );
+
+ curl_setopt($ch, CURLOPT_URL, $this->url);
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+
+ $response = curl_exec($ch);
+
+ if ($response === false) {
+ throw new Exception(curl_error($ch), curl_errno($ch));
+ }
+
+ curl_close($ch);
+
+ return json_decode($response);
+ }
+ }
+}
+\ No newline at end of file
diff --git a/Model/Key.php b/Model/Key.php
@@ -1,23 +1,15 @@
<?php
-
+include 'Controller/api/api.php';
class Key
{
- public function retrieveHumidityTable($key, $generateXml = false)
- {
+ public function retrieveHumidityTable($key, $generateXml = false){
+ get_graph();
$html = '<table>';
- $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2");
- $query = "SELECT distinct temperature, dew_point, nl.name as city, date_time
- FROM weather_data wd
- JOIN station s ON wd.station_name = s.name
- JOIN contract_station cs ON cs.station_name = s.name
- JOIN nearestlocation nl ON nl.station_name = s.name
- JOIN country co ON co.country_code = nl.country_code
- JOIN geolocation geo ON geo.country_code = co.country_code
- JOIN contract c ON cs.contract_id = c.contract_id
- WHERE c.token = ?
+ $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "panda");
+ $query = "SELECT humidity, city, date_time
+ FROM retrieve_facilities
ORDER BY date_time DESC";
$stmt = mysqli_prepare($db, $query);
- $stmt->bind_param("s", $key);
$stmt->execute();
$data = $stmt->get_result();
@@ -30,7 +22,7 @@ class Key
$xml1 = '<location name="Chengdu">';
$xml2 = '<location name="Kangding">';
while ($row = $data->fetch_assoc()) {
- $humidity = Model\Formula::humid($row["temperature"], $row["dew_point"]);
+ $humidity = $row['humidity'];
$current_date = $row['date_time'];
$correct_date = date("d M Y H:i:s", strtotime($current_date));
$city = $row['city'];
@@ -62,25 +54,16 @@ class Key
}
- public function retrieveHData($key, $generateXml = false)
- {
+ public function retrieveHData($key, $generateXml = false){
+ get_windchill();
$html = '';
- $citycity = '';
- $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2");
- $query = "SELECT distinct temperature, wind_speed, dew_point, DATE(date_time) AS date, nl.name as city
- FROM weather_data wd
- JOIN station s ON wd.station_name = s.name
- JOIN contract_station cs ON cs.station_name = s.name
- JOIN nearestlocation nl ON nl.station_name = s.name
- JOIN country co ON co.country_code = nl.country_code
- JOIN geolocation geo ON geo.country_code = co.country_code
- JOIN contract c ON cs.contract_id = c.contract_id
- WHERE c.token = ?
- ORDER BY date DESC, temperature, wind_speed
+ $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "panda");
+ $query = "SELECT distinct wind_chill, date, city
+ FROM retrieve_zoos
+ ORDER BY date DESC, wind_chill
";
$stmt = mysqli_prepare($db, $query);
- $stmt->bind_param("s", $key);
$stmt->execute();
$data = $stmt->get_result();
$xml = '<WCTPD name="Windchill corrected temperature per day">';
@@ -89,7 +72,7 @@ class Key
$locations = array();
while ($row = $data->fetch_assoc()) {
- $wind_chill = Model\Formula::windchill($row["temperature"], $row["wind_speed"]);
+ $wind_chill = $row['wind_chill'];
$current_date = $row['date'];
$city = $row['city'];
diff --git a/index.php b/index.php
@@ -18,14 +18,14 @@ $router->addRoute(["POST"], "/login", $login);
$router->addRoute(["POST", "GET"], "/logout", $logout);
$router->addRoute(["GET"], "/api/:data", function ($key) {
+ include 'Controller/api/api.php';
$weather_data = [];
if($key['data'] == ':3jvl7yb5sRr80s6lTdeOyxV9VTQZkCPRp7bKOWKFWxfL2vhsU4Hhpgcmz9qe0zEk') {
- include 'Controller/api/api.php';
get_windchill();
$db = new mysqli("86.92.67.21", "friedel", "hailiwa", "panda");
$query = "SELECT city, min(wind_chill) as wind_chill, country
FROM retrieve_zoos
- WHERE date = '2023-06-28'
+ WHERE date = curdate()
group by city, country
";
@@ -43,10 +43,9 @@ $router->addRoute(["GET"], "/api/:data", function ($key) {
header('Content-Type: application/json');
echo json_encode($weather_data);
} elseif($key['data'] == ':ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684') {
- include 'Controller/api/api.php';
get_graph();
- $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "panda");
$weather_data = [];
+ $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "panda");
$query = "SELECT distinct humidity, city, date_time
FROM retrieve_facilities
";