fix: decode jt808 registration plate text
This commit is contained in:
@@ -10,6 +10,7 @@ require (
|
|||||||
github.com/redis/go-redis/v9 v9.17.2
|
github.com/redis/go-redis/v9 v9.17.2
|
||||||
github.com/segmentio/kafka-go v0.4.49
|
github.com/segmentio/kafka-go v0.4.49
|
||||||
github.com/taosdata/driver-go/v3 v3.8.1
|
github.com/taosdata/driver-go/v3 v3.8.1
|
||||||
|
golang.org/x/text v0.29.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
package jt808
|
package jt808
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
|
||||||
|
"golang.org/x/text/encoding/simplifiedchinese"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -318,7 +321,18 @@ func fixedText(data []byte) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func freeText(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 {
|
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) {
|
func TestParseFrameParsesAuthenticationToken(t *testing.T) {
|
||||||
env, err := ParseFrame(buildFrame(0x0102, "064646848757", 8, []byte("g7gps")), 1782918600000, "115.231.168.135:43625")
|
env, err := ParseFrame(buildFrame(0x0102, "064646848757", 8, []byte("g7gps")), 1782918600000, "115.231.168.135:43625")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user