-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualSensorsHub.php
364 lines (313 loc) · 11.7 KB
/
virtualSensorsHub.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
// Connect to MySQL
include("assets/connect.php");
$currentNode = "1";
if($_SERVER["REQUEST_METHOD"] == "POST"){
$currentNode = $_POST["currentNode"];
}
// Get current date
date_default_timezone_set('Europe/London');
$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 `Node` = ".$currentNode." AND 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');
}
$readings = array();
array_push($readings, $row);
while($row = mysqli_fetch_array($result))
{
array_push($readings, $row);
}
//RETRIEVE ALL DIFFERENT VALUES OF TIME BASED ON THAT DATE
$sql = "SELECT DISTINCT `Time`,`Date` FROM `readings` WHERE `Node` = ".$currentNode." AND `Date` LIKE '".$newDate->format('Y-m-d')."'";
// Retrieve all records
$result = mysqli_query($con, $sql);
if(empty($result))
{
echo "\nERROR on 2nd QUERY for 24HReadings";
}
$times = array();
while($row = mysqli_fetch_array($result))
{
array_push($times, $row["Time"]);
}
//RETRIEVE ALL FIELDS
$number = count($times);
$soil_temp = array();
for($i = 0; $i < $number; $i++)
{
$sql = "SELECT `Soil Temperature` FROM `readings` WHERE `Date` LIKE '".$newDate->format('Y-m-d')."'"." AND `Time`LIKE '".$times[$i]."'"." AND `Node` = ".$currentNode;
$result = mysqli_query($con, $sql);
if(empty($result))
{
echo "\nERROR on QUERY for NodeReadings";
}
while($row = mysqli_fetch_array($result))
{
array_push($soil_temp, $row);
}
}
/**
* Create Labels array for graphs
* This part is converting the time into a label using a character approach
* Basically the graph won't visualise unless the labes are correctly formated
* so it is necessary to format the time to make sure is using 4 digits total
* for each data point
**/
$labels = array();
$number = count($times);
for($i = 0; $i < $number; $i++)
{
//NOTE: LAST INDEX IS FOR CHARACTER BECAUSE HERE I'M CONSTRUCTING THE STRING FROM THE TIME CHARACTERS
$current = $times[$i][0];
$current = $current.$times[$i][1];
$current = $current.".";
$current = $current.$times[$i][3];
$current = $current.$times[$i][4];
$current = $current.",";
array_push($labels,number_format(floatval($current),2).",");
}
include("assets/getNodes.php");
?>
<html>
<head>
<title>IoT Virtual Sensors Hub</title>
<!-- Theme CSS -->
<link id="theme-style" rel="stylesheet" href="css/style.css">
<!--LOADING CHARTS JS-->
<script src="https://github.com/chartjs/Chart.js/releases/download/v2.7.3/Chart.min.js"></script>
<!--LOADING NN AND JQUERY JS-->
<script src="assets/jquery-3.3.1.js"></script>
</head>
<body>
<div id="bg">
<img src="images/back (2).jpg" alt="">
</div>
<!--MAIN TITLE-->
<h1 style="position: absolute; left: 780px;top: 10px;">Virtual Sensors Hub</h1>
<h2 style="position: absolute; left: 890px;top: 50px;">Node
<?php echo $currentNode ?>
</h2>
<!--GRPAHS-->
<h2 style="position: absolute; left: 450px; top: 250px;">Magic Bean Node</h2>
<div class="chart-container" style="position: absolute; left: 200px; top: 300px;">
<canvas id="chartNode" height="400" width="700"></canvas>
</div>
<h2 style="position: absolute; left: 1250px; top: 250px;">Virtual Bean Node</h2>
<div class="chart-container" style="position: absolute; left: 1000px; top: 300px;">
<canvas id="chartVirtualNode" height="400" width="700"></canvas>
</div>
<h2 id="error" style="position: absolute; left: 890px;top: 200px;">
</h2>
<!--BUTTONS-->
<form id="form_node_select" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input type="submit" name="action" value="LOAD NODE ID" style="position: absolute; left: 810px;top: 120px;width: 120px;" />
<select name="currentNode" form="form_node_select" style="position: absolute; left: 940px;top: 120px;width: 50; text-align:center;">
<?php
$nodes = count($nodesPositions);
for($i = 0; $i < $nodes; $i++)
{
echo "<option value=".'"'.$nodesPositions[$i][0].'"'.">".$nodesPositions[$i][0]."</option>";
}
?>
</select>
<!--<input type="text" value="<?php echo $currentNode ?>" name="currentNode" style="position: absolute; left: 940px;top: 120px;width: 50; text-align:center;" />-->
</form>
<!------------------------------------------------>
<!------------------------------------------------>
<!------------------------------------------------>
<!------------------------------------------------>
<!------------------------------------------------>
<!------------------------------------------------>
<!------------------------------------------------>
<!------------------------------------------------>
<!------------------------------------------------>
<!------------------------------------------------>
<!--GRAPHS CODE FOR DATA LOADING AND FORMATTING -->
<script>
var nodeDataLabels = [<?php
$number = count($labels);
for($i = 0; $i < $number; $i++)
{
echo $labels[$i];
}
?>];
var virtualNodeDataLabels = [<?php
$number = count($labels);
for($i = 1; $i < $number; $i++)
{
echo $labels[$i];
}
?>];
var ctx1 = document.getElementById("chartNode").getContext('2d');
var chart1 = new Chart(ctx1, {
type: 'line',
data: {
"labels": nodeDataLabels,
"datasets": [{
"label": "C° ",
"data": [
<?php
$number = count($soil_temp);
for($i = 0; $i < $number; $i++)
{
echo $soil_temp[$i][0];
echo ",";
}
?>
],
"fill": false,
"borderColor": "rgb(75, 192, 192)",
"lineTension": 0.1
}]
},
options: {
responsive: false,
title: {
display: true,
text: 'Soil Temperature',
fontSize: 26,
},
scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Temperature'
},
ticks: {
beginAtZero: false
}
}],
xAxes: [{
scaleLabel: {
display: true,
labelString: 'Time'
},
ticks: {
beginAtZero: true
}
}]
}
}
});
var ctx2 = document.getElementById("chartVirtualNode").getContext('2d');
var chart2 = new Chart(ctx2, {
type: 'line',
data: {
"labels": virtualNodeDataLabels,
"datasets": [{
"label": "C° ",
"fill": false,
"borderColor": "rgb(75, 192, 192)",
"lineTension": 0.1
}]
},
options: {
responsive: false,
title: {
display: true,
text: 'Soil Temperature',
fontSize: 26,
},
scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Temperature'
},
ticks: {
beginAtZero: false
}
}],
xAxes: [{
scaleLabel: {
display: true,
labelString: 'Time'
},
ticks: {
beginAtZero: true
}
}]
}
}
});
function addData(chart, label, data) {
//chart.data.labels.push(label);
chart.data.datasets.forEach((dataset) => {
dataset.data.push(data);
});
chart.update();
}
function getVirtualData() {
var nodeID = "<?php echo $currentNode ?>";
var date = "<?php echo $newDate->format('Y-m-d'); ?>";
return jQuery.ajax({
type: "POST",
url: "assets/virtual_data.php",
dataType: "json",
data: {
//arguments: [1, 2],
function: "getPredictions",
date: date,
nodeID: nodeID
},
success: function(obj, statusText) {
if (!('error' in obj)) {
//document.getElementById("out").innerHTML = obj.result["lat"];
//console.log("SUCCESS");
//return obj;
} else {
//console.log(obj.error);
//console.log("PHP ERROR: \n" + JSON.stringify(obj.error));
//document.getElementById("out").innerHTML = "PHP ERROR: \n" + JSON.stringify(obj.error);
//return null;
}
},
error: function(error) {
//console.log(error);
//console.log("JQUERY ERROR: \n" + JSON.stringify(error));
//document.getElementById("out").innerHTML = "JQUERY ERROR: \n" + JSON.stringify(error);
//return null;
}
});
}
$.when(getVirtualData()).done(function(query) {
// the code here will be executed when all four ajax requests resolve.
// a1, a2, a3 and a4 are lists of length 3 containing the response text,
// status, and jqXHR object for each of the four ajax calls respectively.
//console.log(query);
if (!(query["result"] === undefined) && query["result"] != null) {
console.log("RECEIVED DATA FROM QUERY");
//ADDING MAKERS
var total = query["result"]["size"];
for (var i = 0; i < total-1; i++) {
var soil_temp = query["result"][i.toString()];
addData(chart2, null, soil_temp);
}
var error = query["result"][(total-1).toString()];
document.getElementById("error").innerHTML = "MSE: " + error;
//virtualNodeDataLabels = 09;
//virtualNodeData = 10;
//addData(chart2, virtualNodeDataLabels, virtualNodeData);
} else
console.log("VIRTUAL DATA: Error retrieving virtual sensors data...");
});
</script>
</body>
</html>