fix: preserve mileage metadata on location events

This commit is contained in:
lingniu
2026-07-01 10:00:38 +08:00
parent 136b8a040c
commit da17acb09a
4 changed files with 37 additions and 2 deletions

View File

@@ -16,6 +16,7 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@@ -164,10 +165,13 @@ public final class Gb32960EventMapper implements EventMapper<Gb32960Message> {
p.longitude, p.latitude, 0.0,
v != null && v.speedKmh != null ? v.speedKmh : 0.0,
0.0, 0, p.statusFlag);
Map<String, String> locationMeta = v != null && v.totalMileageKm != null
? withMeta(meta, "total_mileage_km", formatDouble(v.totalMileageKm))
: meta;
out.add(new VehicleEvent.Location(
UUID.randomUUID().toString(),
header.vin(), ProtocolId.GB32960, eventTime, ingestTime,
null, meta, loc));
null, locationMeta, loc));
}
if (alarm != null && shouldEmitAlarm(alarm)) {
// 只在存在告警等级或氢安全关键位时发 Alarm减少正常高频帧对告警消费者的噪声。
@@ -330,4 +334,17 @@ public final class Gb32960EventMapper implements EventMapper<Gb32960Message> {
if (codes == null || codes.isEmpty()) return;
for (Long c : codes) out.add(prefix + "-" + Long.toHexString(c));
}
private static Map<String, String> withMeta(Map<String, String> meta, String key, String value) {
java.util.HashMap<String, String> copy = new java.util.HashMap<>(meta);
copy.put(key, value == null ? "" : value);
return Map.copyOf(copy);
}
private static String formatDouble(double value) {
String formatted = String.format(Locale.ROOT, "%.6f", value);
return formatted.indexOf('.') < 0
? formatted
: formatted.replaceAll("0+$", "").replaceAll("\\.$", "");
}
}

View File

@@ -42,6 +42,12 @@ class Gb32960EventMapperTest {
assertThat(events).anyMatch(e -> e instanceof VehicleEvent.Location);
assertThat(events).allMatch(e -> e.source() == ProtocolId.GB32960);
assertThat(events).allMatch(e -> e.vin().equals("LTEST000000000002"));
VehicleEvent.Location location = events.stream()
.filter(VehicleEvent.Location.class::isInstance)
.map(VehicleEvent.Location.class::cast)
.findFirst()
.orElseThrow();
assertThat(location.metadata()).containsEntry("total_mileage_km", "123456.7");
}
@Test