forked from Ysurac/FlightAirMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron-sbs.php
executable file
·68 lines (58 loc) · 1.78 KB
/
cron-sbs.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
#!/usr/bin/php
<?php
// This is not a cron job... Use it like a daemon
require_once('require/class.SBS.php');
// Check if schema is at latest version
require_once('require/class.Connection.php');
$schema = new Connection();
if ($schema::latest() === false) {
echo "You MUST update to latest schema. Run install/index.php";
exit();
}
$debug = true;
$SBS=new SBS();
date_default_timezone_set('UTC');
// signal handler - playing nice with sockets and dump1090
if (function_exists('pcntl_fork')) {
pcntl_signal(SIGINT, function($signo) {
global $sock, $db;
echo "\n\nctrl-c or kill signal received. Tidying up ... ";
socket_shutdown($sock, 0);
socket_close($sock);
$db = null;
die("Bye!\n");
});
pcntl_signal_dispatch();
}
// let's try and connect
echo "Connecting to dump1090 ... ";
// create our socket and set it to non-blocking
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Unable to create socket\n");
while (!@socket_connect($sock, $globalSBS1Host, $globalSBS1Port))
{
$err = socket_last_error($sock);
if ($err == 115 || $err == 114)
{
if ((time() - $time) >= $globalSBS1TimeOut)
{
socket_close($sock);
die("Connection timed out.\n");
}
}
die(socket_strerror($err) . "\n");
}
// connected - lets do some work
echo "Connected!\n";
sleep(1);
echo "SCAN MODE \n\n";
while($buffer = socket_read($sock, 3000, PHP_NORMAL_READ)) {
// lets play nice and handle signals such as ctrl-c/kill properly
if (function_exists('pcntl_fork')) pcntl_signal_dispatch();
$dataFound = false;
$SBS::del();
// SBS format is CSV format
$line = explode(',', $buffer);
$SBS::add($line);
}
if (function_exists('pcntl_fork')) pcntl_exec($_,$argv);
?>