aboutsummaryrefslogtreecommitdiff
blob: 4434d47322fd708afa43de96ba74d823dcafd4a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;


class AllDataAverageController extends Controller
{
    /**
     * @Route("/AllDataAverage", name="AllDataAverage")
     */

    public function GetAllDataAverage()
    {
        // Get cURL resource
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, "https://pollutometerapi.azurewebsites.net/api/Readings");
        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);
        $readings = array();
        $results = array();

        usort($data, function($a,$b){
            return $a['TimeStamp'] - $b['TimeStamp'];
        });

        foreach($data as $index => $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;
    }
}