hanze/iwa-panda2

added /test to connect to the iwa-server using curl. reponse is a test response. Proof of concept working. (fb8238beeabca6470143ab3acbae78519ccaaccf)
Repositories | README.md

commit fb8238beeabca6470143ab3acbae78519ccaaccf
parent 7a2445c250dd0d35b645b172f00205b149735f1d
Author: Gerco van Woudenbergh <[email protected]>
Date:   Wed, 28 Jun 2023 01:56:59 +0200

added /test to connect to the iwa-server using curl. reponse is a test response. Proof of concept working.

Diffstat:
MController/api/api.php16+++++++---------
AModel/Connect.php42++++++++++++++++++++++++++++++++++++++++++
MModel/Key.php89++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mindex.php2++
4 files changed, 98 insertions(+), 51 deletions(-)

diff --git a/Controller/api/api.php b/Controller/api/api.php @@ -1,9 +1,7 @@ <?php -$data = json_decode(file_get_contents('php://input'), true); -$token = $data['token']; -$responseData = array( - 'message' => 'Token received successfully', - 'token' => $token - ); -header('Content-Type: application/json'); -echo json_encode($responseData); -\ No newline at end of file +$test = function(&$vars){ + $token = '3jvl7yb5sRr80s6lTdeOyxV9VTQZkCPRp7bKOWKFWxfL2vhsU4Hhpgcmz9qe0zEk'; + $url = 'http://86.92.67.21/data/'; + $connect = new Model\Connect($url, $token); + var_dump($connect-connect()); +}; +\ No newline at end of file diff --git a/Model/Connect.php b/Model/Connect.php @@ -0,0 +1,41 @@ +<?php +namespace Model{ + class Connect{ + protected string $url; + protected string $token; + 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( + 'Authorization: Bearer ' . $this->token, // Your token value + 'Content-Type: application/json', // Example of setting the content type header + ); + + 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); + + response($response); + return $response; + } + private function response($response){ + + } + } +} +\ No newline at end of file diff --git a/Model/Key.php b/Model/Key.php @@ -1,8 +1,10 @@ <?php -Class Key{ +class Key +{ protected array $data; - function retrieveData($key){ + function retrieveData($key) + { $validKeys = [ ':3jvl7yb5sRr80s6lTdeOyxV9VTQZkCPRp7bKOWKFWxfL2vhsU4Hhpgcmz9qe0zEk' => 'retrieveZooData', ':ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684' => 'retrieveRBData', @@ -15,7 +17,8 @@ Class Key{ } } - function retrieveZooData($key){ + function retrieveZooData($key) + { $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2"); $weather_data = []; $query = "SELECT distinct temperature, wind_speed, nl.name as city, co.country as country @@ -35,19 +38,20 @@ Class Key{ $stmt->execute(); $data = $stmt->get_result(); - while ($row = $data->fetch_assoc()) { - $wind_chill = windchill($row["temperature"], $row["wind_speed"]); - $weather_data[] = [ - 'windchill' => $wind_chill, - 'location' => $row['city'], - 'country' => $row['country'] - ]; - } - header('Content-Type: application/json'); - echo json_encode($weather_data); + while ($row = $data->fetch_assoc()) { + $wind_chill = windchill($row["temperature"], $row["wind_speed"]); + $weather_data[] = [ + 'windchill' => $wind_chill, + 'location' => $row['city'], + 'country' => $row['country'] + ]; } + header('Content-Type: application/json'); + echo json_encode($weather_data); + } - function retrieveRBData($key){ + function retrieveRBData($key) + { $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2"); $weather_data = []; $query = "SELECT distinct temperature, dew_point, nl.name as city, date_time @@ -79,7 +83,8 @@ Class Key{ } - function retrieveHumidityTable($key, $generateXml = false) { + function retrieveHumidityTable($key, $generateXml = false) + { $html = '<table>'; $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2"); $query = "SELECT distinct temperature, dew_point, nl.name as city, date_time @@ -138,7 +143,8 @@ Class Key{ } - function retrieveHData($key, $generateXml = false) { + function retrieveHData($key, $generateXml = false) + { $html = ''; $citycity = ''; $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2"); @@ -155,7 +161,7 @@ Class Key{ "; $stmt = mysqli_prepare($db, $query); - $stmt->bind_param("s", $key); + $stmt->bind_param("s", $key); $stmt->execute(); $data = $stmt->get_result(); $xml = '<WCTPD name="Windchill corrected temperature per day">'; @@ -182,29 +188,28 @@ Class Key{ $locations[$current_date][] = $city; } } - $html .= $this->retrieveHumidityTable('ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684'); - - $html .= '<table id="htmlTable">'; - $html .= '<div class="space"></div>'; - $html .= '<div class="content-title"><h2>Windchill corrected temperature per day</h2></div>'; - - foreach ($results as $date => $entries) { - $html .= '<tr><th id="tabledate" colspan="4">' . date("d M Y", strtotime($date)) . '</th></tr>'; - $html .= '<tr><th>Location</th><th>Windchill</th></tr>'; - - foreach ($entries as $entry) { - $citycity = $entry['city']; - $xml .= '<data>' . '<city>' . $citycity . '</city>' . '<wind_chill>' . $entry['windchill'] . '</wind_chill>' . '</data>'; - $html .= '<tr>'; - $html .= '<td>'.$entry['city'].'</td>'; - $html .= '<td>'.$entry['windchill'].'</td>'; - $html .= '</tr>'; - } + $html .= $this->retrieveHumidityTable('ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684'); + + $html .= '<table id="htmlTable">'; + $html .= '<div class="space"></div>'; + $html .= '<div class="content-title"><h2>Windchill corrected temperature per day</h2></div>'; + + foreach ($results as $date => $entries) { + $html .= '<tr><th id="tabledate" colspan="4">' . date("d M Y", strtotime($date)) . '</th></tr>'; + $html .= '<tr><th>Location</th><th>Windchill</th></tr>'; + + foreach ($entries as $entry) { + $xml .= '<data city="' . $entry['city'] . '">' . '<wind_chill>' . $entry['windchill'] . '</wind_chill>' . '</data>'; + $html .= '<tr>'; + $html .= '<td>' . $entry['city'] . '</td>'; + $html .= '<td>' . $entry['windchill'] . '</td>'; + $html .= '</tr>'; } + } - $html .= '</table>'; - $html .= '<a href="?downloadXml=true" class="download-button">Download XML</a>'; - $xml .= '</WCTPD>'; + $html .= '</table>'; + $html .= '<a href="?downloadXml=true" class="download-button">Download XML</a>'; + $xml .= '</WCTPD>'; if ($generateXml) { return $xml; // Return SimpleXMLElement object } @@ -214,14 +219,14 @@ Class Key{ function windchill($temp, $wind): float { - $result = 13.12 + 0.6215 * $temp - 11.37 * pow($wind, 0.16) + 0.3965* $temp * pow($wind, 0.16); + $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)); + $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); } @@ -229,7 +234,7 @@ function humid($temp, $dewp): float|int function checkHumid($temp, $dewp): string { $humidity = humid($temp, $dewp); - if ($humidity<50){ + if ($humidity < 50) { return "warning"; } else { return "dontchu worry"; diff --git a/index.php b/index.php @@ -2,6 +2,7 @@ use Lollipop\Template; require_once "utils/autoloader.php"; +require_once "Controller/api/api.php"; $templater = new Template(); $router = new Lollipop\Router($templater); @@ -13,6 +14,7 @@ $router->addRoute(["POST"], "/login", function($vars){ login_handler(); }); +$router->addRoute(['GET', 'POST'], "/test", $test); //logout $router->addRoute(["POST", "GET"], "/logout", function($vars){ include "Controller/logout/logout.php";