|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BultonFr\NMEA; |
|
4
|
|
|
|
|
5
|
|
|
use \Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Use frame type parser infos to check a line and parse it. |
|
9
|
|
|
* |
|
10
|
|
|
* @package BultonFr\NMEA |
|
11
|
|
|
* @author Vermeulen Maxime <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class Frame |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @const ERR_OBTAIN_MSG_AND_CHECKSUM_FAILED Error code when there is an |
|
17
|
|
|
* error for obtaining the message and the checksum value of the line |
|
18
|
|
|
*/ |
|
19
|
|
|
const ERR_OBTAIN_MSG_AND_CHECKSUM_FAILED = 20101; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @const ERR_CHECKSUM_FAILED Error code when the calculated checksum and |
|
23
|
|
|
* the checksum of the message not corresponding |
|
24
|
|
|
*/ |
|
25
|
|
|
const ERR_CHECKSUM_FAILED = 20102; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @const ERR_FRAME_MSG_FORMAT Error code when the message can not be |
|
29
|
|
|
* cut with the regex => The message format is not correct. |
|
30
|
|
|
*/ |
|
31
|
|
|
const ERR_FRAME_MSG_FORMAT = 20103; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string $frameType The current frame type |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $frameType; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string $frameRegex The regex to use for parse the message |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $frameRegex; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string $line The line to parse |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $line; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string $message The message to parse |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $message; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string $checksum The checksum containing into the line |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $checksum; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Constructor. |
|
60
|
|
|
* * Obtain the message and the checksum contained into the line |
|
61
|
|
|
* * Check if checksum corresponding |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $line The line to parse |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct($line) |
|
66
|
|
|
{ |
|
67
|
1 |
|
$this->line = $line; |
|
68
|
|
|
|
|
69
|
1 |
|
$this->obtainMessageAndChecksum(); |
|
70
|
1 |
|
$this->checksum(); |
|
71
|
1 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function __toString() |
|
74
|
|
|
{ |
|
75
|
|
|
$properties = get_class_vars(get_called_class()); |
|
76
|
|
|
$notDisplay = [ |
|
77
|
|
|
'frameRegex', |
|
78
|
|
|
'line', |
|
79
|
|
|
'message', |
|
80
|
|
|
'checksum' |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
$returnedStr = ''; |
|
84
|
|
|
foreach ($properties as $propName => $propValue) { |
|
85
|
|
|
if (in_array($propName, $notDisplay)) { |
|
86
|
|
|
continue; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$propCurrentValue = $this->{$propName}; |
|
90
|
|
|
if ( |
|
91
|
|
|
is_object($propCurrentValue) && |
|
92
|
|
|
get_class($propCurrentValue) === '\DateTime' |
|
93
|
|
|
) { |
|
94
|
|
|
$format = 'd/m/Y H:i:s.u'; |
|
95
|
|
|
if ($propName === 'utcTime') { |
|
96
|
|
|
$format = 'H:i:s.u'; |
|
|
|
|
|
|
97
|
|
|
} elseif ($propName === 'utcDate') { |
|
98
|
|
|
$format = 'd/m/Y'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$propCurrentValue = $propCurrentValue->format('d/m/Y H:i:s.u'); |
|
102
|
|
|
} elseif ( |
|
103
|
|
|
is_array($propCurrentValue) || |
|
104
|
|
|
is_object($propCurrentValue) |
|
105
|
|
|
) { |
|
106
|
|
|
$propCurrentValue = print_r($propCurrentValue, true); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$returnedStr .= $propName.' : '.$propCurrentValue."\n"; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $returnedStr; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Take all message parts and populate attributes |
|
117
|
|
|
* |
|
118
|
|
|
* @param string[] $msgParts All parts of the message. Cutted with the |
|
119
|
|
|
* regex corresponding to the line frame type. |
|
120
|
|
|
* |
|
121
|
|
|
* @return void |
|
122
|
|
|
*/ |
|
123
|
|
|
abstract protected function decodeFrame($msgParts); |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Getter to property frameType |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getFrameType() |
|
131
|
|
|
{ |
|
132
|
1 |
|
return $this->frameType; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Getter to property line |
|
137
|
|
|
* |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getLine() |
|
141
|
|
|
{ |
|
142
|
1 |
|
return $this->line; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Obtain the message and the checksum contained into the line |
|
147
|
|
|
* |
|
148
|
|
|
* @return void |
|
149
|
|
|
* |
|
150
|
|
|
* @throws Exception If the checksum is not present into the line |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function obtainMessageAndChecksum() |
|
153
|
|
|
{ |
|
154
|
1 |
|
$matched = []; |
|
155
|
1 |
|
if (!preg_match('/^\$(.*)\*([A-Z0-9]{2})/', $this->line, $matched)) { |
|
156
|
1 |
|
throw new Exception( |
|
157
|
1 |
|
'The line is corrupted. The message and/or the checksum has not been found.', |
|
158
|
1 |
|
static::ERR_OBTAIN_MSG_AND_CHECKSUM_FAILED |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
1 |
|
$this->message = $matched[1]; |
|
163
|
1 |
|
$this->checksum = $matched[2]; |
|
164
|
1 |
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Calculates the checksum of the message and comparares it with the |
|
168
|
|
|
* checksum contained into the message. |
|
169
|
|
|
* |
|
170
|
|
|
* @throws Exception If the checksum does not match |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function checksum() |
|
173
|
|
|
{ |
|
174
|
1 |
|
$nbCharInMsg = strlen($this->message); |
|
175
|
1 |
|
$checksum = 0; |
|
176
|
|
|
|
|
177
|
1 |
|
for ($readedChar = 0; $readedChar < $nbCharInMsg; $readedChar++) { |
|
178
|
1 |
|
$checksum ^= ord($this->message{$readedChar}); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
1 |
|
if ($checksum < 16) { |
|
182
|
1 |
|
$checksum = '0'.dechex($checksum); |
|
183
|
|
|
} else { |
|
184
|
1 |
|
$checksum = dechex($checksum); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
1 |
|
if (strtoupper($checksum) !== strtoupper($this->checksum)) { |
|
188
|
1 |
|
throw new Exception( |
|
189
|
1 |
|
'The line is corrupted. The checksum not corresponding.', |
|
190
|
1 |
|
static::ERR_CHECKSUM_FAILED |
|
191
|
|
|
); |
|
192
|
|
|
} |
|
193
|
1 |
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Read the frame and use the regex of the format type to cut the message |
|
197
|
|
|
* into many parts |
|
198
|
|
|
* |
|
199
|
|
|
* @return void |
|
200
|
|
|
* |
|
201
|
|
|
* @throws Exception If the message not corresponding with the regex. |
|
202
|
|
|
*/ |
|
203
|
|
|
public function readFrame() |
|
204
|
|
|
{ |
|
205
|
1 |
|
$matches = []; |
|
206
|
1 |
|
if (!preg_match($this->frameRegex, $this->message, $matches)) { |
|
207
|
1 |
|
throw new Exception( |
|
208
|
1 |
|
'The line is corrupted. It not corresponding to '.$this->frameType.' format', |
|
209
|
1 |
|
static::ERR_FRAME_MSG_FORMAT |
|
210
|
|
|
); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
1 |
|
$this->decodeFrame($matches); |
|
214
|
1 |
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|