commit b02128e885d667addd55ed9a84c38038e79f8672
parent 3fba5c2168babde150b1a2c697245333448615e7
Author: DennyVB <[email protected]>
Date: Sun, 2 Apr 2023 14:19:44 +0200
temperatuurvalidator + testfile
Hij werkt en returned nu de gecorrigeerde temperatuur wanneer nodig
Diffstat:
3 files changed, 57 insertions(+), 26 deletions(-)
diff --git a/datavalidator.php b/datavalidator.php
@@ -1,35 +1,51 @@
<?php
$redis = new Redis();
-$redis->connect('86.92.67.21', 3306);
-$redis->auth('hailiwa');
+$redis->connect('127.0.0.1', 6379);
+
/**
* Validates the temperature against the average of the last 30 rows for the given station name.
*
- * @param string $sname The station name to validate against.
- * @param float $temp The temperature value to validate.
- * @param mysqli $conn The mysqli connection object.
- * @return int Returns 1 if the temperature is within 20% difference from the average temperature, or 0 if it is not.
+ * @param string $station The station name to validate against.
+ * @param float $temperatuur The temperature value to validate.
+ * @param Redis $redis The Redis connection object.
+ * @return float Returns the correct temperature.
*/
-function validate_temperature($station, $temperatuur, $db_connection) {
- // Retrieve the average of the last 30 rows for the given station name
- $sql = "SELECT AVG(temp) AS avg_temp FROM (SELECT temp FROM weerdata
- WHERE station_name = ?
- ORDER BY datum_tijd
- DESC LIMIT 30) AS last_30_rows";
- $stmt = $db_connection->prepare($sql);
- $stmt->bind_param('s', $station);
- $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;
- } else {
- return 1;
+function validate_temperature($station, $temperatuur, $redis) {
+ // Return the correct temperature validated with last 30 values.
+
+ //Deze checkt of het de eerste value is en maakt 2 queues aan en knalt de waardes erin.
+ if(!$redis->exists('values'.$station)) {
+ $redis->lpush('values'.$station, $temperatuur);
+ $redis->lpush('average'.$station, $temperatuur);
+ return $temperatuur;
+ }
+ //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) {
+ $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 $temperatuur;
+ }
+ //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.
+ else {
+ $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;
+ $redis->lpush('average'.$station, $new_avg);
+ $redis->lpush('values'.$station, $temperatuur);
+ return $temperatuur;
+ }
+ else {
+ $new_avg = (($redis->rpop('average'.$station) * 30) - $redis->rpop('values'.$station) + $temperatuur) / 30;
+ $redis->lpush('average'.$station, $new_avg);
+ $redis->lpush('values'.$station, $temperatuur);
+ return $temperatuur;
+ }
+ }
}
}
\ No newline at end of file
diff --git a/redisconnection.php b/redisconnection.php
@@ -4,7 +4,7 @@ $redis->connect('127.0.0.1', 6379);
//$redis->flushAll(); //Dit cleart de hele database :)
-$newValue = 28;
+$newValue = 28;
if(!$redis->exists('values')) {
$redis->lpush('values', $newValue);
diff --git a/testfileValidator.php b/testfileValidator.php
@@ -0,0 +1,15 @@
+<?php
+
+$redis = new Redis();
+$redis->connect('127.0.0.1', 6379);
+
+include'datavalidator.php';
+
+
+
+
+echo validate_temperature(100, 80, $redis);
+echo " ";
+echo $redis->lIndex('average100', 0);
+
+