feat: make gb32960 archive history query production ready
This commit is contained in:
@@ -0,0 +1,330 @@
|
||||
package com.lingniu.ingest.inbound.mqtt.client;
|
||||
|
||||
import com.hivemq.client.mqtt.datatypes.MqttQos;
|
||||
import com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient;
|
||||
import com.hivemq.client.mqtt.mqtt3.Mqtt3Client;
|
||||
import com.lingniu.ingest.api.ProtocolId;
|
||||
import com.lingniu.ingest.api.pipeline.RawFrame;
|
||||
import com.lingniu.ingest.core.dispatcher.Dispatcher;
|
||||
import com.lingniu.ingest.identity.InMemoryVehicleIdentityService;
|
||||
import com.lingniu.ingest.identity.VehicleIdentity;
|
||||
import com.lingniu.ingest.identity.VehicleIdentityLookup;
|
||||
import com.lingniu.ingest.identity.VehicleIdentityResolver;
|
||||
import com.lingniu.ingest.identity.VehicleIdentitySource;
|
||||
import com.lingniu.ingest.inbound.mqtt.config.MqttInboundProperties;
|
||||
import com.lingniu.ingest.inbound.mqtt.model.MqttPayload;
|
||||
import com.lingniu.ingest.inbound.mqtt.profile.MqttProfileRegistry;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* MQTT 多 endpoint 生命周期管理。
|
||||
*
|
||||
* <p>为每个 endpoint 创建一个 HiveMQ 异步客户端 → 连接 → 订阅 → 注册回调 →
|
||||
* 回调里解析 payload → {@link Dispatcher#dispatch(RawFrame)}。
|
||||
*
|
||||
* <p>Netty EventLoop 回调不做业务处理,虚拟线程由 Dispatcher 侧的 Disruptor 承接。
|
||||
*/
|
||||
public final class MqttEndpointManager implements AutoCloseable {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MqttEndpointManager.class);
|
||||
|
||||
private final MqttInboundProperties props;
|
||||
private final MqttProfileRegistry profileRegistry;
|
||||
private final VehicleIdentityResolver identityResolver;
|
||||
private final Dispatcher dispatcher;
|
||||
private final List<Mqtt3AsyncClient> clients = new ArrayList<>();
|
||||
|
||||
public MqttEndpointManager(MqttInboundProperties props,
|
||||
MqttProfileRegistry profileRegistry,
|
||||
Dispatcher dispatcher) {
|
||||
this(props, profileRegistry, new InMemoryVehicleIdentityService(), dispatcher);
|
||||
}
|
||||
|
||||
public MqttEndpointManager(MqttInboundProperties props,
|
||||
MqttProfileRegistry profileRegistry,
|
||||
VehicleIdentityResolver identityResolver,
|
||||
Dispatcher dispatcher) {
|
||||
this.props = props;
|
||||
this.profileRegistry = profileRegistry;
|
||||
this.identityResolver = identityResolver == null ? new InMemoryVehicleIdentityService() : identityResolver;
|
||||
this.dispatcher = dispatcher;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
for (MqttInboundProperties.Endpoint ep : props.getEndpoints()) {
|
||||
try {
|
||||
Mqtt3AsyncClient client = buildClient(ep);
|
||||
clients.add(client);
|
||||
connectAndSubscribe(client, ep);
|
||||
} catch (Exception e) {
|
||||
log.error("mqtt endpoint [{}] init failed, skipped", ep.getName(), e);
|
||||
publishOperationalError(ep, "init", e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Mqtt3AsyncClient buildClient(MqttInboundProperties.Endpoint ep) {
|
||||
URI uri = URI.create(ep.getUri());
|
||||
int port = uri.getPort() > 0 ? uri.getPort() : defaultPort(uri.getScheme());
|
||||
var builder = Mqtt3Client.builder()
|
||||
.identifier(ep.getClientId() == null || ep.getClientId().isBlank()
|
||||
? "lingniu-" + UUID.randomUUID() : ep.getClientId())
|
||||
.serverHost(uri.getHost())
|
||||
.serverPort(port)
|
||||
.automaticReconnectWithDefaultConfig();
|
||||
if (ep.getConnectionTimeoutSeconds() > 0) {
|
||||
builder = builder.transportConfig()
|
||||
.serverHost(uri.getHost())
|
||||
.serverPort(port)
|
||||
.socketConnectTimeout(ep.getConnectionTimeoutSeconds(), TimeUnit.SECONDS)
|
||||
.mqttConnectTimeout(ep.getConnectionTimeoutSeconds(), TimeUnit.SECONDS)
|
||||
.applyTransportConfig();
|
||||
}
|
||||
|
||||
if ("ssl".equalsIgnoreCase(uri.getScheme()) || "tls".equalsIgnoreCase(uri.getScheme())
|
||||
|| "mqtts".equalsIgnoreCase(uri.getScheme())) {
|
||||
if (hasCustomTls(ep.getTls())) {
|
||||
builder = builder.sslConfig(MqttTlsSupport.build(ep.getTls()));
|
||||
} else {
|
||||
builder = builder.sslWithDefaultConfig();
|
||||
}
|
||||
}
|
||||
return builder.buildAsync();
|
||||
}
|
||||
|
||||
private void connectAndSubscribe(Mqtt3AsyncClient client, MqttInboundProperties.Endpoint ep) {
|
||||
var connectBuilder = client.connectWith()
|
||||
.cleanSession(ep.isCleanSession());
|
||||
if (ep.getKeepAliveSeconds() > 0) {
|
||||
connectBuilder = connectBuilder.keepAlive(ep.getKeepAliveSeconds());
|
||||
}
|
||||
if (ep.getUsername() != null && !ep.getUsername().isBlank()) {
|
||||
connectBuilder = connectBuilder.simpleAuth()
|
||||
.username(ep.getUsername())
|
||||
.password(ep.getPassword() == null ? new byte[0] : ep.getPassword().getBytes())
|
||||
.applySimpleAuth();
|
||||
}
|
||||
connectBuilder.send().whenComplete((ack, err) -> {
|
||||
if (err != null) {
|
||||
log.error("mqtt endpoint [{}] connect failed", ep.getName(), err);
|
||||
publishOperationalError(ep, "connect", err.getMessage() == null ? err.getClass().getSimpleName() : err.getMessage());
|
||||
return;
|
||||
}
|
||||
log.info("mqtt endpoint [{}] connected code={}", ep.getName(), ack.getReturnCode());
|
||||
client.subscribeWith()
|
||||
.topicFilter(ep.getTopic())
|
||||
.qos(mapQos(ep.getQos()))
|
||||
.callback(publish -> onMessage(ep, publish.getTopic().toString(), publish.getPayloadAsBytes()))
|
||||
.send()
|
||||
.whenComplete((subAck, subErr) -> {
|
||||
if (subErr != null) {
|
||||
log.error("mqtt endpoint [{}] subscribe failed topic={}", ep.getName(), ep.getTopic(), subErr);
|
||||
publishOperationalError(ep, "subscribe",
|
||||
subErr.getMessage() == null ? subErr.getClass().getSimpleName() : subErr.getMessage());
|
||||
} else {
|
||||
log.info("mqtt endpoint [{}] subscribed topic={} qos={}",
|
||||
ep.getName(), ep.getTopic(), ep.getQos());
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private void onMessage(MqttInboundProperties.Endpoint ep, String topic, byte[] payload) {
|
||||
try {
|
||||
MqttPayload parsed = profileRegistry.parse(ep.getName(), ep.getProfile(), topic, payload);
|
||||
String externalDeviceId = firstNonBlank(parsed.deviceId(), parsed.vin());
|
||||
IdentityResolution identity = resolveIdentity(parsed);
|
||||
Map<String, String> meta = new HashMap<>(4);
|
||||
meta.put("vin", identity.identity().vin());
|
||||
meta.put("externalVin", parsed.externalVin() == null ? "" : parsed.externalVin());
|
||||
meta.put("phone", parsed.phone() == null ? "" : parsed.phone());
|
||||
meta.put("mqttDeviceId", externalDeviceId);
|
||||
meta.put("plateNo", parsed.plateNo() == null ? "" : parsed.plateNo());
|
||||
meta.put("identityResolved", Boolean.toString(identity.identity().resolved()));
|
||||
meta.put("identitySource", identity.identity().source().name());
|
||||
if (identity.errorMessage() != null) {
|
||||
meta.put("identityError", "true");
|
||||
meta.put("identityErrorMessage", identity.errorMessage());
|
||||
}
|
||||
meta.put("endpoint", ep.getName());
|
||||
meta.put("topic", topic);
|
||||
meta.put("profile", parsed.profile() == null ? "" : parsed.profile());
|
||||
if (parsed.data() != null && parsed.data().path("_parseError").asBoolean(false)) {
|
||||
meta.put("parseError", "true");
|
||||
meta.put("parseErrorMessage", parsed.data().path("_parseErrorMessage").asText(""));
|
||||
meta.put("unsupportedProfile",
|
||||
Boolean.toString(parsed.data().path("_unsupportedProfile").asBoolean(false)));
|
||||
}
|
||||
RawFrame rf = new RawFrame(
|
||||
ProtocolId.MQTT_YUTONG, 0, 0,
|
||||
parsed, payload, meta, Instant.now());
|
||||
dispatcher.dispatch(rf);
|
||||
} catch (Exception e) {
|
||||
log.warn("mqtt endpoint [{}] message handling failed topic={} len={}",
|
||||
ep.getName(), topic, payload.length, e);
|
||||
publishMessageError(ep, topic, payload, e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private IdentityResolution resolveIdentity(MqttPayload payload) {
|
||||
String externalDeviceId = firstNonBlank(payload.deviceId(), payload.vin());
|
||||
String externalVin = payload.externalVin() == null ? "" : payload.externalVin();
|
||||
String phone = payload.phone() == null ? "" : payload.phone();
|
||||
String plateNo = payload.plateNo() == null ? "" : payload.plateNo();
|
||||
try {
|
||||
VehicleIdentity bound = identityResolver.resolve(new VehicleIdentityLookup(
|
||||
ProtocolId.MQTT_YUTONG,
|
||||
externalVin,
|
||||
phone,
|
||||
externalDeviceId,
|
||||
plateNo));
|
||||
if (bound.resolved() || !looksLikeVin(externalDeviceId)) {
|
||||
return new IdentityResolution(bound, null);
|
||||
}
|
||||
return new IdentityResolution(identityResolver.resolve(new VehicleIdentityLookup(
|
||||
ProtocolId.MQTT_YUTONG,
|
||||
externalDeviceId,
|
||||
phone,
|
||||
externalDeviceId,
|
||||
plateNo)), null);
|
||||
} catch (RuntimeException e) {
|
||||
return new IdentityResolution(
|
||||
new VehicleIdentity("unknown", false, VehicleIdentitySource.UNKNOWN),
|
||||
e.getMessage() == null ? "" : e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static String firstNonBlank(String... values) {
|
||||
for (String value : values) {
|
||||
if (value != null && !value.isBlank()) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private void publishOperationalError(MqttInboundProperties.Endpoint ep, String phase, String reason) {
|
||||
String endpointName = ep == null ? "" : ep.getName();
|
||||
String profile = ep == null ? "" : ep.getProfile();
|
||||
String topic = ep == null ? "" : ep.getTopic();
|
||||
String json = "{\"operationalError\":true"
|
||||
+ ",\"endpoint\":\"" + escape(endpointName) + "\""
|
||||
+ ",\"profile\":\"" + escape(profile) + "\""
|
||||
+ ",\"topic\":\"" + escape(topic) + "\""
|
||||
+ ",\"phase\":\"" + escape(phase) + "\""
|
||||
+ ",\"reason\":\"" + escape(reason) + "\"}";
|
||||
byte[] raw = json.getBytes(StandardCharsets.UTF_8);
|
||||
MqttPayload payload = MqttProfileRegistry.operationalErrorPayload(
|
||||
endpointName, profile, topic, raw, phase, reason);
|
||||
Map<String, String> meta = new HashMap<>();
|
||||
meta.put("vin", "unknown");
|
||||
meta.put("identityResolved", "false");
|
||||
meta.put("identitySource", "UNKNOWN");
|
||||
meta.put("endpoint", endpointName == null ? "" : endpointName);
|
||||
meta.put("topic", topic == null ? "" : topic);
|
||||
meta.put("profile", profile == null ? "" : profile);
|
||||
meta.put("operationalError", "true");
|
||||
meta.put("phase", phase == null ? "" : phase);
|
||||
meta.put("reason", reason == null ? "" : reason);
|
||||
dispatcher.dispatch(new RawFrame(
|
||||
ProtocolId.MQTT_YUTONG,
|
||||
0,
|
||||
0,
|
||||
payload,
|
||||
raw,
|
||||
meta,
|
||||
Instant.now()));
|
||||
}
|
||||
|
||||
private void publishMessageError(MqttInboundProperties.Endpoint ep, String topic, byte[] raw, String reason) {
|
||||
String endpointName = ep == null ? "" : ep.getName();
|
||||
String profile = ep == null ? "" : ep.getProfile();
|
||||
byte[] payloadBytes = raw == null ? new byte[0] : raw;
|
||||
MqttPayload payload = MqttProfileRegistry.operationalErrorPayload(
|
||||
endpointName,
|
||||
profile,
|
||||
topic,
|
||||
payloadBytes,
|
||||
"message",
|
||||
reason);
|
||||
Map<String, String> meta = new HashMap<>();
|
||||
meta.put("vin", "unknown");
|
||||
meta.put("identityResolved", "false");
|
||||
meta.put("identitySource", "UNKNOWN");
|
||||
meta.put("endpoint", endpointName == null ? "" : endpointName);
|
||||
meta.put("topic", topic == null ? "" : topic);
|
||||
meta.put("profile", profile == null ? "" : profile);
|
||||
meta.put("operationalError", "true");
|
||||
meta.put("phase", "message");
|
||||
meta.put("reason", reason == null ? "" : reason);
|
||||
dispatcher.dispatch(new RawFrame(
|
||||
ProtocolId.MQTT_YUTONG,
|
||||
0,
|
||||
0,
|
||||
payload,
|
||||
payloadBytes,
|
||||
meta,
|
||||
Instant.now()));
|
||||
}
|
||||
|
||||
private static String escape(String value) {
|
||||
if (value == null) {
|
||||
return "";
|
||||
}
|
||||
return value.replace("\\", "\\\\").replace("\"", "\\\"");
|
||||
}
|
||||
|
||||
private static MqttQos mapQos(int level) {
|
||||
return switch (level) {
|
||||
case 0 -> MqttQos.AT_MOST_ONCE;
|
||||
case 2 -> MqttQos.EXACTLY_ONCE;
|
||||
default -> MqttQos.AT_LEAST_ONCE;
|
||||
};
|
||||
}
|
||||
|
||||
private static int defaultPort(String scheme) {
|
||||
if (scheme == null) return 1883;
|
||||
return switch (scheme.toLowerCase()) {
|
||||
case "ssl", "tls", "mqtts" -> 8883;
|
||||
default -> 1883;
|
||||
};
|
||||
}
|
||||
|
||||
private static boolean looksLikeVin(String value) {
|
||||
return value != null && value.trim().length() == 17;
|
||||
}
|
||||
|
||||
private record IdentityResolution(VehicleIdentity identity, String errorMessage) {}
|
||||
|
||||
private static boolean hasCustomTls(MqttInboundProperties.Tls tls) {
|
||||
return tls != null && (hasText(tls.getCaPem())
|
||||
|| (hasText(tls.getClientPem()) && hasText(tls.getClientKey())));
|
||||
}
|
||||
|
||||
private static boolean hasText(String value) {
|
||||
return value != null && !value.isBlank();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
for (Mqtt3AsyncClient c : clients) {
|
||||
try {
|
||||
c.disconnect();
|
||||
} catch (Exception ignored) {
|
||||
// best-effort
|
||||
}
|
||||
}
|
||||
log.info("mqtt endpoint manager stopped, clients={}", clients.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package com.lingniu.ingest.inbound.mqtt.client;
|
||||
|
||||
import com.hivemq.client.mqtt.MqttClientSslConfig;
|
||||
import com.hivemq.client.mqtt.MqttClientSslConfigBuilder;
|
||||
import com.lingniu.ingest.inbound.mqtt.config.MqttInboundProperties;
|
||||
|
||||
import javax.net.ssl.KeyManagerFactory;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.KeyStore;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public final class MqttTlsSupport {
|
||||
|
||||
private MqttTlsSupport() {}
|
||||
|
||||
public static MqttClientSslConfig build(MqttInboundProperties.Tls tls) {
|
||||
try {
|
||||
MqttClientSslConfigBuilder builder = MqttClientSslConfig.builder();
|
||||
if (tls != null && hasText(tls.getCaPem())) {
|
||||
builder.trustManagerFactory(trustManagerFactory(Path.of(tls.getCaPem())));
|
||||
}
|
||||
if (tls != null && hasText(tls.getClientPem()) && hasText(tls.getClientKey())) {
|
||||
builder.keyManagerFactory(keyManagerFactory(Path.of(tls.getClientPem()), Path.of(tls.getClientKey())));
|
||||
}
|
||||
return builder.build();
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("mqtt tls config build failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static TrustManagerFactory trustManagerFactory(Path caPem) throws Exception {
|
||||
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||
trustStore.load(null, null);
|
||||
int i = 0;
|
||||
for (Certificate cert : readCertificates(caPem)) {
|
||||
trustStore.setCertificateEntry("ca-" + i++, cert);
|
||||
}
|
||||
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
||||
tmf.init(trustStore);
|
||||
return tmf;
|
||||
}
|
||||
|
||||
private static KeyManagerFactory keyManagerFactory(Path clientPem, Path clientKey) throws Exception {
|
||||
List<Certificate> chain = readCertificates(clientPem);
|
||||
PrivateKey key = readPrivateKey(clientKey);
|
||||
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||
keyStore.load(null, null);
|
||||
char[] password = new char[0];
|
||||
keyStore.setKeyEntry("mqtt-client", key, password, chain.toArray(Certificate[]::new));
|
||||
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
||||
kmf.init(keyStore, password);
|
||||
return kmf;
|
||||
}
|
||||
|
||||
private static List<Certificate> readCertificates(Path pem) throws Exception {
|
||||
String text = Files.readString(pem, StandardCharsets.UTF_8);
|
||||
List<Certificate> certificates = new ArrayList<>();
|
||||
CertificateFactory factory = CertificateFactory.getInstance("X.509");
|
||||
for (String block : pemBlocks(text, "CERTIFICATE")) {
|
||||
try (ByteArrayInputStream in = new ByteArrayInputStream(Base64.getMimeDecoder().decode(block))) {
|
||||
certificates.add(factory.generateCertificate(in));
|
||||
}
|
||||
}
|
||||
if (certificates.isEmpty()) {
|
||||
throw new IllegalArgumentException("no certificate found in " + pem);
|
||||
}
|
||||
return certificates;
|
||||
}
|
||||
|
||||
private static PrivateKey readPrivateKey(Path pem) throws Exception {
|
||||
String text = Files.readString(pem, StandardCharsets.UTF_8);
|
||||
List<String> blocks = pemBlocks(text, "PRIVATE KEY");
|
||||
if (blocks.isEmpty()) {
|
||||
throw new IllegalArgumentException("no PKCS#8 private key found in " + pem);
|
||||
}
|
||||
byte[] der = Base64.getMimeDecoder().decode(blocks.getFirst());
|
||||
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(der);
|
||||
return KeyFactory.getInstance("RSA").generatePrivate(spec);
|
||||
}
|
||||
|
||||
private static List<String> pemBlocks(String text, String type) {
|
||||
String begin = "-----BEGIN " + type + "-----";
|
||||
String end = "-----END " + type + "-----";
|
||||
List<String> blocks = new ArrayList<>();
|
||||
int pos = 0;
|
||||
while (true) {
|
||||
int start = text.indexOf(begin, pos);
|
||||
if (start < 0) break;
|
||||
int contentStart = start + begin.length();
|
||||
int stop = text.indexOf(end, contentStart);
|
||||
if (stop < 0) break;
|
||||
blocks.add(text.substring(contentStart, stop).replaceAll("\\s+", ""));
|
||||
pos = stop + end.length();
|
||||
}
|
||||
return blocks;
|
||||
}
|
||||
|
||||
private static boolean hasText(String value) {
|
||||
return value != null && !value.isBlank();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.lingniu.ingest.inbound.mqtt.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.lingniu.ingest.core.dispatcher.Dispatcher;
|
||||
import com.lingniu.ingest.identity.VehicleIdentityResolver;
|
||||
import com.lingniu.ingest.inbound.mqtt.client.MqttEndpointManager;
|
||||
import com.lingniu.ingest.inbound.mqtt.handler.MqttRealtimeHandler;
|
||||
import com.lingniu.ingest.inbound.mqtt.mapper.YutongEventMapper;
|
||||
import com.lingniu.ingest.inbound.mqtt.parser.MqttPayloadParser;
|
||||
import com.lingniu.ingest.inbound.mqtt.profile.MqttProfile;
|
||||
import com.lingniu.ingest.inbound.mqtt.profile.MqttProfileRegistry;
|
||||
import com.lingniu.ingest.inbound.mqtt.profile.YutongMqttProfile;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(MqttInboundProperties.class)
|
||||
@ConditionalOnProperty(prefix = "lingniu.ingest.mqtt", name = "enabled", havingValue = "true")
|
||||
public class MqttInboundAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ObjectMapper mqttObjectMapper() {
|
||||
return new ObjectMapper();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MqttPayloadParser mqttPayloadParser(ObjectMapper objectMapper) {
|
||||
return new MqttPayloadParser(objectMapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public YutongEventMapper yutongEventMapper(VehicleIdentityResolver identityResolver) {
|
||||
return new YutongEventMapper(identityResolver);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public YutongMqttProfile yutongMqttProfile(MqttPayloadParser parser, YutongEventMapper mapper) {
|
||||
return new YutongMqttProfile(parser, mapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MqttProfileRegistry mqttProfileRegistry(List<MqttProfile> profiles) {
|
||||
return new MqttProfileRegistry(profiles);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MqttRealtimeHandler mqttRealtimeHandler(MqttProfileRegistry profileRegistry) {
|
||||
return new MqttRealtimeHandler(profileRegistry);
|
||||
}
|
||||
|
||||
@Bean(destroyMethod = "close")
|
||||
@ConditionalOnMissingBean
|
||||
public MqttEndpointManager mqttEndpointManager(MqttInboundProperties props,
|
||||
MqttProfileRegistry profileRegistry,
|
||||
VehicleIdentityResolver identityResolver,
|
||||
Dispatcher dispatcher) {
|
||||
return new MqttEndpointManager(props, profileRegistry, identityResolver, dispatcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Spring Boot 启动完成后触发 MQTT 连接,避免与 Netty / Kafka 启动竞争。
|
||||
*/
|
||||
@Bean
|
||||
public ApplicationRunner mqttStartupRunner(MqttEndpointManager manager) {
|
||||
return new ApplicationRunner() {
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
manager.start();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.lingniu.ingest.inbound.mqtt.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* MQTT 接入配置。支持多 endpoint(每个 endpoint 可对应一个车厂 / 一个 broker)。
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "lingniu.ingest.mqtt")
|
||||
public class MqttInboundProperties {
|
||||
|
||||
private boolean enabled = false;
|
||||
private List<Endpoint> endpoints = new ArrayList<>();
|
||||
|
||||
public boolean isEnabled() { return enabled; }
|
||||
public void setEnabled(boolean enabled) { this.enabled = enabled; }
|
||||
public List<Endpoint> getEndpoints() { return endpoints; }
|
||||
public void setEndpoints(List<Endpoint> endpoints) { this.endpoints = endpoints; }
|
||||
|
||||
public static class Endpoint {
|
||||
/** 逻辑名称,用于指标与日志。 */
|
||||
private String name;
|
||||
/** 对端 URI。形如 ssl://host:port 或 tcp://host:port。 */
|
||||
private String uri;
|
||||
/** 订阅 topic,支持 + / # 通配符。 */
|
||||
private String topic;
|
||||
/** QoS 0/1/2。 */
|
||||
private int qos = 1;
|
||||
private String clientId;
|
||||
private String username;
|
||||
private String password;
|
||||
private boolean cleanSession = false;
|
||||
private int keepAliveSeconds = 20;
|
||||
private int connectionTimeoutSeconds = 10;
|
||||
/** JSON 字段适配器 profile。默认 yutong。未来可支持 haipote / others。 */
|
||||
private String profile = "yutong";
|
||||
private Tls tls = new Tls();
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
public String getUri() { return uri; }
|
||||
public void setUri(String uri) { this.uri = uri; }
|
||||
public String getTopic() { return topic; }
|
||||
public void setTopic(String topic) { this.topic = topic; }
|
||||
public int getQos() { return qos; }
|
||||
public void setQos(int qos) { this.qos = qos; }
|
||||
public String getClientId() { return clientId; }
|
||||
public void setClientId(String clientId) { this.clientId = clientId; }
|
||||
public String getUsername() { return username; }
|
||||
public void setUsername(String username) { this.username = username; }
|
||||
public String getPassword() { return password; }
|
||||
public void setPassword(String password) { this.password = password; }
|
||||
public boolean isCleanSession() { return cleanSession; }
|
||||
public void setCleanSession(boolean cleanSession) { this.cleanSession = cleanSession; }
|
||||
public int getKeepAliveSeconds() { return keepAliveSeconds; }
|
||||
public void setKeepAliveSeconds(int keepAliveSeconds) { this.keepAliveSeconds = keepAliveSeconds; }
|
||||
public int getConnectionTimeoutSeconds() { return connectionTimeoutSeconds; }
|
||||
public void setConnectionTimeoutSeconds(int connectionTimeoutSeconds) { this.connectionTimeoutSeconds = connectionTimeoutSeconds; }
|
||||
public String getProfile() { return profile; }
|
||||
public void setProfile(String profile) { this.profile = profile; }
|
||||
public Tls getTls() { return tls; }
|
||||
public void setTls(Tls tls) { this.tls = tls; }
|
||||
}
|
||||
|
||||
public static class Tls {
|
||||
private String caPem;
|
||||
private String clientPem;
|
||||
private String clientKey;
|
||||
public String getCaPem() { return caPem; }
|
||||
public void setCaPem(String caPem) { this.caPem = caPem; }
|
||||
public String getClientPem() { return clientPem; }
|
||||
public void setClientPem(String clientPem) { this.clientPem = clientPem; }
|
||||
public String getClientKey() { return clientKey; }
|
||||
public void setClientKey(String clientKey) { this.clientKey = clientKey; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.lingniu.ingest.inbound.mqtt.handler;
|
||||
|
||||
import com.lingniu.ingest.api.ProtocolId;
|
||||
import com.lingniu.ingest.api.annotation.EventEmit;
|
||||
import com.lingniu.ingest.api.annotation.MessageMapping;
|
||||
import com.lingniu.ingest.api.annotation.ProtocolHandler;
|
||||
import com.lingniu.ingest.api.annotation.RateLimited;
|
||||
import com.lingniu.ingest.api.event.VehicleEvent;
|
||||
import com.lingniu.ingest.inbound.mqtt.model.MqttPayload;
|
||||
import com.lingniu.ingest.inbound.mqtt.profile.MqttProfileRegistry;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* MQTT 接入 Handler。
|
||||
*
|
||||
* <p>MQTT 没有"命令字"概念,使用默认的 wildcard 匹配({@code command} 为空)。
|
||||
* 同一 Dispatcher 仍能正确路由,因为协议 ID 做了隔离。
|
||||
*/
|
||||
@ProtocolHandler(protocol = ProtocolId.MQTT_YUTONG)
|
||||
public class MqttRealtimeHandler {
|
||||
|
||||
private final MqttProfileRegistry profileRegistry;
|
||||
|
||||
public MqttRealtimeHandler(MqttProfileRegistry profileRegistry) {
|
||||
this.profileRegistry = profileRegistry;
|
||||
}
|
||||
|
||||
@MessageMapping(desc = "MQTT profile 实时数据")
|
||||
@RateLimited(perVin = 20)
|
||||
@EventEmit({VehicleEvent.Realtime.class, VehicleEvent.Location.class, VehicleEvent.Passthrough.class})
|
||||
public List<VehicleEvent> onMqtt(MqttPayload payload) {
|
||||
return profileRegistry.toEvents(payload);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
package com.lingniu.ingest.inbound.mqtt.mapper;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.lingniu.ingest.api.ProtocolId;
|
||||
import com.lingniu.ingest.api.event.LocationPayload;
|
||||
import com.lingniu.ingest.api.event.RealtimePayload;
|
||||
import com.lingniu.ingest.api.event.VehicleEvent;
|
||||
import com.lingniu.ingest.api.spi.EventMapper;
|
||||
import com.lingniu.ingest.identity.InMemoryVehicleIdentityService;
|
||||
import com.lingniu.ingest.identity.VehicleIdentity;
|
||||
import com.lingniu.ingest.identity.VehicleIdentityLookup;
|
||||
import com.lingniu.ingest.identity.VehicleIdentityResolver;
|
||||
import com.lingniu.ingest.identity.VehicleIdentitySource;
|
||||
import com.lingniu.ingest.inbound.mqtt.model.MqttPayload;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 宇通 MQTT payload → 领域事件。
|
||||
*
|
||||
* <p>字段名取自旧项目的 {@code ReceptionDataVO}(大写 + 下划线),对字段映射一致性做出显式断言,
|
||||
* 便于与旧服务双跑对账。
|
||||
*/
|
||||
public final class YutongEventMapper implements EventMapper<MqttPayload> {
|
||||
|
||||
private final VehicleIdentityResolver identityResolver;
|
||||
|
||||
public YutongEventMapper() {
|
||||
this(new InMemoryVehicleIdentityService());
|
||||
}
|
||||
|
||||
public YutongEventMapper(VehicleIdentityResolver identityResolver) {
|
||||
this.identityResolver = identityResolver == null ? new InMemoryVehicleIdentityService() : identityResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<VehicleEvent> toEvents(MqttPayload payload) {
|
||||
JsonNode d = payload.data();
|
||||
if (d == null || !d.isObject()) return List.of();
|
||||
|
||||
Instant ingestTime = Instant.now();
|
||||
String externalDeviceId = firstNonBlank(payload.deviceId(), payload.vin());
|
||||
IdentityResolution identity = resolveIdentity(payload);
|
||||
String vin = identity.vin();
|
||||
Map<String, String> meta = metadata(payload, externalDeviceId, vin, identity);
|
||||
|
||||
if (d.path("_parseError").asBoolean(false)) {
|
||||
return List.of(new VehicleEvent.Passthrough(
|
||||
UUID.randomUUID().toString(),
|
||||
vin == null || vin.isBlank() ? "unknown" : vin,
|
||||
ProtocolId.MQTT_YUTONG,
|
||||
payload.deviceTime(),
|
||||
ingestTime,
|
||||
null,
|
||||
withMeta(meta,
|
||||
"parseError", "true",
|
||||
"parseErrorMessage", d.path("_parseErrorMessage").asText("")),
|
||||
0,
|
||||
rawBytes(d.path("_rawBase64").asText(""))));
|
||||
}
|
||||
|
||||
Double speed = optDouble(d, "METER_SPEED");
|
||||
Double mileage = optDouble(d, "TOTAL_MILEAGE");
|
||||
Double soc = optDouble(d, "BATTERY_CAPACITY_SOC");
|
||||
Double fcVoltage = optDouble(d, "VOLTAGE_OF_FC");
|
||||
Double fcCurrent = optDouble(d, "CURRENT_OF_FC");
|
||||
Double fcTemp = optDouble(d, "TEMPT_OF_FC");
|
||||
Double leftH2 = optDouble(d, "LEFT_HYDROGEN");
|
||||
Double lowH2Pressure = optDouble(d, "HYDROGEN_LOW_PRESSURE");
|
||||
Double longitude = optCoord(d, "LONGITUDE");
|
||||
Double latitude = optCoord(d, "LATITUDE");
|
||||
Double direction = optDouble(d, "GPSDirection");
|
||||
Double acc = optDouble(d, "ACC_PEDAL_APT");
|
||||
Double brake = optDouble(d, "BRAKEPEDALAPT");
|
||||
Integer gear = optInt(d, "ON_GEAR");
|
||||
|
||||
RealtimePayload realtime = new RealtimePayload(
|
||||
speed, mileage, soc, null, null,
|
||||
fcVoltage, fcCurrent, fcTemp, leftH2, null, lowH2Pressure,
|
||||
null, null, null, gear, acc, brake,
|
||||
longitude, latitude, null, direction,
|
||||
null, null);
|
||||
|
||||
List<VehicleEvent> out = new ArrayList<>(2);
|
||||
out.add(new VehicleEvent.Realtime(
|
||||
UUID.randomUUID().toString(),
|
||||
vin, ProtocolId.MQTT_YUTONG, payload.deviceTime(), ingestTime,
|
||||
null, meta, realtime));
|
||||
|
||||
if (longitude != null && latitude != null) {
|
||||
LocationPayload loc = new LocationPayload(
|
||||
longitude, latitude, 0.0,
|
||||
speed == null ? 0.0 : speed,
|
||||
direction == null ? 0.0 : direction,
|
||||
0, 0);
|
||||
out.add(new VehicleEvent.Location(
|
||||
UUID.randomUUID().toString(),
|
||||
vin, ProtocolId.MQTT_YUTONG, payload.deviceTime(), ingestTime,
|
||||
null, meta, loc));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private IdentityResolution resolveIdentity(MqttPayload payload) {
|
||||
String externalDeviceId = firstNonBlank(payload.deviceId(), payload.vin());
|
||||
String externalVin = payload.externalVin() == null ? "" : payload.externalVin();
|
||||
String phone = payload.phone() == null ? "" : payload.phone();
|
||||
String plateNo = payload.plateNo() == null ? "" : payload.plateNo();
|
||||
try {
|
||||
if (externalDeviceId == null
|
||||
|| externalDeviceId.isBlank()
|
||||
|| "unknown".equalsIgnoreCase(externalDeviceId.trim())) {
|
||||
if (!externalVin.isBlank() || !phone.isBlank() || !plateNo.isBlank()) {
|
||||
return IdentityResolution.ok(identityResolver.resolve(new VehicleIdentityLookup(
|
||||
ProtocolId.MQTT_YUTONG,
|
||||
externalVin,
|
||||
phone,
|
||||
"",
|
||||
plateNo)));
|
||||
}
|
||||
return IdentityResolution.ok(new VehicleIdentity("unknown", false, VehicleIdentitySource.UNKNOWN));
|
||||
}
|
||||
VehicleIdentity bound = identityResolver.resolve(new VehicleIdentityLookup(
|
||||
ProtocolId.MQTT_YUTONG,
|
||||
externalVin,
|
||||
phone,
|
||||
externalDeviceId,
|
||||
plateNo));
|
||||
if (bound.resolved() || !looksLikeVin(externalDeviceId)) {
|
||||
return IdentityResolution.ok(bound);
|
||||
}
|
||||
return IdentityResolution.ok(identityResolver.resolve(new VehicleIdentityLookup(
|
||||
ProtocolId.MQTT_YUTONG,
|
||||
externalDeviceId,
|
||||
phone,
|
||||
externalDeviceId,
|
||||
plateNo)));
|
||||
} catch (RuntimeException e) {
|
||||
return new IdentityResolution(
|
||||
new VehicleIdentity("unknown", false, VehicleIdentitySource.UNKNOWN),
|
||||
e.getMessage() == null ? "" : e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static String firstNonBlank(String... values) {
|
||||
for (String value : values) {
|
||||
if (value != null && !value.isBlank()) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private static boolean looksLikeVin(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
String trimmed = value.trim();
|
||||
return trimmed.length() == 17;
|
||||
}
|
||||
|
||||
private static Double optDouble(JsonNode n, String field) {
|
||||
JsonNode v = n.get(field);
|
||||
if (v == null || v.isNull() || !v.isNumber()) return null;
|
||||
return v.asDouble();
|
||||
}
|
||||
|
||||
private static Integer optInt(JsonNode n, String field) {
|
||||
JsonNode v = n.get(field);
|
||||
if (v == null || v.isNull() || !v.isNumber()) return null;
|
||||
return v.asInt();
|
||||
}
|
||||
|
||||
/** 宇通坐标约定:1e6 整数 或 已经是小数。同时过滤无效值 999999 / 0。 */
|
||||
private static Double optCoord(JsonNode n, String field) {
|
||||
Double v = optDouble(n, field);
|
||||
if (v == null) return null;
|
||||
if (v.intValue() == 999_999 || v == 0.0) return null;
|
||||
if (Math.abs(v) > 1000) return v / 1_000_000.0;
|
||||
return v;
|
||||
}
|
||||
|
||||
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 Map<String, String> metadata(MqttPayload payload,
|
||||
String externalDeviceId,
|
||||
String vin,
|
||||
IdentityResolution identity) {
|
||||
java.util.HashMap<String, String> meta = new java.util.HashMap<>();
|
||||
meta.put("endpoint", payload.endpointName() == null ? "" : payload.endpointName());
|
||||
meta.put("topic", payload.topic() == null ? "" : payload.topic());
|
||||
meta.put("profile", payload.profile() == null ? "" : payload.profile());
|
||||
meta.put("externalVin", payload.externalVin() == null ? "" : payload.externalVin());
|
||||
meta.put("phone", payload.phone() == null ? "" : payload.phone());
|
||||
meta.put("mqttDeviceId", externalDeviceId == null ? "" : externalDeviceId);
|
||||
meta.put("plateNo", payload.plateNo() == null ? "" : payload.plateNo());
|
||||
meta.put("vin", vin == null || vin.isBlank() ? "unknown" : vin);
|
||||
meta.put("identityResolved", Boolean.toString(identity.identity().resolved()));
|
||||
meta.put("identitySource", identity.identity().source().name());
|
||||
if (identity.errorMessage() != null) {
|
||||
meta.put("identityError", "true");
|
||||
meta.put("identityErrorMessage", identity.errorMessage());
|
||||
}
|
||||
return Map.copyOf(meta);
|
||||
}
|
||||
|
||||
private static Map<String, String> withMeta(Map<String, String> meta,
|
||||
String firstKey,
|
||||
String firstValue,
|
||||
String secondKey,
|
||||
String secondValue) {
|
||||
java.util.HashMap<String, String> copy = new java.util.HashMap<>(meta);
|
||||
copy.put(firstKey, firstValue == null ? "" : firstValue);
|
||||
copy.put(secondKey, secondValue == null ? "" : secondValue);
|
||||
return Map.copyOf(copy);
|
||||
}
|
||||
|
||||
private static byte[] rawBytes(String base64) {
|
||||
if (base64 == null || base64.isBlank()) {
|
||||
return new byte[0];
|
||||
}
|
||||
try {
|
||||
return Base64.getDecoder().decode(base64);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
|
||||
private record IdentityResolution(VehicleIdentity identity, String errorMessage) {
|
||||
private static IdentityResolution ok(VehicleIdentity identity) {
|
||||
return new IdentityResolution(identity, null);
|
||||
}
|
||||
|
||||
private String vin() {
|
||||
return identity.vin();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.lingniu.ingest.inbound.mqtt.model;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* MQTT 解析后的统一消息体:{@code Dispatcher} 收到的 RawFrame payload 就是这个。
|
||||
*
|
||||
* @param endpointName 来源 endpoint 逻辑名
|
||||
* @param profile 字段映射 profile(如 yutong / haipote)
|
||||
* @param topic 原始 topic
|
||||
* @param vin VIN(解析自 payload 的 device / vin 字段,保留兼容旧调用)
|
||||
* @param externalVin 外部 VIN 字段
|
||||
* @param phone 外部手机号 / SIM 字段
|
||||
* @param deviceId 外部设备号 / 终端 ID / IMEI 字段
|
||||
* @param plateNo 外部车牌字段
|
||||
* @param deviceTime 设备上报时刻
|
||||
* @param data 原始 JSON 树,供 Mapper 按 profile 提取字段
|
||||
*/
|
||||
public record MqttPayload(
|
||||
String endpointName,
|
||||
String profile,
|
||||
String topic,
|
||||
String vin,
|
||||
String externalVin,
|
||||
String phone,
|
||||
String deviceId,
|
||||
String plateNo,
|
||||
Instant deviceTime,
|
||||
JsonNode data
|
||||
) {
|
||||
public MqttPayload(String endpointName,
|
||||
String profile,
|
||||
String topic,
|
||||
String vin,
|
||||
Instant deviceTime,
|
||||
JsonNode data) {
|
||||
this(endpointName, profile, topic, vin, "", "", vin, "", deviceTime, data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.lingniu.ingest.inbound.mqtt.parser;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.lingniu.ingest.api.spi.DecodeException;
|
||||
import com.lingniu.ingest.inbound.mqtt.model.MqttPayload;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* 宇通 MQTT JSON 消息基础解析器(对标旧 {@code VehicleDataReceptionVO} + {@code ReceptionDataVO})。
|
||||
*
|
||||
* <p>约定宇通 MQTT 的 payload JSON 形如:
|
||||
* <pre>{@code
|
||||
* {
|
||||
* "id": "xxx",
|
||||
* "device": "LTEST000000000001", // VIN
|
||||
* "code": "C001",
|
||||
* "time": "20260413100000", // yyyyMMddHHmmss
|
||||
* "data": { "METER_SPEED": 52, "LATITUDE": 39916527, ... }
|
||||
* }
|
||||
* }</pre>
|
||||
*
|
||||
* <p>其它厂商只需新增 {@code MqttProfile} 实现,公共 MQTT endpoint 生命周期不需要修改。
|
||||
*/
|
||||
public final class MqttPayloadParser {
|
||||
|
||||
private static final DateTimeFormatter TIME_FMT = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
|
||||
private static final ZoneId ZONE = ZoneId.of("Asia/Shanghai");
|
||||
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
public MqttPayloadParser(ObjectMapper mapper) {
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
public MqttPayload parse(String endpointName, String profile, String topic, byte[] bytes) {
|
||||
try {
|
||||
JsonNode root = mapper.readTree(bytes);
|
||||
String externalVin = text(root, "vin", "VIN");
|
||||
String deviceId = text(root, "device", "deviceId", "terminalId", "imei", "IMEI");
|
||||
String phone = text(root, "sim", "phone", "mobile");
|
||||
String plateNo = text(root, "plateNo", "carNo", "plate");
|
||||
String vin = !externalVin.isBlank() ? externalVin : deviceId;
|
||||
Instant deviceTime = parseTime(text(root, "time"));
|
||||
JsonNode data = root.has("data") ? root.get("data") : root;
|
||||
return new MqttPayload(endpointName, profile, topic,
|
||||
vin, externalVin, phone, deviceId, plateNo, deviceTime, data);
|
||||
} catch (Exception e) {
|
||||
throw new DecodeException("mqtt payload parse failed: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String text(JsonNode root, String... keys) {
|
||||
for (String k : keys) {
|
||||
JsonNode n = root.get(k);
|
||||
if (n != null && !n.isNull()) return n.asText();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private static Instant parseTime(String raw) {
|
||||
if (raw == null || raw.isEmpty()) return Instant.now();
|
||||
try {
|
||||
return LocalDateTime.parse(raw, TIME_FMT).atZone(ZONE).toInstant();
|
||||
} catch (Exception e) {
|
||||
return Instant.now();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.lingniu.ingest.inbound.mqtt.profile;
|
||||
|
||||
import com.lingniu.ingest.api.event.VehicleEvent;
|
||||
import com.lingniu.ingest.inbound.mqtt.model.MqttPayload;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* MQTT 厂商 profile 边界。
|
||||
*
|
||||
* <p>endpoint 只关心 profile 名称,具体 payload 字段结构、VIN/时间提取和事件映射都收敛在 profile 内。
|
||||
*/
|
||||
public interface MqttProfile {
|
||||
|
||||
String name();
|
||||
|
||||
MqttPayload parse(String endpointName, String topic, byte[] bytes);
|
||||
|
||||
List<VehicleEvent> toEvents(MqttPayload payload);
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package com.lingniu.ingest.inbound.mqtt.profile;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.lingniu.ingest.api.ProtocolId;
|
||||
import com.lingniu.ingest.api.event.VehicleEvent;
|
||||
import com.lingniu.ingest.api.spi.DecodeException;
|
||||
import com.lingniu.ingest.inbound.mqtt.model.MqttPayload;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Base64;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class MqttProfileRegistry {
|
||||
|
||||
private final Map<String, MqttProfile> profiles;
|
||||
|
||||
public MqttProfileRegistry(Collection<MqttProfile> profiles) {
|
||||
Map<String, MqttProfile> indexed = new LinkedHashMap<>();
|
||||
for (MqttProfile profile : profiles) {
|
||||
String key = normalize(profile.name());
|
||||
if (key.isEmpty()) {
|
||||
throw new IllegalArgumentException("mqtt profile name must not be blank");
|
||||
}
|
||||
MqttProfile old = indexed.putIfAbsent(key, profile);
|
||||
if (old != null) {
|
||||
throw new IllegalArgumentException("duplicated mqtt profile: " + key);
|
||||
}
|
||||
}
|
||||
this.profiles = Map.copyOf(indexed);
|
||||
}
|
||||
|
||||
public MqttPayload parse(String endpointName, String profileName, String topic, byte[] bytes) {
|
||||
MqttProfile profile;
|
||||
try {
|
||||
profile = resolve(profileName);
|
||||
} catch (DecodeException e) {
|
||||
return parseErrorPayload(endpointName, profileName, topic, bytes, e.getMessage(), true);
|
||||
}
|
||||
try {
|
||||
return profile.parse(endpointName, topic, bytes);
|
||||
} catch (DecodeException e) {
|
||||
return parseErrorPayload(endpointName, profile.name(), topic, bytes, e.getMessage(), false);
|
||||
}
|
||||
}
|
||||
|
||||
public List<VehicleEvent> toEvents(MqttPayload payload) {
|
||||
if (payload != null && payload.data() != null
|
||||
&& payload.data().path("_operationalError").asBoolean(false)) {
|
||||
return List.of(operationalErrorEvent(payload));
|
||||
}
|
||||
if (payload != null && payload.data() != null
|
||||
&& payload.data().path("_unsupportedProfile").asBoolean(false)) {
|
||||
return List.of(parseErrorEvent(payload));
|
||||
}
|
||||
return resolve(payload.profile()).toEvents(payload);
|
||||
}
|
||||
|
||||
public MqttProfile resolve(String profileName) {
|
||||
String key = normalize(profileName);
|
||||
MqttProfile profile = profiles.get(key);
|
||||
if (profile == null) {
|
||||
throw new DecodeException("unsupported mqtt profile: " + profileName);
|
||||
}
|
||||
return profile;
|
||||
}
|
||||
|
||||
private static String normalize(String raw) {
|
||||
return raw == null ? "" : raw.trim().toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
private static MqttPayload parseErrorPayload(String endpointName,
|
||||
String profileName,
|
||||
String topic,
|
||||
byte[] bytes,
|
||||
String message,
|
||||
boolean unsupportedProfile) {
|
||||
ObjectNode data = JsonNodeFactory.instance.objectNode();
|
||||
data.put("_parseError", true);
|
||||
data.put("_parseErrorMessage", message == null ? "" : message);
|
||||
data.put("_unsupportedProfile", unsupportedProfile);
|
||||
data.put("_rawBase64", Base64.getEncoder().encodeToString(bytes == null ? new byte[0] : bytes));
|
||||
return new MqttPayload(endpointName, normalize(profileName), topic, "unknown", Instant.now(), data);
|
||||
}
|
||||
|
||||
public static MqttPayload operationalErrorPayload(String endpointName,
|
||||
String profileName,
|
||||
String topic,
|
||||
byte[] bytes,
|
||||
String phase,
|
||||
String reason) {
|
||||
ObjectNode data = JsonNodeFactory.instance.objectNode();
|
||||
data.put("_operationalError", true);
|
||||
data.put("_phase", phase == null ? "" : phase);
|
||||
data.put("_reason", reason == null ? "" : reason);
|
||||
data.put("_rawBase64", Base64.getEncoder().encodeToString(bytes == null ? new byte[0] : bytes));
|
||||
return new MqttPayload(endpointName, normalize(profileName), topic, "unknown", Instant.now(), data);
|
||||
}
|
||||
|
||||
private static VehicleEvent.Passthrough parseErrorEvent(MqttPayload payload) {
|
||||
Map<String, String> metadata = new HashMap<>();
|
||||
metadata.put("endpoint", payload.endpointName() == null ? "" : payload.endpointName());
|
||||
metadata.put("topic", payload.topic() == null ? "" : payload.topic());
|
||||
metadata.put("profile", payload.profile() == null ? "" : payload.profile());
|
||||
metadata.put("parseError", "true");
|
||||
metadata.put("unsupportedProfile", Boolean.toString(payload.data().path("_unsupportedProfile").asBoolean(false)));
|
||||
metadata.put("parseErrorMessage", payload.data().path("_parseErrorMessage").asText(""));
|
||||
addUnknownIdentity(metadata);
|
||||
return new VehicleEvent.Passthrough(
|
||||
UUID.randomUUID().toString(),
|
||||
"unknown",
|
||||
ProtocolId.MQTT_YUTONG,
|
||||
payload.deviceTime(),
|
||||
Instant.now(),
|
||||
null,
|
||||
Map.copyOf(metadata),
|
||||
0,
|
||||
rawBytes(payload.data().path("_rawBase64").asText("")));
|
||||
}
|
||||
|
||||
private static VehicleEvent.Passthrough operationalErrorEvent(MqttPayload payload) {
|
||||
Map<String, String> metadata = new HashMap<>();
|
||||
metadata.put("endpoint", payload.endpointName() == null ? "" : payload.endpointName());
|
||||
metadata.put("topic", payload.topic() == null ? "" : payload.topic());
|
||||
metadata.put("profile", payload.profile() == null ? "" : payload.profile());
|
||||
metadata.put("operationalError", "true");
|
||||
metadata.put("phase", payload.data().path("_phase").asText(""));
|
||||
metadata.put("reason", payload.data().path("_reason").asText(""));
|
||||
addUnknownIdentity(metadata);
|
||||
return new VehicleEvent.Passthrough(
|
||||
UUID.randomUUID().toString(),
|
||||
"unknown",
|
||||
ProtocolId.MQTT_YUTONG,
|
||||
payload.deviceTime(),
|
||||
Instant.now(),
|
||||
null,
|
||||
Map.copyOf(metadata),
|
||||
0,
|
||||
rawBytes(payload.data().path("_rawBase64").asText("")));
|
||||
}
|
||||
|
||||
private static void addUnknownIdentity(Map<String, String> metadata) {
|
||||
metadata.put("vin", "unknown");
|
||||
metadata.put("identityResolved", "false");
|
||||
metadata.put("identitySource", "UNKNOWN");
|
||||
}
|
||||
|
||||
private static byte[] rawBytes(String base64) {
|
||||
if (base64 == null || base64.isBlank()) {
|
||||
return new byte[0];
|
||||
}
|
||||
try {
|
||||
return Base64.getDecoder().decode(base64);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.lingniu.ingest.inbound.mqtt.profile;
|
||||
|
||||
import com.lingniu.ingest.api.event.VehicleEvent;
|
||||
import com.lingniu.ingest.inbound.mqtt.mapper.YutongEventMapper;
|
||||
import com.lingniu.ingest.inbound.mqtt.model.MqttPayload;
|
||||
import com.lingniu.ingest.inbound.mqtt.parser.MqttPayloadParser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public final class YutongMqttProfile implements MqttProfile {
|
||||
|
||||
public static final String NAME = "yutong";
|
||||
|
||||
private final MqttPayloadParser parser;
|
||||
private final YutongEventMapper mapper;
|
||||
|
||||
public YutongMqttProfile(MqttPayloadParser parser, YutongEventMapper mapper) {
|
||||
this.parser = parser;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MqttPayload parse(String endpointName, String topic, byte[] bytes) {
|
||||
return parser.parse(endpointName, NAME, topic, bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<VehicleEvent> toEvents(MqttPayload payload) {
|
||||
return mapper.toEvents(payload);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
com.lingniu.ingest.inbound.mqtt.config.MqttInboundAutoConfiguration
|
||||
Reference in New Issue
Block a user