Files
lingniu-vehicle-ingest/go/vehicle-gateway/internal/protocol/jt808/parser_test.go
2026-07-01 21:43:16 +08:00

108 lines
3.2 KiB
Go

package jt808
import (
"encoding/hex"
"testing"
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
)
const sampleLocationFrame = "7E020000320133077954250001000000000048000301D2C4C707376139000A00E6004F26063016235701040001900C2504000000000202000030011F31010F867E"
func TestExtractFramesUnescapesAndParsesLocationWithTotalMileage(t *testing.T) {
stream, err := hex.DecodeString(sampleLocationFrame + "7E02000000")
if err != nil {
t.Fatal(err)
}
frames, remainder, err := ExtractFrames(stream)
if err != nil {
t.Fatalf("ExtractFrames() error = %v", err)
}
if len(frames) != 1 {
t.Fatalf("expected one complete frame, got %d", len(frames))
}
if hex.EncodeToString(remainder) != "7e02000000" {
t.Fatalf("expected partial remainder, got %s", hex.EncodeToString(remainder))
}
env, err := ParseFrame(frames[0], 1782745114999, "115.231.168.135:43625")
if err != nil {
t.Fatalf("ParseFrame() error = %v", err)
}
if env.Protocol != envelope.ProtocolJT808 {
t.Fatalf("protocol = %q", env.Protocol)
}
if env.MessageID != "0x0200" {
t.Fatalf("message id = %q", env.MessageID)
}
if env.Phone != "013307795425" {
t.Fatalf("phone = %q", env.Phone)
}
if env.Sequence != 1 {
t.Fatalf("sequence = %d", env.Sequence)
}
assertFloatField(t, env, envelope.FieldLatitude, 30.590151)
assertFloatField(t, env, envelope.FieldLongitude, 121.069881)
assertFloatField(t, env, envelope.FieldSpeedKMH, 23.0)
assertFloatField(t, env, envelope.FieldTotalMileageKM, 10241.2)
if env.Fields["direction_deg"] != uint16(79) {
t.Fatalf("direction = %#v", env.Fields["direction_deg"])
}
location, ok := env.Parsed["location"].(map[string]any)
if !ok {
t.Fatalf("parsed location missing: %#v", env.Parsed)
}
additional, ok := location["additional"].([]map[string]any)
if !ok || len(additional) == 0 {
t.Fatalf("additional fields missing: %#v", location["additional"])
}
if additional[0]["id"] != "0x01" || additional[0]["value_hex"] != "0001900C" {
t.Fatalf("unexpected first additional item: %#v", additional[0])
}
}
func TestExtractFramesHandlesEscapedPayload(t *testing.T) {
payload := []byte{0x02, 0x00, 0x00, 0x02, 0x01, 0x33, 0x07, 0x79, 0x54, 0x25, 0x00, 0x01, 0x7e, 0x7d}
frame := append([]byte{0x7e}, escape(append(payload, checksum(payload)))...)
frame = append(frame, 0x7e)
frames, remainder, err := ExtractFrames(frame)
if err != nil {
t.Fatalf("ExtractFrames() error = %v", err)
}
if len(frames) != 1 || len(remainder) != 0 {
t.Fatalf("unexpected split result frames=%d remainder=%s", len(frames), hex.EncodeToString(remainder))
}
if hex.EncodeToString(frames[0][:len(payload)]) != hex.EncodeToString(payload) {
t.Fatalf("frame was not unescaped: %s", hex.EncodeToString(frames[0]))
}
}
func assertFloatField(t *testing.T, env envelope.FrameEnvelope, key string, want float64) {
t.Helper()
got, ok := env.Fields[key].(float64)
if !ok {
t.Fatalf("field %s missing or not float64: %#v", key, env.Fields[key])
}
if got != want {
t.Fatalf("field %s = %v, want %v", key, got, want)
}
}
func escape(payload []byte) []byte {
var out []byte
for _, value := range payload {
switch value {
case 0x7e:
out = append(out, 0x7d, 0x02)
case 0x7d:
out = append(out, 0x7d, 0x01)
default:
out = append(out, value)
}
}
return out
}