source: src/webapp/webservice/inc/GnuBoat/Nmea/Nmea0183/Nmea0183Sentence.php@ fc99a86

debian/bullseye
Last change on this file since fc99a86 was 74f0621, checked in by Joachim Langenbach <joachim.langenbach@…>, 9 years ago

Nmea Parser now also parses lines starting with date tine string

  • Property mode set to 100644
File size: 2.3 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
21class Nmea0183Sentence extends \GnuBoat\Nmea\NmeaSentence
22{
23 function __construct($type, $talker, $checksum, $sentence)
24 {
25 parent::__construct($type, $talker, $sentence);
26
27 $this->_checksum = strtolower($checksum);
28 }
29
30 public function checksum()
31 {
32 return $this->_checksum;
33 }
34
35 public function checksumValid(&$errorMessage)
36 {
37 // extract characters to calculate checksum
38 $matches = array();
39 $result = preg_match('/^[^\$!]{0,}[\$!]{1}([^\*]+)\*([\da-zA-Z]+)$/', $this->sentence(), $matches);
40 if($result === false || $result == 0 || count($matches) != 3){
41 $errorMessage .= "Unable to determine ascii content and checksum from ". $this->sentence() ."\n";
42 return false;
43 }
44
45 $sentenceChecksum = strtolower($matches[2]);
46 if($this->checksum() != $sentenceChecksum){
47 $errorMessage .= "Checksum from sentece (". $sentenceChecksum .") is not equal stored checksum (". $this->checksum() .")\n";
48 return false;
49 }
50
51 // calculate checksum
52 $checksum = 0;
53 for($i = 0; $i < strlen($matches[1]); $i++)
54 $checksum = $checksum ^ ord($matches[1][$i]);
55
56 if($sentenceChecksum == strtolower(dechex($checksum)))
57 return true;
58 $errorMessage .= "Checksum from sentence (". $sentenceChecksum .") is not equal calculated checksum (". dechex($checksum) .")\n";
59 return false;
60 }
61
62 /**
63 * @brief Abstract function to provide additional checks in your implementation.
64 *
65 * Called by valid() and must return true or false
66 */
67 protected function validate(&$errorMessage)
68 {
69 return $this->checksumValid($errorMessage);
70 }
71
72 private $_checksum;
73}
74
75?>
Note: See TracBrowser for help on using the repository browser.