-
Notifications
You must be signed in to change notification settings - Fork 4
/
ttnlora_env_last.php
65 lines (53 loc) · 2.43 KB
/
ttnlora_env_last.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
<?php
include './ttnlora_env_vars.php';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$filter_area = null;
if(isset($_GET['area'])) {
$filter_area = $_GET["area"];
$filter_area = $conn->real_escape_string($filter_area);
}
$first = true;
if ($filter_area != null && $filter_area != 'all') {
$sql = "SELECT m1.DevID AS Code, m1.TimestampUTC AS Time, m1.RSSI AS Rssi, m1.Temperature AS Temp, m1.Humidity AS Humidity, m1.Pressure AS Pressure, m1.Batt AS Batt, n.Latitude AS Latitude, n.Longitude AS Longitude, na.AreaID as Area, n.Description AS Description FROM Measurement m1 INNER JOIN (SELECT mi.DevID as DevID, MAX(mi.TimestampUTC) AS maxtimestamp FROM Measurement mi GROUP BY mi.DevID) m2 ON (m1.TimestampUTC = m2.maxtimestamp AND m1.DevID = m2.DevID) INNER JOIN Node n ON n.DevID = m1.DevID INNER JOIN NodeArea na ON m1.DevID = na.DevID WHERE na.AreaID = '" . $filter_area . "' ORDER BY n.DevID";
} else {
$sql = "SELECT m1.DevID AS Code, m1.TimestampUTC AS Time, m1.RSSI AS Rssi, m1.Temperature AS Temp, m1.Humidity AS Humidity, m1.Pressure AS Pressure, m1.Batt AS Batt, n.Latitude AS Latitude, n.Longitude AS Longitude, na.AreaID as Area , n.Description AS Description FROM Measurement m1 INNER JOIN (SELECT mi.DevID as DevID, MAX(mi.TimestampUTC) AS maxtimestamp FROM Measurement mi GROUP BY mi.DevID) m2 ON (m1.TimestampUTC = m2.maxtimestamp AND m1.DevID = m2.DevID) INNER JOIN Node n ON n.DevID = m1.DevID INNER JOIN NodeArea na ON m1.DevID = na.DevID ORDER BY n.DevID";
}
//echo "// filter_area = ". $filter_area;
//echo "// sql = ". $sql;
echo "var lastmeasurment = [\n";
$result = $conn->query($sql);
if (!$result) {
echo 'Invalid query: ' . $conn->error;
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
if (!$first)
{
echo ",\n";
}
$first = false;
$code = $row["Code"];
$time = $row["Time"];
$rssi = $row["Rssi"];
$temp = $row["Temp"];
$humidity = $row["Humidity"];
$pressure = $row["Pressure"];
$batt = $row["Batt"];
$lat = $row["Latitude"];
$lng = $row["Longitude"];
$area = $row["Area"];
$description = $row["Description"];
$value = "['$code','$time','$rssi','$lat','$lng', '$temp', '$humidity', '$pressure', '$batt', '$area', '$description']";
echo " $value";
}
}
echo "\n];\n";
$conn->close();
?>