-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06032be
commit 25845fc
Showing
11 changed files
with
573 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package model | ||
|
||
import ( | ||
"github.com/cuteLittleDevil/go-jt808/protocol/jt808" | ||
"github.com/cuteLittleDevil/go-jt808/shared/consts" | ||
) | ||
|
||
type BaseHandle struct{} | ||
|
||
func (b *BaseHandle) Parse(_ *jt808.JTMessage) error { | ||
return nil | ||
} | ||
|
||
func (b *BaseHandle) HasReply() bool { | ||
return true | ||
} | ||
|
||
func (b *BaseHandle) ReplyBody(jtMsg *jt808.JTMessage) []byte { | ||
head := jtMsg.Header | ||
// 通用应答 | ||
p8001 := &P0x8001{ | ||
RespondSerialNumber: head.SerialNumber, | ||
RespondID: head.ID, | ||
Result: 0x00, // 0-成功 1-失败 2-消息有误 3-不支持 4-报警处理确认 默认成功 | ||
} | ||
return p8001.Encode() | ||
} | ||
|
||
func (b *BaseHandle) ReplyProtocol() uint16 { | ||
return uint16(consts.P8001GeneralRespond) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package model | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"github.com/cuteLittleDevil/go-jt808/protocol" | ||
"github.com/cuteLittleDevil/go-jt808/protocol/jt808" | ||
"github.com/cuteLittleDevil/go-jt808/shared/consts" | ||
"strings" | ||
) | ||
|
||
type P0x8001 struct { | ||
// RespondSerialNumber 应答消息流水号 = 这个消息发送时候的流水号 | ||
RespondSerialNumber uint16 | ||
// RespondID 应答消息ID = 这个消息发送时候的ID | ||
RespondID uint16 | ||
// Result 结果 // 0-成功 1-失败 2-消息有误 3-不支持 4-报警处理确认 | ||
Result byte | ||
} | ||
|
||
func (p *P0x8001) Protocol() uint16 { | ||
return uint16(consts.P8001GeneralRespond) | ||
} | ||
|
||
func (p *P0x8001) ReplyProtocol() uint16 { | ||
return 0 | ||
} | ||
|
||
func (p *P0x8001) Parse(jtMsg *jt808.JTMessage) error { | ||
body := jtMsg.Body | ||
if len(body) != 5 { | ||
return protocol.ErrBodyLengthInconsistency | ||
} | ||
p.RespondSerialNumber = binary.BigEndian.Uint16(body[:2]) | ||
p.RespondID = binary.BigEndian.Uint16(body[2:4]) | ||
p.Result = body[4] | ||
return nil | ||
} | ||
|
||
func (p *P0x8001) Encode() []byte { | ||
return []byte{ | ||
byte(p.RespondSerialNumber >> 8), | ||
byte(p.RespondSerialNumber & 0xFF), | ||
byte(p.RespondID >> 8), | ||
byte(p.RespondID & 0xFF), | ||
p.Result, | ||
} | ||
} | ||
|
||
func (p *P0x8001) String() string { | ||
str := "数据体对象:{\n" | ||
str += fmt.Sprintf("\t%s:[%10x]", consts.P8001GeneralRespond, p.Encode()) | ||
return strings.Join([]string{ | ||
str, | ||
fmt.Sprintf("\t[%04x] 应答消息流水号:[%d]", p.RespondSerialNumber, p.RespondSerialNumber), | ||
fmt.Sprintf("\t[%04x] 应答消息ID:[%d]", p.RespondID, p.RespondID), | ||
fmt.Sprintf("\t[%02x] 结果:[%d] 0-成功 1-失败 "+ | ||
"2-消息有误 3-不支持 4-报警处理确认", p.Result, p.Result), | ||
"}", | ||
}, "\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package model | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"github.com/cuteLittleDevil/go-jt808/protocol" | ||
"github.com/cuteLittleDevil/go-jt808/protocol/jt808" | ||
"github.com/cuteLittleDevil/go-jt808/protocol/utils" | ||
"github.com/cuteLittleDevil/go-jt808/shared/consts" | ||
"strings" | ||
) | ||
|
||
type P0x8100 struct { | ||
// RespondSerialNumber 应答消息流水号 = 这个消息发送时候的流水号 | ||
RespondSerialNumber uint16 | ||
// Result 结果 0-成功 1-车辆已被注册 2-数据库中无该车辆 3-终端已被注册 4-数据库中无该终端 | ||
Result byte | ||
// AuthCode 鉴权码 | ||
AuthCode string | ||
} | ||
|
||
func (p *P0x8100) Protocol() uint16 { | ||
return uint16(consts.P8100RegisterRespond) | ||
} | ||
|
||
func (p *P0x8100) ReplyProtocol() uint16 { | ||
return 0 | ||
} | ||
|
||
func (p *P0x8100) Encode() []byte { | ||
code := utils.String2FillingBytes(p.AuthCode, len(p.AuthCode)) | ||
tmp := []byte{ | ||
byte(p.RespondSerialNumber >> 8), | ||
byte(p.RespondSerialNumber & 0xFF), | ||
p.Result, | ||
} | ||
tmp = append(tmp, code...) | ||
return tmp | ||
} | ||
|
||
func (p *P0x8100) Parse(jtMsg *jt808.JTMessage) error { | ||
body := jtMsg.Body | ||
if len(body) < 3 { | ||
return protocol.ErrBodyLengthInconsistency | ||
} | ||
p.RespondSerialNumber = binary.BigEndian.Uint16(body[:2]) | ||
p.Result = body[2] | ||
p.AuthCode = string(body[3:]) | ||
return nil | ||
} | ||
|
||
func (p *P0x8100) String() string { | ||
str := "数据体对象:{\n" | ||
body := p.Encode() | ||
str += fmt.Sprintf("\t注册消息应答:[%x]", body) | ||
return strings.Join([]string{ | ||
str, | ||
fmt.Sprintf("\t[%04x] 应答流水号:[%d]", p.RespondSerialNumber, p.RespondSerialNumber), | ||
fmt.Sprintf("\t[%02x] 结果:[%d] 0-成功 1-失败 "+ | ||
"2-消息有误 3-不支持 4-报警处理确认", p.Result, p.Result), | ||
fmt.Sprintf("\t[%x] 鉴权码:[%s]", body[3:], p.AuthCode), | ||
"}", | ||
}, "\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package model | ||
|
||
import ( | ||
"encoding/hex" | ||
"errors" | ||
"github.com/cuteLittleDevil/go-jt808/protocol" | ||
"github.com/cuteLittleDevil/go-jt808/protocol/jt808" | ||
"testing" | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
type Handler interface { | ||
Parse(*jt808.JTMessage) error | ||
String() string | ||
} | ||
type args struct { | ||
msg string | ||
Handler | ||
body []byte // 用于覆盖率100测试 强制替换了解析正确的body | ||
} | ||
tests := []struct { | ||
name string | ||
fields Handler | ||
args args | ||
}{ | ||
{ | ||
name: "T0X0001 终端-通用应答", | ||
args: args{ | ||
msg: "7e000100050123456789017fff007b01c803bd7e", | ||
Handler: &T0x0001{}, | ||
body: []byte{0, 123, 1, 200}, | ||
}, | ||
fields: &T0x0001{ | ||
SerialNumber: 123, | ||
ID: 456, | ||
Result: 3, | ||
}, | ||
}, | ||
{ | ||
name: "P0X8001 平台-通用应答", | ||
args: args{ | ||
msg: "7e8001000501234567890100007fff0002008e7e", | ||
Handler: &P0x8001{}, | ||
body: []byte{0, 0, 0, 0}, | ||
}, | ||
fields: &P0x8001{ | ||
RespondSerialNumber: 32767, | ||
RespondID: 2, | ||
Result: 0, | ||
}, | ||
}, | ||
{ | ||
name: "P0X8100 终端-注册消息应答", | ||
args: args{ | ||
msg: "7e8100000e01234567890100000000003132333435363738393031377e", | ||
Handler: &P0x8100{}, | ||
body: []byte{0, 0}, | ||
}, | ||
fields: &P0x8100{ | ||
RespondSerialNumber: 0, | ||
Result: 0, | ||
AuthCode: "12345678901", | ||
}, | ||
}, | ||
{ | ||
name: "T0X0002 终端-心跳", | ||
args: args{ | ||
msg: "7e0002000001234567890100008a7e", | ||
Handler: &T0x0002{}, | ||
}, | ||
fields: &T0x0002{}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
data, _ := hex.DecodeString(tt.args.msg) | ||
jtMsg := jt808.NewJTMessage() | ||
if err := jtMsg.Decode(data); err != nil { | ||
t.Errorf("Decode() error = %v", err) | ||
return | ||
} | ||
if err := tt.args.Parse(jtMsg); err != nil { | ||
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) | ||
return | ||
} | ||
if tt.args.body != nil { | ||
jtMsg.Body = tt.args.body | ||
if err := tt.args.Parse(jtMsg); err != nil { | ||
if errors.Is(err, protocol.ErrBodyLengthInconsistency) { | ||
return | ||
} | ||
t.Errorf("Parse() error = %v", err) | ||
return | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.