feat(go): add realtime kv projection
This commit is contained in:
78
go/vehicle-gateway/internal/realtime/kv_test.go
Normal file
78
go/vehicle-gateway/internal/realtime/kv_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package realtime
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
|
||||
)
|
||||
|
||||
func TestRealtimeKVFieldsFromGB32960ParsedDomains(t *testing.T) {
|
||||
rows := realtimeKVFields(envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolGB32960,
|
||||
VIN: "VIN001",
|
||||
EventTimeMS: 1000,
|
||||
ReceivedAtMS: 1100,
|
||||
EventID: "event-1",
|
||||
}, map[string]any{
|
||||
"data_units": []any{
|
||||
map[string]any{"type": "0x01", "name": "vehicle", "value": map[string]any{"soc_percent": 88.0}},
|
||||
map[string]any{"type": "0x30", "name": "gd_fc_stack", "value": map[string]any{
|
||||
"stack_count": 1,
|
||||
"summaries": []any{
|
||||
map[string]any{"stack_water_outlet_temp_c": 63, "hydrogen_inlet_pressure_kpa": 130},
|
||||
},
|
||||
}},
|
||||
},
|
||||
})
|
||||
|
||||
values := kvMap(rows)
|
||||
if values["vehicle/soc_percent"] != "88" {
|
||||
t.Fatalf("vehicle soc kv missing: %#v", values)
|
||||
}
|
||||
if values["gd_fc_stack/stack_water_outlet_temp_c"] != "63" {
|
||||
t.Fatalf("stack temp kv missing: %#v", values)
|
||||
}
|
||||
if values["gd_fc_stack/hydrogen_inlet_pressure_kpa"] != "130" {
|
||||
t.Fatalf("stack pressure kv missing: %#v", values)
|
||||
}
|
||||
if values["gd_fc_stack/summaries.0.stack_water_outlet_temp_c"] != "" {
|
||||
t.Fatalf("stack single summary should be flattened onto domain, got %#v", values)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRealtimeKVFieldsFromJT808LocationAndMQTTFields(t *testing.T) {
|
||||
jtRows := realtimeKVFields(envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolJT808,
|
||||
VIN: "VIN001",
|
||||
Fields: map[string]any{
|
||||
envelope.FieldLongitude: 121.1,
|
||||
envelope.FieldLatitude: 30.2,
|
||||
envelope.FieldTotalMileageKM: 10241.2,
|
||||
},
|
||||
}, map[string]any{})
|
||||
jtValues := kvMap(jtRows)
|
||||
if jtValues["location/total_mileage_km"] != "10241.2" || jtValues["location/longitude"] != "121.1" {
|
||||
t.Fatalf("jt808 location kv missing: %#v", jtValues)
|
||||
}
|
||||
|
||||
mqttRows := realtimeKVFields(envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolYutongMQTT,
|
||||
VIN: "VIN002",
|
||||
Fields: map[string]any{
|
||||
envelope.FieldSOCPercent: 76,
|
||||
"gear": 3,
|
||||
},
|
||||
}, map[string]any{})
|
||||
mqttValues := kvMap(mqttRows)
|
||||
if mqttValues["vehicle/soc_percent"] != "76" || mqttValues["vehicle/gear"] != "3" {
|
||||
t.Fatalf("mqtt vehicle kv missing: %#v", mqttValues)
|
||||
}
|
||||
}
|
||||
|
||||
func kvMap(rows []RealtimeKVField) map[string]string {
|
||||
out := map[string]string{}
|
||||
for _, row := range rows {
|
||||
out[row.Domain+"/"+row.Field] = row.Value
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user