iwa-panda1

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

commit beb5e0b50d95130125250bdec568a942629fb89c
parent 273aef4aa9307833e3de5a1cd69c2ea8dba29b5a
Author: LennartSchroot <[email protected]>
Date:   Sun,  2 Apr 2023 17:31:44 +0200

updateske

Diffstat:
Mdatainserter.php | 22+++++++++++++---------
Mdatavalidator.php | 12++++++------
MtestfileValidator.php | 7++++---
3 files changed, 23 insertions(+), 18 deletions(-)

diff --git a/datainserter.php b/datainserter.php @@ -18,6 +18,7 @@ if (!$db_connection) { // Check if request method is POST if ($_SERVER['REQUEST_METHOD'] == 'POST') { + // Include data validator function include 'datavalidator.php'; @@ -88,17 +89,17 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { mysqli_stmt_bind_param($stmt, "isiddddddddsiiiiiidi", $stn, $datetime, $valid, $temp, $dewp, $stp, $slp, $visib, $wdsp, $prcp, $sndp, $frshtt, $vorst, $regen, $sneeuw, $hagel, $onweer, $tornado, $cldc, $wnddir); // Execute prepared statement, only if the data is not within the first 30 entries - if ($valid == 1 || $valid == 0) { - $result = mysqli_stmt_execute($stmt); - } + $result = mysqli_stmt_execute($stmt); + // wanneer $valid -1 is zal deze tot de eerste 30 waarden van een station behoren, en dus niet in de database worden gemikt - if ($valid == -1) { - echo "entry geskipt"; - } +// if ($valid == -1) { +// echo "entry geskipt"; +// $result = false; +// } // Second Insert for shadow table - if ($valid == 0) { + if ($valid == -1) { $sql2 = "SELECT data_id from weerdata where station_name = ? and datum_tijd = ?"; $stmt2 = mysqli_prepare($db_connection, $sql2); mysqli_stmt_bind_param($stmt2, "is", $stn, $datetime); @@ -111,14 +112,17 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { windsnelheid,neerslag,sneeuwdpt,gebeurtenissen, vorst,regen,sneeuw,hagel,onweer,tornado, bewolking,windrichting) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $stmt3 = mysqli_prepare($db_connection, $sql3); - mysqli_stmt_bind_param($stmt3, "iisddddddddsiiiiiidi", $stn, $data_id, $datetime, $temp, $dewp, $stp, $slp, $visib, $wdsp, $prcp, $sndp, $frshtt, $vorst, $regen, $sneeuw, $hagel, $onweer, $tornado, $cldc, $wnddir); + $new_temp = round($redis->lIndex('average'.$stn, 0), 1); + mysqli_stmt_bind_param($stmt3, "iisddddddddsiiiiiidi", $stn, $data_id, $datetime, $new_temp, $dewp, $stp, $slp, $visib, $wdsp, $prcp, $sndp, $frshtt, $vorst, $regen, $sneeuw, $hagel, $onweer, $tornado, $cldc, $wnddir); $result = mysqli_stmt_execute($stmt3); } + // Check if the insertion was successful if ($result) { echo "Data inserted successfully"; - } else { + } + else { echo "Error inserting data: " . mysqli_error($db_connection); } } diff --git a/datavalidator.php b/datavalidator.php @@ -16,15 +16,15 @@ function validate_temperature($station, $temperatuur, $redis) { if(!$redis->exists('values'.$station)) { $redis->lpush('values'.$station, $temperatuur); $redis->lpush('average'.$station, $temperatuur); - return -1; + return 0; } //Als het aantal values de 30 nog niet bereikt heeft, berekend hij gewoon het gemiddelde en voegt de value toe aan de queue else { - if($redis->llen('values'.$station) < 30) { + if($redis->llen('values'.$station) < 3) { $avg = ($redis->rpop('average'.$station) * $redis->llen('values'.$station) + $temperatuur) / ($redis->llen('values'.$station) + 1); $redis->lpush('average'.$station, $avg); $redis->lpush('values'.$station, $temperatuur); - return -1; + return 0; } //Bij 30 values zal de volgende value eerst gecheckt worden met het bekende gemiddelde en eventueel aangepast worden. //Daarna zal deze toegevoegd worden aan de value en average queue. De laatste waarde zal eruit gehaald worden. @@ -32,13 +32,13 @@ function validate_temperature($station, $temperatuur, $redis) { $current_avg = $redis->lIndex('average'.$station, 0); if(abs(($temperatuur - $current_avg) / $current_avg) > 0.2) { $temperatuur = $current_avg; - $new_avg = (($redis->rpop('average'.$station) * 30) - $redis->rpop('values'.$station) + $temperatuur) / 30; + $new_avg = (($redis->rpop('average'.$station) * 3) - $redis->rpop('values'.$station) + $temperatuur) / 3; $redis->lpush('average'.$station, $new_avg); $redis->lpush('values'.$station, $temperatuur); - return 0; + return -1; } else { - $new_avg = (($redis->rpop('average'.$station) * 30) - $redis->rpop('values'.$station) + $temperatuur) / 30; + $new_avg = (($redis->rpop('average'.$station) * 3) - $redis->rpop('values'.$station) + $temperatuur) / 3; $redis->lpush('average'.$station, $new_avg); $redis->lpush('values'.$station, $temperatuur); return 1; diff --git a/testfileValidator.php b/testfileValidator.php @@ -5,11 +5,12 @@ $redis->connect('127.0.0.1', 6379); include'datavalidator.php'; +$success = $redis->flushDB(); -echo validate_temperature(100, 80, $redis); -echo " "; -echo $redis->lIndex('average100', 0); +//echo validate_temperature(100, 80, $redis); +//echo " "; +//echo $redis->lIndex('average100', 0);