-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
256 lines (217 loc) · 12.7 KB
/
index.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
include("util.php");
$requestType = $_SERVER['REQUEST_METHOD'];
switch ($requestType) {
case 'POST':
handlePostRequest();
break;
case 'GET':
handleGetRequest();
break;
default:
//request type that isn't being handled.
break;
}
function handlePostRequest(){
$json_string = file_get_contents('php://input');
$data = json_decode($json_string, true);
if ($data) {
$servername = "dramco.be.mysql";
$username = "dramco_be_dramco_uno";
$db_name = "dramco_be_dramco_uno";
$password = "dQVvudX98NjMRsFS";
$conn = mysqli_connect($servername, $username, $password, $db_name);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$app_id = "'".$data["app_id"]."'";
$dev_id = "'".$data["dev_id"]."'";
$accelerometer_x = "NULL";
if(isset($data["payload_fields"]["accelerometer_0"]["x"])){
$accelerometer_x = mysqli_real_escape_string($conn, $data["payload_fields"]["accelerometer_0"]["x"]);
}
$accelerometer_y = "NULL";
if(isset($data["payload_fields"]["accelerometer_0"]["y"])){
$accelerometer_y = "'".mysqli_real_escape_string($conn, $data["payload_fields"]["accelerometer_0"]["y"])."'";
}
$accelerometer_z = "NULL";
if(isset($data["payload_fields"]["accelerometer_0"]["z"])){
$accelerometer_z = "'".mysqli_real_escape_string($conn, $data["payload_fields"]["accelerometer_0"]["z"])."'";
}
$luminosity = "NULL";
if(isset($data["payload_fields"]["luminosity_0"])){
$luminosity = "'".mysqli_real_escape_string($conn, $data["payload_fields"]["luminosity_0"])."'";
}
$temperature = "NULL";
if(isset($data["payload_fields"]["temperature_0"])){
$temperature = "'".mysqli_real_escape_string($conn, $data["payload_fields"]["temperature_0"])."'";
}
$soil_moisture = "NULL";
if(isset($data["payload_fields"]["analog_0"])){
$soil_moisture = "'".mysqli_real_escape_string($conn, $data["payload_fields"]["analog_0"])."'";
}
$meta = json_encode($data["metadata"]);
$search = array("timestamp", "time", "frequency", "modulation", "data_rate", "coding_rate", "gateways", "channel", "rf_chain");
$replace = array("ts", "t", "f", "mod", "dr", "cr", "gtw", "ch", "rf");
$meta = str_replace($search, $replace, $meta);
//$meta = gzencode($meta, 9);
$meta = "'".mysqli_real_escape_string($conn, $meta)."'"; // TODO: If more than 1 gateway: check for length
$sql = "INSERT INTO messages (`timestamp`, `app_id`, `dev_id`, `accelerometer_x`, `accelerometer_y`, `accelerometer_z`, `luminosity`, `temperature`, `soil_moisture`, `meta`) VALUES (CURRENT_TIMESTAMP(), $app_id, $dev_id, $accelerometer_x, $accelerometer_y, $accelerometer_z, $luminosity, $temperature, $soil_moisture, $meta)";
echo $sql."\n";
if (!mysqli_query($conn, $sql)) {
echo ("Error description: " . mysqli_error($conn)."\n");
}
mysqli_close($conn);
}
}
function handleGetRequest(){
global $sensorNames, $sensorUnits, $keyNames;
if(!(isset($_GET["dev_id"]) && isset($_GET["app_id"]))){
echo("Please provide both Application ID and Device ID.");
}else{
$servername = "dramco.be.mysql";
$username = "dramco_be_dramco_uno";
$db_name = "dramco_be_dramco_uno";
$password = "dQVvudX98NjMRsFS";
$mysqli = new mysqli($servername, $username, $password, $db_name);
// Check connection
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$app_id = $mysqli->real_escape_string($_GET["app_id"]);
$dev_id = $mysqli->real_escape_string($_GET["dev_id"]);
if(isset($_GET["graph"]) && isset($_GET["key"]))
$limit = 30;
else
$limit = 1;
if(isset($_GET["limit"])){
$limit = intval($mysqli->real_escape_string($_GET["limit"]));
}
$sql = "SELECT `id`, `timestamp`, `app_id`, `dev_id`, `accelerometer_x`, `accelerometer_y`, `accelerometer_z`, `luminosity`, `temperature`, `soil_moisture` FROM `messages` WHERE `app_id`='$app_id' AND `dev_id`='$dev_id' ORDER BY `id` DESC LIMIT $limit";
if (!($result = $mysqli->query($sql))) {
echo ("Error description: " . $mysqli->error($conn)."\n");
}
$messages = array();
$i = 0;
while($row = $result->fetch_assoc()){
unset($row["id"]);
foreach ($row as $key => $value) {
if($value == NULL){ // Find NULL values
unset($row[$key]);
}
}
foreach ($row as $key => $value) { // Re-iterate for float conversion
if($key == "accelerometer_x" || $key == "accelerometer_y" || $key == "accelerometer_z" || $key == "luminosity" || $key == "temperature" || $key == "soil_moisture"){
$row[$key] = (float)$value;
}
if($key == "timestamp"){
$row[$key] = strtotime($value)*1000;
$row["elapsed"] = timeElapsedString($value);
}
}
array_push($messages, $row);
$i++;
}
date_default_timezone_set("Europe/Brussels");
if(!isset($_GET["graph"])){
if(isset($_GET["format"]) && $_GET["format"] == "json"){
if($i == 1){
echo(json_encode($messages[0]));
}
else{
echo(json_encode($messages));
}
}else if (!isset($_GET["format"]) || $_GET["format"] == "html"){
if(isset($_GET["style"]) && $_GET["style"] == "plain"){
foreach($messages as $message) {
$time = date("d-m-Y G:i", $message["timestamp"]/1000);
echo("<div class=\"message\">");
echo("<h5 class=\"ids\"><span class=\"app_id\">$app_id</span> / <span class=\"dev_id\">$dev_id</span></h5><p class=\"time\">$time</p>");
echo("<table class=\"sensors\">");
foreach ($message as $key => $value) {
if(isset($_GET["key"]) && $_GET["key"] != "" && $keyNames[$_GET["key"]] != "" ){
$keyRequested = $keyNames[$_GET["key"]];
if($key == $keyRequested || ($keyRequested == "accelerometer" && $key == "accelerometer_x") || ($keyRequested == "accelerometer" && $key == "accelerometer_y") || ($keyRequested == "accelerometer" && $key == "accelerometer_z")){
$name = $sensorNames[$key];
$unit = $sensorUnits[$key];
echo("<tr class=\"sensor\"><td class=\"sensor_name\">$name</td> <td class=\"sensor_value\">$value <span class=\"sensor_unit\">$unit</span></td></tr>");
}
}else if($key == "accelerometer_x" || $key == "accelerometer_y" || $key == "accelerometer_z" || $key == "luminosity" || $key == "temperature" || $key == "soil_moisture"){
$name = $sensorNames[$key];
$unit = $sensorUnits[$key];
echo("<tr class=\"sensor\"><td class=\"sensor_name\">$name</td> <td class=\"sensor_value\">$value <span class=\"sensor_unit\">$unit</span></td></tr>");
}
}
echo("</table></div>");
}
}else if(!isset($_GET["style"]) || isset($_GET["style"]) && $_GET["style"] == "light" || isset($_GET["style"]) && $_GET["style"] == "dark"){
$cardStyleClasses = "bg-light";
$tableStyleClasses = "";
$bodyStyleClasses = "";
if($_GET["style"] == "dark"){
$cardStyleClasses = "bg-dark text-white";
$tableStyleClasses = "table-dark";
$bodyStyle = "background: #212529;";
}
$refresh = "";
if(isset($_GET["refresh"]) && intval($_GET["refresh"]) > 0){
$r = intval($_GET["refresh"]);
$refresh = "<meta http-equiv=\"refresh\" content=\"$r\">";
}
echo("<html><header><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">$refresh<link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css\" integrity=\"sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2\" crossorigin=\"anonymous\"></header><body style=\"$bodyStyle\">");
foreach($messages as $message) {
$time = date("d-m-Y G:i", $message["timestamp"]/1000);
echo("<div class=\"message card $cardStyleClasses\" style=\"width: 18rem; margin: 10px; float: left;\">");
echo("<div class=\"card-body\"><h5 class=\"ids\"><span class=\"app_id\">$app_id</span> / <span class=\"dev_id\">$dev_id</span></h5><p class=\"h5\"><small class=\"text-muted\">Updated: <span class=\"time\">$time</span></small></p></div>");
echo("<table class=\"sensors table $tableStyleClasses\">");
foreach ($message as $key => $value) {
if(isset($_GET["key"]) && $_GET["key"] != "" && $keyNames[$_GET["key"]] != "" ){
$keyRequested = $keyNames[$_GET["key"]];
if($key == $keyRequested || ($keyRequested == "accelerometer" && $key == "accelerometer_x") || ($keyRequested == "accelerometer" && $key == "accelerometer_y") || ($keyRequested == "accelerometer" && $key == "accelerometer_z")){
$name = $sensorNames[$key];
$unit = $sensorUnits[$key];
echo("<tr class=\"sensor\"><td class=\"sensor_name\">$name</td> <td class=\"sensor_value\">$value <span class=\"sensor_unit\">$unit</span></td></tr>");
}
}else if($key == "accelerometer_x" || $key == "accelerometer_y" || $key == "accelerometer_z" || $key == "luminosity" || $key == "temperature" || $key == "soil_moisture"){
$name = $sensorNames[$key];
$unit = $sensorUnits[$key];
echo("<tr class=\"sensor\"><td class=\"sensor_name\">$name</td> <td class=\"sensor_value\">$value <span class=\"sensor_unit\">$unit</span></td></tr>");
}
}
echo("</table></div></body></html>");
}
}
}
}else if(isset($_GET["key"])){
$requestedKey = $keyNames[$_GET["key"]];
$graphData = array();
$min = PHP_FLOAT_MAX ;
$max = PHP_FLOAT_MIN ;
foreach($messages as $message) {
$time = date("d-m-Y G:i", $message["timestamp"]/1000);
if($requestedKey == "accelerometer"){
$value = array_push($graphData, array($time, $message["accelerometer_x"], $message["accelerometer_y"], $message["accelerometer_z"]));
$min = min($min, $message["accelerometer_x"], $message["accelerometer_y"], $message["accelerometer_z"]);
$max = max($max, $message["accelerometer_x"], $message["accelerometer_y"], $message["accelerometer_z"]);
$format = [1, 2, 3];
$legend = [$sensorNames["accelerometer_x"], $sensorNames["accelerometer_y"], $sensorNames["accelerometer_z"]];
}else{
array_push($graphData, array($time, $message[$requestedKey], NULL));
$max = max($max, $message[$requestedKey]+1);
$min = min($min, $message[$requestedKey]-1);
$format = 1;
$legend = NULL;
}
}
if(isset($_GET["max"])){
$max = $_GET["max"];
}
if(isset($_GET["min"])){
$min = $_GET["min"];
}
include("graph.php");
}
}
}
?>