Completed
Push — master ( 0f8c33...116ecf )
by Yannick
07:29
created
require/class.AIS.php 1 patch
Spacing   +93 added lines, -93 removed lines patch added patch discarded remove patch
@@ -29,31 +29,31 @@  discard block
 block discarded – undo
29 29
 
30 30
 	private function make_latf($temp) { // unsigned long 
31 31
 		$flat = 0.0; // float
32
-		$temp = $temp & 0x07FFFFFF;
33
-		if ($temp & 0x04000000) {
34
-			$temp = $temp ^ 0x07FFFFFF;
32
+		$temp = $temp&0x07FFFFFF;
33
+		if ($temp&0x04000000) {
34
+			$temp = $temp^0x07FFFFFF;
35 35
 			$temp += 1;
36
-			$flat = (float)($temp / (60.0 * 10000.0));
36
+			$flat = (float) ($temp/(60.0*10000.0));
37 37
 			$flat *= -1.0;
38
-		} else $flat = (float)($temp / (60.0 * 10000.0));
38
+		} else $flat = (float) ($temp/(60.0*10000.0));
39 39
 		return $flat; // float
40 40
 	}
41 41
 
42 42
 	private function make_lonf($temp) { // unsigned long
43 43
 		$flon = 0.0; // float
44
-		$temp = $temp & 0x0FFFFFFF;
45
-		if ($temp & 0x08000000) {
46
-			$temp = $temp ^ 0x0FFFFFFF;
44
+		$temp = $temp&0x0FFFFFFF;
45
+		if ($temp&0x08000000) {
46
+			$temp = $temp^0x0FFFFFFF;
47 47
 			$temp += 1;
48
-			$flon = (float)($temp / (60.0 * 10000.0));
48
+			$flon = (float) ($temp/(60.0*10000.0));
49 49
 			$flon *= -1.0;
50
-		} else $flon = (float)($temp / (60.0 * 10000.0));
50
+		} else $flon = (float) ($temp/(60.0*10000.0));
51 51
 		return $flon;
52 52
 	}
53 53
 
54 54
 	private function ascii_2_dec($chr) {
55
-		$dec=ord($chr);//get decimal ascii code
56
-		$hex=dechex($dec);//convert decimal to hex
55
+		$dec = ord($chr); //get decimal ascii code
56
+		$hex = dechex($dec); //convert decimal to hex
57 57
 		return ($dec);
58 58
 	}
59 59
 	
@@ -72,15 +72,15 @@  discard block
 block discarded – undo
72 72
 		//only process in the following range: 48-87, 96-119
73 73
 		if ($ascii < 48) { }
74 74
 		else {
75
-			if($ascii>119) { }
75
+			if ($ascii > 119) { }
76 76
 			else {
77
-				if ($ascii>87 && $ascii<96) ;
77
+				if ($ascii > 87 && $ascii < 96);
78 78
 				else {
79
-					$ascii=$ascii+40;
80
-					if ($ascii>128){
81
-						$ascii=$ascii+32;
79
+					$ascii = $ascii + 40;
80
+					if ($ascii > 128) {
81
+						$ascii = $ascii + 32;
82 82
 					} else {
83
-						$ascii=$ascii+40;
83
+						$ascii = $ascii + 40;
84 84
 					}
85 85
 				}
86 86
 			}
@@ -89,7 +89,7 @@  discard block
 block discarded – undo
89 89
 	}
90 90
 
91 91
 	private function dec_2_6bit($dec) {
92
-		$bin=decbin($dec);
92
+		$bin = decbin($dec);
93 93
 		return(substr($bin, -6)); 
94 94
 	}
95 95
 
@@ -106,11 +106,11 @@  discard block
 block discarded – undo
106 106
 		    '<', '=', '>', '?'
107 107
 		);
108 108
 		$rv = '';
109
-		if ($_size % 6 == 0) {
110
-			$len = $_size / 6;
111
-			for ($i=0; $i<$len; $i++) {
112
-				$offset = $i * 6;
113
-				$rv .= $ais_chars[ bindec(substr($_str,$_start + $offset,6)) ];
109
+		if ($_size%6 == 0) {
110
+			$len = $_size/6;
111
+			for ($i = 0; $i < $len; $i++) {
112
+				$offset = $i*6;
113
+				$rv .= $ais_chars[bindec(substr($_str, $_start + $offset, 6))];
114 114
 			}
115 115
 		}
116 116
 		return $rv;
@@ -127,40 +127,40 @@  discard block
 block discarded – undo
127 127
 		$ro->lon = 0.0;
128 128
 		$ro->lat = 0.0;
129 129
 		$ro->ts = time();
130
-		$ro->id = bindec(substr($_aisdata,0,6));
131
-		$ro->mmsi = bindec(substr($_aisdata,8,30));
130
+		$ro->id = bindec(substr($_aisdata, 0, 6));
131
+		$ro->mmsi = bindec(substr($_aisdata, 8, 30));
132 132
 		if ($ro->id >= 1 && $ro->id <= 3) {
133
-			$ro->cog = bindec(substr($_aisdata,116,12))/10;
134
-			$ro->sog = bindec(substr($_aisdata,50,10))/10;
135
-			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,61,28)));
136
-			$ro->lat = $this->make_latf(bindec(substr($_aisdata,89,27)));
133
+			$ro->cog = bindec(substr($_aisdata, 116, 12))/10;
134
+			$ro->sog = bindec(substr($_aisdata, 50, 10))/10;
135
+			$ro->lon = $this->make_lonf(bindec(substr($_aisdata, 61, 28)));
136
+			$ro->lat = $this->make_latf(bindec(substr($_aisdata, 89, 27)));
137 137
 			$ro->cls = 1; // class A
138 138
 		} else if ($ro->id == 5) {
139 139
 			//$imo = bindec(substr($_aisdata,40,30));
140 140
 			//$cs = $this->binchar($_aisdata,70,42);
141
-			$ro->name = $this->binchar($_aisdata,112,120);
141
+			$ro->name = $this->binchar($_aisdata, 112, 120);
142 142
 			$ro->cls = 1; // class A
143 143
 		} else if ($ro->id == 18) {
144
-			$ro->cog = bindec(substr($_aisdata,112,12))/10;
145
-			$ro->sog = bindec(substr($_aisdata,46,10))/10;
146
-			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,57,28)));
147
-			$ro->lat = $this->make_latf(bindec(substr($_aisdata,85,27)));
144
+			$ro->cog = bindec(substr($_aisdata, 112, 12))/10;
145
+			$ro->sog = bindec(substr($_aisdata, 46, 10))/10;
146
+			$ro->lon = $this->make_lonf(bindec(substr($_aisdata, 57, 28)));
147
+			$ro->lat = $this->make_latf(bindec(substr($_aisdata, 85, 27)));
148 148
 			$ro->cls = 2; // class B
149 149
 		} else if ($ro->id == 19) {
150
-			$ro->cog = bindec(substr($_aisdata,112,12))/10;
151
-			$ro->sog = bindec(substr($_aisdata,46,10))/10;
152
-			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,61,28)));
153
-			$ro->lat = $this->make_latf(bindec(substr($_aisdata,89,27)));
154
-			$ro->name = $this->binchar($_aisdata,143,120);
150
+			$ro->cog = bindec(substr($_aisdata, 112, 12))/10;
151
+			$ro->sog = bindec(substr($_aisdata, 46, 10))/10;
152
+			$ro->lon = $this->make_lonf(bindec(substr($_aisdata, 61, 28)));
153
+			$ro->lat = $this->make_latf(bindec(substr($_aisdata, 89, 27)));
154
+			$ro->name = $this->binchar($_aisdata, 143, 120);
155 155
 			$ro->cls = 2; // class B
156 156
 		} else if ($ro->id == 24) {
157
-			$pn = bindec(substr($_aisdata,38,2));
157
+			$pn = bindec(substr($_aisdata, 38, 2));
158 158
 			if ($pn == 0) {
159
-				$ro->name = $this->binchar($_aisdata,40,120);
159
+				$ro->name = $this->binchar($_aisdata, 40, 120);
160 160
 			}
161 161
 			$ro->cls = 2; // class B
162 162
 		}
163
-		$ro->statusid = bindec(substr($_aisdata,38,4));
163
+		$ro->statusid = bindec(substr($_aisdata, 38, 4));
164 164
 		$ro->status = $this->getStatus($ro->statusid);
165 165
 		//var_dump($ro); // dump results here for demo purpose
166 166
 		return $ro;
@@ -206,14 +206,14 @@  discard block
 block discarded – undo
206 206
 		global $port; // tcpip port...
207 207
 		
208 208
 		static $debug_counter = 0;
209
-		$aisdata168='';//six bit array of ascii characters
209
+		$aisdata168 = ''; //six bit array of ascii characters
210 210
 		$ais_nmea_array = str_split($_itu); // convert to an array
211 211
 		foreach ($ais_nmea_array as $value) {
212 212
 			$dec = $this->ascii_2_dec($value);
213 213
 			$bit8 = $this->asciidec_2_8bit($dec);
214 214
 			$bit6 = $this->dec_2_6bit($bit8);
215 215
 			//echo $value ."-" .$bit6 ."";
216
-			$aisdata168 .=$bit6;
216
+			$aisdata168 .= $bit6;
217 217
 		}
218 218
 		//echo $aisdata168 . "<br/>";
219 219
 		//return $this->decode_ais($aisdata168, $aux);
@@ -236,20 +236,20 @@  discard block
 block discarded – undo
236 236
 		// calculate checksum after ! till *
237 237
 		// assume 1st ! is valid
238 238
 		// find * ensure that it is at correct position
239
-		$end = strrpos ( $rawdata , '*' );
239
+		$end = strrpos($rawdata, '*');
240 240
 		if ($end === FALSE) return -1; // check for NULLS!!!
241
-		$cs = substr( $rawdata, $end + 1 );
242
-		if ( strlen($cs) != 2 ) return -1; // correct cs length
243
-		$dcs = (int)hexdec( $cs );
244
-		for ( $alias=1; $alias<$end; $alias++) $chksum ^= ord( $rawdata[$alias] ); // perform XOR for NMEA checksum
245
-		if ( $chksum == $dcs ) { // NMEA checksum pass
241
+		$cs = substr($rawdata, $end + 1);
242
+		if (strlen($cs) != 2) return -1; // correct cs length
243
+		$dcs = (int) hexdec($cs);
244
+		for ($alias = 1; $alias < $end; $alias++) $chksum ^= ord($rawdata[$alias]); // perform XOR for NMEA checksum
245
+		if ($chksum == $dcs) { // NMEA checksum pass
246 246
 			$pcs = explode(',', $rawdata);
247 247
 			// !AI??? identifier
248
-			$num_seq = (int)$pcs[1]; // number of sequences
249
-			$seq = (int)$pcs[2]; // get sequence
248
+			$num_seq = (int) $pcs[1]; // number of sequences
249
+			$seq = (int) $pcs[2]; // get sequence
250 250
 			// get msg sequence id
251 251
 			if ($pcs[3] == '') $msg_sid = -1; // non-multipart message, set to -1
252
-			else $msg_sid = (int)$pcs[3]; // multipart message
252
+			else $msg_sid = (int) $pcs[3]; // multipart message
253 253
 			$ais_ch = $pcs[4]; // get AIS channel
254 254
 			// message sequence checking
255 255
 			if ($num_seq < 1 || $num_seq > 9) {
@@ -282,7 +282,7 @@  discard block
 block discarded – undo
282 282
 					}
283 283
 				}
284 284
 				$itu = $itu.$pcs[5]; // get itu message
285
-				$filler += (int)$pcs[6][0]; // get filler
285
+				$filler += (int) $pcs[6][0]; // get filler
286 286
 				if ($num_seq == 1 // valid single message
287 287
 				    || $num_seq == $pseq // valid multi-part message
288 288
 				) {
@@ -302,13 +302,13 @@  discard block
 block discarded – undo
302 302
 		$cbuf = $cbuf.$ibuf;
303 303
 		$last_pos = 0;
304 304
 		$result = new stdClass();
305
-		while ( ($start = strpos($cbuf,"VDM",$last_pos)) !== FALSE) {
305
+		while (($start = strpos($cbuf, "VDM", $last_pos)) !== FALSE) {
306 306
 		//while ( ($start = strpos($cbuf,"!AI",$last_pos)) !== FALSE) {
307 307
 			//DEBUG echo $cbuf;
308
-			if ( ($end = strpos($cbuf,"\r\n", $start)) !== FALSE) { //TBD need to trim?
308
+			if (($end = strpos($cbuf, "\r\n", $start)) !== FALSE) { //TBD need to trim?
309 309
 				$tst = substr($cbuf, $start - 3, ($end - $start + 3));
310 310
 				//DEBUG echo "[$start $end $tst]\n";
311
-				$result = $this->process_ais_raw( $tst, "" );
311
+				$result = $this->process_ais_raw($tst, "");
312 312
 				$last_pos = $end + 1;
313 313
 			} else break;
314 314
 		}
@@ -320,41 +320,41 @@  discard block
 block discarded – undo
320 320
 	// incoming data from serial or IP comms
321 321
 	public function process_ais_line($cbuf) {
322 322
 		$result = new stdClass();
323
-		$start = strpos($cbuf,"VDM");
323
+		$start = strpos($cbuf, "VDM");
324 324
 		$tst = substr($cbuf, $start - 3);
325
-		$result = $this->process_ais_raw( $tst, "" );
325
+		$result = $this->process_ais_raw($tst, "");
326 326
 		return $result;
327 327
 	}
328 328
 
329 329
 	/* AIS Encoding
330 330
 	*/
331
-	private function mk_ais_lat( $lat ) {
331
+	private function mk_ais_lat($lat) {
332 332
 		//$lat = 1.2569;
333
-		if ($lat<0.0) {
333
+		if ($lat < 0.0) {
334 334
 			$lat = -$lat;
335
-			$neg=true;
336
-		} else $neg=false;
335
+			$neg = true;
336
+		} else $neg = false;
337 337
 		$latd = 0x00000000;
338
-		$latd = intval ($lat * 600000.0);
339
-		if ($neg==true) {
338
+		$latd = intval($lat*600000.0);
339
+		if ($neg == true) {
340 340
 			$latd = ~$latd;
341
-			$latd+=1;
341
+			$latd += 1;
342 342
 			$latd &= 0x07FFFFFF;
343 343
 		}
344 344
 		return $latd;
345 345
 	}
346 346
 
347
-	private function mk_ais_lon( $lon ) {
347
+	private function mk_ais_lon($lon) {
348 348
 		//$lon = 103.851;
349
-		if ($lon<0.0) {
349
+		if ($lon < 0.0) {
350 350
 			$lon = -$lon;
351
-			$neg=true;
352
-		} else $neg=false;
351
+			$neg = true;
352
+		} else $neg = false;
353 353
 		$lond = 0x00000000;
354
-		$lond = intval ($lon * 600000.0);
355
-		if ($neg==true) {
354
+		$lond = intval($lon*600000.0);
355
+		if ($neg == true) {
356 356
 			$lond = ~$lond;
357
-			$lond+=1;
357
+			$lond += 1;
358 358
 			$lond &= 0x0FFFFFFF;
359 359
 		}
360 360
 		return $lond;
@@ -362,8 +362,8 @@  discard block
 block discarded – undo
362 362
 
363 363
 	private function char2bin($name, $max_len) {
364 364
 		$len = strlen($name);
365
-		if ($len > $max_len) $name = substr($name,0,$max_len);
366
-		if ($len < $max_len) $pad = str_repeat('0', ($max_len - $len) * 6);
365
+		if ($len > $max_len) $name = substr($name, 0, $max_len);
366
+		if ($len < $max_len) $pad = str_repeat('0', ($max_len - $len)*6);
367 367
 		else $pad = '';
368 368
 		$rv = '';
369 369
 		$ais_chars = array(
@@ -379,26 +379,26 @@  discard block
 block discarded – undo
379 379
 		if ($_a) foreach ($_a as $_1) {
380 380
 			if (isset($ais_chars[$_1])) $dec = $ais_chars[$_1];
381 381
 			else $dec = 0;
382
-			$bin = str_pad(decbin( $dec ), 6, '0', STR_PAD_LEFT);
382
+			$bin = str_pad(decbin($dec), 6, '0', STR_PAD_LEFT);
383 383
 			$rv .= $bin;
384 384
 			//echo "$_1 $dec ($bin)<br/>";
385 385
 		}
386 386
 		return $rv.$pad;
387 387
 	}
388 388
 
389
-	private function mk_ais($_enc, $_part=1,$_total=1,$_seq='',$_ch='A') {
389
+	private function mk_ais($_enc, $_part = 1, $_total = 1, $_seq = '', $_ch = 'A') {
390 390
 		$len_bit = strlen($_enc);
391
-		$rem6 = $len_bit % 6;
391
+		$rem6 = $len_bit%6;
392 392
 		$pad6_len = 0;
393 393
 		if ($rem6) $pad6_len = 6 - $rem6;
394 394
 		//echo  $pad6_len.'<br>';
395 395
 		$_enc .= str_repeat("0", $pad6_len); // pad the text...
396
-		$len_enc = strlen($_enc) / 6;
396
+		$len_enc = strlen($_enc)/6;
397 397
 		//echo $_enc.' '.$len_enc.'<br/>';
398 398
 		$itu = '';
399
-		for ($i=0; $i<$len_enc; $i++) {
400
-			$offset = $i * 6;
401
-			$dec = bindec(substr($_enc,$offset,6));
399
+		for ($i = 0; $i < $len_enc; $i++) {
400
+			$offset = $i*6;
401
+			$dec = bindec(substr($_enc, $offset, 6));
402 402
 			if ($dec < 40) $dec += 48;
403 403
 			else $dec += 56;
404 404
 			//echo chr($dec)." $dec<br/>";
@@ -408,15 +408,15 @@  discard block
 block discarded – undo
408 408
 		$chksum = 0;
409 409
 		$itu = "AIVDM,$_part,$_total,$_seq,$_ch,".$itu.",0";
410 410
 		$len_itu = strlen($itu);
411
-		for ($i=0; $i<$len_itu; $i++) {
412
-			$chksum ^= ord( $itu[$i] );
411
+		for ($i = 0; $i < $len_itu; $i++) {
412
+			$chksum ^= ord($itu[$i]);
413 413
 		}
414
-		$hex_arr = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
415
-		$lsb = $chksum & 0x0F;
416
-		if ($lsb >=0 && $lsb <= 15 ) $lsbc = $hex_arr[$lsb];
414
+		$hex_arr = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
415
+		$lsb = $chksum&0x0F;
416
+		if ($lsb >= 0 && $lsb <= 15) $lsbc = $hex_arr[$lsb];
417 417
 		else $lsbc = '0';
418
-		$msb = (($chksum & 0xF0) >> 4) & 0x0F;
419
-		if ($msb >=0 && $msb <= 15 ) $msbc = $hex_arr[$msb];
418
+		$msb = (($chksum&0xF0) >> 4)&0x0F;
419
+		if ($msb >= 0 && $msb <= 15) $msbc = $hex_arr[$msb];
420 420
 		else $msbc = '0';
421 421
 		$itu = '!'.$itu."*{$msbc}{$lsbc}\r\n";
422 422
 		return $itu;
@@ -442,13 +442,13 @@  discard block
 block discarded – undo
442 442
 	public function parse_line($buffer) {
443 443
 		$result = array();
444 444
 		$data = new stdClass();
445
-		$start = strpos($buffer,"VDM");
445
+		$start = strpos($buffer, "VDM");
446 446
 		$tst = substr($buffer, $start - 3);
447
-		$data = $this->process_ais_raw( $tst, "" );
447
+		$data = $this->process_ais_raw($tst, "");
448 448
 		if (!is_object($data)) return array();
449 449
 		if ($data->lon != 0) $result['longitude'] = $data->lon;
450 450
 		if ($data->lat != 0) $result['latitude'] = $data->lat;
451
-		$result['ident'] = trim(str_replace('@','',$data->name));
451
+		$result['ident'] = trim(str_replace('@', '', $data->name));
452 452
 		$result['timestamp'] = $data->ts;
453 453
 		$result['mmsi'] = $data->mmsi;
454 454
 		if ($data->sog != -1.0) $result['speed'] = $data->sog;
Please login to merge, or discard this patch.