From ed01011dfb563e7d8ab13b6a0718eed7bf857880 Mon Sep 17 00:00:00 2001 From: Marcin Zelent Date: Fri, 16 Mar 2018 17:48:44 +0100 Subject: Moved PHP website to separate directory. --- pollutometer-php/src/.htaccess | 7 ++ pollutometer-php/src/AppBundle/AppBundle.php | 9 ++ .../Controller/AllDataAverageController.php | 66 +++++++++++++ .../src/AppBundle/Controller/AllDataController.php | 43 ++++++++ .../src/AppBundle/Controller/HomeController.php | 60 +++++++++++ .../Controller/LastWeekDataAverageController.php | 66 +++++++++++++ .../Controller/LastWeekDataController.php | 42 ++++++++ .../src/AppBundle/Controller/NewDataController.php | 42 ++++++++ .../Controller/TrainScheduleController.php | 110 +++++++++++++++++++++ pollutometer-php/src/AppBundle/Utils/Aqi.php | 43 ++++++++ 10 files changed, 488 insertions(+) create mode 100644 pollutometer-php/src/.htaccess create mode 100644 pollutometer-php/src/AppBundle/AppBundle.php create mode 100644 pollutometer-php/src/AppBundle/Controller/AllDataAverageController.php create mode 100644 pollutometer-php/src/AppBundle/Controller/AllDataController.php create mode 100644 pollutometer-php/src/AppBundle/Controller/HomeController.php create mode 100644 pollutometer-php/src/AppBundle/Controller/LastWeekDataAverageController.php create mode 100644 pollutometer-php/src/AppBundle/Controller/LastWeekDataController.php create mode 100644 pollutometer-php/src/AppBundle/Controller/NewDataController.php create mode 100644 pollutometer-php/src/AppBundle/Controller/TrainScheduleController.php create mode 100644 pollutometer-php/src/AppBundle/Utils/Aqi.php (limited to 'pollutometer-php/src') diff --git a/pollutometer-php/src/.htaccess b/pollutometer-php/src/.htaccess new file mode 100644 index 0000000..fb1de45 --- /dev/null +++ b/pollutometer-php/src/.htaccess @@ -0,0 +1,7 @@ + + Require all denied + + + Order deny,allow + Deny from all + diff --git a/pollutometer-php/src/AppBundle/AppBundle.php b/pollutometer-php/src/AppBundle/AppBundle.php new file mode 100644 index 0000000..05123b6 --- /dev/null +++ b/pollutometer-php/src/AppBundle/AppBundle.php @@ -0,0 +1,9 @@ + $item) + { + $data[$index]['TimeStamp'] = gmdate('d F l', $item['TimeStamp']); + $readings[$data[$index]['TimeStamp']][] = $data[$index]; + } + + foreach ($readings as $key => $item) + { + $gasAverage = array('Co' => 0, 'No' => 0, 'So' => 0); + foreach ($readings[$key] as $index => $values) + { + $gasAverage['Co'] += $readings[$key][$index]['Co']; + $gasAverage['No'] += $readings[$key][$index]['No']; + $gasAverage['So'] += $readings[$key][$index]['So']; + + if($index === count($readings[$key]) - 1) + { + $gasAverage['Co'] /= $index + 1; + $gasAverage['No'] /= $index + 1; + $gasAverage['So'] /= $index + 1; + } + $results[$key] = $gasAverage; + } + } + + $data = json_encode($results); + + $response = new Response($data); + $response->headers->set('Content-Type', 'application/json'); + return $response; + } +} \ No newline at end of file diff --git a/pollutometer-php/src/AppBundle/Controller/AllDataController.php b/pollutometer-php/src/AppBundle/Controller/AllDataController.php new file mode 100644 index 0000000..b45d2c9 --- /dev/null +++ b/pollutometer-php/src/AppBundle/Controller/AllDataController.php @@ -0,0 +1,43 @@ + $item) + { + $data[$index]['TimeStamp'] = gmdate("l jS \of F Y h:i:s A", $item['TimeStamp']); + } + + $parametersToTwig = array("data" => $data); + + return $this->render('default/AllData.html.twig',$parametersToTwig); + + } +} \ No newline at end of file diff --git a/pollutometer-php/src/AppBundle/Controller/HomeController.php b/pollutometer-php/src/AppBundle/Controller/HomeController.php new file mode 100644 index 0000000..d84ab76 --- /dev/null +++ b/pollutometer-php/src/AppBundle/Controller/HomeController.php @@ -0,0 +1,60 @@ + array('breakpoints' => [0, 4.4, 4.5, 9.4, 9.5, 12.4, 12.5, 15.4, 15.5, 30.4, 30.5, 40.4, 40.5, 50.4], + 'aq' => [0, 50, 51, 100, 101, 150, 151, 200, 201, 300, 301, 400, 401, 500]), + 'So' => array('breakpoints' => [0.000, 0.034, 0.035, 0.144, 0.145, 0.224, 0.225, 0.304, 0.305, 0.604, 0.605, 0.804, 0.805, 1.004], + 'aq' => [0, 50, 51, 100, 101, 150, 151, 200, 201, 300, 301, 400, 401, 500]), + 'No' => array('breakpoints' => [0,0.05,0.08,0.10,0.15,0.20,0.25 ,0.31,0.65, 1.24, 1.25, 1.64, 1.65, 2.04], + 'aq' => [0 ,50 ,51 ,100 ,101 ,150 ,151,200,201, 300, 301, 400, 401, 500]) + ); + + + $tableObj = json_decode(json_encode($table)); + + // Get cURL resource + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, "https://pollutometerapi.azurewebsites.net/api/Readings/latest"); + curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json')); // Assuming you're requesting JSON + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + // Send the request & save response to $resp + $resp = curl_exec($curl); + // Close request to clear up some resources + curl_close($curl); + + $data = json_decode($resp, true); + $data['TimeStamp'] = gmdate("l jS \of F Y h:i:s A", $data['TimeStamp']); + + $arr = []; + $CO = is_nan($aqi->calculateAQI("Co", $data['Co'], $tableObj)) ? 0 : $aqi->calculateAQI("Co", $data['Co'], $tableObj); + $SO = is_nan($aqi->calculateAQI("So", $data['So'], $tableObj)) ? 0 : $aqi->calculateAQI("So", $data['So'], $tableObj); + $NO = is_nan($aqi->calculateAQI("No", $data['No'], $tableObj)) ? 0 : $aqi->calculateAQI("No", $data['No'], $tableObj); + + array_push($arr, $CO, $SO, $NO); + $max = max($arr); + $data['Aqi'] = $max; + + return $this->render('default/index.html.twig', $data); + } +} diff --git a/pollutometer-php/src/AppBundle/Controller/LastWeekDataAverageController.php b/pollutometer-php/src/AppBundle/Controller/LastWeekDataAverageController.php new file mode 100644 index 0000000..e25b0a8 --- /dev/null +++ b/pollutometer-php/src/AppBundle/Controller/LastWeekDataAverageController.php @@ -0,0 +1,66 @@ + $item) + { + $data[$index]['TimeStamp'] = gmdate('d F l', $item['TimeStamp']); + $readings[$data[$index]['TimeStamp']][] = $data[$index]; + } + + foreach ($readings as $key => $item) + { + $gasAverage = array('Co' => 0, 'No' => 0, 'So' => 0); + foreach ($readings[$key] as $index => $values) + { + $gasAverage['Co'] += $readings[$key][$index]['Co']; + $gasAverage['No'] += $readings[$key][$index]['No']; + $gasAverage['So'] += $readings[$key][$index]['So']; + + if($index === count($readings[$key]) - 1) + { + $gasAverage['Co'] /= $index + 1; + $gasAverage['No'] /= $index + 1; + $gasAverage['So'] /= $index + 1; + } + $results[$key] = $gasAverage; + } + } + + $data = json_encode($results); + + $response = new Response($data); + $response->headers->set('Content-Type', 'application/json'); + return $response; + } +} \ No newline at end of file diff --git a/pollutometer-php/src/AppBundle/Controller/LastWeekDataController.php b/pollutometer-php/src/AppBundle/Controller/LastWeekDataController.php new file mode 100644 index 0000000..7b5d993 --- /dev/null +++ b/pollutometer-php/src/AppBundle/Controller/LastWeekDataController.php @@ -0,0 +1,42 @@ + $item) + { + $data[$index]['TimeStamp'] = gmdate("l jS \of F Y h:i:s A", $item['TimeStamp']); + } + + $parametersToTwig = array("data" => $data); + + return $this->render('default/LastWeekData.html.twig',$parametersToTwig); + } +} \ No newline at end of file diff --git a/pollutometer-php/src/AppBundle/Controller/NewDataController.php b/pollutometer-php/src/AppBundle/Controller/NewDataController.php new file mode 100644 index 0000000..5dd440f --- /dev/null +++ b/pollutometer-php/src/AppBundle/Controller/NewDataController.php @@ -0,0 +1,42 @@ +headers->set('Content-Type', 'application/json'); + return $response; + } + +} diff --git a/pollutometer-php/src/AppBundle/Controller/TrainScheduleController.php b/pollutometer-php/src/AppBundle/Controller/TrainScheduleController.php new file mode 100644 index 0000000..a6b517d --- /dev/null +++ b/pollutometer-php/src/AppBundle/Controller/TrainScheduleController.php @@ -0,0 +1,110 @@ + time()) + { + $trains[$i]['direction'] = 0; + continue; + } + + foreach ($readings as $reading) { + if (abs($reading['TimeStamp'] - $trainTimeStamp) < abs($closest - $trainTimeStamp)) + $closest = $reading['TimeStamp']; + } + + $closestReading = $readings[0]; + foreach ($readings as $reading) + { + if($reading['TimeStamp'] == $closest) $closestReading = $reading; + } + $trains[$i]['direction'] = $this->getAqi($closestReading); + } + + $parametersToTwig = array("data" => $trains); + + return $this->render('default/TrainSchedule.html.twig', $parametersToTwig); + } + + private function getAqi(array $data) + { + $aqi = new Aqi(); + + $table = array( + 'Co' => array('breakpoints' => [0, 4.4, 4.5, 9.4, 9.5, 12.4, 12.5, 15.4, 15.5, 30.4, 30.5, 40.4, 40.5, 50.4], + 'aq' => [0, 50, 51, 100, 101, 150, 151, 200, 201, 300, 301, 400, 401, 500]), + 'So' => array('breakpoints' => [0.000, 0.034, 0.035, 0.144, 0.145, 0.224, 0.225, 0.304, 0.305, 0.604, 0.605, 0.804, 0.805, 1.004], + 'aq' => [0, 50, 51, 100, 101, 150, 151, 200, 201, 300, 301, 400, 401, 500]), + 'No' => array('breakpoints' => [0,0.05,0.08,0.10,0.15,0.20,0.25 ,0.31,0.65, 1.24, 1.25, 1.64, 1.65, 2.04], + 'aq' => [0 ,50 ,51 ,100 ,101 ,150 ,151,200,201, 300, 301, 400, 401, 500]) + ); + + $tableObj = json_decode(json_encode($table)); + + $arr = []; + $CO = is_nan($aqi->calculateAQI("Co", $data['Co'], $tableObj)) ? 0 : $aqi->calculateAQI("Co", $data['Co'], $tableObj); + $SO = is_nan($aqi->calculateAQI("So", $data['So'], $tableObj)) ? 0 : $aqi->calculateAQI("So", $data['So'], $tableObj); + $NO = is_nan($aqi->calculateAQI("No", $data['No'], $tableObj)) ? 0 : $aqi->calculateAQI("No", $data['No'], $tableObj); + + array_push($arr, $CO, $SO, $NO); + $max = max($arr); + + return $max; + } +} \ No newline at end of file diff --git a/pollutometer-php/src/AppBundle/Utils/Aqi.php b/pollutometer-php/src/AppBundle/Utils/Aqi.php new file mode 100644 index 0000000..1b324a8 --- /dev/null +++ b/pollutometer-php/src/AppBundle/Utils/Aqi.php @@ -0,0 +1,43 @@ +{$gasName} ->{'breakpoints'}; + foreach ($arr as $index => $value) { + if ($value <= $concentration && $table->{$gasName}->{'breakpoints'}[$index + 1] >= $concentration) { + $bpLow = $value; + $bpLowIndex = $index; + } + + if ($value >= $concentration && $table->{$gasName}->{'breakpoints'}[$index - 1] <= $concentration) { + $bpHi = $value; + $bpHiIndex = $index; + } + + }; + + + + $airQualityIndex = (($table->{$gasName}->{'aq'}[$bpHiIndex] - $table->{$gasName}->{'aq'}[$bpLowIndex]) / ($bpHi - $bpLow)) * ($concentration - $bpLow) + $table->{$gasName}->{'aq'}[$bpLowIndex]; + + return $airQualityIndex; + + } +} + +?> \ No newline at end of file -- cgit v1.2.3