fix: decode jt808 registration plate text

This commit is contained in:
lingniu
2026-07-01 23:21:53 +08:00
parent a72977cd79
commit 7b44f97ddb
3 changed files with 35 additions and 1 deletions

View File

@@ -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 (

View File

@@ -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 {

View File

@@ -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 {