-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotifier.php
386 lines (354 loc) · 13.6 KB
/
notifier.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/**
* adsbTelegramNotifier
*
* Notifies via Telegram when an aircraft passes over a certain area.
*
* @author RundesBalli <[email protected]>
* @copyright 2022 RundesBalli
* @see https://github.com/RundesBalli/adsbTelegramNotifier
*/
/**
* Including the configuration and function loader.
*/
require_once(__DIR__.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR.'loader.php');
/**
* This script always runs in verbose mode. Therefore its output has to be discarded when using cron.
*/
/**
* Clear screen and show opening ascii art.
*/
echo CLEAR_SCREEN;
openingArt();
/**
* Check config version
*/
echo logEcho($lang['notifier']['checkConfigVersion'], 'INFO', COLOR_INFO);
if($configVersion < MIN_CONFIG_VERSION) {
fwrite(STDERR, logEcho($lang['notifier']['updateConfig'], 'CRIT', COLOR_CRIT));
if(sendMessageToTelegram(EMOJI_WARN.' '.$lang['notifier']['updateConfig'])) {
echo logEcho($lang['sendMessage']['ok'], 'OK', COLOR_OK);
} else {
fwrite(STDERR, logEcho($lang['sendMessage']['failed'], 'WARN', COLOR_WARN));
}
fwrite(STDERR, logEcho($lang['exiting'], 'CRIT', COLOR_CRIT));
die();
}
echo logEcho($lang['notifier']['checkConfigVersionDone'], 'OK', COLOR_OK);
/**
* Send test message to telegram when test parameter is provided and exit.
*/
$options = getopt('', ['test-telegram']);
if(isset($options['test-telegram'])) {
echo logEcho($lang['notifier']['sendTestMessage'], 'INFO', COLOR_INFO);
if(sendMessageToTelegram(EMOJI_SPEAKER.' '.$lang['sendMessage']['testMessage'])) {
echo logEcho($lang['sendMessage']['ok'], 'OK', COLOR_OK);
} else {
fwrite(STDERR, logEcho($lang['sendMessage']['failed'], 'WARN', COLOR_WARN));
}
echo logEcho($lang['exiting'], 'OK', COLOR_OK);
die();
}
/**
* Check if the radius in the configuration file was entered in kilometers or nautical miles.
* If the value was entered in kilometers, it must be converted to nautical miles, since readsb gives
* distances in nautical miles.
*/
if($useMetric !== FALSE) {
$radius = $radius*0.539956803;
}
/**
* Count runs and errors
*/
$runs = 0;
$errors = 0;
/**
* Loop everything, because it runs as a systemd service.
*/
while(1) {
/**
* Log microtime
*/
$microtimeStart = microtime(true);
/**
* Increment run counter.
*/
$runs++;
/**
* Check version if the run count is divideable by 100.
*/
if($runs % 100 == 0) {
echo logEcho($lang['notifier']['checkUpdateAvailable'], 'INFO', COLOR_INFO);
checkVersion();
}
/**
* Read the previous seen aircraft file.
*/
$previousFile = (!empty($previousJsonFile) ? $previousJsonFile : __DIR__.DIRECTORY_SEPARATOR.'previous.json');
echo logEcho(sprintf($lang['notifier']['previousFile'], $previousFile), 'FILE', COLOR_FILE);
if(!file_exists($previousFile)) {
$previous = [];
echo logEcho($lang['notifier']['previousFileNotExists'], 'INFO', COLOR_INFO);
} else {
if(filesize($previousFile) == 0) {
$previous = [];
unlink($previousFile);
fwrite(STDERR, logEcho($lang['notifier']['previousFileEmpty'], 'WARN', COLOR_WARN));
} else {
$fp = fopen($previousFile, 'r');
$previous = json_decode(fread($fp, filesize($previousFile)), TRUE);
fclose($fp);
if(!is_array($previous)) {
$previous = [];
unlink($previousFile);
fwrite(STDERR, logEcho($lang['notifier']['previousFileReadFailed'], 'WARN', COLOR_WARN));
} else {
echo logEcho($lang['notifier']['previousFileRead'], 'OK', COLOR_OK);
}
}
}
/**
* Delete all aircraft that are outside the timeout.
*/
if(!empty($previous)) {
echo logEcho(sprintf($lang['notifier']['checkTimeout'], $timeout), 'INFO', COLOR_INFO);
$timeoutAircrafts = 0;
foreach($previous AS $icao => $time) {
if($time <= (time()-$timeout)) {
$timeoutAircrafts++;
unset($previous[$icao]);
echo logEcho(sprintf($lang['notifier']['deleteAircraft'], $icao, date('d.m.Y, H:i:s', $time)), 'OK', COLOR_OK);
}
}
if($timeoutAircrafts > 0) {
echo logEcho(sprintf($lang['notifier']['deleteAircraftCount'], $timeoutAircrafts), 'OK', COLOR_OK);
} else {
echo logEcho($lang['notifier']['noDeletedAircrafts'], 'OK', COLOR_OK);
}
}
/**
* Read the contents from the current decoder output.
*/
echo logEcho(sprintf($lang['notifier']['aircraftJsonFile'], $aircraftJsonFile), 'FILE', COLOR_FILE);
if(!file_exists($aircraftJsonFile)) {
fwrite(STDERR, logEcho($lang['notifier']['aircraftJsonFileNotExists'], 'CRIT', COLOR_CRIT));
if(sendMessageToTelegram(EMOJI_WARN.' '.$lang['notifier']['aircraftJsonFileNotExists'])) {
echo logEcho($lang['sendMessage']['ok'], 'OK', COLOR_OK);
} else {
fwrite(STDERR, logEcho($lang['sendMessage']['failed'], 'WARN', COLOR_WARN));
}
fwrite(STDERR, logEcho($lang['exiting'], 'CRIT', COLOR_CRIT));
die();
}
$fp = fopen($aircraftJsonFile, 'r');
$aircrafts = json_decode(fread($fp, filesize($aircraftJsonFile)), TRUE);
fclose($fp);
/**
* If the decoder output is no array, the script will exit.
*/
if(!is_array($aircrafts)) {
fwrite(STDERR, logEcho($lang['notifier']['aircraftJsonDataFailed'], 'CRIT', COLOR_CRIT));
$errors++;
if($errors >= 3) {
if(sendMessageToTelegram(EMOJI_WARN.' '.$lang['notifier']['aircraftJsonDataFailed'])) {
echo logEcho($lang['sendMessage']['ok'], 'OK', COLOR_OK);
} else {
fwrite(STDERR, logEcho($lang['sendMessage']['failed'], 'WARN', COLOR_WARN));
}
} else {
continue;
}
fwrite(STDERR, logEcho($lang['exiting'], 'CRIT', COLOR_CRIT));
die();
}
/**
* Check if the decoder output is empty or not.
*/
if(!empty($aircrafts)) {
$aircrafts = $aircrafts['aircraft'];
/**
* Iterate through the decoder data.
*/
foreach($aircrafts as $aircraft) {
/**
* Check if the datasource from the aircraft is in the configured array.
*/
if(array_search('all', $dataSources) === FALSE AND array_search($aircraft['type'], $dataSources) === FALSE) {
echo logEcho(sprintf($lang['notifier']['aircraftWrongDataSource'], $aircraft['hex']), 'INFO', COLOR_INFO);
continue;
}
/**
* Check if the distance from the aircraft to the station is within the configured radius.
* If no r_dst is provided, there are no coordinates available.
*/
if(empty($aircraft['r_dst']) OR (!is_numeric($aircraft['r_dst']) OR $aircraft['r_dst'] > $radius)) {
echo logEcho(sprintf($lang['notifier']['aircraftOutOfRange'], $aircraft['hex']), 'INFO', COLOR_INFO);
continue;
}
/**
* Check if the $minAlt is reached.
*/
if(empty($aircraft['alt_baro']) OR $aircraft['alt_baro'] < $minAlt) {
echo logEcho(sprintf($lang['notifier']['aircraftBelowMinAlt'], $aircraft['hex']), 'INFO', COLOR_INFO);
continue;
}
/**
* Check if the $maxAlt is reached.
*/
if(empty($aircraft['alt_baro']) OR $aircraft['alt_baro'] > $maxAlt) {
echo logEcho(sprintf($lang['notifier']['aircraftAboveMaxAlt'], $aircraft['hex']), 'INFO', COLOR_INFO);
continue;
}
/**
* Check if the aircraft was seen before.
*/
if(array_key_exists($aircraft['hex'], $previous)) {
/**
* Aircraft has been seen within the defined timeout time and does not need to be notified again. The
* time of the last sighting is updated.
*/
echo logEcho(sprintf($lang['notifier']['aircraftUpdated'], $aircraft['hex']), 'OK', COLOR_OK);
$previous[$aircraft['hex']] = time();
continue;
} else {
/**
* Aircraft is new within the observation radius.
*/
/**
* Prepare text
*/
$text = sprintf(EMOJI_AIRCRAFT.' '.$lang['notifier']['newAircraftTelegram'], $aircraft['hex']);
$text.= sprintf($lang['notifier']['aircraftHexLink'], $aircraft['hex'], $aircraft['hex']);
if(!empty($aircraft['flight']) AND !empty(trim($aircraft['flight']))) {
$text.= sprintf($lang['notifier']['aircraftFlightLink'], trim($aircraft['flight']), trim($aircraft['flight']));
}
/**
* The following data 'r' (registration) and 'desc' (long description of the aircraft) is only
* available, if the readsb decoder has the db-file included (see ReadMe).
*/
if(!empty($aircraft['r'])) {
$text.= sprintf($lang['notifier']['aircraftRegistration'], trim($aircraft['r']));
if(!empty($aircraft['desc'])) {
$text.= sprintf($lang['notifier']['aircraftDesc'], trim($aircraft['desc']));
}
/**
* If the registration is available, the aircraft.csv is included and therefore the dbFlags field is
* available on special flights.
*/
$currentDbFlag = (empty($aircraft['dbFlags']) ? 0 : intval($aircraft['dbFlags']));
if(!in_array($currentDbFlag, $dbFlags, TRUE)) {
echo logEcho(sprintf($lang['notifier']['aircraftWrongDbFlag'], $aircraft['hex']), 'INFO', COLOR_INFO);
continue;
} else {
$text.= sprintf($lang['notifier']['aircraftDbFlag'], DBFLAGS[$currentDbFlag]);
}
} elseif(empty($aircraft['r']) AND $skipWithoutReg === TRUE) {
/**
* Skip aircraft, if no registration is available and the skip option is set.
*/
echo logEcho(sprintf($lang['notifier']['aircraftSkipWithoutReg'], $aircraft['hex']), 'INFO', COLOR_INFO);
continue;
}
/**
* Check if the coordinates are available and should be posted.
*/
if((!empty($aircraft['lastPosition']) OR (!empty($aircraft['lat']) AND !empty($aircraft['lon']))) AND $showCoordinates === TRUE) {
if(!empty($aircraft['lat']) AND !empty($aircraft['lon'])) {
$lat = $aircraft['lat'];
$lon = $aircraft['lon'];
} else {
$lat = $aircraft['lastPosition']['lat'];
$lon = $aircraft['lastPosition']['lon'];
}
if(!empty($linkToMaps)) {
/**
* A map link should be posted.
*/
$text.= sprintf($lang['notifier']['aircraftCoordinatesLinked'], $lat, $lon, sprintf($linkToMaps, $lat, $lon));
} else {
/**
* No map link should be posted.
*/
$text.= sprintf($lang['notifier']['aircraftCoordinates'], $lat, $lon);
}
}
/**
* Check if the altitude is available and should be posted.
*/
if(!empty($aircraft[$altType]) AND $showAlt === TRUE) {
$text.= sprintf($lang['notifier']['aircraftAlt'], number_format(($useMetric ? ($aircraft[$altType]/3.28084) : $aircraft[$altType]), 0, $lang['decimalSeparator'], $lang['thousandsSeparator']), ($useMetric ? 'm' : 'ft'));
}
/**
* Show log message, that the aircraft will be notified.
*/
echo logEcho(sprintf($lang['notifier']['newAircraft'], $aircraft['hex']), 'OK', COLOR_OK);
$previous[$aircraft['hex']] = time();
/**
* Check if planespotters picture is requested.
*/
if($planespotters == TRUE) {
echo logEcho($lang['notifier']['planespottersApiCall'], 'INFO', COLOR_INFO);
$photo = getPlanespottersPhoto($aircraft['hex']);
if($photo !== FALSE AND is_array($photo)) {
echo logEcho($lang['notifier']['planespottersApiCallSuccessful'], 'OK', COLOR_OK);
$textPhoto = sprintf($lang['notifier']['planespottersNote'], $photo['linkToPhoto'], $photo['author']);
/**
* Send prepared text with photograph.
*/
if(sendPhotoToTelegram($text.$textPhoto, $photo['url'])) {
echo logEcho($lang['sendMessage']['ok'], 'OK', COLOR_OK);
continue;
} else {
fwrite(STDERR, logEcho($lang['sendMessage']['failed'], 'WARN', COLOR_WARN));
}
} else {
fwrite(STDERR, logEcho($lang['notifier']['planespottersApiCallFailed'], 'WARN', COLOR_WARN));
}
/**
* If no photograph is available and no notification without photograph is wanted, the aircraft is
* skipped.
*
* To avoid that the aircraft is requested every time again at planespotters.net, it will be marked as
* notified.
*/
if($skipNoPhoto === TRUE) {
fwrite(STDERR, logEcho($lang['notifier']['planespottersSkipNoPhoto'], 'WARN', COLOR_WARN));
$previous[$aircraft['hex']] = time();
continue;
}
}
/**
* Send prepared text if no planespotters picture is requested or available.
*/
if(sendMessageToTelegram($text)) {
echo logEcho($lang['sendMessage']['ok'], 'OK', COLOR_OK);
} else {
fwrite(STDERR, logEcho($lang['sendMessage']['failed'], 'WARN', COLOR_WARN));
}
}
}
}
/**
* Write new previous.json file.
*/
echo logEcho($lang['notifier']['previousFileTruncate'], 'FILE', COLOR_FILE);
$fp = fopen($previousFile, 'w+');
fwrite($fp, json_encode($previous));
fclose($fp);
/**
* Done :-)
*/
echo logEcho($lang['done'], 'OK', COLOR_OK);
/**
* Output execution time
*/
$executionTime = microtime(true)-$microtimeStart;
echo $lang['notifier']['executionTime'].round($executionTime*1000, 1)." ms\n-------------------------------------------------------------\n";
$usleep = intval(floor(($runEvery*1000000)-$executionTime));
if($usleep <= 0) {
continue;
} else {
usleep($usleep);
}
}
?>