www/getco2pressurelog.php

changeset 838
ce5f39b66a51
parent 600
c136dd22f662
child 852
71f0fa38b634
equal deleted inserted replaced
837:b5e2ff72cc73 838:ce5f39b66a51
1 <?php 1 <?php
2 2
3 require_once('config.php'); 3 require_once('config.php');
4 4
5 if (isset($_GET["code"])) 5 if (isset($_GET["code"]))
6 $code = $_GET["code"] . ' ' . $_GET["name"]; 6 $code = $_GET["code"];
7 else 7 else
8 $code = "CB0080 Op stoom"; 8 $code = "CB0080";
9
10 $filename = 'log/co2pressure/' . $code . '.log';
11 if (! file_exists($filename)) {
12 header("Content-type: application/json");
13 echo '{}';
14 exit;
15 }
16
17
18 /*
19 * From Stackoverflow, the fastest way to count the lines in a file.
20 */
21 $file = new \SplFileObject($filename, 'r');
22 $file->seek(PHP_INT_MAX);
23
24 define ('MAX_INTERVALS', 10);
25 $GRAPH_INTERVAL = array ( 0, 1, 5, 15, 30, 60, 120, 240, 480, 720 );
26 $GRAPH_DATALINES = array ( 0, 800, 3200, 12000, 24000, 48000, 96000, 192000, 384000, 768000 );
27
28
29 for ($graphstep = 1; $graphstep <= MAX_INTERVALS; $graphstep++) {
30 if ($file->key() < $GRAPH_DATALINES[$graphstep])
31 break;
32 }
33 if ($graphstep > MAX_INTERVALS)
34 $graphstep = MAX_INTERVALS;
35 9
36 10
37 /* 11 /*
38 * 2014-11-15 18:39:12,TEMPERATURE,PRESSURE,UUID 12 * 2014-11-15 18:39:12,TEMPERATURE,PRESSURE,UUID
39 * | | | | 13 * | | | |
41 * temperature ------------+ | | 15 * temperature ------------+ | |
42 * pressure --------------------------+ | 16 * pressure --------------------------+ |
43 * unit uuid --------------------------------+ 17 * unit uuid --------------------------------+
44 */ 18 */
45 19
20 $connect = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME);
21 if (! $connect) {
22 die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
23 }
24 mysqli_set_charset($connect, "utf8" );
46 25
47 $handle = @fopen($filename, "r"); 26 $query = "SELECT * FROM log_co2pressure WHERE code='".$code."' ORDER BY datetime;";
48 if ($handle) { 27 $result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect));
49 $lines = 0; 28 while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
50 while (($buffer = fgets($handle, 4096)) !== false) {
51 29
52 $buffer = preg_replace( "/\r|\n/", "", $buffer); 30 $logs[] = array(
53 $row = explode(",", $buffer); 31 'date' => substr($row['datetime'],0,16),
54 32 'temperature' => $row['temperature'],
55 $hr = intval(substr($buffer, 11, 2)); 33 'pressure' => $row['pressure']
56 if ((($graphstep == 1)) || 34 );
57 (($graphstep == 2) && ((substr($buffer, 15, 1) == '0') || (substr($buffer, 15, 1) == '5'))) ||
58 (($graphstep == 3) && ((substr($buffer, 14, 2) == '00') || (substr($buffer, 14, 2) == '15') ||
59 (substr($buffer, 14, 2) == '30') || (substr($buffer, 14, 2) == '45'))) ||
60 (($graphstep == 4) && ((substr($buffer, 14, 2) == '00') || (substr($buffer, 14, 2) == '30'))) ||
61 (($graphstep == 5) && (substr($buffer, 14, 2) == '00')) ||
62 (($graphstep == 6) && (substr($buffer, 14, 2) == '00') && (($hr % 2) == 0)) ||
63 (($graphstep == 7) && (substr($buffer, 14, 2) == '00') && (($hr % 4) == 0)) ||
64 (($graphstep == 8) && (substr($buffer, 14, 2) == '00') && (($hr % 8) == 0)) ||
65 (($graphstep == 9) && (substr($buffer, 14, 2) == '00') && (($hr % 12) == 0))) {
66 $lines++;
67 $logs[] = array(
68 'date' => $row[0],
69 'temperature' => $row[1],
70 'pressure' => $row[2]
71 );
72 }
73 }
74 if (!feof($handle)) {
75 echo "Error: unexpected fgets() fail\n";
76 }
77 fclose($handle);
78 } 35 }
79 header("Content-type: application/json"); 36 header("Content-type: application/json");
80 echo json_encode($logs); 37 echo json_encode($logs);

mercurial