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"]; |
7 else |
7 else |
8 $code = "CB0068"; |
8 $code = "TB0015"; |
9 |
9 |
10 $query = "SELECT datetime,temperature_air,temperature_beer,temperature_chiller,temperature_room,target_low,target_high,heater_power,cooler_power FROM log_fermentation WHERE product_code='".$code."'"; |
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."'"; |
11 $connect = mysqli_connect(DBASE_HOST,DBASE_USER,DBASE_PASS,DBASE_NAME); |
11 $connect = mysqli_connect(DBASE_HOST,DBASE_USER,DBASE_PASS,DBASE_NAME); |
12 if (! $connect) { |
12 if (! $connect) { |
13 echo "[]"; |
13 echo "[]"; |
14 return; |
14 return; |
15 } |
15 } |
|
16 $step = 600; // Must be a multiple of 300 |
|
17 $ld = 0; |
|
18 $lh = 0; |
|
19 $ch = 0; |
|
20 $lc = 0; |
|
21 $cc = 0; |
16 $result = mysqli_query($connect, $query); |
22 $result = mysqli_query($connect, $query); |
17 while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { |
23 while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { |
18 $logs[] = array( |
24 |
19 'date' => $row['datetime'], |
25 $dt = new DateTime($row['datetime'], new DateTimeZone('Europe/Amsterdam')); |
20 'air' => $row['temperature_air'], |
26 $ts = $dt->getTimestamp(); |
21 'beer' => $row['temperature_beer'], |
27 $diff = $ts - $ld; |
22 'chiller' => $row['temperature_chiller'], |
28 |
23 'room' => $row['temperature_room'], |
29 if ($diff < $step) { |
24 'tlo' => $row['target_low'], |
30 // Read on |
25 'thi' => $row['target_high'], |
31 } else if ($diff > $step) { |
26 'heater' => $row['heater_power'], |
32 // Reset, out of sync. |
27 'cooler' => $row['cooler_power'] |
33 $ld = $ts; |
28 ); |
34 $lh = $ch = $row['heater_usage']; |
|
35 $lc = $cc = $row['cooler_usage']; |
|
36 } else { |
|
37 // This is a valid timeframe. |
|
38 $ch = $row['heater_usage']; |
|
39 $cc = $row['cooler_usage']; |
|
40 $heat = round(($ch - $lh) / ($step / 100)); |
|
41 $cool = round(($cc - $lc) / ($step / 100)); |
|
42 // echo $heat . ' ' . $cool . PHP_EOL; |
|
43 $logs[] = array( |
|
44 'date' => $row['datetime'], |
|
45 'air' => $row['temperature_air'], |
|
46 'beer' => $row['temperature_beer'], |
|
47 'chiller' => $row['temperature_chiller'], |
|
48 'room' => $row['temperature_room'], |
|
49 'tlo' => $row['target_low'], |
|
50 'thi' => $row['target_high'], |
|
51 'heater' => $heat, |
|
52 'cooler' => $cool |
|
53 ); |
|
54 $ld = $ts; |
|
55 $lh = $ch; |
|
56 $lc = $cc; |
|
57 } |
29 } |
58 } |
30 echo json_encode($logs); |
59 echo json_encode($logs); |