-
Notifications
You must be signed in to change notification settings - Fork 4
/
ttnlora_env_alarmwarning.php
229 lines (200 loc) · 6.99 KB
/
ttnlora_env_alarmwarning.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
<?php
// needed for sendEmail
include './ttnlora_env_mailer.php';
include './ttnlora_env_telegram.php';
// needs global $conn
function levelToStr($level)
{
// Alarm=0, Warning=1
if ($level == 0) return "Alarm";
else if ($level == 1) return "Warning";
return "Uknown";
}
function typeToStr($type)
{
// Temp=0, Humidty=1, Pressure=2, Batt=3, RSSI=4
if ($type == 0) return "TempLower";
else if ($type == 1) return "TempUpper";
else if ($type == 2) return "HumidityLower";
else if ($type == 3) return "HumidityUpper";
else if ($type == 4) return "PressureLower";
else if ($type == 5) return "PressureUpper";
else if ($type == 6) return "BattLower";
else if ($type == 7) return "BattUpper";
else if ($type == 8) return "RSSILower";
else if ($type == 9) return "RSSIUpper";
return "Uknown";
}
function unitToType($unit)
{
$type = -1;
// Temp=0, Humidty=1, Pressure=2, Batt=3, RSSI=4
if ($unit == "TempLower") $type = 0;
else if ($unit == "TempUpper") $type = 1;
else if ($unit == "HumidityLower") $type = 2;
else if ($unit == "HumidityUpper") $type = 3;
else if ($unit == "PressureLower") $type = 4;
else if ($unit == "PressureUpper") $type = 5;
else if ($unit == "BattLower") $type = 6;
else if ($unit == "BattUpper") $type = 7;
else if ($unit == "RSSILower") $type = 8;
else if ($unit == "RSSIUpper") $type = 9;
//echo "unit : $unit > type : $type\n";
return $type;
}
function sendAlarmWarning($dev_id, $level, $type, $value, $raise, $timestamputcstart, $timestamputcend, $action)
{
// choice you're tool
global $mailfrom;
global $telegrambotid;
$typeStr = typeToStr($type);
$levelStr = levelToStr($level);
if ($raise)
{
$subject = "$dev_id : New $levelStr of type $typeStr";
$body = "For node $dev_id on $timestamputcstart a $levelStr of type $typeStr was raised for the value of $value.";
}
else
{
$subject = "$dev_id : End of $levelStr of type $typeStr";
$body = "For node $dev_id the $levelStr of type $typeStr raised on $timestamputcstart was ended on $timestamputcend for the value of $value.";
}
if ($mailfrom != null)
{
// send email
// TODO change $to from DB -> rip apart $action
sendEmail('[email protected]', $subject, $body);
}
if ($telegrambotid != null)
{
// send telegram message (299412663 = lex)
// TODO change $chatid from DB -> rip apart $action
sendTelegramMessage('299412663', 'ENV', $body);
}
}
function handleAlarmWarning($dev_id, $level, $type, $value, $raise, $time, $action)
{
// check if there is a open alarm for this kind of unit
global $conn;
$sql = "SELECT DevID, Level, Type, TimestampUTCStart, TimestampUTCEnd FROM AlarmWarning WHERE DevID = '$dev_id' AND Level = '$level' AND Type = '$type' AND TimestampUTCEnd IS NULL";
//echo "SQL : $sql\n";
$result = $conn->query($sql);
if (!$result)
{
error_log('Invalid query: ' . $conn->error);
}
if ($result->num_rows > 0)
{
echo "open alarm/warning\n";
while($row = $result->fetch_assoc())
{
//var_dump($row);
// we need timestamputcstart because we don't want to reset all the other alarms and warnings in the history
$timestamputcstart = $row["TimestampUTCStart"];
if (!$raise)
{
echo "should close alarmwarning DevID = '$dev_id' AND Level = $level AND Type = $type\n";
$sql = "UPDATE AlarmWarning SET TimestampUTCEnd='$time' WHERE DevID = '$dev_id' AND Level = $level AND Type = $type AND TimestampUTCStart = '$timestamputcstart'";
//echo "SQL > $sql\n";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully\n";
// TODO : send notification
sendAlarmWarning($dev_id, $level, $type, $value, $raise, $timestamputcstart, $time, $action);
} else {
error_log("Error updating record: " . $conn->error);
}
}
else
{
echo "still alarmwarning DevID = '$dev_id' AND Level = $level AND Type = $type\n";
}
}
}
else
{
if ($raise)
{
error_log("create alarm/warning");
$sql = "INSERT INTO AlarmWarning (DevID, Level, Type, Value, TimestampUTCStart, TimestampUTCEnd ) VALUES ('$dev_id', $level, $type, $value, '$time', NULL)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully\n";
// TODO : send email
sendAlarmWarning($dev_id, $level, $type, $value, $raise, $time, NULL, $action);
} else {
error_log("Error: " . $sql . "<br>" . $conn->error);
}
}
}
}
function checkLimits($dev_id, $level, $unit, $time, $value, $lowerLimit, $upperLimit, $action)
{
global $conn;
if (!empty($lowerLimit))
{
echo "$unit lower : $value <= $lowerLimit : ";
$result = ($value <= $lowerLimit);
$resultstr = $result ? 'true' : 'false';
echo "$resultstr\n";
handleAlarmWarning($dev_id, $level, unitToType($unit . 'Lower'), $value, $result, $time, $action);
}
if (!empty($upperLimit))
{
echo "$unit upper : $value >= $upperLimit : ";
$result = ($value >= $upperLimit);
$resultstr = $result ? 'true' : 'false';
echo "$resultstr\n";
handleAlarmWarning($dev_id, $level, unitToType($unit . 'Upper'), $value, $result, $time, $action);
}
}
function checkForAlarmWarning($dev_id, $time, $rssi, $temp, $humidity, $pressure, $batt )
{
global $conn;
echo "<pre>\n";
$awsql = "SELECT DevID, Level, TemperatureLower, TemperatureUpper, HumidityLower, HumidityUpper, PressureLower, PressureUpper, BattLower, BattUpper, RSSILower, RSSIUpper, Action FROM AlarmWarningLevels
WHERE DevID = '$dev_id'";
$awresult = $conn->query($awsql);
if (!$awresult)
{
error_log('Invalid query: ' . $conn->error);
}
else if ($awresult->num_rows > 0)
{
// output data of each row
while($row = $awresult->fetch_assoc())
{
echo "------< AlarmWarningLevels >------\n";
$level = $row["Level"];
$templow = $row["TemperatureLower"];
$temphigh = $row["TemperatureUpper"];
$humlow = $row["HumidityLower"];
$humhigh = $row["HumidityUpper"];
$presslow = $row["PressureLower"];
$presshigh = $row["PressureUpper"];
$battlow = $row["BattLower"];
$batthigh = $row["BattUpper"];
$rssilow = $row["RSSILower"];
$rssihigh = $row["RSSIUpper"];
$action = $row["Action"];
echo "level:$level\n";
echo "templow:$templow\n";
echo "temphigh:$temphigh\n";
echo "humlow:$humlow\n";
echo "humhigh:$humhigh\n";
echo "presslow:$presslow\n";
echo "presshigh:$presshigh\n";
echo "battlow:$battlow\n";
echo "batthigh:$batthigh\n";
echo "rssilow:$rssilow\n";
echo "rssihigh:$rssihigh\n";
echo "action:$action\n";
echo "------< Result >------\n";
checkLimits($dev_id, $level, "Temp", $time, $temp, $templow, $temphigh, $action);
checkLimits($dev_id, $level, "Humidity", $time, $humidity, $humlow, $humhigh, $action);
checkLimits($dev_id, $level, "Pressure", $time, $pressure, $presslow, $presshigh, $action);
checkLimits($dev_id, $level, "Batt", $time, $batt, $battlow, $batthigh, $action);
checkLimits($dev_id, $level, "RSSI", $time, $rssi, $rssilow, $rssihigh, $action);
echo "------< End >------\n";
}
}
echo "</pre>\n";
}