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.Base64;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
@@ -96,10 +97,13 @@ public final class YutongEventMapper implements EventMapper<MqttPayload> {
speed == null ? 0.0 : speed,
direction == null ? 0.0 : direction,
0, 0);
Map<String, String> locationMeta = mileage == null
? meta
: withMeta(meta, "total_mileage_km", formatDouble(mileage));
out.add(new VehicleEvent.Location(
UUID.randomUUID().toString(),
vin, ProtocolId.MQTT_YUTONG, payload.deviceTime(), ingestTime,
null, meta, loc));
null, locationMeta, loc));
}
return out;
}
@@ -233,6 +237,13 @@ public final class YutongEventMapper implements EventMapper<MqttPayload> {
}
}
private static String formatDouble(double value) {
String formatted = String.format(Locale.ROOT, "%.6f", value);
return formatted.indexOf('.') < 0
? formatted
: formatted.replaceAll("0+$", "").replaceAll("\\.$", "");
}
private record IdentityResolution(VehicleIdentity identity, String errorMessage) {
private static IdentityResolution ok(VehicleIdentity identity) {
return new IdentityResolution(identity, null);

View File

@@ -73,6 +73,7 @@ class YutongEventMapperTest {
VehicleEvent.Location loc = (VehicleEvent.Location) events.stream()
.filter(e -> e instanceof VehicleEvent.Location).findFirst().orElseThrow();
assertThat(loc.payload().longitude()).isEqualTo(116.397128, org.assertj.core.data.Offset.offset(0.000001));
assertThat(loc.metadata()).containsEntry("total_mileage_km", "123456.7");
}
@Test