Skip to content

Commit

Permalink
feat: 增加注册等指令
Browse files Browse the repository at this point in the history
  • Loading branch information
cuteLittleDevil committed Oct 7, 2024
1 parent 0865aba commit 519b30b
Show file tree
Hide file tree
Showing 11 changed files with 685 additions and 29 deletions.
5 changes: 4 additions & 1 deletion protocol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/cuteLittleDevil/go-jt808/protocol

go 1.23.2

require github.com/cuteLittleDevil/go-jt808/shared v0.1.0
require (
github.com/cuteLittleDevil/go-jt808/shared v0.1.0
golang.org/x/text v0.19.0
)
2 changes: 2 additions & 0 deletions protocol/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/cuteLittleDevil/go-jt808/shared v0.1.0 h1:Y6uDHbSFqa/VsHcGa713vrB0qz70xidXqDwEkFNe2ro=
github.com/cuteLittleDevil/go-jt808/shared v0.1.0/go.mod h1:BMWFmkDRLNjcXcuiPm/yphfWfZ6xNuTAJDkDDNhysOM=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
131 changes: 130 additions & 1 deletion protocol/model/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,120 @@ func TestParse(t *testing.T) {
Version: consts.JT808Protocol2019,
},
},
{
name: "T0x0100 终端注册 2011版本",
args: args{
msg: "7e010000200123456789010000001f007363640000007777772e3830382e3736353433323101b2e24131323334a17e",
Handler: &T0x0100{},
bodyLens: []int{24},
},
fields: &T0x0100{
ProvinceID: 31,
CityID: 115,
ManufacturerID: "cd",
TerminalModel: "www.808.",
TerminalID: "7654321",
PlateColor: 1,
LicensePlateNumber: "测A1234",
Version: consts.JT808Protocol2011,
},
},
{
name: "T0x0100 终端注册 2013版本",
args: args{
msg: "7e0100002c0123456789010000001f007363640000007777772e3830382e636f6d0000000000000000003736353433323101b2e24131323334cc7e",
Handler: &T0x0100{},
bodyLens: []int{36},
},
fields: &T0x0100{
ProvinceID: 31,
CityID: 115,
ManufacturerID: "cd",
TerminalModel: "www.808.com",
TerminalID: "7654321",
PlateColor: 1,
LicensePlateNumber: "测A1234",
Version: consts.JT808Protocol2013,
},
},
{
name: "T0x0100 终端注册 2019版本",
args: args{
msg: "7e0100405301000000000172998417380000001f007363640000000000000000007777772e3830382e636f6d0000000000000000000000000000000000000037363534333231000000000000000000000000000000000000000000000001b2e241313233343b7e",
Handler: &T0x0100{},
bodyLens: []int{75},
},
fields: &T0x0100{
ProvinceID: 31,
CityID: 115,
ManufacturerID: "cd",
TerminalModel: "www.808.com",
TerminalID: "7654321",
PlateColor: 1,
LicensePlateNumber: "测A1234",
Version: consts.JT808Protocol2019,
},
},
{
name: "T0x0200 位置上报",
args: args{
msg: "7e0200001c0123456789010000000004000000080007203b7d0202633df70138000300632410012359591c7e",
Handler: &T0x0200{},
bodyLens: []int{27},
},
fields: &T0x0200{
T0x0200LocationItem: T0x0200LocationItem{
AlarmSign: 1024,
StatusSign: 2048,
Latitude: 119552894,
Longitude: 40058359,
Altitude: 312,
Speed: 3,
Direction: 99,
DateTime: "2024-10-01 23:59:59",
},
},
},
{
name: "T0x0704 位置批量上传",
args: args{
msg: "7e0704003f0123456789010000000200001c000004000000080007203b7d0202633df7013800030063241001235959001c000004000000080007203b7d0202633df7013800030063241001235959b67e",
Handler: &T0x0704{},
bodyLens: []int{30, 60, 68},
},
fields: &T0x0704{
Num: 2,
LocationType: 0,
Items: []T0x0704LocationItem{
{
Len: 28,
T0x0200LocationItem: T0x0200LocationItem{
AlarmSign: 1024,
StatusSign: 2048,
Latitude: 119552894,
Longitude: 40058359,
Altitude: 312,
Speed: 3,
Direction: 99,
DateTime: "2024-10-01 23:59:59",
},
},
{
Len: 28,
T0x0200LocationItem: T0x0200LocationItem{
AlarmSign: 1024,
StatusSign: 2048,
Latitude: 119552894,
Longitude: 40058359,
Altitude: 312,
Speed: 3,
Direction: 99,
DateTime: "2024-10-01 23:59:59",
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -113,7 +227,6 @@ func TestParse(t *testing.T) {
t.Errorf("Parse() error = %v", err)
return
}

//fmt.Println(tt.args.Handler.String())
if tt.args.Handler.String() != tt.fields.String() {
t.Errorf("Parse() want: \n%v\nactual:\n%v", tt.args, tt.fields)
Expand All @@ -132,3 +245,19 @@ func TestParse(t *testing.T) {
})
}
}

// 为了覆盖率100%增加的测试 ------------------------------------
func TestT0x0704Parse(t *testing.T) {
msg := "7e0704003f0123456789010000000200001c000004000000080007203b7d0202633df7013800030063241001235959001c000004000000080007203b7d0202633df7013800030063241001235959b67e"
data, _ := hex.DecodeString(msg)
jtMsg := jt808.NewJTMessage()
_ = jtMsg.Decode(data)
handler := &T0x0704{}
// 强制错误情况
jtMsg.Body = jtMsg.Body[:63]
jtMsg.Body[4] = 0x00
if err := handler.Parse(jtMsg); !errors.Is(err, protocol.ErrBodyLengthInconsistency) {
t.Errorf("T0x0704 Parse() err[%v]", err)
return
}
}
18 changes: 18 additions & 0 deletions protocol/model/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ func TestReplyProtocol(t *testing.T) {
wantProtocol: uint16(consts.T0102RegisterAuth),
wantReplyProtocol: consts.P8001GeneralRespond,
},
{
name: "T0x0100 终端-注册",
args: &T0x0100{},
wantProtocol: uint16(consts.T0100Register),
wantReplyProtocol: consts.P8100RegisterRespond,
},
{
name: "T0x0200 终端-位置上报",
args: &T0x0200{},
wantProtocol: uint16(consts.T0200LocationReport),
wantReplyProtocol: consts.P8001GeneralRespond,
},
{
name: "T0x0704 终端-位置批量上传",
args: &T0x0704{},
wantProtocol: uint16(consts.T0704LocationBatchUpload),
wantReplyProtocol: consts.P8001GeneralRespond,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
37 changes: 36 additions & 1 deletion protocol/model/reply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ func TestReply(t *testing.T) {
result2019: "7e80014005010000000001729984173800000000010200877e",
},
},
{
name: "T0x0100 终端-注册",
args: args{
Handler: &T0x0100{},
msg2011: "7e010000200123456789010000001f007363640000007777772e3830382e3736353433323101b2e24131323334a17e",
msg2013: "7e0100002c0123456789010000001f007363640000007777772e3830382e636f6d0000000000000000003736353433323101b2e24131323334cc7e",
msg2019: "7e0100405301000000000172998417380000001f007363640000000000000000007777772e3830382e636f6d0000000000000000000000000000000000000037363534333231000000000000000000000000000000000000000000000001b2e241313233343b7e",
},
want: want{
result2011: "7e8100000e01234567890100000000003132333435363738393031377e",
result2013: "7e8100000e01234567890100000000003132333435363738393031377e",
result2019: "7e8100400e010000000001729984173800000000003137323939383431373338ba7e",
},
},
}
checkReplyInfo := func(t *testing.T, msg string, handler Handler, expectedResult string) {
if msg == "" {
Expand Down Expand Up @@ -114,10 +128,31 @@ func TestT0x0102Reply(t *testing.T) {
}
}

func TestT0x002Encode(t *testing.T) {
func TestT0x0002Encode(t *testing.T) {
handler := &T0x0002{}
got := handler.Encode()
if got != nil {
t.Errorf("T0x002 Encode() got = [%x]", got)
}
}

func TestT0x0200Encode(t *testing.T) {
handler := &T0x0200{
T0x0200LocationItem: T0x0200LocationItem{
AlarmSign: 1024,
StatusSign: 2048,
Latitude: 119552894,
Longitude: 40058359,
Altitude: 312,
Speed: 3,
Direction: 99,
DateTime: "2024-10-01 23:59:59",
},
}
body := handler.Encode()
got := fmt.Sprintf("%x", body)
want := "000004000000080007203b7e02633df7013800030063241001235959"
if got != want {
t.Errorf("T0x0200 Encode() got = %s\n want = %s", got, want)
}
}
Loading

0 comments on commit 519b30b

Please sign in to comment.