fix: decode jt808 registration plate text
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
package jt808
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -318,7 +321,18 @@ func fixedText(data []byte) string {
|
||||
}
|
||||
|
||||
func freeText(data []byte) string {
|
||||
return strings.TrimRight(strings.TrimSpace(string(data)), "\x00")
|
||||
data = bytes.Trim(data, "\x00 ")
|
||||
if len(data) == 0 {
|
||||
return ""
|
||||
}
|
||||
if utf8.Valid(data) {
|
||||
return string(data)
|
||||
}
|
||||
decoded, err := simplifiedchinese.GBK.NewDecoder().Bytes(data)
|
||||
if err == nil && utf8.Valid(decoded) {
|
||||
return string(decoded)
|
||||
}
|
||||
return strings.ToValidUTF8(string(data), "")
|
||||
}
|
||||
|
||||
func parseBCDTime(data []byte) time.Time {
|
||||
|
||||
@@ -153,6 +153,25 @@ func TestParseFrameParsesRegistrationIdentity(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFrameDecodesGBKRegistrationPlate(t *testing.T) {
|
||||
body := make([]byte, 0, 46)
|
||||
body = binary.BigEndian.AppendUint16(body, 16)
|
||||
body = binary.BigEndian.AppendUint16(body, 32)
|
||||
body = appendFixedASCII(body, "YUTNG", 5)
|
||||
body = appendFixedASCII(body, "ZK6105CHEVNPG4", 20)
|
||||
body = appendFixedASCII(body, "DEV0002", 7)
|
||||
body = append(body, 2)
|
||||
body = append(body, []byte{0xBB, 0xA6, 'A', '5', '3', '3', '0', '1'}...)
|
||||
|
||||
env, err := ParseFrame(buildFrame(0x0100, "013079963380", 9, body), 1782918600000, "115.231.168.135:43625")
|
||||
if err != nil {
|
||||
t.Fatalf("ParseFrame() error = %v", err)
|
||||
}
|
||||
if env.Plate != "沪A53301" {
|
||||
t.Fatalf("plate = %q", env.Plate)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFrameParsesAuthenticationToken(t *testing.T) {
|
||||
env, err := ParseFrame(buildFrame(0x0102, "064646848757", 8, []byte("g7gps")), 1782918600000, "115.231.168.135:43625")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user