www/getfermentlog.php

changeset 194
d202777ebae5
parent 179
8adaeecd9dc8
child 296
69fadd1aded2
equal deleted inserted replaced
193:3cbd81ea9391 194:d202777ebae5
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"]; 6 $code = $_GET["code"] . ' ' . $_GET["name"];
7 else 7 else
8 $code = "TB0015"; 8 $code = "TB0015 Winterbier-2";
9 9
10 $query = "SELECT datetime,temperature_air,temperature_beer,temperature_chiller,temperature_room,target_low,target_high,heater_usage,cooler_usage FROM log_fermentation WHERE product_code='".$code."'"; 10 $filename = 'log/fermentation/' . $code . '.log';
11 $connect = mysqli_connect(DBASE_HOST,DBASE_USER,DBASE_PASS,DBASE_NAME); 11 /*
12 if (! $connect) { 12 * From Stackoverflow, the fastest way to count the lines in a file.
13 echo "[]"; 13 */
14 return; 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;
15 } 26 }
16 $step = 600; // Must be a multiple of 300 27 if ($graphstep > MAX_INTERVALS)
17 $ld = 0; 28 $graphstep = MAX_INTERVALS;
18 $lh = 0;
19 $ch = 0;
20 $lc = 0;
21 $cc = 0;
22 $result = mysqli_query($connect, $query);
23 while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
24 29
25 $dt = new DateTime($row['datetime'], new DateTimeZone('Europe/Amsterdam')); 30 //echo $code . '.log lines=' . $file->key() . ' interval=' . $GRAPH_INTERVAL[$graphstep] . ' graphstep=' . $graphstep . PHP_EOL;
26 $ts = $dt->getTimestamp();
27 $diff = $ts - $ld;
28 31
29 if ($diff < $step) { 32 /*
30 // Read on 33 * 2014-11-15 18:39,BEER,PRIMARY,20.312,19.750,-1.500,20.5,18.6,18.8,35,12345,0,67890,Whatsup
31 } else if ($diff > $step) { 34 * | | | | | | | | | | | | | |
32 // Reset, out of sync. 35 * datetime + | | | | | | | | | | | | |
33 $ld = $ts; 36 * mode --------------+ | | | | | | | | | | | |
34 $lh = $ch = $row['heater_usage']; 37 * stage -------------------+ | | | | | | | | | | |
35 $lc = $cc = $row['cooler_usage']; 38 * temp air -----------------------+ | | | | | | | | | |
36 } else { 39 * temp beer -----------------------------+ | | | | | | | | |
37 // This is a valid timeframe. 40 * temp chiller ---------------------------------+ | | | | | | | |
38 $ch = $row['heater_usage']; 41 * temp room -------------------------------------------+ | | | | | | |
39 $cc = $row['cooler_usage']; 42 * setpoint low ---------------------------------------------+ | | | | | |
40 $heat = round(($ch - $lh) / ($step / 100)); 43 * setpoint high -------------------------------------------------+ | | | | |
41 $cool = round(($cc - $lc) / ($step / 100)); 44 * heater power ------------------------------------------------------+ | | | |
42 // echo $heat . ' ' . $cool . PHP_EOL; 45 * heater usage ----------------------------------------------------------+ | | |
43 $logs[] = array( 46 * cooler power --------------------------------------------------------------+ | |
44 'date' => $row['datetime'], 47 * cooler usage ------------------------------------------------------------------+ |
45 'air' => $row['temperature_air'], 48 * event --------------------------------------------------------------------------------+
46 'beer' => $row['temperature_beer'], 49 */
47 'chiller' => $row['temperature_chiller'], 50
48 'room' => $row['temperature_room'], 51 $heater_l = 0;
49 'tlo' => $row['target_low'], 52 $cooler_l = 0;
50 'thi' => $row['target_high'], 53
51 'heater' => $heat, 54 $handle = @fopen($filename, "r");
52 'cooler' => $cool 55 if ($handle) {
53 ); 56 $lines = 0;
54 $ld = $ts; 57 while (($buffer = fgets($handle, 4096)) !== false) {
55 $lh = $ch; 58
56 $lc = $cc; 59 $buffer = preg_replace( "/\r|\n/", "", $buffer);
60 $row = explode(",", $buffer);
61 if ($row[1] != 'Mode') {
62
63 $hr = intval(substr($buffer, 11, 2));
64 if ((($graphstep == 1)) ||
65 (($graphstep == 2) && ((substr($buffer, 15, 1) == '0') || (substr($buffer, 15, 1) == '5'))) ||
66 (($graphstep == 3) && ((substr($buffer, 14, 2) == '00') || (substr($buffer, 14, 2) == '15') ||
67 (substr($buffer, 14, 2) == '30') || (substr($buffer, 14, 2) == '45'))) ||
68 (($graphstep == 4) && ((substr($buffer, 14, 2) == '00') || (substr($buffer, 14, 2) == '30'))) ||
69 (($graphstep == 5) && (substr($buffer, 14, 2) == '00')) ||
70 (($graphstep == 6) && (substr($buffer, 14, 2) == '00') && (($hr % 2) == 0)) ||
71 (($graphstep == 7) && (substr($buffer, 14, 2) == '00') && (($hr % 4) == 0)) ||
72 (($graphstep == 8) && (substr($buffer, 14, 2) == '00') && (($hr % 8) == 0)) ||
73 (($graphstep == 9) && (substr($buffer, 14, 2) == '00') && (($hr % 12) == 0))) {
74 // echo $hr . ' ' . $row[1] . ' ' . $buffer;
75 $lines++;
76
77 $heat_used = $cool_used = 0;
78 if ($row[10] && ($row[10] != "NA") && ($heater_l > 0)) {
79 $heat_used = round((intval($row[10]) - $heater_l) * 100 / ($GRAPH_INTERVAL[$graphstep] * 60));
80 }
81 if ($row[12] && ($row[12] != "NA") && ($cooler_l > 0)) {
82 $cool_used = round((intval($row[12]) - $cooler_l) * 100 / ($GRAPH_INTERVAL[$graphstep] * 60));
83 }
84
85
86 // echo $GRAPH_INTERVAL[$graphstep] . ' ' . $heat_used . ' ' . $cool_used . PHP_EOL;
87
88 if (($heat_used <= 100) && ($cool_used <= 100)) {
89 $logs[] = array(
90 'date' => $row[0],
91 'air' => $row[3],
92 'beer' => $row[4],
93 'chiller' => $row[5],
94 'room' => $row[6],
95 'tlo' => $row[7],
96 'thi' => $row[8],
97 'heater' => $heat_used,
98 'cooler' => $cool_used,
99 'event' => $row[13]
100 );
101 }
102
103 if ($row[10] && ($row[10] != "NA"))
104 $heater_l = intval($row[10]);
105 if ($row[12] && ($row[12] != "NA"))
106 $cooler_l = intval($row[12]);
107 }
108 }
109
57 } 110 }
111 if (!feof($handle)) {
112 echo "Error: unexpected fgets() fail\n";
113 }
114 fclose($handle);
58 } 115 }
116
59 echo json_encode($logs); 117 echo json_encode($logs);
118 //echo $lines . ' lines' . PHP_EOL;

mercurial