fix: verify mysql identity and jt808 field aliases
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
@@ -48,5 +48,10 @@
|
||||
<artifactId>spring-boot-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.lingniu.ingest.identity;
|
||||
|
||||
import com.lingniu.ingest.api.ProtocolId;
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class MySqlVehicleIdentityServiceJdbcTest {
|
||||
|
||||
@Test
|
||||
void registrationVinWriteBackIsQueryableThroughRealJdbc() throws Exception {
|
||||
JdbcDataSource dataSource = new JdbcDataSource();
|
||||
dataSource.setURL("jdbc:h2:mem:" + UUID.randomUUID()
|
||||
+ ";MODE=MySQL;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1");
|
||||
|
||||
try (MySqlVehicleIdentityService service =
|
||||
new MySqlVehicleIdentityService(dataSource, "vehicle_identity_binding")) {
|
||||
service.register(new VehicleRegistrationBinding(
|
||||
ProtocolId.JT808,
|
||||
"unknown",
|
||||
"13079961001",
|
||||
"dev61001",
|
||||
"粤B61001",
|
||||
44,
|
||||
4401,
|
||||
"maker-a",
|
||||
"type-a",
|
||||
1));
|
||||
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
Statement statement = connection.createStatement()) {
|
||||
ResultSet registered = statement.executeQuery("""
|
||||
SELECT vin, device_id, plate, province, city, maker, device_type, plate_color
|
||||
FROM vehicle_identity_binding_registration
|
||||
WHERE protocol = 'JT808' AND phone = '13079961001'
|
||||
""");
|
||||
assertThat(registered.next()).isTrue();
|
||||
assertThat(registered.getString("vin")).isEqualTo("unknown");
|
||||
assertThat(registered.getString("device_id")).isEqualTo("DEV61001");
|
||||
assertThat(registered.getString("plate")).isEqualTo("粤B61001");
|
||||
assertThat(registered.getInt("province")).isEqualTo(44);
|
||||
assertThat(registered.getInt("city")).isEqualTo(4401);
|
||||
assertThat(registered.getString("maker")).isEqualTo("MAKER-A");
|
||||
assertThat(registered.getString("device_type")).isEqualTo("TYPE-A");
|
||||
assertThat(registered.getInt("plate_color")).isEqualTo(1);
|
||||
|
||||
statement.executeUpdate("""
|
||||
UPDATE vehicle_identity_binding_registration
|
||||
SET vin = 'LNVIN00000061001'
|
||||
WHERE protocol = 'JT808' AND phone = '13079961001'
|
||||
""");
|
||||
}
|
||||
|
||||
service.refresh();
|
||||
|
||||
assertThat(service.resolve(new VehicleIdentityLookup(
|
||||
ProtocolId.JT808, "", "13079961001", "", "")).vin())
|
||||
.isEqualTo("LNVIN00000061001");
|
||||
assertThat(service.resolve(new VehicleIdentityLookup(
|
||||
ProtocolId.JT808, "", "", "dev61001", "")).vin())
|
||||
.isEqualTo("LNVIN00000061001");
|
||||
assertThat(service.resolve(new VehicleIdentityLookup(
|
||||
ProtocolId.JT808, "", "", "", "粤B61001")).vin())
|
||||
.isEqualTo("LNVIN00000061001");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@ConditionalOnProperty(prefix = "lingniu.ingest.event-history", name = "enabled", havingValue = "true")
|
||||
@@ -30,6 +31,15 @@ import java.util.Locale;
|
||||
@Tag(name = "telemetry-field-history-controller", description = "遥测字段历史分页查询接口。")
|
||||
public final class TelemetryFieldHistoryController {
|
||||
|
||||
private static final Map<String, String> JT808_FIELD_ALIASES = Map.of(
|
||||
"location.longitude", "longitude",
|
||||
"location.latitude", "latitude",
|
||||
"location.altitude_m", "altitude_m",
|
||||
"location.speed_kmh", "speed_kmh",
|
||||
"location.direction_deg", "direction_deg",
|
||||
"location.alarm_flag", "location_alarm_flag",
|
||||
"location.status_flag", "location_status_raw");
|
||||
|
||||
private final TdengineHistoryReader reader;
|
||||
|
||||
public TelemetryFieldHistoryController(TdengineHistoryReader reader) {
|
||||
@@ -67,7 +77,9 @@ public final class TelemetryFieldHistoryController {
|
||||
@Parameter(description = "上一页返回的 nextCursor.id。", example = "event-xxx#0")
|
||||
@RequestParam(required = false) String cursorId) throws IOException {
|
||||
String normalizedProtocol = require(protocol, "protocol is required for telemetry field query").toUpperCase(Locale.ROOT);
|
||||
String normalizedFieldKey = require(fieldKey, "fieldKey is required for telemetry field query");
|
||||
String normalizedFieldKey = normalizeFieldKey(
|
||||
normalizedProtocol,
|
||||
require(fieldKey, "fieldKey is required for telemetry field query"));
|
||||
QueryTimeRange range = QueryTimeRange.parse(dateFrom, dateTo);
|
||||
TdenginePage<TdengineTelemetryFieldRow> page = reader.queryTelemetryFields(new TdengineTelemetryFieldQuery(
|
||||
normalizedProtocol,
|
||||
@@ -81,6 +93,13 @@ public final class TelemetryFieldHistoryController {
|
||||
return TelemetryFieldPageResponse.from(page);
|
||||
}
|
||||
|
||||
private static String normalizeFieldKey(String protocol, String fieldKey) {
|
||||
if (!"JT808".equals(protocol)) {
|
||||
return fieldKey;
|
||||
}
|
||||
return JT808_FIELD_ALIASES.getOrDefault(fieldKey, fieldKey);
|
||||
}
|
||||
|
||||
private static String resolveVehicleKey(String protocol, String vehicleKey, String vin, String phone) {
|
||||
String explicitVehicleKey = trimToNull(vehicleKey);
|
||||
if (explicitVehicleKey != null) {
|
||||
|
||||
@@ -78,6 +78,7 @@ class TelemetryFieldHistoryControllerTest {
|
||||
null);
|
||||
|
||||
assertThat(reader.query.vehicleKey()).isEqualTo("jt808:g7gps");
|
||||
assertThat(reader.query.fieldKey()).isEqualTo("speed_kmh");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user