From 7b44f97ddb3c67ffaec4b734e536543058bf83d2 Mon Sep 17 00:00:00 2001 From: lingniu Date: Wed, 1 Jul 2026 23:21:53 +0800 Subject: [PATCH] fix: decode jt808 registration plate text --- go/vehicle-gateway/go.mod | 1 + .../internal/protocol/jt808/parser.go | 16 +++++++++++++++- .../internal/protocol/jt808/parser_test.go | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/go/vehicle-gateway/go.mod b/go/vehicle-gateway/go.mod index 34455b63..9be8d5bc 100644 --- a/go/vehicle-gateway/go.mod +++ b/go/vehicle-gateway/go.mod @@ -10,6 +10,7 @@ require ( github.com/redis/go-redis/v9 v9.17.2 github.com/segmentio/kafka-go v0.4.49 github.com/taosdata/driver-go/v3 v3.8.1 + golang.org/x/text v0.29.0 ) require ( diff --git a/go/vehicle-gateway/internal/protocol/jt808/parser.go b/go/vehicle-gateway/internal/protocol/jt808/parser.go index 28fd49a9..78c88891 100644 --- a/go/vehicle-gateway/internal/protocol/jt808/parser.go +++ b/go/vehicle-gateway/internal/protocol/jt808/parser.go @@ -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 { diff --git a/go/vehicle-gateway/internal/protocol/jt808/parser_test.go b/go/vehicle-gateway/internal/protocol/jt808/parser_test.go index 2e084bf1..1ebe81bd 100644 --- a/go/vehicle-gateway/internal/protocol/jt808/parser_test.go +++ b/go/vehicle-gateway/internal/protocol/jt808/parser_test.go @@ -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 {