commit 273aef4aa9307833e3de5a1cd69c2ea8dba29b5a
parent 32724d6da6e10ff8c8809c59cc280142632904db
Author: LennartSchroot <[email protected]>
Date: Sun, 2 Apr 2023 15:33:30 +0200
Redis/SQL
Redis werkt nu als het goed is en de data word geupload naar de SQL database (na 30 waarden van een bepaald station)
Diffstat:
2 files changed, 37 insertions(+), 18 deletions(-)
diff --git a/datainserter.php b/datainserter.php
@@ -5,6 +5,9 @@ $username = "friedel";
$password = "hailiwa";
$dbname = "wap2";
+$redis = new Redis();
+$redis->connect('127.0.0.1', 6379);
+
// Create connection
$db_connection = mysqli_connect($servername, $username, $password, $dbname);
@@ -35,17 +38,32 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$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'];
+ $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'];
+// $stn = 637200;
+// $date = "2022-02-09";
+// $time = "00:00:58";
+// $temp = 10.1;
+// $dewp = 1.5;
+// $stp = 984.1;
+// $slp = 1012.6;
+// $visib = 23.4;
+// $wdsp = 13.8;
+// $prcp = 0.00;
+// $sndp = 0.0;
+// $frshtt = "000000";
+// $cldc = 96.8;
+// $wnddir = 228;
+
// Validate temperature
- $valid = validate_temperature($stn, $temp, $db_connection);
+ $valid = validate_temperature($stn, $temp, $redis);
// Prepare SQL INSERT-statement
$sql = "INSERT INTO weerdata (station_name,datum_tijd,gevalideerd,temp,dauwpunt,ldrk_znv,ldrk_station,zichtbaarheid,
@@ -59,21 +77,26 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$datetime = $date . " " . $time;
// Extract the boolean values from $frshtt
- $vorst = (int) substr($frshtt, 0, 1);
- $regen = (int) substr($frshtt, 1, 1);
- $sneeuw = (int) substr($frshtt, 2, 1);
- $hagel = (int) substr($frshtt, 3, 1);
- $onweer = (int) substr($frshtt, 4, 1);
- $tornado = (int) substr($frshtt, 5, 1);
+ $vorst = (int)substr($frshtt, 0, 1);
+ $regen = (int)substr($frshtt, 1, 1);
+ $sneeuw = (int)substr($frshtt, 2, 1);
+ $hagel = (int)substr($frshtt, 3, 1);
+ $onweer = (int)substr($frshtt, 4, 1);
+ $tornado = (int)substr($frshtt, 5, 1);
// Bind parameters to prepared statement
- 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);
+ 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);
}
+ // 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";
+ }
+
// Second Insert for shadow table
if ($valid == 0) {
$sql2 = "SELECT data_id from weerdata where station_name = ? and datum_tijd = ?";
@@ -88,9 +111,8 @@ 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);
+ 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);
$result = mysqli_stmt_execute($stmt3);
-
}
// Check if the insertion was successful
diff --git a/datavalidator.php b/datavalidator.php
@@ -1,8 +1,5 @@
<?php
-$redis = new Redis();
-$redis->connect('127.0.0.1', 6379);
-
/**
* Validates the temperature against the average of the last 30 rows for the given station name.