iwa-panda1

Manage Weather Data by International Weather Agency (Version 1)
Log | Files | Refs

commit 3db2c144d97d374e0f884af91710ed650c3b3208
parent 4172bcb4e93128b7c493fc64be94ad5401a1d9b4
Author: Gerco van Woudenbergh <[email protected]>
Date:   Sat, 25 Mar 2023 18:24:15 +0100

dataverwerker werkt met de database Kars, Johs, Friedel en Gerco

Diffstat:
Mdatainserter.php | 95++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mdatavalidator.php | 4+++-
Alogin.html | 18++++++++++++++++++
Mlogin.php | 58++++++++++++++++++++++++++++++++++------------------------
ApostWeatherData.php | 44++++++++++++++++++++++++++++++++++++++++++++
Aweather_data.txt | 10++++++++++
6 files changed, 167 insertions(+), 62 deletions(-)

diff --git a/datainserter.php b/datainserter.php @@ -5,7 +5,6 @@ $username = "friedel"; $password = "hailiwa"; $dbname = "wap2"; - // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); @@ -15,42 +14,64 @@ if (!$conn) { } // Check if request method is POST -if ($_SERVER["REQUEST_METHOD"] == "POST") { - - // Include datavalidator +if ($_SERVER['REQUEST_METHOD'] == 'POST') { + // Include data validator function include 'datavalidator.php'; - // Collect value of input fields - $station = $_POST['STN']; - $date = $_POST['DATE']; - $time = $_POST['TIME']; - $temperatuur = $_POST['TEMP']; - $dauwpunt = $_POST['DEWP']; - $luchtdrukstationniveau = $_POST['STP']; - $luchtdrukzeeniveau = $_POST['SLP']; - $zichtbaarheid = $_POST['VISIB']; - $windsnelheid = $_POST['WDSP']; - $neerslag = $_POST['PRCP']; - $sneeuwdiepte = $_POST['SNDP']; - $gebeurtenissen = $_POST['FRSHTT']; - $bewolking = $_POST['CLDC']; - $windrichting = $_POST['WNDDIR']; - - // Validate temperature - $valid = validate_temperature($station, $temperatuur, $conn); - - // Prepare SQL INSERT-statement - $sql = "INSERT INTO weerdata VALUES (?, DATE_FORMAT(?, '%Y%m%d%H%i'), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - - // Create prepared statement with the SQL statement - $stmt = mysqli_prepare($conn, $sql); - - // Concatenate date and time - $datetime = $date . $time; - - // Bind parameters to prepared statement - mysqli_stmt_bind_param($stmt, "ssiiiiiiiisii", $station, $datetime, $valid, $dauwpunt, $luchtdrukstationniveau, $luchtdrukzeeniveau, $zichtbaarheid, $windsnelheid, $neerslag, $sneeuwdiepte, $gebeurtenissen, $bewolking, $windrichting); - - // Execute prepared statement - $result = mysqli_stmt_execute($stmt); + // Retrieve the raw request body data + $request_body = file_get_contents('php://input'); + + // Decode the JSON data into a PHP associative array + $data = json_decode($request_body, true); + + // Access the "WEATHERDATA" array from the decoded data + $weather_data = $data['WEATHERDATA']; + + // Loop through each weather data object and insert it into the database + foreach ($weather_data as $weather_obj) { + $stn = $weather_obj['STN']; + $date = $weather_obj['DATE']; + $time = $weather_obj['TIME']; + $temp = $weather_obj['TEMP']; + $dewp = $weather_obj['DEWP']; + $stp = $weather_obj['STP']; + $slp = $weather_obj['SLP']; + $visib = $weather_obj['VISIB']; + $wdsp = $weather_obj['WDSP']; + $prcp = $weather_obj['PRCP']; + $sndp = $weather_obj['SNDP']; + $frshtt = $weather_obj['FRSHTT']; + $cldc = $weather_obj['CLDC']; + $wnddir = $weather_obj['WNDDIR']; + + // Validate temperature + $valid = validate_temperature($stn, $temp, $conn); + + // Prepare SQL INSERT-statement + $sql = "INSERT INTO weerdata (station_name,datum_tijd,gevalideerd,temp,dauwpunt,ldrk_znv,ldrk_station,zichtbaarheid, + windsnelheid,neerslag,sneeuwdpt,gebeurtenissen,bewolking,windrichting) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + + // Create prepared statement with the SQL statement + $stmt = mysqli_prepare($conn, $sql); + + // Concatenate date and time + $datetime = $date . " " . $time; + + // Bind parameters to prepared statement + mysqli_stmt_bind_param($stmt, "isiddddddddsdi", $stn, $datetime, $valid, $temp, $dewp, $stp, $slp, $visib, $wdsp, $prcp, $sndp, $frshtt, $cldc, $wnddir); + + // Execute prepared statement + $result = mysqli_stmt_execute($stmt); + + // Check if the insertion was successful + if ($result) { + echo "Data inserted successfully"; + } else { + echo "Error inserting data: " . mysqli_error($conn); + } + } } + +// Close the database connection +mysqli_close($conn); +?> diff --git a/datavalidator.php b/datavalidator.php @@ -18,7 +18,9 @@ function validate_temperature($station, $temperatuur, $conn) { $stmt->execute(); $result = $stmt->get_result(); $avg_temp = mysqli_fetch_assoc($result)['avg_temp']; - + + if($avg_temp == 0) + $avg_temp = 1; // Check if $temp is within 20% difference from average temperature in database if(abs($temperatuur - $avg_temp) / $avg_temp > 0.2) { return 0; diff --git a/login.html b/login.html @@ -0,0 +1,17 @@ +<!DOCTYPE html> +<html> + <head> + <title>Login Page</title> + </head> + <body> + <h2>Login</h2> + <form method="POST" action="login.php"> + <label>Username:</label> + <input type="text" name="username"><br><br> + <label>Password:</label> + <input type="password" name="password"><br><br> + + <input type="submit" name="login" value="Login"> + </form> + </body> +</html> +\ No newline at end of file diff --git a/login.php b/login.php @@ -1,14 +1,26 @@ <!DOCTYPE html> <html> - <head> - <title>Login Page</title> - </head> - <body> - <h2>Login</h2> + <head> + <title>Login Page</title> + </head> + <body> + <h2>Login</h2> + <form method="POST" action="login.php"> + <label>Username:</label> + <input type="text" name="username"><br><br> + <label>Password:</label> + <input type="password" name="password"><br><br> + + <input type="submit" name="login" value="Login"> + </form> <?php // fetch data from the form - $gebruikersnaam = $_POST['username']; - $wachtwoord = $_POST['password']; + if(isset($_POST['username']) && isset($_POST['password'])){ + $gebruikersnaam = $_POST['username']; + $wachtwoord = $_POST['password']; + }else{ + echo "One of the forms was empty"; + } // perform validation and authentication $servername = "86.92.67.21"; @@ -29,23 +41,20 @@ } //verification logic and $_SESSION start - if($gebruikersnaam == $row['medewerker_id'] && password_verify($wachtwoord, $row['wachtwoord'])) - { - session_start(); - $_SESSION['gebruikers_id'] = $row['medewerker_id']; - $_SESSION['permissions'] = $row['permissie_niveau']; - header('Location: dashboard.php'); - echo"gebruikers id". $_SESSION['gebruikers_id'] ."permissie niveau " . $_SESSION['permissie_niveau']; + if($row > 0){ + if($gebruikersnaam == $row['medewerker_id'] && password_verify($wachtwoord, $row['wachtwoord'])) + { + session_start(); + $_SESSION['gebruikers_id'] = $row['medewerker_id']; + $_SESSION['permissions'] = $row['permissie_niveau']; + header('Location: dashboard.php'); + echo"gebruikers id". $_SESSION['gebruikers_id'] ."permissie niveau " . $_SESSION['permissie_niveau']; + } else { + echo '<p style="color:red">Invalid username or password.</p>'; + } } else { echo '<p style="color:red">Invalid username or password.</p>'; - } + } ?> - <form method="POST"> - <label>Username:</label> - <input type="text" name="username"><br><br> - <label>Password:</label> - <input type="password" name="password"><br><br> - <input type="submit" name="login" value="Login"> - </form> - </body> -</html> + </body> +</html> +\ No newline at end of file diff --git a/postWeatherData.php b/postWeatherData.php @@ -0,0 +1,44 @@ +<?php + +// Check if request method is POST +if ($_SERVER['REQUEST_METHOD'] == 'POST') { + // Include data validator function + include 'datavalidator.php'; + + // Retrieve the raw request body data + $request_body = file_get_contents('php://input'); + + // Decode the JSON data into a PHP associative array + $data = json_decode($request_body, true); + + // Access the "WEATHERDATA" array from the decoded data + $weather_data = $data['WEATHERDATA']; + + // Open the text file for writing + $file = fopen('weather_data.txt', 'w'); + + // Loop through each weather data object and write it to the text file + foreach ($weather_data as $weather_obj) { + $stn = $weather_obj['STN']; + $date = $weather_obj['DATE']; + $time = $weather_obj['TIME']; + $temp = $weather_obj['TEMP']; + $dewp = $weather_obj['DEWP']; + $stp = $weather_obj['STP']; + $slp = $weather_obj['SLP']; + $visib = $weather_obj['VISIB']; + $wdsp = $weather_obj['WDSP']; + $prcp = $weather_obj['PRCP']; + $sndp = $weather_obj['SNDP']; + $frshtt = $weather_obj['FRSHTT']; + $cldc = $weather_obj['CLDC']; + $wnddir = $weather_obj['WNDDIR']; + + // Write the weather data to the text file + fwrite($file, "$stn,$date,$time,$temp,$dewp,$stp,$slp,$visib,$wdsp,$prcp,$sndp,$frshtt,$cldc,$wnddir\n"); + } + + // Close the text file + fclose($file); +} +?> diff --git a/weather_data.txt b/weather_data.txt @@ -0,0 +1,10 @@ +726710,2023-03-25,17:21:03,-1.5,-9.9,1026.1,1015.8,14.6,9.7,0.03,0,110000,50,283 +602300,2023-03-25,17:21:03,13.8,5.7,1014.8,1017.2,18.4,9.2,0.01,0,010000,90.2,174 +726700,2023-03-25,17:21:03,0.2,-11.4,983.2,997.6,26.6,12.9,0,0,000000,18,105 +74000,2023-03-25,17:21:03,12.4,8.3,976.1,999.4,14.4,43.6,0.3,0,010000,50,326 +108690,2023-03-25,17:21:03,9.9,5.5,976.5,1006.3,10.7,11.3,0,0,000000,32.6,75 +277190,2023-03-25,17:21:03,-0.2,-4.3,995.6,1016.9,9.2,9,0.09,11.8,111000,50,335 +338370,2023-03-25,17:21:03,3.2,0.5,1013,1016.7,6.7,17,0.04,0.1,011000,50,117 +91610,2023-03-25,17:21:03,7.1,3.7,1006.2,1008.8,11.4,21.2,0.16,0,010000,74.8,166 +637230,2023-03-25,17:21:03,14.4,2.9,1040.9,1007.4,26.4,11.9,0,0,000000,66.7,None +386180,2023-03-25,17:21:03,4.3,-6.1,1002,1023.1,9.9,8.9,0,0,000000,57.3,15