| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Uro\TeltonikaFmParser; |
||
| 6 | |||
| 7 | use Uro\TeltonikaFmParser\Exception\ParserException; |
||
| 8 | use Uro\TeltonikaFmParser\Model\Data; |
||
| 9 | use Uro\TeltonikaFmParser\Model\Imei; |
||
| 10 | |||
| 11 | class TcpDecoder implements Decoder |
||
| 12 | { |
||
| 13 | public function isAuthentication(string $payload): bool |
||
| 14 | { |
||
| 15 | $firstByte = substr($payload, 0, 8); |
||
| 16 | |||
| 17 | return hexdec($firstByte) !== 0; |
||
| 18 | } |
||
| 19 | |||
| 20 | public function isData(string $payload): bool |
||
| 21 | { |
||
| 22 | return !$this->isAuthentication($payload); |
||
| 23 | } |
||
| 24 | |||
| 25 | public function decodeAuthentication(string $payload): Imei |
||
| 26 | { |
||
| 27 | $hexImei = substr($payload, 4); |
||
| 28 | |||
| 29 | return Imei::createFromHex($hexImei); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @todo: Finish CRC, and Sensors |
||
| 34 | * |
||
| 35 | * @param string $payload |
||
| 36 | * |
||
| 37 | * @return array |
||
| 38 | * @throws ParserException |
||
| 39 | */ |
||
| 40 | public function decodeData(string $payload): array |
||
| 41 | { |
||
| 42 | $crc = substr($payload, \strlen($payload) - 8, 8); |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 43 | |||
| 44 | $avlDataWithChecks = substr($payload, 16, -8); |
||
| 45 | |||
| 46 | // Validating number of data; |
||
| 47 | if (substr($avlDataWithChecks, 2, 2) !== substr($avlDataWithChecks, \strlen($avlDataWithChecks) - 2, 2)) { |
||
| 48 | throw new ParserException('First element count check is different than last element count check'); |
||
| 49 | } |
||
| 50 | |||
| 51 | $numberOfElements = hexdec(substr($avlDataWithChecks, 2, 2)); |
||
| 52 | $avlData = substr($avlDataWithChecks, 4, -2); |
||
| 53 | |||
| 54 | $position = 0; |
||
| 55 | $resultData = []; |
||
| 56 | |||
| 57 | for ($i = 0; $i < $numberOfElements; $i++) { |
||
| 58 | $resultData[] = Data::createFromHex($avlData, $position); |
||
| 59 | } |
||
| 60 | |||
| 61 | return $resultData; |
||
| 62 | } |
||
| 63 | } |
||
| 64 |