Skip to content

Commit

Permalink
fix: 修复模拟终端报文请求头生成错误
Browse files Browse the repository at this point in the history
  • Loading branch information
cuteLittleDevil committed Oct 13, 2024
1 parent e49ddba commit 969b337
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
| 飞哥的开发内功修炼 | https://github.com/yanfeizhang/coder-kung-fu?tab=readme-ov-file |
| 协议文档 | https://gitee.com/yezhihao/jt808-server/tree/master/协议文档 |
| 协议解析网站 | https://jttools.smallchi.cn/jt808 |
| bcd转dec编码 | https://github.com/deatil/lakego-admin |
| bcd转dec编码 | https://github.com/deatil/lakego-admin/tree/main/pkg/lakego-pkg/go-encoding/bcd |

## 性能测试
- java模拟器(QQ群下载 373203450)
Expand Down
16 changes: 14 additions & 2 deletions terminal/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/cuteLittleDevil/go-jt808/protocol/jt808"
"github.com/cuteLittleDevil/go-jt808/protocol/utils"
"github.com/cuteLittleDevil/go-jt808/shared/consts"
"log/slog"
"strings"
)

Expand Down Expand Up @@ -50,11 +51,22 @@ func WithHeader(protocolVersion consts.ProtocolVersionType, phone string) Option
code := utils.CreateVerifyCode(bodyData)
data := []byte{0x7e}
data = append(data, bodyData...)
data = append(data, code)
switch code {
case 0x7e:
data = append(data, []byte{0x7d, 0x02}...)
case 0x7d:
data = append(data, []byte{0x7d, 0x01}...)
default:
data = append(data, code)
}
data = append(data, 0x7e)
var jtMsg *jt808.JTMessage
jtMsg = jt808.NewJTMessage()
_ = jtMsg.Decode(data)
if err := jtMsg.Decode(data); err != nil {
slog.Error("decode",
slog.String("phone", phone),
slog.Any("err", err))
}
jtMsg.Header.TerminalPhoneNo = phone // 终端手机号
jtMsg.Header.SerialNumber = 0 // 流水号
jtMsg.Header.ProtocolVersion = protocolVersion
Expand Down
18 changes: 18 additions & 0 deletions terminal/option_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package terminal

import (
"fmt"
"github.com/cuteLittleDevil/go-jt808/shared/consts"
"testing"
)

func TestWithHeader(t *testing.T) {
tmp := New(WithHeader(consts.JT808Protocol2013, "678"))
data := tmp.CreateDefaultCommandData(consts.T0002HeartBeat)
msg := fmt.Sprintf("%x", data)
want := "7e0002000000000000067800017d017e"
if msg != want {
t.Errorf("WithHeader()=%s\n want %s", msg, want)
return
}
}
15 changes: 10 additions & 5 deletions terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@ import (
)

type Terminal struct {
TerminalPhoneNo string
header *jt808.Header
protocolHandles map[consts.JT808CommandType]Handler
}

func New(opts ...Option) *Terminal {
msg := "7e000100050123456789017fff007b01c803bd7e"
jtMsg := jt808.NewJTMessage()
data, _ := hex.DecodeString(msg)
_ = jtMsg.Decode(data)
header := jtMsg.Header
var header *jt808.Header
options := NewOptions(opts)
if options.Header != nil {
header = options.Header
}
if header == nil {
msg := "7e000200001234567820130001387e"
jtMsg := jt808.NewJTMessage()
data, _ := hex.DecodeString(msg)
_ = jtMsg.Decode(data)
header = jtMsg.Header
}
return &Terminal{
TerminalPhoneNo: header.TerminalPhoneNo,
header: header,
protocolHandles: defaultProtocolHandles(options.Header.ProtocolVersion),
}
Expand Down

0 comments on commit 969b337

Please sign in to comment.