refactor: centralize default history protocols

This commit is contained in:
lingniu
2026-07-01 11:28:16 +08:00
parent ebbf2ca8fb
commit d4c54b5d0f
4 changed files with 55 additions and 13 deletions

View File

@@ -0,0 +1,27 @@
package com.lingniu.ingest.eventhistory;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
import java.util.Locale;
import java.util.Set;
final class DefaultProductionProtocols {
private static final Set<String> SUPPORTED = Set.of(
"GB32960",
"JT808",
"MQTT_YUTONG");
private static final String SUPPORTED_MESSAGE = "protocol must be one of GB32960, JT808, MQTT_YUTONG";
private DefaultProductionProtocols() {
}
static String requireSupported(String protocol) {
String normalized = protocol == null ? "" : protocol.trim().toUpperCase(Locale.ROOT);
if (!SUPPORTED.contains(normalized)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, SUPPORTED_MESSAGE);
}
return normalized;
}
}

View File

@@ -21,7 +21,6 @@ import org.springframework.web.server.ResponseStatusException;
import java.io.IOException;
import java.time.Instant;
import java.util.List;
import java.util.Locale;
@RestController
@ConditionalOnProperty(prefix = "lingniu.ingest.event-history", name = "enabled", havingValue = "true")
@@ -64,7 +63,8 @@ public final class LocationHistoryController {
@RequestParam(required = false) String cursorTs,
@Parameter(description = "上一页返回的 nextCursor.id。", example = "location-xxx")
@RequestParam(required = false) String cursorId) throws IOException {
String normalizedProtocol = require(protocol, "protocol is required for location query").toUpperCase(Locale.ROOT);
String normalizedProtocol = DefaultProductionProtocols.requireSupported(
require(protocol, "protocol is required for location query"));
QueryTimeRange range = QueryTimeRange.parse(dateFrom, dateTo);
TdenginePage<TdengineLocationRow> page = reader.queryLocations(new TdengineLocationQuery(
normalizedProtocol,

View File

@@ -29,7 +29,6 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
@RestController
@ConditionalOnProperty(prefix = "lingniu.ingest.event-history", name = "enabled", havingValue = "true")
@@ -42,11 +41,6 @@ public final class RawFrameHistoryController {
private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<>() {};
private static final int LOCATION_ENRICH_LIMIT = 1;
private static final int TELEMETRY_ENRICH_LIMIT = 500;
private static final Set<String> DEFAULT_PRODUCTION_PROTOCOLS = Set.of(
"GB32960",
"JT808",
"MQTT_YUTONG");
private final TdengineHistoryReader reader;
public RawFrameHistoryController(TdengineHistoryReader reader) {
@@ -93,11 +87,7 @@ public final class RawFrameHistoryController {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"vehicleKey, vin, phone, messageId or parseStatus is required for raw frame query");
}
String normalizedProtocol = normalize(protocol).toUpperCase(Locale.ROOT);
if (!DEFAULT_PRODUCTION_PROTOCOLS.contains(normalizedProtocol)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"protocol must be one of GB32960, JT808, MQTT_YUTONG");
}
String normalizedProtocol = DefaultProductionProtocols.requireSupported(protocol);
QueryTimeRange range = QueryTimeRange.parse(dateFrom, dateTo);
TdenginePage<TdengineRawFrameRow> page = reader.queryRawFrames(new TdengineRawFrameQuery(
normalizedProtocol,