source: src/webapp/webservice/inc/GnuBoat/Nmea/Nmea0183/Parser.php@ 89c510f

0.5 debian/bullseye debian/jessie
Last change on this file since 89c510f was 008bce9, checked in by Joachim Langenbach <joachim.langenbach@…>, 9 years ago

First more or less working version of visualising logfiles on the map

  • Property mode set to 100644
File size: 3.1 KB
Line 
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
19namespace GnuBoat\Nmea\Nmea0183;
20
21require_once(__DIR__ ."/../../../autoloader.php");
22
23class 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 $message = "Skipped sentence on line ". $line .": ". $sentence;
64 error_log($message);
65 if($debug > 0)
66 print $message ."\n";
67 }
68 }
69 fclose($fileHandler);
70 return $result;
71 }
72
73 public function parseSentence($sentence, $debug)
74 {
75 $sentence = trim($sentence);
76 if(empty($sentence)){
77 $message = "Empty sentence";
78 error_log($message);
79 if($debug > 0)
80 print $message ."\n";
81 return null;
82 }
83
84 $matches = array();
85 $result = preg_match('/^[\$!]{1}([\w]{2})([\w]{3})([^\*]+)\*([\da-zA-Z]+)$/', $sentence, $matches);
86 if($result === false || $result != 1 || count($matches) != 5){
87 $message = "Could not detect talker, type and checksum from ". $sentence;
88 error_log($message);
89 if($debug > 0)
90 print $message ."\n";
91 return null;
92 }
93
94 $talker = $matches[1];
95 $type = $matches[2];
96 $checksum = $matches[4];
97
98 if(!array_key_exists(strtolower($type), $this->_types)){
99 $message = "Skipping unknown NMEA0183 sentence of type ". $type .": ". $sentence;
100 error_log($message);
101 if($debug > 0)
102 print $message ."\n";
103 return null;
104 }
105
106 $reflectionClass = new \ReflectionClass($this->_types[strtolower($type)]);
107 return $reflectionClass->newInstanceArgs([$talker, $checksum, $sentence]);
108 }
109}
110
111?>
Note: See TracBrowser for help on using the repository browser.