123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php
- class NmeaParser{
- private $Nmea;
- private $TimeStamp;
- private $maxHDOP;
- private $maxVDOP;
- private $CurrentUTC;
-
- function __construct(){
- $this->Nmea = array();
- $this->TimeStamp= 0;
- $this->maxHDOP = 0.0;
- $this->maxVDOP = 0.0;
- $this->CurrentUTC = 0;
- $this->CurrentTime=0;
- }
-
-
-
-
-
-
-
-
- public function SetMinSatellites($minSats=4){
- $this->minSats = $minSats;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public function SetMaxHdop($maxHDOP=10){
- $this->maxHDOP = $maxHDOP;
- }
-
-
-
-
-
-
-
-
-
-
- public function SetMaxVdop($maxVDOP=10){
- $this->maxVDOP = $maxVDOP;
- }
-
-
-
-
-
-
-
- private function NMEAtoUnixTime($utc,$date){
- $h = substr($utc,0,2);
- $i = substr($utc,2,2);
- $s = substr($utc,4,2);
- $d = substr($date,0,2);
- $m = substr($date,2,2);
- $y = substr($date,4,2);
-
- return mktime($h,$i,$s,$m,$d,$y);
- }
-
-
-
-
-
-
-
- public function ParseLine($line){
- $this->NmeaType = $this->SetNmeaType($line);
- switch($this->type){
- case "GPGGA": $this->GPGGA($line);break;
- case "GPGLL": $this->GPGLL($line);break;
- case "GPGSA": $this->GPGSA($line);break;
- case "GPGSV": $this->GPGSV($line);break;
- case "GPRMC": $this->GPRMC($line);break;
- case "GPVTG": $this->GPVTG($line);break;
- default: return;
- }
- }
-
-
-
-
-
-
- public function DumpNmea(){
- return $this->Nmea;
- }
-
-
- function GoodEnough(){
- return isset($this->Nmea[$this->CurrentUTC]['date']) && isset($this->Nmea[$this->CurrentUTC]['utc']) && isset($this->Nmea[$this->CurrentUTC]['lat']) && isset($this->Nmea[$this->CurrentUTC]['long']);
-
- }
-
-
-
-
-
-
-
- private function SetNmeaType($line){
- $this->type = trim(strtoupper(substr($line,1,5)));
- return $this->type;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private function GPGGA($geostr){
- $split=explode(",",$geostr);
- $this->CurrentUTC = $this->fixUTC($split[1]);
- $this->Nmea[$this->CurrentUTC]['type']['GPGGA']=true;
- $this->Nmea[$this->CurrentUTC]['utc']=$this->fixUTC($split[1]);
- $this->Nmea[$this->CurrentUTC]['lat']=$this->degree2decimal($split[2],$split[3]);
- $this->Nmea[$this->CurrentUTC]['ns']=$split[3];
- $this->Nmea[$this->CurrentUTC]['long']=$this->degree2decimal($split[4],$split[5]);
- $this->Nmea[$this->CurrentUTC]['ew']=$split[5];
- $this->Nmea[$this->CurrentUTC]['gpsqual']=$split[6];
- $this->Nmea[$this->CurrentUTC]['numsat']=$split[7];
- $this->Nmea[$this->CurrentUTC]['hdp']=$split[8];
- $this->Nmea[$this->CurrentUTC]['alt']=$split[9];
- $this->Nmea[$this->CurrentUTC]['un_alt']=$split[10];
- $this->Nmea[$this->CurrentUTC]['geoidal']=$split[11];
- $this->Nmea[$this->CurrentUTC]['un_geoidal']=$split[12];
- $this->Nmea[$this->CurrentUTC]['dgps']=$split[13];
- $this->Nmea[$this->CurrentUTC]['diffstat']=trim($split[14]);
- }
-
-
-
-
-
-
-
-
-
-
-
- private function GPGLL($geostr){
- $split=explode(",",$geostr);
- $this->Nmea[$this->CurrentUTC]['type']['GPGLL']=true;
- $this->CurrentUTC = $this->fixUTC($split[3]);
- $this->Nmea[$this->CurrentUTC]['utc']=$this->fixUTC($split[3]);
- $this->Nmea[$this->CurrentUTC]['status']=$this->dataStatus($split[4]);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private function GPGSA($geostr){
- $split=explode(",",$geostr);
- $this->Nmea[$this->CurrentUTC]['type']['GPGSA']=true;
- $this->Nmea[$this->CurrentUTC]['selectmode']=$split[1];
- $this->Nmea[$this->CurrentUTC]['mode']=$split[2];
- $this->Nmea[$this->CurrentUTC]['sat1']=$split[3];
- $this->Nmea[$this->CurrentUTC]['sat2']=$split[4];
- $this->Nmea[$this->CurrentUTC]['sat3']=$split[5];
- $this->Nmea[$this->CurrentUTC]['sat4']=$split[6];
- $this->Nmea[$this->CurrentUTC]['sat5']=$split[7];
- $this->Nmea[$this->CurrentUTC]['sat6']=$split[8];
- $this->Nmea[$this->CurrentUTC]['sat7']=$split[9];
- $this->Nmea[$this->CurrentUTC]['sat8']=$split[10];
- $this->Nmea[$this->CurrentUTC]['sat9']=$split[11];
- $this->Nmea[$this->CurrentUTC]['sat10']=$split[12];
- $this->Nmea[$this->CurrentUTC]['sat11']=$split[13];
- $this->Nmea[$this->CurrentUTC]['sat12']=$split[14];
- $this->Nmea[$this->CurrentUTC]['pdop']=$split[15];
- $this->Nmea[$this->CurrentUTC]['hdop']=$split[16];
- $this->Nmea[$this->CurrentUTC]['vdop']=$split[17];
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private function GPGSV($geostr){
- $split=explode(",",$geostr);
- $this->Nmea[$this->CurrentUTC]['type']['GPGSV']=true;
- $this->Nmea[$this->CurrentUTC]['satmessages']=$split[1];
- $this->Nmea[$this->CurrentUTC]['messnum']=$split[2];
- $this->Nmea[$this->CurrentUTC]['satview']=$split[3];
- $this->Nmea[$this->CurrentUTC]['satnum']=$split[4];
- $this->Nmea[$this->CurrentUTC]['elevdeg']=$split[5];
- $this->Nmea[$this->CurrentUTC]['azimuthdeg']=$split[6];
- $this->Nmea[$this->CurrentUTC]['snr']=$split[7];
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private function GPRMC($geostr){
- $split=explode(",",$geostr);
- $this->CurrentUTC = $this->fixUTC($split[1]);
- $this->Nmea[$this->CurrentUTC]['utc']=$this->fixUTC($split[1]);
- $this->Nmea[$this->CurrentUTC]['type']['GPRMC']=true;
- $this->Nmea[$this->CurrentUTC]['statusrmc']=$split[2];
- $this->Nmea[$this->CurrentUTC]['speed']=$split[7];
- $this->Nmea[$this->CurrentUTC]['track']=$split[8];
- $this->Nmea[$this->CurrentUTC]['date']=$split[9];
- $this->Nmea[$this->CurrentUTC]['magvar']=$split[10];
- $this->Nmea[$this->CurrentUTC]['mag_ew']=trim($split[11]);
- if($this->CurrentUTC && $split[9])
- $this->Nmea[$this->CurrentUTC]['Unix'] = $this->NMEAtoUnixTime($this->CurrentUTC,$split[9]);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private function GPVTG($geostr){
- $split=explode(",",$geostr);
- $this->Nmea[$this->CurrentUTC]['type']['GPVTG']=true;
- $this->Nmea[$this->CurrentUTC]['trkdeg1']=$split[1];
- $this->Nmea[$this->CurrentUTC]['t']=$split[2];
- $this->Nmea[$this->CurrentUTC]['trkdeg2']=$split[3];
- $this->Nmea[$this->CurrentUTC]['m']=$split[4];
- $this->Nmea[$this->CurrentUTC]['spdknots']=$spdk=$split[5];
- $this->Nmea[$this->CurrentUTC]['knots']=$split[6];
- $this->Nmea[$this->CurrentUTC]['spdkmph']=$split[7];
- $this->Nmea[$this->CurrentUTC]['kph']=$split[8];
- }
-
-
-
-
-
-
-
-
-
-
- private function degree2decimal($deg_coord,$direction,$precision=6){
- $degree=(int)($deg_coord/100);
- $minutes= $deg_coord-($degree*100);
- $dotdegree=$minutes/60;
- $decimal=$degree+$dotdegree;
-
- if (($direction=="S") or ($direction=="W"))
- {
- $decimal=$decimal*(-1);
- }
- $decimal=number_format($decimal,$precision,'.','');
- return $decimal;
- }
-
-
-
-
-
-
-
- private function TimeChanged($Time){
- return $this->CurrentTime==$Time;
- }
-
-
-
-
-
-
-
- public function GetNmeaData(){
- return $this->Nmea;
- }
-
-
-
-
-
-
-
- private function fixUTCKey($UTC,$Unix){
-
- $arr[$newkey] = $arr[$oldkey];
- unset($arr[$oldkey]);
- }
-
-
-
-
-
-
-
- private function fixUTC($UTC){
-
- return $UTC;
- }
-
-
- }
- ?>
|