Advertisement
andrum99

gettemp.php

Feb 15th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.12 KB | None | 0 0
  1. <?php
  2.     // php script to output JSON data to be used client side
  3.     // by jqPlot to produce a graph
  4.     // by Andrew Pattison
  5.    
  6.     // set timezone
  7.     $t = date_default_timezone_set("Europe/London");
  8.    
  9.     // get start and end date/time from parameters
  10.     // passed on URL
  11.     $start = filter_input(INPUT_GET, "start");
  12.     $end = filter_input(INPUT_GET, "end");
  13.     // range - number of minutes to average temperature over
  14.     // a value of 1 means no averaging, unless temperatures are
  15.     // being collected more than once a minute
  16.     $range = filter_input(INPUT_GET, "range");
  17.     // sensor - which sensor to get data from
  18.     $sensor = filter_input(INPUT_GET, "sensor");
  19.  
  20.     // only proceed if all 3 parameters are present
  21.     if ($start && $end && $range)
  22.     {
  23.         $db = new SQLite3("observation.db");
  24.        
  25.         $result = $db->query("SELECT
  26.                                 datetime,
  27.                                 " . $sensor . "
  28.                                 FROM
  29.                                     temperature
  30.                                 WHERE
  31.                                     datetime > " . $start . "
  32.                                     AND
  33.                                     datetime <= " . $end . "
  34.                                 ORDER BY
  35.                                     datetime ASC");
  36.         $string = "[[";
  37.         $rowcount = 0;
  38.         // initialise count of rows for use in creating averages
  39.         $c = 0;
  40.         // initialise array to hold values to be averaged
  41.         $a = array();
  42.         $startTS = 0;
  43.         $rawrowcount = 0;
  44.         while ($row = $result->fetchArray()) {
  45.             $rawrowcount++;
  46.             $current = new DateTime($row['datetime']);
  47.             $currentTS = $current->getTimestamp();
  48.             // first time round loop - set start of datetime range
  49.             // we are using to compute averages
  50.             if ($c == 0) {
  51.                 $start = new DateTime($row['datetime']);
  52.                 $startTS = $start->getTimestamp();
  53.             }
  54.             // if current datetime is within range, add value
  55.             // of temperature to array for averaging
  56.             if ($currentTS - $startTS < $range * 60) {
  57.                 array_push($a, $row[$sensor]);
  58.                 $c++;
  59.             } else {
  60.                 // first deal with values already in array
  61.                 $avg = round(array_sum($a) / $c, 2);
  62.                 // compute average time as the start datetime
  63.                 // plus half the range
  64.                 if (count($a) == 1) {
  65.                     $avgdatetime = date('Y-m-d H:i:s', $startTS);
  66.                 } else {
  67.                     $avgdatetime = date('Y-m-d H:i:s', $startTS + $range * 30);
  68.                 }
  69.                 $s = json_encode([$avgdatetime, $avg]);
  70.                 $start = new DateTime($row['datetime']);
  71.                 $startTS = $start->getTimestamp();
  72.                 $a = array();
  73.                 $c = 0;
  74.                 $string .= $s . ",";
  75.                 $rowcount++;
  76.                 // next deal with the current row
  77.                 array_push($a, $row[$sensor]);
  78.                 $c = 1;
  79.             }
  80.         }
  81.         // handle case where there are some items in the array
  82.         // but no more rows left to return from the database
  83.         if (count($a) != 0) {
  84.             $rowcount++;
  85.             $avg = round(array_sum($a) / count($a), 2);
  86.             if (count($a) == 1) {
  87.                 $avgdatetime = date('Y-m-d H:i:s', $startTS);
  88.             } else {
  89.                 $avgdatetime = date('Y-m-d H:i:s', $startTS + $range * 30);
  90.             }
  91.             $s = json_encode([$avgdatetime, $avg]);
  92.             $string .= $s . ",";
  93.         }
  94.        
  95.         // strip last comma
  96.         $string = substr($string,0,-1);
  97.         $string .= "]]";
  98.         if ($rowcount == 0) {
  99.             // special case - if no rows then output null
  100.             // to keep jqPlot happy, otherwise it does not
  101.             // output a graph
  102.             $string = "[[null]]";
  103.         }
  104.         echo $string;
  105.     }
  106. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement