@@ -18,30 +18,30 @@ discard block |
||
| 18 | 18 | public function parse($buffer) { |
| 19 | 19 | // Not yet finished, no CRC checks |
| 20 | 20 | $data = array(); |
| 21 | - $typehex = substr($buffer,0,1); |
|
| 21 | + $typehex = substr($buffer, 0, 1); |
|
| 22 | 22 | if ($typehex == '*' || $typehex == ':') { |
| 23 | - $hex = substr($buffer,1); |
|
| 24 | - if(substr($hex,-1,1)==";") { |
|
| 25 | - $hex=substr($hex,0,-1); |
|
| 23 | + $hex = substr($buffer, 1); |
|
| 24 | + if (substr($hex, -1, 1) == ";") { |
|
| 25 | + $hex = substr($hex, 0, -1); |
|
| 26 | 26 | } |
| 27 | 27 | } |
| 28 | 28 | //elseif ($typehex == '@' || $typehex == '%') $hex = substr($buffer,13,-13); |
| 29 | - elseif ($typehex == '@' || $typehex == '%') $hex = substr($buffer,13,-1); |
|
| 30 | - else $hex = substr($buffer,1,-1); |
|
| 31 | - $bin = gmp_strval( gmp_init($hex,16), 2); |
|
| 29 | + elseif ($typehex == '@' || $typehex == '%') $hex = substr($buffer, 13, -1); |
|
| 30 | + else $hex = substr($buffer, 1, -1); |
|
| 31 | + $bin = gmp_strval(gmp_init($hex, 16), 2); |
|
| 32 | 32 | //if (strlen($hex) == 28 && $this->parityCheck($hex,$bin)) { |
| 33 | 33 | //if (strlen($hex) == 28) { |
| 34 | 34 | //echo strlen($hex); |
| 35 | 35 | //echo $hex; |
| 36 | 36 | if (strlen($hex) == 28 || strlen($hex) == 16) { |
| 37 | - $df = intval(substr($bin,0,5),2); |
|
| 37 | + $df = intval(substr($bin, 0, 5), 2); |
|
| 38 | 38 | //$ca = intval(substr($bin,5,3),2); |
| 39 | 39 | // Only support DF17 for now |
| 40 | 40 | //if ($df == 17 || ($df == 18 && ($ca == 0 || $ca == 1 || $ca == 6))) { |
| 41 | - if (($df == 17 || $df == 18) && ($this->parityCheck($hex,$bin) || $typehex == '@')) { |
|
| 42 | - $icao = substr($hex,2,6); |
|
| 41 | + if (($df == 17 || $df == 18) && ($this->parityCheck($hex, $bin) || $typehex == '@')) { |
|
| 42 | + $icao = substr($hex, 2, 6); |
|
| 43 | 43 | $data['hex'] = $icao; |
| 44 | - $tc = intval(substr($bin,32,5),2); |
|
| 44 | + $tc = intval(substr($bin, 32, 5), 2); |
|
| 45 | 45 | if ($tc >= 1 && $tc <= 4) { |
| 46 | 46 | // Category: |
| 47 | 47 | // 1 = light aircraft <= 7000 kg |
@@ -64,67 +64,67 @@ discard block |
||
| 64 | 64 | // 22 = fixed ground or tethered obstruction |
| 65 | 65 | // 23 to 24 = reserved |
| 66 | 66 | //$data['category'] = intval(substr($bin,5,3),2); |
| 67 | - $data['category'] = intval(substr($bin,37,3),2); |
|
| 67 | + $data['category'] = intval(substr($bin, 37, 3), 2); |
|
| 68 | 68 | //callsign |
| 69 | - $csbin = substr($bin,40,56); |
|
| 69 | + $csbin = substr($bin, 40, 56); |
|
| 70 | 70 | $charset = str_split('#ABCDEFGHIJKLMNOPQRSTUVWXYZ#####_###############0123456789######'); |
| 71 | 71 | $cs = ''; |
| 72 | - $cs .= $charset[intval(substr($csbin,0,6),2)]; |
|
| 73 | - $cs .= $charset[intval(substr($csbin,6,6),2)]; |
|
| 74 | - $cs .= $charset[intval(substr($csbin,12,6),2)]; |
|
| 75 | - $cs .= $charset[intval(substr($csbin,18,6),2)]; |
|
| 76 | - $cs .= $charset[intval(substr($csbin,24,6),2)]; |
|
| 77 | - $cs .= $charset[intval(substr($csbin,30,6),2)]; |
|
| 78 | - $cs .= $charset[intval(substr($csbin,36,6),2)]; |
|
| 79 | - $cs .= $charset[intval(substr($csbin,42,6),2)]; |
|
| 80 | - $cs = str_replace('_','',$cs); |
|
| 81 | - $cs = str_replace('#','',$cs); |
|
| 72 | + $cs .= $charset[intval(substr($csbin, 0, 6), 2)]; |
|
| 73 | + $cs .= $charset[intval(substr($csbin, 6, 6), 2)]; |
|
| 74 | + $cs .= $charset[intval(substr($csbin, 12, 6), 2)]; |
|
| 75 | + $cs .= $charset[intval(substr($csbin, 18, 6), 2)]; |
|
| 76 | + $cs .= $charset[intval(substr($csbin, 24, 6), 2)]; |
|
| 77 | + $cs .= $charset[intval(substr($csbin, 30, 6), 2)]; |
|
| 78 | + $cs .= $charset[intval(substr($csbin, 36, 6), 2)]; |
|
| 79 | + $cs .= $charset[intval(substr($csbin, 42, 6), 2)]; |
|
| 80 | + $cs = str_replace('_', '', $cs); |
|
| 81 | + $cs = str_replace('#', '', $cs); |
|
| 82 | 82 | $callsign = $cs; |
| 83 | 83 | $data['ident'] = $callsign; |
| 84 | 84 | } elseif ($tc >= 9 && $tc <= 18) { |
| 85 | 85 | // Check Q-bit |
| 86 | - $q = substr($bin,47,1); |
|
| 86 | + $q = substr($bin, 47, 1); |
|
| 87 | 87 | if ($q) { |
| 88 | - $n = intval(substr($bin,40,7).substr($bin,48,4),2); |
|
| 89 | - $alt = $n*25-1000; |
|
| 88 | + $n = intval(substr($bin, 40, 7).substr($bin, 48, 4), 2); |
|
| 89 | + $alt = $n*25 - 1000; |
|
| 90 | 90 | $data['altitude'] = $alt; |
| 91 | 91 | } |
| 92 | 92 | // Check odd/even flag |
| 93 | - $oe = substr($bin,53,1); |
|
| 93 | + $oe = substr($bin, 53, 1); |
|
| 94 | 94 | //if ($oe) => odd else even |
| 95 | 95 | // 131072 is 2^17 since CPR latitude and longitude are encoded in 17 bits. |
| 96 | - $cprlat = intval(substr($bin,54,17),2)/131072.0; |
|
| 97 | - $cprlon = intval(substr($bin,71,17),2)/131072.0; |
|
| 98 | - if ($oe == 0) $this::$latlon[$icao] = array('latitude' => $cprlat,'longitude' => $cprlon,'created' => time()); |
|
| 96 | + $cprlat = intval(substr($bin, 54, 17), 2)/131072.0; |
|
| 97 | + $cprlon = intval(substr($bin, 71, 17), 2)/131072.0; |
|
| 98 | + if ($oe == 0) $this::$latlon[$icao] = array('latitude' => $cprlat, 'longitude' => $cprlon, 'created' => time()); |
|
| 99 | 99 | elseif (isset($this::$latlon[$icao]) && (time() - $this::$latlon[$icao]['created']) < 10) { |
| 100 | 100 | $cprlat_odd = $cprlat; |
| 101 | 101 | $cprlon_odd = $cprlon; |
| 102 | 102 | $cprlat_even = $this::$latlon[$icao]['latitude']; |
| 103 | 103 | $cprlon_even = $this::$latlon[$icao]['longitude']; |
| 104 | - $j = 59*$cprlat_even-60*$cprlat_odd+0.5; |
|
| 105 | - $lat_even = (360.0/60)*($j%60+$cprlat_even); |
|
| 106 | - $lat_odd = (360.0/59)*($j%59+$cprlat_odd); |
|
| 104 | + $j = 59*$cprlat_even - 60*$cprlat_odd + 0.5; |
|
| 105 | + $lat_even = (360.0/60)*($j%60 + $cprlat_even); |
|
| 106 | + $lat_odd = (360.0/59)*($j%59 + $cprlat_odd); |
|
| 107 | 107 | if ($lat_even >= 270) $lat_even = $lat_even - 360; |
| 108 | 108 | if ($lat_odd >= 270) $lat_odd = $lat_odd - 360; |
| 109 | 109 | // check latitude zone |
| 110 | 110 | if ($this->cprNL($lat_even) == $this->cprNL($lat_odd)) { |
| 111 | 111 | if ($this::$latlon[$icao]['created'] > time()) { |
| 112 | - $ni = $this->cprN($lat_even,0); |
|
| 113 | - $m = floor($cprlon_even*($this->cprNL($lat_even)-1) - $cprlon_odd * $this->cprNL($lat_even)+0.5); |
|
| 114 | - $lon = (360.0/$ni)*($m%$ni+$cprlon_even); |
|
| 112 | + $ni = $this->cprN($lat_even, 0); |
|
| 113 | + $m = floor($cprlon_even*($this->cprNL($lat_even) - 1) - $cprlon_odd*$this->cprNL($lat_even) + 0.5); |
|
| 114 | + $lon = (360.0/$ni)*($m%$ni + $cprlon_even); |
|
| 115 | 115 | $lat = $lat_even; |
| 116 | - if ($lon > 180) $lon = $lon -360; |
|
| 116 | + if ($lon > 180) $lon = $lon - 360; |
|
| 117 | 117 | if ($lat > -91 && $lat < 91 && $lon > -181 && $lon < 181) { |
| 118 | 118 | //if ($globalDebug) echo 'cs : '.$cs.' - hex : '.$hex.' - lat : '.$lat.' - lon : '.$lon; |
| 119 | 119 | $data['latitude'] = $lat; |
| 120 | 120 | $data['longitude'] = $lon; |
| 121 | 121 | } |
| 122 | 122 | } else { |
| 123 | - $ni = $this->cprN($lat_odd,1); |
|
| 124 | - $m = floor($cprlon_even*($this->cprNL($lat_odd)-1) - $cprlon_odd * $this->cprNL($lat_odd)+0.5); |
|
| 125 | - $lon = (360.0/$ni)*($m%$ni+$cprlon_odd); |
|
| 123 | + $ni = $this->cprN($lat_odd, 1); |
|
| 124 | + $m = floor($cprlon_even*($this->cprNL($lat_odd) - 1) - $cprlon_odd*$this->cprNL($lat_odd) + 0.5); |
|
| 125 | + $lon = (360.0/$ni)*($m%$ni + $cprlon_odd); |
|
| 126 | 126 | $lat = $lat_odd; |
| 127 | - if ($lon > 180) $lon = $lon -360; |
|
| 127 | + if ($lon > 180) $lon = $lon - 360; |
|
| 128 | 128 | if ($lat > -91 && $lat < 91 && $lon > -181 && $lon < 181) { |
| 129 | 129 | //if ($globalDebug) echo 'icao : '.$icao.' - hex : '.$hex.' - lat : '.$lat.' - lon : '.$lon.' second'."\n"; |
| 130 | 130 | $data['latitude'] = $lat; |
@@ -136,15 +136,15 @@ discard block |
||
| 136 | 136 | } |
| 137 | 137 | } elseif ($tc == 19) { |
| 138 | 138 | // speed & heading |
| 139 | - $v_ew_dir = intval(substr($bin,45,1)); |
|
| 140 | - $v_ew = intval(substr($bin,46,10),2); |
|
| 141 | - $v_ns_dir = intval(substr($bin,56,1)); |
|
| 142 | - $v_ns = intval(substr($bin,57,10),2); |
|
| 139 | + $v_ew_dir = intval(substr($bin, 45, 1)); |
|
| 140 | + $v_ew = intval(substr($bin, 46, 10), 2); |
|
| 141 | + $v_ns_dir = intval(substr($bin, 56, 1)); |
|
| 142 | + $v_ns = intval(substr($bin, 57, 10), 2); |
|
| 143 | 143 | if ($v_ew_dir) $v_ew = -1*$v_ew; |
| 144 | 144 | if ($v_ns_dir) $v_ns = -1*$v_ns; |
| 145 | - $speed = sqrt($v_ns*$v_ns+$v_ew*$v_ew); |
|
| 146 | - $heading = atan2($v_ew,$v_ns)*360.0/(2*pi()); |
|
| 147 | - if ($heading <0) $heading = $heading+360; |
|
| 145 | + $speed = sqrt($v_ns*$v_ns + $v_ew*$v_ew); |
|
| 146 | + $heading = atan2($v_ew, $v_ns)*360.0/(2*pi()); |
|
| 147 | + if ($heading < 0) $heading = $heading + 360; |
|
| 148 | 148 | $data['speed'] = $speed; |
| 149 | 149 | $data['heading'] = $heading; |
| 150 | 150 | } |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | * Lookup table to convert the latitude to index. |
| 160 | 160 | */ |
| 161 | 161 | private function cprNL($lat) { |
| 162 | - if ($lat < 0) $lat = -$lat; // Table is simmetric about the equator. |
|
| 162 | + if ($lat < 0) $lat = -$lat; // Table is simmetric about the equator. |
|
| 163 | 163 | if ($lat < 10.47047130) return 59; |
| 164 | 164 | if ($lat < 14.82817437) return 58; |
| 165 | 165 | if ($lat < 18.18626357) return 57; |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | return 1; |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | - private function cprN($lat,$isodd) { |
|
| 224 | + private function cprN($lat, $isodd) { |
|
| 225 | 225 | $nl = $this->cprNL($lat) - $isodd; |
| 226 | 226 | if ($nl > 1) return $nl; |
| 227 | 227 | else return 1; |
@@ -245,9 +245,9 @@ discard block |
||
| 245 | 245 | 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000 |
| 246 | 246 | ); |
| 247 | 247 | $crc = 0; |
| 248 | - $checksum = intval(substr($msg,22,6),16); |
|
| 248 | + $checksum = intval(substr($msg, 22, 6), 16); |
|
| 249 | 249 | for ($j = 0; $j < strlen($bin); $j++) { |
| 250 | - if ($bin[$j]) $crc = $crc^intval($modes_checksum_table[$j],0); |
|
| 250 | + if ($bin[$j]) $crc = $crc^intval($modes_checksum_table[$j], 0); |
|
| 251 | 251 | } |
| 252 | 252 | if ($crc == $checksum) return true; |
| 253 | 253 | else { |
@@ -269,10 +269,10 @@ discard block |
||
| 269 | 269 | $msg['aircraftid'] = hexdec($data['address']); |
| 270 | 270 | $msg['hex'] = $data['address']; |
| 271 | 271 | $msg['flightid'] = hexdec($data['address']); |
| 272 | - $msg['date_gen'] = date('Y/m/d',$data['timestamp']); |
|
| 273 | - $msg['time_gen'] = date('H:i:s',$data['timestamp']).'.180'; |
|
| 274 | - $msg['date_log'] = date('Y/m/d',$data['timestamp']); |
|
| 275 | - $msg['time_log'] = date('H:i:s',$data['timestamp']).'.180'; |
|
| 272 | + $msg['date_gen'] = date('Y/m/d', $data['timestamp']); |
|
| 273 | + $msg['time_gen'] = date('H:i:s', $data['timestamp']).'.180'; |
|
| 274 | + $msg['date_log'] = date('Y/m/d', $data['timestamp']); |
|
| 275 | + $msg['time_log'] = date('H:i:s', $data['timestamp']).'.180'; |
|
| 276 | 276 | $msg['callsign'] = $data['ident']; |
| 277 | 277 | $msg['altitude'] = ''; |
| 278 | 278 | $msg['speed'] = ''; |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | $msg['emergency'] = ''; |
| 286 | 286 | $msg['SPI'] = ''; |
| 287 | 287 | $msg['ground'] = ''; |
| 288 | - $result[] = implode(',',$msg); |
|
| 288 | + $result[] = implode(',', $msg); |
|
| 289 | 289 | } |
| 290 | 290 | if (isset($data['latitude']) && $data['latitude'] != 0) { |
| 291 | 291 | $msg = array(); |
@@ -295,10 +295,10 @@ discard block |
||
| 295 | 295 | $msg['aircraftid'] = hexdec($data['address']); |
| 296 | 296 | $msg['hex'] = $data['address']; |
| 297 | 297 | $msg['flightid'] = hexdec($data['address']); |
| 298 | - $msg['date_gen'] = date('Y/m/d',$data['timestamp']); |
|
| 299 | - $msg['time_gen'] = date('H:i:s',$data['timestamp']).'.180'; |
|
| 300 | - $msg['date_log'] = date('Y/m/d',$data['timestamp']); |
|
| 301 | - $msg['time_log'] = date('H:i:s',$data['timestamp']).'.180'; |
|
| 298 | + $msg['date_gen'] = date('Y/m/d', $data['timestamp']); |
|
| 299 | + $msg['time_gen'] = date('H:i:s', $data['timestamp']).'.180'; |
|
| 300 | + $msg['date_log'] = date('Y/m/d', $data['timestamp']); |
|
| 301 | + $msg['time_log'] = date('H:i:s', $data['timestamp']).'.180'; |
|
| 302 | 302 | $msg['callsign'] = ''; |
| 303 | 303 | if (isset($data['altitude'])) $msg['altitude'] = $data['altitude']; |
| 304 | 304 | else $msg['altitude'] = ''; |
@@ -316,7 +316,7 @@ discard block |
||
| 316 | 316 | $msg['SPI'] = 0; |
| 317 | 317 | if (isset($data['ground'])) $msg['ground'] = 1; |
| 318 | 318 | else $msg['ground'] = 0; |
| 319 | - $result[] = implode(',',$msg); |
|
| 319 | + $result[] = implode(',', $msg); |
|
| 320 | 320 | } |
| 321 | 321 | return $result; |
| 322 | 322 | } |