-
Notifications
You must be signed in to change notification settings - Fork 0
/
displayLast24hReadings.php
201 lines (171 loc) · 7.49 KB
/
displayLast24hReadings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
// Connect to MySQL
include("assets/connect.php");
include("assets/session.php");
include("assets/getThresholds.php");
date_default_timezone_set('Europe/London');
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
$currentDate = $_POST["currentDate"];
$newDate = new DateTime($currentDate);
if($_POST["action"] == "PREVIOUS") {
$newDate->modify('-1 day');
}
else if($_POST["action"] == "NEXT") {
$newDate->modify('+1 day');
}
//Query the database
$sql="SELECT * FROM readings WHERE Date LIKE '".$newDate->format('Y-m-d')."'";
// Retrieve all records
$result = mysqli_query($con, $sql);
if(!empty($result))
{
//echo "\nERROR on QUERY for 24HReadings";
}
$row = mysqli_fetch_array($result);
/**
* Store records in a new array.
* Basically just push all of the values in an array.
**/
$readings = array();
array_push($readings, $row);
while($row = mysqli_fetch_array($result))
{
array_push($readings, $row);
}
$currentDate = $newDate->format('Y-m-d');
}
else
{
// Get current date
$date = date("Y-m-d", time());
$time = date("H:i", time());
$found = false;
$row;
$newDate = new DateTime($date);
while(!$found)
{
//Query the database
$sql="SELECT * FROM readings WHERE Date LIKE '".$newDate->format('Y-m-d')."'";
// Retrieve all records
$result = mysqli_query($con, $sql);
if(empty($result))
{
echo "\nERROR on QUERY for 24HReadings";
}
$row = mysqli_fetch_array($result);
if(!empty($row))
$found = true;
else
$newDate->modify('-1 day');
}
/**
* Store records in a new array.
* Basically just push all of the values in an array.
**/
$readings = array();
array_push($readings, $row);
while($row = mysqli_fetch_array($result))
{
array_push($readings, $row);
}
$currentDate = $newDate->format('Y-m-d');
}
mysqli_close($con);
?>
<!DOCTYPE html>
<html>
<head>
<title>IoT Readings Table</title>
</head>
<body>
<div id="bg">
<img src="images/back (2).jpg" alt="">
</div>
<!-- Theme CSS -->
<link id="theme-style" rel="stylesheet" href="css/style.css">
<h2 style="position: absolute; left: 20px;top: 0px;">Readings <?php echo $currentDate; ?></h2>
<!--BUTTONS-->
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input type="submit" name="action" value="PREVIOUS" style="position: absolute; left: 300px;top: 25px;width: 100px;" />
<input type="hidden" value="<?php echo $currentDate ?>" name="currentDate"/>
</form>
<h2 style="position: absolute; left: 420px;top: 0px;"> - </h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input type="submit" name="action" value="NEXT" style="position: absolute; left: 450px;top: 25px;width: 100px;" />
<input type="hidden" value="<?php echo $currentDate ?>" name="currentDate"/>
</form>
<h2 style="position: absolute; left: 600px;top: 0px;">GO TO </h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input type="submit" name="action" value="GO" style="position: absolute; left: 800px;top: 25px;width: 50px;" />
<input type="text" value="<?php echo $currentDate ?>" name="currentDate" style="position: absolute; left: 680px;top: 25px;width: 100px;"/>
</form>
<h2 style="position: absolute; left: 900px;top: 0px;">TODAY</h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input type="submit" name="action" value="TODAY" style="position: absolute; left: 980px;top: 25px;width: 70px;" />
</form>
<table border="0" cellspacing="0" cellpadding="4" style="position: absolute; left: 20px;top: 60px;">
<tr>
<td class="table_titles">ID</td>
<td class="table_titles">Node</td>
<td class="table_titles">Soil Temperature</td>
<td class="table_titles">Soil Moisture</td>
<td class="table_titles">Air Temperature</td>
<td class="table_titles">Air Humidity</td>
<td class="table_titles">Dew Point</td>
<td class="table_titles">Battery Level</td>
<td class="table_titles">Date</td>
<td class="table_titles">Time</td>
</tr>
<?php
// Used for row color toggle
$oddrow = true;
// process every record
$number = count($readings);
//echo "<h2 style='position: absolute; left: 500px;top: 0px;'>".$number."</h2>";
for($i = 0; $i < $number; $i++)
{
if ($oddrow)
{
$css_class=' class="table_cells_odd"';
}
else
{
$css_class=' class="table_cells_even"';
}
$oddrow = !$oddrow;
echo '<tr>';
echo ' <td'.$css_class.'>'.$readings[$i]["ID"].'</td>';
echo ' <td'.$css_class.'>'.$readings[$i]["Node"].'</td>';
if($readings[$i]["Soil Temperature"] > $thresholds_up[0] || $readings[$i]["Soil Temperature"] < $thresholds_down[0])
echo ' <td'.$css_class.'>'."<font color=".'"'."red".'"'.">".$readings[$i]["Soil Temperature"].'</font></td>';
else
echo ' <td'.$css_class.'>'.$readings[$i]["Soil Temperature"].'</td>';
if($readings[$i]["Soil Moisture"] > $thresholds_up[1] || $readings[$i]["Soil Moisture"] < $thresholds_down[1])
echo ' <td'.$css_class.'>'."<font color=".'"'."red".'"'.">".$readings[$i]["Soil Moisture"].'</font></td>';
else
echo ' <td'.$css_class.'>'.$readings[$i]["Soil Moisture"].'</td>';
if($readings[$i]["Air Temperature"] > $thresholds_up[2] || $readings[$i]["Air Temperature"] < $thresholds_down[2])
echo ' <td'.$css_class.'>'."<font color=".'"'."red".'"'.">".$readings[$i]["Air Temperature"].'</font></td>';
else
echo ' <td'.$css_class.'>'.$readings[$i]["Air Temperature"].'</td>';
if($readings[$i]["Air Humidity"] > $thresholds_up[3] || $readings[$i]["Air Humidity"] < $thresholds_down[3])
echo ' <td'.$css_class.'>'."<font color=".'"'."red".'"'.">".$readings[$i]["Air Humidity"].'</font></td>';
else
echo ' <td'.$css_class.'>'.$readings[$i]["Air Humidity"].'</td>';
if($readings[$i]["Dew Point"] > $thresholds_up[4] || $readings[$i]["Dew Point"] < $thresholds_down[4])
echo ' <td'.$css_class.'>'."<font color=".'"'."red".'"'.">".$readings[$i]["Dew Point"].'</font></td>';
else
echo ' <td'.$css_class.'>'.$readings[$i]["Dew Point"].'</td>';
if($readings[$i]["Battery"] > $thresholds_up[5] || $readings[$i]["Battery"] < $thresholds_down[5])
echo ' <td'.$css_class.'>'."<font color=".'"'."red".'"'.">".$readings[$i]["Battery"].'</font></td>';
else
echo ' <td'.$css_class.'>'.$readings[$i]["Battery"].'</td>';
echo ' <td'.$css_class.'>'.$readings[$i]["Date"].'</td>';
echo ' <td'.$css_class.'>'.$readings[$i]["Time"].'</td>';
echo '</tr>';
}
?>
</table>
</body>
</html>