@@ -117,31 +117,31 @@ discard block |
||
| 117 | 117 | |
| 118 | 118 | private function make_latf($temp) { // unsigned long |
| 119 | 119 | $flat = 0.0; // float |
| 120 | - $temp = $temp & 0x07FFFFFF; |
|
| 121 | - if ($temp & 0x04000000) { |
|
| 122 | - $temp = $temp ^ 0x07FFFFFF; |
|
| 120 | + $temp = $temp&0x07FFFFFF; |
|
| 121 | + if ($temp&0x04000000) { |
|
| 122 | + $temp = $temp^0x07FFFFFF; |
|
| 123 | 123 | $temp += 1; |
| 124 | - $flat = (float)($temp / (60.0 * 10000.0)); |
|
| 124 | + $flat = (float) ($temp/(60.0*10000.0)); |
|
| 125 | 125 | $flat *= -1.0; |
| 126 | - } else $flat = (float)($temp / (60.0 * 10000.0)); |
|
| 126 | + } else $flat = (float) ($temp/(60.0*10000.0)); |
|
| 127 | 127 | return $flat; // float |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | private function make_lonf($temp) { // unsigned long |
| 131 | 131 | $flon = 0.0; // float |
| 132 | - $temp = $temp & 0x0FFFFFFF; |
|
| 133 | - if ($temp & 0x08000000) { |
|
| 134 | - $temp = $temp ^ 0x0FFFFFFF; |
|
| 132 | + $temp = $temp&0x0FFFFFFF; |
|
| 133 | + if ($temp&0x08000000) { |
|
| 134 | + $temp = $temp^0x0FFFFFFF; |
|
| 135 | 135 | $temp += 1; |
| 136 | - $flon = (float)($temp / (60.0 * 10000.0)); |
|
| 136 | + $flon = (float) ($temp/(60.0*10000.0)); |
|
| 137 | 137 | $flon *= -1.0; |
| 138 | - } else $flon = (float)($temp / (60.0 * 10000.0)); |
|
| 138 | + } else $flon = (float) ($temp/(60.0*10000.0)); |
|
| 139 | 139 | return $flon; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | private function ascii_2_dec($chr) { |
| 143 | - $dec=ord($chr);//get decimal ascii code |
|
| 144 | - $hex=dechex($dec);//convert decimal to hex |
|
| 143 | + $dec = ord($chr); //get decimal ascii code |
|
| 144 | + $hex = dechex($dec); //convert decimal to hex |
|
| 145 | 145 | return ($dec); |
| 146 | 146 | } |
| 147 | 147 | |
@@ -160,15 +160,15 @@ discard block |
||
| 160 | 160 | //only process in the following range: 48-87, 96-119 |
| 161 | 161 | if ($ascii < 48) { } |
| 162 | 162 | else { |
| 163 | - if($ascii>119) { } |
|
| 163 | + if ($ascii > 119) { } |
|
| 164 | 164 | else { |
| 165 | - if ($ascii>87 && $ascii<96) ; |
|
| 165 | + if ($ascii > 87 && $ascii < 96); |
|
| 166 | 166 | else { |
| 167 | - $ascii=$ascii+40; |
|
| 168 | - if ($ascii>128){ |
|
| 169 | - $ascii=$ascii+32; |
|
| 167 | + $ascii = $ascii + 40; |
|
| 168 | + if ($ascii > 128) { |
|
| 169 | + $ascii = $ascii + 32; |
|
| 170 | 170 | } else { |
| 171 | - $ascii=$ascii+40; |
|
| 171 | + $ascii = $ascii + 40; |
|
| 172 | 172 | } |
| 173 | 173 | } |
| 174 | 174 | } |
@@ -177,7 +177,7 @@ discard block |
||
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | private function dec_2_6bit($dec) { |
| 180 | - $bin=decbin($dec); |
|
| 180 | + $bin = decbin($dec); |
|
| 181 | 181 | return(substr($bin, -6)); |
| 182 | 182 | } |
| 183 | 183 | |
@@ -195,11 +195,11 @@ discard block |
||
| 195 | 195 | ); |
| 196 | 196 | // " |
| 197 | 197 | $rv = ''; |
| 198 | - if ($_size % 6 == 0) { |
|
| 199 | - $len = $_size / 6; |
|
| 200 | - for ($i=0; $i<$len; $i++) { |
|
| 201 | - $offset = $i * 6; |
|
| 202 | - $rv .= $ais_chars[ bindec(substr($_str,$_start + $offset,6)) ]; |
|
| 198 | + if ($_size%6 == 0) { |
|
| 199 | + $len = $_size/6; |
|
| 200 | + for ($i = 0; $i < $len; $i++) { |
|
| 201 | + $offset = $i*6; |
|
| 202 | + $rv .= $ais_chars[bindec(substr($_str, $_start + $offset, 6))]; |
|
| 203 | 203 | } |
| 204 | 204 | } |
| 205 | 205 | return $rv; |
@@ -226,93 +226,93 @@ discard block |
||
| 226 | 226 | $ro->eta_hour = ''; |
| 227 | 227 | $ro->eta_minute = ''; |
| 228 | 228 | $ro->ts = time(); |
| 229 | - $ro->id = bindec(substr($_aisdata,0,6)); |
|
| 230 | - $ro->mmsi = bindec(substr($_aisdata,8,30)); |
|
| 229 | + $ro->id = bindec(substr($_aisdata, 0, 6)); |
|
| 230 | + $ro->mmsi = bindec(substr($_aisdata, 8, 30)); |
|
| 231 | 231 | if ($ro->id >= 1 && $ro->id <= 3) { |
| 232 | - $ro->cog = bindec(substr($_aisdata,116,12))/10; |
|
| 233 | - $ro->sog = bindec(substr($_aisdata,50,10))/10; |
|
| 234 | - $ro->lon = $this->make_lonf(bindec(substr($_aisdata,61,28))); |
|
| 235 | - $ro->lat = $this->make_latf(bindec(substr($_aisdata,89,27))); |
|
| 232 | + $ro->cog = bindec(substr($_aisdata, 116, 12))/10; |
|
| 233 | + $ro->sog = bindec(substr($_aisdata, 50, 10))/10; |
|
| 234 | + $ro->lon = $this->make_lonf(bindec(substr($_aisdata, 61, 28))); |
|
| 235 | + $ro->lat = $this->make_latf(bindec(substr($_aisdata, 89, 27))); |
|
| 236 | 236 | $ro->cls = 1; // class A |
| 237 | 237 | } else if ($ro->id == 4) { |
| 238 | - $ro->lon = $this->make_lonf(bindec(substr($_aisdata,79,28))); |
|
| 239 | - $ro->lat = $this->make_latf(bindec(substr($_aisdata,107,27))); |
|
| 238 | + $ro->lon = $this->make_lonf(bindec(substr($_aisdata, 79, 28))); |
|
| 239 | + $ro->lat = $this->make_latf(bindec(substr($_aisdata, 107, 27))); |
|
| 240 | 240 | $ro->cls = 1; // class A |
| 241 | 241 | } else if ($ro->id == 5) { |
| 242 | - $ro->imo = bindec(substr($_aisdata,40,30)); |
|
| 243 | - $ro->callsign = $this->binchar($_aisdata,70,42); |
|
| 244 | - $ro->name = $this->binchar($_aisdata,112,120); |
|
| 245 | - $ro->typeid = bindec(substr($_aisdata,232,8)); |
|
| 242 | + $ro->imo = bindec(substr($_aisdata, 40, 30)); |
|
| 243 | + $ro->callsign = $this->binchar($_aisdata, 70, 42); |
|
| 244 | + $ro->name = $this->binchar($_aisdata, 112, 120); |
|
| 245 | + $ro->typeid = bindec(substr($_aisdata, 232, 8)); |
|
| 246 | 246 | $ro->type = $this->getShipType($ro->typeid); |
| 247 | 247 | //$ro->to_bow = bindec(substr($_aisdata,240,9)); |
| 248 | 248 | //$ro->to_stern = bindec(substr($_aisdata,249,9)); |
| 249 | 249 | //$ro->to_port = bindec(substr($_aisdata,258,6)); |
| 250 | 250 | //$ro->to_starboard = bindec(substr($_aisdata,264,6)); |
| 251 | - $ro->eta_month = bindec(substr($_aisdata,274,4)); |
|
| 252 | - $ro->eta_day = bindec(substr($_aisdata,278,5)); |
|
| 253 | - $ro->eta_hour = bindec(substr($_aisdata,283,5)); |
|
| 254 | - $ro->eta_minute = bindec(substr($_aisdata,288,6)); |
|
| 251 | + $ro->eta_month = bindec(substr($_aisdata, 274, 4)); |
|
| 252 | + $ro->eta_day = bindec(substr($_aisdata, 278, 5)); |
|
| 253 | + $ro->eta_hour = bindec(substr($_aisdata, 283, 5)); |
|
| 254 | + $ro->eta_minute = bindec(substr($_aisdata, 288, 6)); |
|
| 255 | 255 | //$ro->draught = bindec(substr($_aisdata,294,8)); |
| 256 | - $ro->destination = $this->binchar($_aisdata,302,120); |
|
| 256 | + $ro->destination = $this->binchar($_aisdata, 302, 120); |
|
| 257 | 257 | $ro->cls = 1; // class A |
| 258 | 258 | } else if ($ro->id == 9) { |
| 259 | 259 | // Search and Rescue aircraft position report |
| 260 | 260 | } else if ($ro->id == 18) { |
| 261 | - $ro->cog = bindec(substr($_aisdata,112,12))/10; |
|
| 262 | - $ro->sog = bindec(substr($_aisdata,46,10))/10; |
|
| 263 | - $ro->lon = $this->make_lonf(bindec(substr($_aisdata,57,28))); |
|
| 264 | - $ro->lat = $this->make_latf(bindec(substr($_aisdata,85,27))); |
|
| 265 | - $ro->heading = bindec(substr($_aisdata,124,9)); |
|
| 261 | + $ro->cog = bindec(substr($_aisdata, 112, 12))/10; |
|
| 262 | + $ro->sog = bindec(substr($_aisdata, 46, 10))/10; |
|
| 263 | + $ro->lon = $this->make_lonf(bindec(substr($_aisdata, 57, 28))); |
|
| 264 | + $ro->lat = $this->make_latf(bindec(substr($_aisdata, 85, 27))); |
|
| 265 | + $ro->heading = bindec(substr($_aisdata, 124, 9)); |
|
| 266 | 266 | if ($ro->heading == 511) $ro->heading = ''; |
| 267 | 267 | $ro->cls = 2; // class B |
| 268 | 268 | } else if ($ro->id == 19) { |
| 269 | - $ro->cog = bindec(substr($_aisdata,112,12))/10; |
|
| 270 | - $ro->sog = bindec(substr($_aisdata,46,10))/10; |
|
| 271 | - $ro->lon = $this->make_lonf(bindec(substr($_aisdata,61,28))); |
|
| 272 | - $ro->lat = $this->make_latf(bindec(substr($_aisdata,89,27))); |
|
| 273 | - $ro->name = $this->binchar($_aisdata,143,120); |
|
| 269 | + $ro->cog = bindec(substr($_aisdata, 112, 12))/10; |
|
| 270 | + $ro->sog = bindec(substr($_aisdata, 46, 10))/10; |
|
| 271 | + $ro->lon = $this->make_lonf(bindec(substr($_aisdata, 61, 28))); |
|
| 272 | + $ro->lat = $this->make_latf(bindec(substr($_aisdata, 89, 27))); |
|
| 273 | + $ro->name = $this->binchar($_aisdata, 143, 120); |
|
| 274 | 274 | $ro->cls = 2; // class B |
| 275 | - $ro->heading = bindec(substr($_aisdata,124,9)); |
|
| 275 | + $ro->heading = bindec(substr($_aisdata, 124, 9)); |
|
| 276 | 276 | if ($ro->heading == 511) $ro->heading = ''; |
| 277 | - $ro->typeid = bindec(substr($_aisdata,263,8)); |
|
| 277 | + $ro->typeid = bindec(substr($_aisdata, 263, 8)); |
|
| 278 | 278 | $ro->type = $this->getShipType($ro->typeid); |
| 279 | 279 | //$ro->to_bow = bindec(substr($_aisdata,271,9)); |
| 280 | 280 | //$ro->to_stern = bindec(substr($_aisdata,280,9)); |
| 281 | 281 | //$ro->to_port = bindec(substr($_aisdata,289,6)); |
| 282 | 282 | //$ro->to_starboard = bindec(substr($_aisdata,295,6)); |
| 283 | 283 | } else if ($ro->id == 21) { |
| 284 | - $ro->lon = $this->make_lonf(bindec(substr($_aisdata,164,28))); |
|
| 285 | - $ro->lat = $this->make_latf(bindec(substr($_aisdata,192,27))); |
|
| 286 | - $ro->name = $this->binchar($_aisdata,43,120); |
|
| 284 | + $ro->lon = $this->make_lonf(bindec(substr($_aisdata, 164, 28))); |
|
| 285 | + $ro->lat = $this->make_latf(bindec(substr($_aisdata, 192, 27))); |
|
| 286 | + $ro->name = $this->binchar($_aisdata, 43, 120); |
|
| 287 | 287 | //$ro->to_bow = bindec(substr($_aisdata,219,9)); |
| 288 | 288 | //$ro->to_stern = bindec(substr($_aisdata,228,9)); |
| 289 | 289 | //$ro->to_port = bindec(substr($_aisdata,237,6)); |
| 290 | 290 | //$ro->to_starboard = bindec(substr($_aisdata,243,6)); |
| 291 | 291 | $ro->cls = 2; // class B |
| 292 | 292 | } else if ($ro->id == 24) { |
| 293 | - $pn = bindec(substr($_aisdata,38,2)); |
|
| 293 | + $pn = bindec(substr($_aisdata, 38, 2)); |
|
| 294 | 294 | if ($pn == 0) { |
| 295 | - $ro->name = $this->binchar($_aisdata,40,120); |
|
| 295 | + $ro->name = $this->binchar($_aisdata, 40, 120); |
|
| 296 | 296 | } |
| 297 | - $ro->typeid = bindec(substr($_aisdata,40,8)); |
|
| 297 | + $ro->typeid = bindec(substr($_aisdata, 40, 8)); |
|
| 298 | 298 | $ro->type = $this->getShipType($ro->typeid); |
| 299 | - $ro->callsign = $this->binchar($_aisdata,90,42); |
|
| 299 | + $ro->callsign = $this->binchar($_aisdata, 90, 42); |
|
| 300 | 300 | //$ro->to_bow = bindec(substr($_aisdata,132,9)); |
| 301 | 301 | //$ro->to_stern = bindec(substr($_aisdata,141,9)); |
| 302 | 302 | //$ro->to_port = bindec(substr($_aisdata,150,6)); |
| 303 | 303 | //$ro->to_starboard = bindec(substr($_aisdata,156,6)); |
| 304 | 304 | $ro->cls = 2; // class B |
| 305 | 305 | } else if ($ro->id == 27) { |
| 306 | - $ro->cog = bindec(substr($_aisdata,85,9)); |
|
| 306 | + $ro->cog = bindec(substr($_aisdata, 85, 9)); |
|
| 307 | 307 | if ($ro->cog == 511) $ro->cog = 0.0; |
| 308 | - $ro->sog = bindec(substr($_aisdata,79,6)); |
|
| 308 | + $ro->sog = bindec(substr($_aisdata, 79, 6)); |
|
| 309 | 309 | if ($ro->sog == 63) $ro->sog = 0.0; |
| 310 | - $ro->lon = $this->make_lonf(bindec(substr($_aisdata,44,18))*10); |
|
| 311 | - $ro->lat = $this->make_latf(bindec(substr($_aisdata,62,17))*10); |
|
| 310 | + $ro->lon = $this->make_lonf(bindec(substr($_aisdata, 44, 18))*10); |
|
| 311 | + $ro->lat = $this->make_latf(bindec(substr($_aisdata, 62, 17))*10); |
|
| 312 | 312 | $ro->cls = 1; // class A |
| 313 | 313 | |
| 314 | 314 | } |
| 315 | - $ro->statusid = bindec(substr($_aisdata,38,4)); |
|
| 315 | + $ro->statusid = bindec(substr($_aisdata, 38, 4)); |
|
| 316 | 316 | $ro->status = $this->getStatus($ro->statusid); |
| 317 | 317 | return $ro; |
| 318 | 318 | } |
@@ -359,7 +359,7 @@ discard block |
||
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | public function getShipTypeID($type) { |
| 362 | - $typeid = array_search($type,$this->shiptype); |
|
| 362 | + $typeid = array_search($type, $this->shiptype); |
|
| 363 | 363 | if ($typeid !== FALSE) return $typeid; |
| 364 | 364 | elseif ($type == 'Cargo' || $type == 'General Cargo' || $type == 'Bulk Carrier' || $type == 'Container Ship' || $type == 'Cargo A' || $type == 'Reefer' || $type == 'Vehicles Carrier') return 70; |
| 365 | 365 | elseif ($type == 'Passengers Ship' || $type == 'Passenger' || $type == 'Ro-Ro/Passenger Ship' || $type == 'Motor Passenger') return 60; |
@@ -380,14 +380,14 @@ discard block |
||
| 380 | 380 | public function process_ais_itu($_itu, $_len, $_filler, $aux /*, $ais_ch*/) { |
| 381 | 381 | global $port; // tcpip port... |
| 382 | 382 | static $debug_counter = 0; |
| 383 | - $aisdata168='';//six bit array of ascii characters |
|
| 383 | + $aisdata168 = ''; //six bit array of ascii characters |
|
| 384 | 384 | $ais_nmea_array = str_split($_itu); // convert to an array |
| 385 | 385 | foreach ($ais_nmea_array as $value) { |
| 386 | 386 | $dec = $this->ascii_2_dec($value); |
| 387 | 387 | $bit8 = $this->asciidec_2_8bit($dec); |
| 388 | 388 | $bit6 = $this->dec_2_6bit($bit8); |
| 389 | 389 | //echo $value ."-" .$bit6 .""; |
| 390 | - $aisdata168 .=$bit6; |
|
| 390 | + $aisdata168 .= $bit6; |
|
| 391 | 391 | } |
| 392 | 392 | //echo $aisdata168 . "<br/>"; |
| 393 | 393 | //return $this->decode_ais($aisdata168, $aux); |
@@ -410,24 +410,24 @@ discard block |
||
| 410 | 410 | // calculate checksum after ! till * |
| 411 | 411 | // assume 1st ! is valid |
| 412 | 412 | // find * ensure that it is at correct position |
| 413 | - $end = strrpos ( $rawdata , '*' ); |
|
| 413 | + $end = strrpos($rawdata, '*'); |
|
| 414 | 414 | if ($end === FALSE) return -1; // check for NULLS!!! |
| 415 | - $cs = substr( $rawdata, $end + 1 ); |
|
| 416 | - if ( strlen($cs) != 2 ) return -1; // correct cs length |
|
| 417 | - $dcs = (int)hexdec( $cs ); |
|
| 418 | - for ( $alias=1; $alias<$end; $alias++) $chksum ^= ord( $rawdata[$alias] ); // perform XOR for NMEA checksum |
|
| 419 | - if ( $chksum == $dcs ) { // NMEA checksum pass |
|
| 415 | + $cs = substr($rawdata, $end + 1); |
|
| 416 | + if (strlen($cs) != 2) return -1; // correct cs length |
|
| 417 | + $dcs = (int) hexdec($cs); |
|
| 418 | + for ($alias = 1; $alias < $end; $alias++) $chksum ^= ord($rawdata[$alias]); // perform XOR for NMEA checksum |
|
| 419 | + if ($chksum == $dcs) { // NMEA checksum pass |
|
| 420 | 420 | $pcs = explode(',', $rawdata); |
| 421 | 421 | // !AI??? identifier |
| 422 | 422 | if (!isset($pcs[1])) { |
| 423 | 423 | echo "ERROR,INVALID_DATA ".time()." $rawdata\n"; |
| 424 | 424 | return -1; |
| 425 | 425 | } |
| 426 | - $num_seq = (int)$pcs[1]; // number of sequences |
|
| 427 | - $seq = (int)$pcs[2]; // get sequence |
|
| 426 | + $num_seq = (int) $pcs[1]; // number of sequences |
|
| 427 | + $seq = (int) $pcs[2]; // get sequence |
|
| 428 | 428 | // get msg sequence id |
| 429 | 429 | if ($pcs[3] == '') $msg_sid = -1; // non-multipart message, set to -1 |
| 430 | - else $msg_sid = (int)$pcs[3]; // multipart message |
|
| 430 | + else $msg_sid = (int) $pcs[3]; // multipart message |
|
| 431 | 431 | $ais_ch = $pcs[4]; // get AIS channel |
| 432 | 432 | // message sequence checking |
| 433 | 433 | if ($num_seq < 1 || $num_seq > 9) { |
@@ -460,7 +460,7 @@ discard block |
||
| 460 | 460 | } |
| 461 | 461 | } |
| 462 | 462 | $itu = $itu.$pcs[5]; // get itu message |
| 463 | - $filler += (int)$pcs[6][0]; // get filler |
|
| 463 | + $filler += (int) $pcs[6][0]; // get filler |
|
| 464 | 464 | if ($num_seq == 1 // valid single message |
| 465 | 465 | || $num_seq == $pseq // valid multi-part message |
| 466 | 466 | ) { |
@@ -482,13 +482,13 @@ discard block |
||
| 482 | 482 | $cbuf = $cbuf.$ibuf; |
| 483 | 483 | $last_pos = 0; |
| 484 | 484 | $result = new stdClass(); |
| 485 | - while ( ($start = strpos($cbuf,"VDM",$last_pos)) !== FALSE) { |
|
| 485 | + while (($start = strpos($cbuf, "VDM", $last_pos)) !== FALSE) { |
|
| 486 | 486 | //while ( ($start = strpos($cbuf,"!AI",$last_pos)) !== FALSE) { |
| 487 | 487 | //DEBUG echo $cbuf; |
| 488 | - if ( ($end = strpos($cbuf,"\r\n", $start)) !== FALSE) { //TBD need to trim? |
|
| 488 | + if (($end = strpos($cbuf, "\r\n", $start)) !== FALSE) { //TBD need to trim? |
|
| 489 | 489 | $tst = substr($cbuf, $start - 3, ($end - $start + 3)); |
| 490 | 490 | //DEBUG echo "[$start $end $tst]\n"; |
| 491 | - $result = $this->process_ais_raw( $tst, "" ); |
|
| 491 | + $result = $this->process_ais_raw($tst, ""); |
|
| 492 | 492 | $last_pos = $end + 1; |
| 493 | 493 | } else break; |
| 494 | 494 | } |
@@ -500,41 +500,41 @@ discard block |
||
| 500 | 500 | // incoming data from serial or IP comms |
| 501 | 501 | public function process_ais_line($cbuf) { |
| 502 | 502 | $result = new stdClass(); |
| 503 | - $start = strpos($cbuf,"VDM"); |
|
| 503 | + $start = strpos($cbuf, "VDM"); |
|
| 504 | 504 | $tst = substr($cbuf, $start - 3); |
| 505 | - $result = $this->process_ais_raw( $tst, "" ); |
|
| 505 | + $result = $this->process_ais_raw($tst, ""); |
|
| 506 | 506 | return $result; |
| 507 | 507 | } |
| 508 | 508 | |
| 509 | 509 | /* AIS Encoding |
| 510 | 510 | */ |
| 511 | - private function mk_ais_lat( $lat ) { |
|
| 511 | + private function mk_ais_lat($lat) { |
|
| 512 | 512 | //$lat = 1.2569; |
| 513 | - if ($lat<0.0) { |
|
| 513 | + if ($lat < 0.0) { |
|
| 514 | 514 | $lat = -$lat; |
| 515 | - $neg=true; |
|
| 516 | - } else $neg=false; |
|
| 515 | + $neg = true; |
|
| 516 | + } else $neg = false; |
|
| 517 | 517 | $latd = 0x00000000; |
| 518 | - $latd = intval ($lat * 600000.0); |
|
| 519 | - if ($neg==true) { |
|
| 518 | + $latd = intval($lat*600000.0); |
|
| 519 | + if ($neg == true) { |
|
| 520 | 520 | $latd = ~$latd; |
| 521 | - $latd+=1; |
|
| 521 | + $latd += 1; |
|
| 522 | 522 | $latd &= 0x07FFFFFF; |
| 523 | 523 | } |
| 524 | 524 | return $latd; |
| 525 | 525 | } |
| 526 | 526 | |
| 527 | - private function mk_ais_lon( $lon ) { |
|
| 527 | + private function mk_ais_lon($lon) { |
|
| 528 | 528 | //$lon = 103.851; |
| 529 | - if ($lon<0.0) { |
|
| 529 | + if ($lon < 0.0) { |
|
| 530 | 530 | $lon = -$lon; |
| 531 | - $neg=true; |
|
| 532 | - } else $neg=false; |
|
| 531 | + $neg = true; |
|
| 532 | + } else $neg = false; |
|
| 533 | 533 | $lond = 0x00000000; |
| 534 | - $lond = intval ($lon * 600000.0); |
|
| 535 | - if ($neg==true) { |
|
| 534 | + $lond = intval($lon*600000.0); |
|
| 535 | + if ($neg == true) { |
|
| 536 | 536 | $lond = ~$lond; |
| 537 | - $lond+=1; |
|
| 537 | + $lond += 1; |
|
| 538 | 538 | $lond &= 0x0FFFFFFF; |
| 539 | 539 | } |
| 540 | 540 | return $lond; |
@@ -542,8 +542,8 @@ discard block |
||
| 542 | 542 | |
| 543 | 543 | private function char2bin($name, $max_len) { |
| 544 | 544 | $len = strlen($name); |
| 545 | - if ($len > $max_len) $name = substr($name,0,$max_len); |
|
| 546 | - if ($len < $max_len) $pad = str_repeat('0', ($max_len - $len) * 6); |
|
| 545 | + if ($len > $max_len) $name = substr($name, 0, $max_len); |
|
| 546 | + if ($len < $max_len) $pad = str_repeat('0', ($max_len - $len)*6); |
|
| 547 | 547 | else $pad = ''; |
| 548 | 548 | $rv = ''; |
| 549 | 549 | $ais_chars = array( |
@@ -560,26 +560,26 @@ discard block |
||
| 560 | 560 | if ($_a) foreach ($_a as $_1) { |
| 561 | 561 | if (isset($ais_chars[$_1])) $dec = $ais_chars[$_1]; |
| 562 | 562 | else $dec = 0; |
| 563 | - $bin = str_pad(decbin( $dec ), 6, '0', STR_PAD_LEFT); |
|
| 563 | + $bin = str_pad(decbin($dec), 6, '0', STR_PAD_LEFT); |
|
| 564 | 564 | $rv .= $bin; |
| 565 | 565 | //echo "$_1 $dec ($bin)<br/>"; |
| 566 | 566 | } |
| 567 | 567 | return $rv.$pad; |
| 568 | 568 | } |
| 569 | 569 | |
| 570 | - private function mk_ais($_enc, $_part=1,$_total=1,$_seq='',$_ch='A') { |
|
| 570 | + private function mk_ais($_enc, $_part = 1, $_total = 1, $_seq = '', $_ch = 'A') { |
|
| 571 | 571 | $len_bit = strlen($_enc); |
| 572 | - $rem6 = $len_bit % 6; |
|
| 572 | + $rem6 = $len_bit%6; |
|
| 573 | 573 | $pad6_len = 0; |
| 574 | 574 | if ($rem6) $pad6_len = 6 - $rem6; |
| 575 | 575 | //echo $pad6_len.'<br>'; |
| 576 | 576 | $_enc .= str_repeat("0", $pad6_len); // pad the text... |
| 577 | - $len_enc = strlen($_enc) / 6; |
|
| 577 | + $len_enc = strlen($_enc)/6; |
|
| 578 | 578 | //echo $_enc.' '.$len_enc.'<br/>'; |
| 579 | 579 | $itu = ''; |
| 580 | - for ($i=0; $i<$len_enc; $i++) { |
|
| 581 | - $offset = $i * 6; |
|
| 582 | - $dec = bindec(substr($_enc,$offset,6)); |
|
| 580 | + for ($i = 0; $i < $len_enc; $i++) { |
|
| 581 | + $offset = $i*6; |
|
| 582 | + $dec = bindec(substr($_enc, $offset, 6)); |
|
| 583 | 583 | if ($dec < 40) $dec += 48; |
| 584 | 584 | else $dec += 56; |
| 585 | 585 | //echo chr($dec)." $dec<br/>"; |
@@ -589,15 +589,15 @@ discard block |
||
| 589 | 589 | $chksum = 0; |
| 590 | 590 | $itu = "AIVDM,$_part,$_total,$_seq,$_ch,".$itu.",0"; |
| 591 | 591 | $len_itu = strlen($itu); |
| 592 | - for ($i=0; $i<$len_itu; $i++) { |
|
| 593 | - $chksum ^= ord( $itu[$i] ); |
|
| 592 | + for ($i = 0; $i < $len_itu; $i++) { |
|
| 593 | + $chksum ^= ord($itu[$i]); |
|
| 594 | 594 | } |
| 595 | - $hex_arr = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'); |
|
| 596 | - $lsb = $chksum & 0x0F; |
|
| 597 | - if ($lsb >=0 && $lsb <= 15 ) $lsbc = $hex_arr[$lsb]; |
|
| 595 | + $hex_arr = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'); |
|
| 596 | + $lsb = $chksum&0x0F; |
|
| 597 | + if ($lsb >= 0 && $lsb <= 15) $lsbc = $hex_arr[$lsb]; |
|
| 598 | 598 | else $lsbc = '0'; |
| 599 | - $msb = (($chksum & 0xF0) >> 4) & 0x0F; |
|
| 600 | - if ($msb >=0 && $msb <= 15 ) $msbc = $hex_arr[$msb]; |
|
| 599 | + $msb = (($chksum&0xF0) >> 4)&0x0F; |
|
| 600 | + if ($msb >= 0 && $msb <= 15) $msbc = $hex_arr[$msb]; |
|
| 601 | 601 | else $msbc = '0'; |
| 602 | 602 | $itu = '!'.$itu."*{$msbc}{$lsbc}\r\n"; |
| 603 | 603 | return $itu; |
@@ -623,14 +623,14 @@ discard block |
||
| 623 | 623 | |
| 624 | 624 | public function mmsitype($mmsi) { |
| 625 | 625 | if (strlen($mmsi) == 9) { |
| 626 | - if (substr($mmsi,0,3) == '974') return 'EPIRB (Emergency Position Indicating Radio Beacon) AIS'; |
|
| 627 | - elseif (substr($mmsi,0,3) == '972') return 'MOB (Man Overboard) device'; |
|
| 628 | - elseif (substr($mmsi,0,3) == '970') return 'AIS SART (Search and Rescue Transmitter)'; |
|
| 629 | - elseif (substr($mmsi,0,3) == '111') return 'SAR (Search and Rescue) aircraft'; |
|
| 630 | - elseif (substr($mmsi,0,2) == '98') return 'Auxiliary craft associated with a parent ship'; |
|
| 631 | - elseif (substr($mmsi,0,2) == '99') return 'Aids to Navigation'; |
|
| 632 | - elseif (substr($mmsi,0,2) == '00') return 'Coastal stations'; |
|
| 633 | - elseif (substr($mmsi,0,1) == '0') return 'Group of ships'; |
|
| 626 | + if (substr($mmsi, 0, 3) == '974') return 'EPIRB (Emergency Position Indicating Radio Beacon) AIS'; |
|
| 627 | + elseif (substr($mmsi, 0, 3) == '972') return 'MOB (Man Overboard) device'; |
|
| 628 | + elseif (substr($mmsi, 0, 3) == '970') return 'AIS SART (Search and Rescue Transmitter)'; |
|
| 629 | + elseif (substr($mmsi, 0, 3) == '111') return 'SAR (Search and Rescue) aircraft'; |
|
| 630 | + elseif (substr($mmsi, 0, 2) == '98') return 'Auxiliary craft associated with a parent ship'; |
|
| 631 | + elseif (substr($mmsi, 0, 2) == '99') return 'Aids to Navigation'; |
|
| 632 | + elseif (substr($mmsi, 0, 2) == '00') return 'Coastal stations'; |
|
| 633 | + elseif (substr($mmsi, 0, 1) == '0') return 'Group of ships'; |
|
| 634 | 634 | else return 'Ship'; |
| 635 | 635 | } |
| 636 | 636 | |
@@ -640,19 +640,19 @@ discard block |
||
| 640 | 640 | public function parse_line($buffer) { |
| 641 | 641 | global $globalDebug; |
| 642 | 642 | $result = array(); |
| 643 | - $start = strpos($buffer,"VDM"); |
|
| 643 | + $start = strpos($buffer, "VDM"); |
|
| 644 | 644 | $tst = substr($buffer, $start - 3); |
| 645 | - $data = $this->process_ais_raw( $tst, "" ); |
|
| 645 | + $data = $this->process_ais_raw($tst, ""); |
|
| 646 | 646 | if (!is_object($data)) { |
| 647 | 647 | //if ($globalDebug) echo '==== Line format not supported : '.$buffer."\n"; |
| 648 | 648 | return array(); |
| 649 | 649 | } |
| 650 | 650 | if ($data->lon != 0) $result['longitude'] = $data->lon; |
| 651 | 651 | if ($data->lat != 0) $result['latitude'] = $data->lat; |
| 652 | - $result['ident'] = trim(str_replace('@','',$data->name)); |
|
| 652 | + $result['ident'] = trim(str_replace('@', '', $data->name)); |
|
| 653 | 653 | $result['timestamp'] = $data->ts; |
| 654 | 654 | $result['mmsi'] = $data->mmsi; |
| 655 | - if (strlen($result['mmsi']) == 8 && substr($result['mmsi'],0,3) == '669') $result['mmsi'] = '3'.$result['mmsi']; |
|
| 655 | + if (strlen($result['mmsi']) == 8 && substr($result['mmsi'], 0, 3) == '669') $result['mmsi'] = '3'.$result['mmsi']; |
|
| 656 | 656 | $result['mmsi_type'] = $this->mmsitype($result['mmsi']); |
| 657 | 657 | if ($data->sog != -1.0) $result['speed'] = $data->sog; |
| 658 | 658 | if ($data->heading !== '') $result['heading'] = $data->heading; |
@@ -662,16 +662,16 @@ discard block |
||
| 662 | 662 | if ($data->type !== '') $result['type'] = $data->type; |
| 663 | 663 | if ($data->typeid !== '') $result['typeid'] = $data->typeid; |
| 664 | 664 | if ($data->imo !== '') $result['imo'] = $data->imo; |
| 665 | - if ($data->callsign !== '') $result['callsign'] = trim(str_replace('@','',$data->callsign)); |
|
| 665 | + if ($data->callsign !== '') $result['callsign'] = trim(str_replace('@', '', $data->callsign)); |
|
| 666 | 666 | if (is_numeric($data->eta_month) && $data->eta_month != 0 && is_numeric($data->eta_day) && $data->eta_day != 0 && $data->eta_hour !== '' && $data->eta_minute !== '') { |
| 667 | - $eta_ts = strtotime(date('Y').'-'.sprintf("%02d",$data->eta_month).'-'.sprintf("%02d",$data->eta_day).' '.sprintf("%02d",$data->eta_hour).':'.sprintf("%02d",$data->eta_minute).':00'); |
|
| 667 | + $eta_ts = strtotime(date('Y').'-'.sprintf("%02d", $data->eta_month).'-'.sprintf("%02d", $data->eta_day).' '.sprintf("%02d", $data->eta_hour).':'.sprintf("%02d", $data->eta_minute).':00'); |
|
| 668 | 668 | if ($eta_ts != '') $result['eta_ts'] = $eta_ts; |
| 669 | 669 | } elseif (is_numeric($data->eta_hour) && is_numeric($data->eta_minute)) { |
| 670 | - $eta_ts = strtotime(date('Y-m-d').' '.sprintf("%02d",$data->eta_hour).':'.sprintf("%02d",$data->eta_minute).':00'); |
|
| 670 | + $eta_ts = strtotime(date('Y-m-d').' '.sprintf("%02d", $data->eta_hour).':'.sprintf("%02d", $data->eta_minute).':00'); |
|
| 671 | 671 | if ($eta_ts != '') $result['eta_ts'] = $eta_ts; |
| 672 | 672 | } |
| 673 | 673 | if ($data->destination != '') { |
| 674 | - $dest = trim(str_replace('@','',$data->destination)); |
|
| 674 | + $dest = trim(str_replace('@', '', $data->destination)); |
|
| 675 | 675 | if ($dest != '') $result['destination'] = $dest; |
| 676 | 676 | } |
| 677 | 677 | $result['all'] = (array) $data; |
@@ -123,7 +123,9 @@ discard block |
||
| 123 | 123 | $temp += 1; |
| 124 | 124 | $flat = (float)($temp / (60.0 * 10000.0)); |
| 125 | 125 | $flat *= -1.0; |
| 126 | - } else $flat = (float)($temp / (60.0 * 10000.0)); |
|
| 126 | + } else { |
|
| 127 | + $flat = (float)($temp / (60.0 * 10000.0)); |
|
| 128 | + } |
|
| 127 | 129 | return $flat; // float |
| 128 | 130 | } |
| 129 | 131 | |
@@ -135,7 +137,9 @@ discard block |
||
| 135 | 137 | $temp += 1; |
| 136 | 138 | $flon = (float)($temp / (60.0 * 10000.0)); |
| 137 | 139 | $flon *= -1.0; |
| 138 | - } else $flon = (float)($temp / (60.0 * 10000.0)); |
|
| 140 | + } else { |
|
| 141 | + $flon = (float)($temp / (60.0 * 10000.0)); |
|
| 142 | + } |
|
| 139 | 143 | return $flon; |
| 140 | 144 | } |
| 141 | 145 | |
@@ -158,10 +162,8 @@ discard block |
||
| 158 | 162 | */ |
| 159 | 163 | private function asciidec_2_8bit($ascii) { |
| 160 | 164 | //only process in the following range: 48-87, 96-119 |
| 161 | - if ($ascii < 48) { } |
|
| 162 | - else { |
|
| 163 | - if($ascii>119) { } |
|
| 164 | - else { |
|
| 165 | + if ($ascii < 48) { } else { |
|
| 166 | + if($ascii>119) { } else { |
|
| 165 | 167 | if ($ascii>87 && $ascii<96) ; |
| 166 | 168 | else { |
| 167 | 169 | $ascii=$ascii+40; |
@@ -263,7 +265,9 @@ discard block |
||
| 263 | 265 | $ro->lon = $this->make_lonf(bindec(substr($_aisdata,57,28))); |
| 264 | 266 | $ro->lat = $this->make_latf(bindec(substr($_aisdata,85,27))); |
| 265 | 267 | $ro->heading = bindec(substr($_aisdata,124,9)); |
| 266 | - if ($ro->heading == 511) $ro->heading = ''; |
|
| 268 | + if ($ro->heading == 511) { |
|
| 269 | + $ro->heading = ''; |
|
| 270 | + } |
|
| 267 | 271 | $ro->cls = 2; // class B |
| 268 | 272 | } else if ($ro->id == 19) { |
| 269 | 273 | $ro->cog = bindec(substr($_aisdata,112,12))/10; |
@@ -273,7 +277,9 @@ discard block |
||
| 273 | 277 | $ro->name = $this->binchar($_aisdata,143,120); |
| 274 | 278 | $ro->cls = 2; // class B |
| 275 | 279 | $ro->heading = bindec(substr($_aisdata,124,9)); |
| 276 | - if ($ro->heading == 511) $ro->heading = ''; |
|
| 280 | + if ($ro->heading == 511) { |
|
| 281 | + $ro->heading = ''; |
|
| 282 | + } |
|
| 277 | 283 | $ro->typeid = bindec(substr($_aisdata,263,8)); |
| 278 | 284 | $ro->type = $this->getShipType($ro->typeid); |
| 279 | 285 | //$ro->to_bow = bindec(substr($_aisdata,271,9)); |
@@ -304,9 +310,13 @@ discard block |
||
| 304 | 310 | $ro->cls = 2; // class B |
| 305 | 311 | } else if ($ro->id == 27) { |
| 306 | 312 | $ro->cog = bindec(substr($_aisdata,85,9)); |
| 307 | - if ($ro->cog == 511) $ro->cog = 0.0; |
|
| 313 | + if ($ro->cog == 511) { |
|
| 314 | + $ro->cog = 0.0; |
|
| 315 | + } |
|
| 308 | 316 | $ro->sog = bindec(substr($_aisdata,79,6)); |
| 309 | - if ($ro->sog == 63) $ro->sog = 0.0; |
|
| 317 | + if ($ro->sog == 63) { |
|
| 318 | + $ro->sog = 0.0; |
|
| 319 | + } |
|
| 310 | 320 | $ro->lon = $this->make_lonf(bindec(substr($_aisdata,44,18))*10); |
| 311 | 321 | $ro->lat = $this->make_latf(bindec(substr($_aisdata,62,17))*10); |
| 312 | 322 | $ro->cls = 1; // class A |
@@ -354,27 +364,46 @@ discard block |
||
| 354 | 364 | } |
| 355 | 365 | |
| 356 | 366 | public function getShipType($code) { |
| 357 | - if (isset($this->shiptype[$code])) return $this->shiptype[$code]; |
|
| 358 | - else return ''; |
|
| 367 | + if (isset($this->shiptype[$code])) { |
|
| 368 | + return $this->shiptype[$code]; |
|
| 369 | + } else { |
|
| 370 | + return ''; |
|
| 371 | + } |
|
| 359 | 372 | } |
| 360 | 373 | |
| 361 | 374 | public function getShipTypeID($type) { |
| 362 | 375 | $typeid = array_search($type,$this->shiptype); |
| 363 | - if ($typeid !== FALSE) return $typeid; |
|
| 364 | - elseif ($type == 'Cargo' || $type == 'General Cargo' || $type == 'Bulk Carrier' || $type == 'Container Ship' || $type == 'Cargo A' || $type == 'Reefer' || $type == 'Vehicles Carrier') return 70; |
|
| 365 | - elseif ($type == 'Passengers Ship' || $type == 'Passenger' || $type == 'Ro-Ro/Passenger Ship' || $type == 'Motor Passenger') return 60; |
|
| 366 | - elseif ($type == 'Tanker' || $type == 'Crude Oil Tanker' || $type == 'Oil Products Tanker' || $type == 'Oil/Chemical Tanker') return 80; |
|
| 367 | - elseif ($type == 'Lpg Tanker') return 84; |
|
| 368 | - elseif ($type == 'Sailing Vessel') return 36; |
|
| 369 | - elseif ($type == 'Yacht') return 37; |
|
| 370 | - elseif ($type == 'Trailing Suction Hopper Dredger' || $type == 'Drilling Jack Up' || $type == 'Suction Dredger' || $type == 'Hopper Dredger') return 33; |
|
| 371 | - elseif ($type == 'High speed craft') return 40; |
|
| 372 | - elseif ($type == 'Icebreaker') return 52; |
|
| 373 | - elseif ($type == 'Pollution Control Vessel') return 54; |
|
| 374 | - elseif ($type == 'Other Type') return 90; |
|
| 375 | - elseif ($type == 'Anchor Handling Vessel' || $type == 'Offshore Supply Ship' || $type == 'Research/Survey Vessel' || $type == 'Utility Vessel' || $type == 'Pipe Burying Vessel') return 59; |
|
| 376 | - elseif ($type == 'Fish Carrier' || $type == 'Trawler' || $type == 'Trawlers' || $type == 'Fishing Vessel' || $type == 'Trawlers|unknown Not Fishing' || $type == 'Purse Seines|trawlers' || $type == 'Purse Seines') return 30; |
|
| 377 | - else return ''; |
|
| 376 | + if ($typeid !== FALSE) { |
|
| 377 | + return $typeid; |
|
| 378 | + } elseif ($type == 'Cargo' || $type == 'General Cargo' || $type == 'Bulk Carrier' || $type == 'Container Ship' || $type == 'Cargo A' || $type == 'Reefer' || $type == 'Vehicles Carrier') { |
|
| 379 | + return 70; |
|
| 380 | + } elseif ($type == 'Passengers Ship' || $type == 'Passenger' || $type == 'Ro-Ro/Passenger Ship' || $type == 'Motor Passenger') { |
|
| 381 | + return 60; |
|
| 382 | + } elseif ($type == 'Tanker' || $type == 'Crude Oil Tanker' || $type == 'Oil Products Tanker' || $type == 'Oil/Chemical Tanker') { |
|
| 383 | + return 80; |
|
| 384 | + } elseif ($type == 'Lpg Tanker') { |
|
| 385 | + return 84; |
|
| 386 | + } elseif ($type == 'Sailing Vessel') { |
|
| 387 | + return 36; |
|
| 388 | + } elseif ($type == 'Yacht') { |
|
| 389 | + return 37; |
|
| 390 | + } elseif ($type == 'Trailing Suction Hopper Dredger' || $type == 'Drilling Jack Up' || $type == 'Suction Dredger' || $type == 'Hopper Dredger') { |
|
| 391 | + return 33; |
|
| 392 | + } elseif ($type == 'High speed craft') { |
|
| 393 | + return 40; |
|
| 394 | + } elseif ($type == 'Icebreaker') { |
|
| 395 | + return 52; |
|
| 396 | + } elseif ($type == 'Pollution Control Vessel') { |
|
| 397 | + return 54; |
|
| 398 | + } elseif ($type == 'Other Type') { |
|
| 399 | + return 90; |
|
| 400 | + } elseif ($type == 'Anchor Handling Vessel' || $type == 'Offshore Supply Ship' || $type == 'Research/Survey Vessel' || $type == 'Utility Vessel' || $type == 'Pipe Burying Vessel') { |
|
| 401 | + return 59; |
|
| 402 | + } elseif ($type == 'Fish Carrier' || $type == 'Trawler' || $type == 'Trawlers' || $type == 'Fishing Vessel' || $type == 'Trawlers|unknown Not Fishing' || $type == 'Purse Seines|trawlers' || $type == 'Purse Seines') { |
|
| 403 | + return 30; |
|
| 404 | + } else { |
|
| 405 | + return ''; |
|
| 406 | + } |
|
| 378 | 407 | } |
| 379 | 408 | |
| 380 | 409 | public function process_ais_itu($_itu, $_len, $_filler, $aux /*, $ais_ch*/) { |
@@ -411,11 +440,20 @@ discard block |
||
| 411 | 440 | // assume 1st ! is valid |
| 412 | 441 | // find * ensure that it is at correct position |
| 413 | 442 | $end = strrpos ( $rawdata , '*' ); |
| 414 | - if ($end === FALSE) return -1; // check for NULLS!!! |
|
| 443 | + if ($end === FALSE) { |
|
| 444 | + return -1; |
|
| 445 | + } |
|
| 446 | + // check for NULLS!!! |
|
| 415 | 447 | $cs = substr( $rawdata, $end + 1 ); |
| 416 | - if ( strlen($cs) != 2 ) return -1; // correct cs length |
|
| 448 | + if ( strlen($cs) != 2 ) { |
|
| 449 | + return -1; |
|
| 450 | + } |
|
| 451 | + // correct cs length |
|
| 417 | 452 | $dcs = (int)hexdec( $cs ); |
| 418 | - for ( $alias=1; $alias<$end; $alias++) $chksum ^= ord( $rawdata[$alias] ); // perform XOR for NMEA checksum |
|
| 453 | + for ( $alias=1; $alias<$end; $alias++) { |
|
| 454 | + $chksum ^= ord( $rawdata[$alias] ); |
|
| 455 | + } |
|
| 456 | + // perform XOR for NMEA checksum |
|
| 419 | 457 | if ( $chksum == $dcs ) { // NMEA checksum pass |
| 420 | 458 | $pcs = explode(',', $rawdata); |
| 421 | 459 | // !AI??? identifier |
@@ -426,8 +464,14 @@ discard block |
||
| 426 | 464 | $num_seq = (int)$pcs[1]; // number of sequences |
| 427 | 465 | $seq = (int)$pcs[2]; // get sequence |
| 428 | 466 | // get msg sequence id |
| 429 | - if ($pcs[3] == '') $msg_sid = -1; // non-multipart message, set to -1 |
|
| 430 | - else $msg_sid = (int)$pcs[3]; // multipart message |
|
| 467 | + if ($pcs[3] == '') { |
|
| 468 | + $msg_sid = -1; |
|
| 469 | + } |
|
| 470 | + // non-multipart message, set to -1 |
|
| 471 | + else { |
|
| 472 | + $msg_sid = (int)$pcs[3]; |
|
| 473 | + } |
|
| 474 | + // multipart message |
|
| 431 | 475 | $ais_ch = $pcs[4]; // get AIS channel |
| 432 | 476 | // message sequence checking |
| 433 | 477 | if ($num_seq < 1 || $num_seq > 9) { |
@@ -490,10 +534,18 @@ discard block |
||
| 490 | 534 | //DEBUG echo "[$start $end $tst]\n"; |
| 491 | 535 | $result = $this->process_ais_raw( $tst, "" ); |
| 492 | 536 | $last_pos = $end + 1; |
| 493 | - } else break; |
|
| 537 | + } else { |
|
| 538 | + break; |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | + if ($last_pos > 0) { |
|
| 542 | + $cbuf = substr($cbuf, $last_pos); |
|
| 494 | 543 | } |
| 495 | - if ($last_pos > 0) $cbuf = substr($cbuf, $last_pos); // move... |
|
| 496 | - if (strlen($cbuf) > 1024) $cbuf = ""; // prevent overflow simple mode... |
|
| 544 | + // move... |
|
| 545 | + if (strlen($cbuf) > 1024) { |
|
| 546 | + $cbuf = ""; |
|
| 547 | + } |
|
| 548 | + // prevent overflow simple mode... |
|
| 497 | 549 | return $result; |
| 498 | 550 | } |
| 499 | 551 | |
@@ -513,7 +565,9 @@ discard block |
||
| 513 | 565 | if ($lat<0.0) { |
| 514 | 566 | $lat = -$lat; |
| 515 | 567 | $neg=true; |
| 516 | - } else $neg=false; |
|
| 568 | + } else { |
|
| 569 | + $neg=false; |
|
| 570 | + } |
|
| 517 | 571 | $latd = 0x00000000; |
| 518 | 572 | $latd = intval ($lat * 600000.0); |
| 519 | 573 | if ($neg==true) { |
@@ -529,7 +583,9 @@ discard block |
||
| 529 | 583 | if ($lon<0.0) { |
| 530 | 584 | $lon = -$lon; |
| 531 | 585 | $neg=true; |
| 532 | - } else $neg=false; |
|
| 586 | + } else { |
|
| 587 | + $neg=false; |
|
| 588 | + } |
|
| 533 | 589 | $lond = 0x00000000; |
| 534 | 590 | $lond = intval ($lon * 600000.0); |
| 535 | 591 | if ($neg==true) { |
@@ -542,9 +598,14 @@ discard block |
||
| 542 | 598 | |
| 543 | 599 | private function char2bin($name, $max_len) { |
| 544 | 600 | $len = strlen($name); |
| 545 | - if ($len > $max_len) $name = substr($name,0,$max_len); |
|
| 546 | - if ($len < $max_len) $pad = str_repeat('0', ($max_len - $len) * 6); |
|
| 547 | - else $pad = ''; |
|
| 601 | + if ($len > $max_len) { |
|
| 602 | + $name = substr($name,0,$max_len); |
|
| 603 | + } |
|
| 604 | + if ($len < $max_len) { |
|
| 605 | + $pad = str_repeat('0', ($max_len - $len) * 6); |
|
| 606 | + } else { |
|
| 607 | + $pad = ''; |
|
| 608 | + } |
|
| 548 | 609 | $rv = ''; |
| 549 | 610 | $ais_chars = array( |
| 550 | 611 | '@'=>0, 'A'=>1, 'B'=>2, 'C'=>3, 'D'=>4, 'E'=>5, 'F'=>6, 'G'=>7, 'H'=>8, 'I'=>9, |
@@ -557,9 +618,12 @@ discard block |
||
| 557 | 618 | ); |
| 558 | 619 | // " |
| 559 | 620 | $_a = str_split($name); |
| 560 | - if ($_a) foreach ($_a as $_1) { |
|
| 621 | + if ($_a) { |
|
| 622 | + foreach ($_a as $_1) { |
|
| 561 | 623 | if (isset($ais_chars[$_1])) $dec = $ais_chars[$_1]; |
| 562 | - else $dec = 0; |
|
| 624 | + } else { |
|
| 625 | + $dec = 0; |
|
| 626 | + } |
|
| 563 | 627 | $bin = str_pad(decbin( $dec ), 6, '0', STR_PAD_LEFT); |
| 564 | 628 | $rv .= $bin; |
| 565 | 629 | //echo "$_1 $dec ($bin)<br/>"; |
@@ -571,7 +635,9 @@ discard block |
||
| 571 | 635 | $len_bit = strlen($_enc); |
| 572 | 636 | $rem6 = $len_bit % 6; |
| 573 | 637 | $pad6_len = 0; |
| 574 | - if ($rem6) $pad6_len = 6 - $rem6; |
|
| 638 | + if ($rem6) { |
|
| 639 | + $pad6_len = 6 - $rem6; |
|
| 640 | + } |
|
| 575 | 641 | //echo $pad6_len.'<br>'; |
| 576 | 642 | $_enc .= str_repeat("0", $pad6_len); // pad the text... |
| 577 | 643 | $len_enc = strlen($_enc) / 6; |
@@ -580,8 +646,11 @@ discard block |
||
| 580 | 646 | for ($i=0; $i<$len_enc; $i++) { |
| 581 | 647 | $offset = $i * 6; |
| 582 | 648 | $dec = bindec(substr($_enc,$offset,6)); |
| 583 | - if ($dec < 40) $dec += 48; |
|
| 584 | - else $dec += 56; |
|
| 649 | + if ($dec < 40) { |
|
| 650 | + $dec += 48; |
|
| 651 | + } else { |
|
| 652 | + $dec += 56; |
|
| 653 | + } |
|
| 585 | 654 | //echo chr($dec)." $dec<br/>"; |
| 586 | 655 | $itu .= chr($dec); |
| 587 | 656 | } |
@@ -594,26 +663,42 @@ discard block |
||
| 594 | 663 | } |
| 595 | 664 | $hex_arr = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'); |
| 596 | 665 | $lsb = $chksum & 0x0F; |
| 597 | - if ($lsb >=0 && $lsb <= 15 ) $lsbc = $hex_arr[$lsb]; |
|
| 598 | - else $lsbc = '0'; |
|
| 666 | + if ($lsb >=0 && $lsb <= 15 ) { |
|
| 667 | + $lsbc = $hex_arr[$lsb]; |
|
| 668 | + } else { |
|
| 669 | + $lsbc = '0'; |
|
| 670 | + } |
|
| 599 | 671 | $msb = (($chksum & 0xF0) >> 4) & 0x0F; |
| 600 | - if ($msb >=0 && $msb <= 15 ) $msbc = $hex_arr[$msb]; |
|
| 601 | - else $msbc = '0'; |
|
| 672 | + if ($msb >=0 && $msb <= 15 ) { |
|
| 673 | + $msbc = $hex_arr[$msb]; |
|
| 674 | + } else { |
|
| 675 | + $msbc = '0'; |
|
| 676 | + } |
|
| 602 | 677 | $itu = '!'.$itu."*{$msbc}{$lsbc}\r\n"; |
| 603 | 678 | return $itu; |
| 604 | 679 | } |
| 605 | 680 | |
| 606 | 681 | public function parse($buffer) { |
| 607 | 682 | $data = $this->process_ais_buf($buffer); |
| 608 | - if (!is_object($data)) return array(); |
|
| 683 | + if (!is_object($data)) { |
|
| 684 | + return array(); |
|
| 685 | + } |
|
| 609 | 686 | $result = array(); |
| 610 | - if ($data->lon != 0) $result['longitude'] = $data->lon; |
|
| 611 | - if ($data->lat != 0) $result['latitude'] = $data->lat; |
|
| 687 | + if ($data->lon != 0) { |
|
| 688 | + $result['longitude'] = $data->lon; |
|
| 689 | + } |
|
| 690 | + if ($data->lat != 0) { |
|
| 691 | + $result['latitude'] = $data->lat; |
|
| 692 | + } |
|
| 612 | 693 | $result['ident'] = trim($data->name); |
| 613 | 694 | $result['timestamp'] = $data->ts; |
| 614 | 695 | $result['mmsi'] = $data->mmsi; |
| 615 | - if ($data->sog != -1.0) $result['speed'] = $data->sog; |
|
| 616 | - if ($data->cog != 0) $result['heading'] = $data->cog; |
|
| 696 | + if ($data->sog != -1.0) { |
|
| 697 | + $result['speed'] = $data->sog; |
|
| 698 | + } |
|
| 699 | + if ($data->cog != 0) { |
|
| 700 | + $result['heading'] = $data->cog; |
|
| 701 | + } |
|
| 617 | 702 | /* |
| 618 | 703 | $ro->cls = 0; // AIS class undefined, also indicate unparsed msg |
| 619 | 704 | $ro->id = bindec(substr($_aisdata,0,6)); |
@@ -623,15 +708,25 @@ discard block |
||
| 623 | 708 | |
| 624 | 709 | public function mmsitype($mmsi) { |
| 625 | 710 | if (strlen($mmsi) == 9) { |
| 626 | - if (substr($mmsi,0,3) == '974') return 'EPIRB (Emergency Position Indicating Radio Beacon) AIS'; |
|
| 627 | - elseif (substr($mmsi,0,3) == '972') return 'MOB (Man Overboard) device'; |
|
| 628 | - elseif (substr($mmsi,0,3) == '970') return 'AIS SART (Search and Rescue Transmitter)'; |
|
| 629 | - elseif (substr($mmsi,0,3) == '111') return 'SAR (Search and Rescue) aircraft'; |
|
| 630 | - elseif (substr($mmsi,0,2) == '98') return 'Auxiliary craft associated with a parent ship'; |
|
| 631 | - elseif (substr($mmsi,0,2) == '99') return 'Aids to Navigation'; |
|
| 632 | - elseif (substr($mmsi,0,2) == '00') return 'Coastal stations'; |
|
| 633 | - elseif (substr($mmsi,0,1) == '0') return 'Group of ships'; |
|
| 634 | - else return 'Ship'; |
|
| 711 | + if (substr($mmsi,0,3) == '974') { |
|
| 712 | + return 'EPIRB (Emergency Position Indicating Radio Beacon) AIS'; |
|
| 713 | + } elseif (substr($mmsi,0,3) == '972') { |
|
| 714 | + return 'MOB (Man Overboard) device'; |
|
| 715 | + } elseif (substr($mmsi,0,3) == '970') { |
|
| 716 | + return 'AIS SART (Search and Rescue Transmitter)'; |
|
| 717 | + } elseif (substr($mmsi,0,3) == '111') { |
|
| 718 | + return 'SAR (Search and Rescue) aircraft'; |
|
| 719 | + } elseif (substr($mmsi,0,2) == '98') { |
|
| 720 | + return 'Auxiliary craft associated with a parent ship'; |
|
| 721 | + } elseif (substr($mmsi,0,2) == '99') { |
|
| 722 | + return 'Aids to Navigation'; |
|
| 723 | + } elseif (substr($mmsi,0,2) == '00') { |
|
| 724 | + return 'Coastal stations'; |
|
| 725 | + } elseif (substr($mmsi,0,1) == '0') { |
|
| 726 | + return 'Group of ships'; |
|
| 727 | + } else { |
|
| 728 | + return 'Ship'; |
|
| 729 | + } |
|
| 635 | 730 | } |
| 636 | 731 | |
| 637 | 732 | |
@@ -647,32 +742,61 @@ discard block |
||
| 647 | 742 | //if ($globalDebug) echo '==== Line format not supported : '.$buffer."\n"; |
| 648 | 743 | return array(); |
| 649 | 744 | } |
| 650 | - if ($data->lon != 0) $result['longitude'] = $data->lon; |
|
| 651 | - if ($data->lat != 0) $result['latitude'] = $data->lat; |
|
| 745 | + if ($data->lon != 0) { |
|
| 746 | + $result['longitude'] = $data->lon; |
|
| 747 | + } |
|
| 748 | + if ($data->lat != 0) { |
|
| 749 | + $result['latitude'] = $data->lat; |
|
| 750 | + } |
|
| 652 | 751 | $result['ident'] = trim(str_replace('@','',$data->name)); |
| 653 | 752 | $result['timestamp'] = $data->ts; |
| 654 | 753 | $result['mmsi'] = $data->mmsi; |
| 655 | - if (strlen($result['mmsi']) == 8 && substr($result['mmsi'],0,3) == '669') $result['mmsi'] = '3'.$result['mmsi']; |
|
| 754 | + if (strlen($result['mmsi']) == 8 && substr($result['mmsi'],0,3) == '669') { |
|
| 755 | + $result['mmsi'] = '3'.$result['mmsi']; |
|
| 756 | + } |
|
| 656 | 757 | $result['mmsi_type'] = $this->mmsitype($result['mmsi']); |
| 657 | - if ($data->sog != -1.0) $result['speed'] = $data->sog; |
|
| 658 | - if ($data->heading !== '') $result['heading'] = $data->heading; |
|
| 659 | - elseif ($data->cog != 0) $result['heading'] = $data->cog; |
|
| 660 | - if ($data->status != '') $result['status'] = $data->status; |
|
| 661 | - if ($data->statusid !== '') $result['statusid'] = $data->statusid; |
|
| 662 | - if ($data->type !== '') $result['type'] = $data->type; |
|
| 663 | - if ($data->typeid !== '') $result['typeid'] = $data->typeid; |
|
| 664 | - if ($data->imo !== '') $result['imo'] = $data->imo; |
|
| 665 | - if ($data->callsign !== '') $result['callsign'] = trim(str_replace('@','',$data->callsign)); |
|
| 758 | + if ($data->sog != -1.0) { |
|
| 759 | + $result['speed'] = $data->sog; |
|
| 760 | + } |
|
| 761 | + if ($data->heading !== '') { |
|
| 762 | + $result['heading'] = $data->heading; |
|
| 763 | + } elseif ($data->cog != 0) { |
|
| 764 | + $result['heading'] = $data->cog; |
|
| 765 | + } |
|
| 766 | + if ($data->status != '') { |
|
| 767 | + $result['status'] = $data->status; |
|
| 768 | + } |
|
| 769 | + if ($data->statusid !== '') { |
|
| 770 | + $result['statusid'] = $data->statusid; |
|
| 771 | + } |
|
| 772 | + if ($data->type !== '') { |
|
| 773 | + $result['type'] = $data->type; |
|
| 774 | + } |
|
| 775 | + if ($data->typeid !== '') { |
|
| 776 | + $result['typeid'] = $data->typeid; |
|
| 777 | + } |
|
| 778 | + if ($data->imo !== '') { |
|
| 779 | + $result['imo'] = $data->imo; |
|
| 780 | + } |
|
| 781 | + if ($data->callsign !== '') { |
|
| 782 | + $result['callsign'] = trim(str_replace('@','',$data->callsign)); |
|
| 783 | + } |
|
| 666 | 784 | if (is_numeric($data->eta_month) && $data->eta_month != 0 && is_numeric($data->eta_day) && $data->eta_day != 0 && $data->eta_hour !== '' && $data->eta_minute !== '') { |
| 667 | 785 | $eta_ts = strtotime(date('Y').'-'.sprintf("%02d",$data->eta_month).'-'.sprintf("%02d",$data->eta_day).' '.sprintf("%02d",$data->eta_hour).':'.sprintf("%02d",$data->eta_minute).':00'); |
| 668 | - if ($eta_ts != '') $result['eta_ts'] = $eta_ts; |
|
| 786 | + if ($eta_ts != '') { |
|
| 787 | + $result['eta_ts'] = $eta_ts; |
|
| 788 | + } |
|
| 669 | 789 | } elseif (is_numeric($data->eta_hour) && is_numeric($data->eta_minute)) { |
| 670 | 790 | $eta_ts = strtotime(date('Y-m-d').' '.sprintf("%02d",$data->eta_hour).':'.sprintf("%02d",$data->eta_minute).':00'); |
| 671 | - if ($eta_ts != '') $result['eta_ts'] = $eta_ts; |
|
| 791 | + if ($eta_ts != '') { |
|
| 792 | + $result['eta_ts'] = $eta_ts; |
|
| 793 | + } |
|
| 672 | 794 | } |
| 673 | 795 | if ($data->destination != '') { |
| 674 | 796 | $dest = trim(str_replace('@','',$data->destination)); |
| 675 | - if ($dest != '') $result['destination'] = $dest; |
|
| 797 | + if ($dest != '') { |
|
| 798 | + $result['destination'] = $dest; |
|
| 799 | + } |
|
| 676 | 800 | } |
| 677 | 801 | $result['all'] = (array) $data; |
| 678 | 802 | /* |