www/getco2pressurelog.php

changeset 507
17f244137a9b
child 600
c136dd22f662
equal deleted inserted replaced
506:8ab0e87d579e 507:17f244137a9b
1 <?php
2
3 require_once('config.php');
4
5 if (isset($_GET["code"]))
6 $code = $_GET["code"] . ' ' . $_GET["name"];
7 else
8 $code = "TB0015 Winterbier-2";
9
10 $filename = 'log/co2pressure/' . $code . '.log';
11 /*
12 * From Stackoverflow, the fastest way to count the lines in a file.
13 */
14 $file = new \SplFileObject($filename, 'r');
15 $file->seek(PHP_INT_MAX);
16 // $file->key() = lines in the file.
17
18 define ('MAX_INTERVALS', 10);
19 $GRAPH_INTERVAL = array ( 0, 1, 5, 15, 30, 60, 120, 240, 480, 720 );
20 $GRAPH_DATALINES = array ( 0, 800, 3200, 12000, 24000, 48000, 96000, 192000, 384000, 768000 );
21
22
23 for ($graphstep = 1; $graphstep <= MAX_INTERVALS; $graphstep++) {
24 if ($file->key() < $GRAPH_DATALINES[$graphstep])
25 break;
26 }
27 if ($graphstep > MAX_INTERVALS)
28 $graphstep = MAX_INTERVALS;
29
30 //echo $code . '.log lines=' . $file->key() . ' interval=' . $GRAPH_INTERVAL[$graphstep] . ' graphstep=' . $graphstep . PHP_EOL;
31
32 /*
33 * 2014-11-15 18:39:12,TEMPERATURE,PRESSURE,UUID
34 * | | | |
35 * datetime + | | |
36 * temperature ------------+ | |
37 * pressure --------------------------+ |
38 * unit uuid --------------------------------+
39 */
40
41
42 $handle = @fopen($filename, "r");
43 if ($handle) {
44 $lines = 0;
45 while (($buffer = fgets($handle, 4096)) !== false) {
46
47 $buffer = preg_replace( "/\r|\n/", "", $buffer);
48 $row = explode(",", $buffer);
49
50 $hr = intval(substr($buffer, 11, 2));
51 if ((($graphstep == 1)) ||
52 (($graphstep == 2) && ((substr($buffer, 15, 1) == '0') || (substr($buffer, 15, 1) == '5'))) ||
53 (($graphstep == 3) && ((substr($buffer, 14, 2) == '00') || (substr($buffer, 14, 2) == '15') ||
54 (substr($buffer, 14, 2) == '30') || (substr($buffer, 14, 2) == '45'))) ||
55 (($graphstep == 4) && ((substr($buffer, 14, 2) == '00') || (substr($buffer, 14, 2) == '30'))) ||
56 (($graphstep == 5) && (substr($buffer, 14, 2) == '00')) ||
57 (($graphstep == 6) && (substr($buffer, 14, 2) == '00') && (($hr % 2) == 0)) ||
58 (($graphstep == 7) && (substr($buffer, 14, 2) == '00') && (($hr % 4) == 0)) ||
59 (($graphstep == 8) && (substr($buffer, 14, 2) == '00') && (($hr % 8) == 0)) ||
60 (($graphstep == 9) && (substr($buffer, 14, 2) == '00') && (($hr % 12) == 0))) {
61 // echo $hr . ' ' . $row[1] . ' ' . $buffer;
62 $lines++;
63
64 // $heat_used = $cool_used = 0;
65 // if ($row[10] && ($row[10] != "NA") && ($heater_l > 0)) {
66 // $heat_used = round((intval($row[10]) - $heater_l) * 100 / ($GRAPH_INTERVAL[$graphstep] * 60));
67 // }
68 // if ($row[12] && ($row[12] != "NA") && ($cooler_l > 0)) {
69 // $cool_used = round((intval($row[12]) - $cooler_l) * 100 / ($GRAPH_INTERVAL[$graphstep] * 60));
70 // }
71
72
73 // echo $GRAPH_INTERVAL[$graphstep] . ' ' . $heat_used . ' ' . $cool_used . PHP_EOL;
74
75 // if (($heat_used <= 100) && ($cool_used <= 100)) {
76 $logs[] = array(
77 'date' => $row[0],
78 'temperature' => $row[1],
79 'pressure' => $row[2]
80 );
81 // }
82
83 // if ($row[10] && ($row[10] != "NA"))
84 // $heater_l = intval($row[10]);
85 // if ($row[12] && ($row[12] != "NA"))
86 // $cooler_l = intval($row[12]);
87 }
88
89 }
90 if (!feof($handle)) {
91 echo "Error: unexpected fgets() fail\n";
92 }
93 fclose($handle);
94 }
95 header("Content-type: application/json");
96 echo json_encode($logs);
97 //echo $lines . ' lines' . PHP_EOL;

mercurial