Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Commit

Permalink
更新批量上报定位0704协议
Browse files Browse the repository at this point in the history
  • Loading branch information
skiy committed Dec 28, 2018
1 parent 85c47bf commit d4775f9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 50 deletions.
55 changes: 48 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
中国交通部GPS车载终端通讯协议解析
中国交通部GPS车载终端通讯协议解析
------
部标GPS数据流解析 (字符串处理版)。

## 安装
```
composer require skiy/gnss-protocol:dev-master
```

## 使用
### 解析
-
```php
<?php

require 'vendor/autoload.php';

//位置
Expand Down Expand Up @@ -38,14 +43,50 @@ var_dump($reply_info);
//获取应答消息类
$reply = $gps->getReply();
var_dump($reply);

```

## TODO
### 封装
客户端消息发送
- 初始化终端
```php
//方式一
$msg = new Terminal();

### 解析
-
$setting = [
'msg_id' => MessageId::REGISTER,
'msg_number' => '0000',
'msg_mobile' => '015697794619',
];

### 封装
-
$msg->setting($setting);

//方式二, 通过上条消息获取基本信息
$msg = new \ChinaGnss\Terminal($reply);
```
- 注册
```php
$plate_number = '粤A12345';

$register_params = [
'province_id' => '0000',
'city_id' => '0000',
'manufacturer_id' => '5943000000',
'terminal_model' => '59432d3131000000000000000000000000000000',
'terminal_id' => '00058032343758',
'plate_color' => '02',
'plate_number' => Format::str2Hex($plate_number, 'gbk'),
];

$msg->register($register_params);

$send_data_str = $msg->compile(0);
```
- 自定义内容
```php
$local_body = '00000000000c00010000000000000000000000000000%s25040000000001040000000030011b310100';
$body = sprintf($local_body, date('ymdHis')); //自定义内容
$term = new \ChinaGnss\Terminal($reply);
$term->setBody(\ChinaGnss\MessageId::LOCATION_REPORTING, $body); //$body为缺省时,发送空消息
$location = $term->compile();

```
32 changes: 0 additions & 32 deletions src/Gps.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,36 +126,4 @@ public function reply(int $code = 4, int $number = 0, int $type = 2, array $opti
public function getReply() : Reply {
return $this->reply;
}

/**
* 客户端 - 应答消息内容
* @param string $reply_id 消息ID
* @param string $message 消息内容
* @param int $number 应答流水号
* @param int $type 应答类型 (0.应答消息(十六进制字符串), 1.应答消息体, 其它.应答消息(已封装的十六进制数据流))
* @param array $options 扩展选项 [is_pack,encrypt_type,keep] 是否分包,加密方式,保留位
* @return string
*/
public function create(string $reply_id, string $message = '', int $number = 0, int $type = 2, array $options = []) : string {
$this->reply = new Reply($this->message->head_msg_id, $this->message->head_msg_number, $this->message->head_msg_mobile);

$this->reply->reply_id = $reply_id;
$this->reply->reply_body = $message;

if ($number > 0) {
$this->reply->setNumber($number);
}

if (! empty($options)) {
$is_pack = isset($options['is_pack']) ? (bool)$options['is_pack'] : false;
$encrypt_type = (! empty($options['encrypt_type']) && (mb_strlen($options['encrypt_type']) == 3)) ? $options['encrypt_type'] : '000';
$keep = (! empty($options['keep']) && (mb_strlen($options['keep']) == 2)) ? $options['keep'] : '00';

$this->reply->setParams($is_pack, $encrypt_type, $keep);
}

$reply = $this->reply->reply(-1, $type);

return $reply;
}
}
31 changes: 21 additions & 10 deletions src/Protocol/LocatingData.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,32 @@ public function split(): void {
* @return mixed
*/
public function analyze(): array {
$msg_len = Format::subByte($this->items, 0, 2); //位置汇报数据体长度
$msg_info = Format::subByte($this->items, 2); //位置汇报数据体
$count = Format::hex2Dec($this->count);

$reporting = new LocationReporting($msg_info);
$location = $reporting->analyze();
$items = [];

$start = 0;
for ($i = 0; $i < $count; $i++) {
$length_hex = Format::subByte($this->items, $start, 2); //位置汇报数据体长度
$length = (int)Format::hex2Dec($length_hex);
$msg_info = Format::subByte($this->items, $start + 2, $length); //位置汇报数据体

$start = $start + 2 + $length;

$reporting = new LocationReporting($msg_info);
$location = $reporting->analyze();

$items[] = [
'length' => $length,
'info' => $location,
];
}

$msg = [
'count' => Format::hex2Dec($this->count),
'count' => $count,
'type' => Format::hex2Dec($this->type),
'items' => [
'length' => Format::hex2Dec($msg_len),
'location' => $location,
]
'items' => $items,
];
// var_dump($this);
return $msg;
}
}
3 changes: 2 additions & 1 deletion tests/BodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ public function testLocatingData() {

$gps = new Gps();

// var_dump($str);
$gps->analytical($str);

// $msg = $gps->getMessage();
$msg = $gps->getMessage();
// var_dump($msg);
$info = $gps->getInfo();
// var_dump($info);
Expand Down

0 comments on commit d4775f9

Please sign in to comment.