-
Notifications
You must be signed in to change notification settings - Fork 4
/
ttnlora_env.php
144 lines (127 loc) · 4.02 KB
/
ttnlora_env.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
<?php
$entityBody = file_get_contents('php://input');
$logfile = '/home/lex/Zendamateur_www/TTN/PH2LB_enviromental_beacon_ttnlora.log';
$content = "$entityBody\r\n";
file_put_contents($logfile, $content, FILE_APPEND | LOCK_EX);
include './ttnlora_env_vars.php';
include './ttnlora_env_alarmwarning.php';
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
/*
if (!isset($headers['apikey'])) {
die("apikey is not set");
}
if ($headers['apikey'] != $apikey) {
die("apikey isn't what it should be");
}
*/
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// {"payload":"LSgCcQAA","fields":{"batt":4.5,"bytes":"LSgCcQAA","humidity":40,"pkcount":0,"temperature":22.5,"txresult":0,"txretrycount":0},"port":1,"counter":0,"dev_eui":"0004A30B001AD790","metadata":[{"frequency":867.5,"datarate":"SF7BW125","codingrate":"4/5","gateway_timestamp":3992695868,"channel":5,"server_time":"2016-11-11T10:34:10.458885697Z","rssi":-81,"lsnr":6.8,"rfchain":0,"crc":1,"modulation":"LORA","gateway_eui":"1DEE026C7BFC3E5C","altitude":44,"longitude":6.83681,"latitude":52.2402}]}
// http://www.mapcoordinates.net/en
$dev_id = "NULL";
$time = "NULL";
$batt = "NULL";
$humidity = "NULL";
$temperature = "NULL";
$rssi = "NULL";
$ldr = "NULL";
$framecounter = "NULL" ;
$packetcounter = "NULL";
$txresult = "NULL";
$txretrycount = "NULL";
$pressure = "NULL";
function getData($parent,$arr)
{
global $dev_id,$batt,$humidity,$pressure,$time,$temperature,$rssi,$ldr,$framecounter,$packetcounter,$txresult,$txretrycount;
//echo "parent = $parent > ";
foreach($arr as $key => $value)
{
if (is_numeric($value))
{
//echo "0.key = $key\n";
switch($key) {
case "batt" : $batt = $value; break;
case "humidity" : $humidity = $value; break;
case "pressure" : $pressure = $value; break;
case "temperature": $temperature = $value; break;
case "pkcount": $packetcounter = $value; break;
case "txresult": $txresult = $value; break;
case "txretrycount": $txretrycount = $value; break;
case "rssi" :
if ($rssi == "NULL")
$rssi = $value;
else if ($value > $rssi)
$rssi = $value;
break;
case "counter": $framecounter = $value; break;
}
}
else if (is_array($value))
{
getData($key, $value);
}
else if (is_object($value))
{
getData($key, $value);
}
else
{
if ($parent == "metadata" && !is_numeric($parent))
{
//echo "1.key = $key\n";
switch($key) {
case "time" : $time = $value; break;
}
}
else
{
//echo "2.key = $key\n";
switch($key) {
case "dev_id" : $dev_id = $value; break;
case "app_id" : $app_id = $value; break;
}
}
}
}
}
$my_arr = json_decode($content);
getData("", $my_arr);
if ($pressure > 1100)
$pressure = 'NULL';
if ($time != "NULL" && $dev_id != "NULL")
{
$time = str_replace("Z", "", $time);
$time = $conn->real_escape_string($time);
$dev_id = $conn->real_escape_string($dev_id);
$temperature = $conn->real_escape_string($temperature);
$humidity = $conn->real_escape_string($humidity);
$pressure = $conn->real_escape_string($pressure);
$batt = $conn->real_escape_string($batt);
$rssi = $conn->real_escape_string($rssi);
$content = $conn->real_escape_string($content);
$sql = "INSERT INTO Measurement (TimestampUTC, DevID, Temperature, Humidity, Pressure, Batt, RSSI, Raw) VALUES ('$time', '$dev_id', $temperature, $humidity, $pressure, $batt, $rssi, '$content')";
if ($conn->query($sql) === TRUE)
{
echo "New record created successfully\n";
//error_log("Now checking for alarms and warnings");
checkForAlarmWarning( $dev_id, $time, $rssi, $temperature, $humidity, $pressure, $batt );
//error_log("Done checking for alarms and warnings");
}
else
{
error_log('Invalid query: ' .$sql. ' ' . $conn->error);
}
}
else
{
echo "Nothing to do.";
}
$conn->close();
?>