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 = "TB0015 Winterbier-2"; |
8 $code = "TB0015"; |
9 |
9 |
10 $filename = 'log/fermentation/' . $code . '.log'; |
10 $connect = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME); |
11 if (! file_exists($filename)) { |
11 if (! $connect) { |
12 header("Content-type: application/json"); |
12 die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); |
13 echo '{}'; |
13 } |
14 exit; |
14 mysqli_set_charset($connect, "utf8" ); |
|
15 |
|
16 $query = "SELECT * FROM log_fermenter WHERE code='".$code."' ORDER BY datetime;"; |
|
17 $result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect)); |
|
18 while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { |
|
19 |
|
20 $logs[] = array( |
|
21 'date' => substr($row['datetime'],0,16), |
|
22 'air' => $row['temp_air'], |
|
23 'beer' => $row['temp_beer'], |
|
24 'chiller' => $row['temp_chiller'], |
|
25 'room' => $row['temp_room'], |
|
26 'tlo' => $row['sp_low'], |
|
27 'thi' => $row['sp_high'], |
|
28 'heater' => $row['heater_power'], |
|
29 'cooler' => $row['cooler_power'], |
|
30 'event' => $row['event'] |
|
31 ); |
15 } |
32 } |
16 |
33 |
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 |
|
36 /* |
|
37 * 2014-11-15 18:39,BEER,PRIMARY,20.312,19.750,-1.500,20.5,18.6,18.8,35,12345,0,67890,Whatsup |
|
38 * | | | | | | | | | | | | | | |
|
39 * datetime + | | | | | | | | | | | | | |
|
40 * mode --------------+ | | | | | | | | | | | | |
|
41 * stage -------------------+ | | | | | | | | | | | |
|
42 * temp air -----------------------+ | | | | | | | | | | |
|
43 * temp beer -----------------------------+ | | | | | | | | | |
|
44 * temp chiller ---------------------------------+ | | | | | | | | |
|
45 * temp room -------------------------------------------+ | | | | | | | |
|
46 * setpoint low ---------------------------------------------+ | | | | | | |
|
47 * setpoint high -------------------------------------------------+ | | | | | |
|
48 * heater power ------------------------------------------------------+ | | | | |
|
49 * heater usage ----------------------------------------------------------+ | | | |
|
50 * cooler power --------------------------------------------------------------+ | | |
|
51 * cooler usage ------------------------------------------------------------------+ | |
|
52 * event --------------------------------------------------------------------------------+ |
|
53 */ |
|
54 |
|
55 $heater_l = 0; |
|
56 $cooler_l = 0; |
|
57 |
|
58 $handle = @fopen($filename, "r"); |
|
59 if ($handle) { |
|
60 $lines = 0; |
|
61 while (($buffer = fgets($handle, 4096)) !== false) { |
|
62 |
|
63 $buffer = preg_replace( "/\r|\n/", "", $buffer); |
|
64 $row = explode(",", $buffer); |
|
65 if ($row[1] != 'Mode') { |
|
66 |
|
67 $hr = intval(substr($buffer, 11, 2)); |
|
68 if ((($graphstep == 1)) || |
|
69 (($graphstep == 2) && ((substr($buffer, 15, 1) == '0') || (substr($buffer, 15, 1) == '5'))) || |
|
70 (($graphstep == 3) && ((substr($buffer, 14, 2) == '00') || (substr($buffer, 14, 2) == '15') || |
|
71 (substr($buffer, 14, 2) == '30') || (substr($buffer, 14, 2) == '45'))) || |
|
72 (($graphstep == 4) && ((substr($buffer, 14, 2) == '00') || (substr($buffer, 14, 2) == '30'))) || |
|
73 (($graphstep == 5) && (substr($buffer, 14, 2) == '00')) || |
|
74 (($graphstep == 6) && (substr($buffer, 14, 2) == '00') && (($hr % 2) == 0)) || |
|
75 (($graphstep == 7) && (substr($buffer, 14, 2) == '00') && (($hr % 4) == 0)) || |
|
76 (($graphstep == 8) && (substr($buffer, 14, 2) == '00') && (($hr % 8) == 0)) || |
|
77 (($graphstep == 9) && (substr($buffer, 14, 2) == '00') && (($hr % 12) == 0))) { |
|
78 // echo $hr . ' ' . $row[1] . ' ' . $buffer; |
|
79 $lines++; |
|
80 |
|
81 $heat_used = $cool_used = 0; |
|
82 if ($row[10] && ($row[10] != "NA") && ($heater_l > 0)) { |
|
83 $heat_used = round((intval($row[10]) - $heater_l) * 100 / ($GRAPH_INTERVAL[$graphstep] * 60)); |
|
84 } |
|
85 if ($row[12] && ($row[12] != "NA") && ($cooler_l > 0)) { |
|
86 $cool_used = round((intval($row[12]) - $cooler_l) * 100 / ($GRAPH_INTERVAL[$graphstep] * 60)); |
|
87 } |
|
88 |
|
89 |
|
90 // echo $GRAPH_INTERVAL[$graphstep] . ' ' . $heat_used . ' ' . $cool_used . PHP_EOL; |
|
91 |
|
92 if (($heat_used <= 100) && ($cool_used <= 100)) { |
|
93 $logs[] = array( |
|
94 'date' => $row[0], |
|
95 'air' => $row[3], |
|
96 'beer' => $row[4], |
|
97 'chiller' => $row[5], |
|
98 'room' => $row[6], |
|
99 'tlo' => $row[7], |
|
100 'thi' => $row[8], |
|
101 'heater' => $heat_used, |
|
102 'cooler' => $cool_used, |
|
103 'event' => $row[13] |
|
104 ); |
|
105 } |
|
106 |
|
107 if ($row[10] && ($row[10] != "NA")) |
|
108 $heater_l = intval($row[10]); |
|
109 if ($row[12] && ($row[12] != "NA")) |
|
110 $cooler_l = intval($row[12]); |
|
111 } |
|
112 } |
|
113 |
|
114 } |
|
115 if (!feof($handle)) { |
|
116 echo "Error: unexpected fgets() fail\n"; |
|
117 } |
|
118 fclose($handle); |
|
119 } |
|
120 header("Content-type: application/json"); |
34 header("Content-type: application/json"); |
121 echo json_encode($logs); |
35 echo json_encode($logs); |