| 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | Copyright (C) 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 4 |
|
|---|
| 5 | This program is free software: you can redistribute it and/or modify
|
|---|
| 6 | it under the terms of the GNU General Public License as published by
|
|---|
| 7 | the Free Software Foundation, either version 3 of the License, or
|
|---|
| 8 | (at your option) any later version.
|
|---|
| 9 |
|
|---|
| 10 | This program is distributed in the hope that it will be useful,
|
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | GNU General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU General Public License
|
|---|
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | namespace GnuBoat\Nmea\Nmea0183;
|
|---|
| 20 |
|
|---|
| 21 | require_once(__DIR__ ."/../../../autoloader.php");
|
|---|
| 22 |
|
|---|
| 23 | class Parser extends \GnuBoat\Nmea\NmeaParser
|
|---|
| 24 | {
|
|---|
| 25 | function __construct()
|
|---|
| 26 | {
|
|---|
| 27 | $this->_types = array();
|
|---|
| 28 | $this->_types["rmc"] = "GnuBoat\Nmea\Nmea0183\Nmea0183Rmc";
|
|---|
| 29 | $this->_types["dpt"] = "GnuBoat\Nmea\Nmea0183\Nmea0183Dpt";
|
|---|
| 30 | $this->_types["mtw"] = "GnuBoat\Nmea\Nmea0183\Nmea0183Mtw";
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | public function parseFile($file, $debug)
|
|---|
| 34 | {
|
|---|
| 35 | $result = array();
|
|---|
| 36 | if(!file_exists($file)){
|
|---|
| 37 | $message = "NMEA file to parse does not exists: ". $file;
|
|---|
| 38 | error_log($message);
|
|---|
| 39 | if($debug > 0)
|
|---|
| 40 | print $message ."\n";
|
|---|
| 41 | return $result;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | $fileHandler = fopen($file, 'r');
|
|---|
| 45 | if(!$fileHandler){
|
|---|
| 46 | $message = "NMEA file to parse could not be opend for reading: ". $file;
|
|---|
| 47 | error_log($message);
|
|---|
| 48 | if($debug > 0)
|
|---|
| 49 | print $message ."\n";
|
|---|
| 50 | return $result;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | // despite the standards uses CR LF line ends, we are more flexible with auto here
|
|---|
| 54 | ini_set('auto_detect_line_endings',true);
|
|---|
| 55 |
|
|---|
| 56 | $line = 1;
|
|---|
| 57 | while(!feof($fileHandler)){
|
|---|
| 58 | $sentence = fgets($fileHandler);
|
|---|
| 59 | $parsedSentence = $this->parseSentence($sentence, $debug);
|
|---|
| 60 | if($parsedSentence != null)
|
|---|
| 61 | $result[] = $parsedSentence;
|
|---|
| 62 | else{
|
|---|
| 63 | if($debug > 0)
|
|---|
| 64 | error_log("Skipped sentence on line ". $line .": ". $sentence);
|
|---|
| 65 | }
|
|---|
| 66 | $line++;
|
|---|
| 67 | }
|
|---|
| 68 | fclose($fileHandler);
|
|---|
| 69 | return $result;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | public function parseSentence($sentence, $debug)
|
|---|
| 73 | {
|
|---|
| 74 | $sentence = trim($sentence);
|
|---|
| 75 | if(empty($sentence)){
|
|---|
| 76 | if($debug > 0)
|
|---|
| 77 | error_log("Empty sentence");
|
|---|
| 78 | return null;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | $matches = array();
|
|---|
| 82 | $result = preg_match('/^[\$!]{1}([\w]{2})([\w]{3})([^\*]+)\*([\da-zA-Z]+)$/', $sentence, $matches);
|
|---|
| 83 | if($result === false || $result != 1 || count($matches) != 5){
|
|---|
| 84 | $message = "Could not detect talker, type and checksum from ". $sentence;
|
|---|
| 85 | error_log($message);
|
|---|
| 86 | if($debug > 0)
|
|---|
| 87 | print $message ."\n";
|
|---|
| 88 | return null;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | $talker = $matches[1];
|
|---|
| 92 | $type = $matches[2];
|
|---|
| 93 | $checksum = $matches[4];
|
|---|
| 94 |
|
|---|
| 95 | if(!array_key_exists(strtolower($type), $this->_types)){
|
|---|
| 96 | $message = "Skipping unknown NMEA0183 sentence of type ". $type .": ". $sentence;
|
|---|
| 97 | error_log($message);
|
|---|
| 98 | if($debug > 0)
|
|---|
| 99 | print $message ."\n";
|
|---|
| 100 | return null;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | $reflectionClass = new \ReflectionClass($this->_types[strtolower($type)]);
|
|---|
| 104 | return $reflectionClass->newInstanceArgs([$talker, $checksum, $sentence]);
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | ?>
|
|---|