From 18c51d40be82435289ba3be6bd5f8e15fdf786f7 Mon Sep 17 00:00:00 2001
From: cl <1442464845@qq.com>
Date: 星期六, 04 四月 2026 00:26:56 +0800
Subject: [PATCH] mqtt

---
 src/main/java/com/zy/iot/config/IotProperties.java |   61 +++++++++++++++++++++++++++---
 1 files changed, 54 insertions(+), 7 deletions(-)

diff --git a/src/main/java/com/zy/iot/config/IotProperties.java b/src/main/java/com/zy/iot/config/IotProperties.java
index 42585ce..24b0ac5 100644
--- a/src/main/java/com/zy/iot/config/IotProperties.java
+++ b/src/main/java/com/zy/iot/config/IotProperties.java
@@ -1,12 +1,16 @@
 package com.zy.iot.config;
 
 import com.zy.iot.entity.IotTopicConfig;
+import lombok.AccessLevel;
 import lombok.Data;
+import lombok.Getter;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
 
 import java.util.LinkedHashMap;
+import java.util.Locale;
 import java.util.Map;
+import java.util.concurrent.ThreadLocalRandom;
 
 @Data
 @Component
@@ -43,27 +47,70 @@
 
     private Map<String, Integer> pickStationMappings = new LinkedHashMap<>();
 
+    @Getter(AccessLevel.NONE)
+    private volatile String resolvedClientIdCache;
+
     public String getResolvedClientId() {
-        if (clientId != null && clientId.trim().length() > 0) {
-            return clientId.trim();
+        if (resolvedClientIdCache != null) {
+            return resolvedClientIdCache;
         }
-        return thingName;
+        synchronized (this) {
+            if (resolvedClientIdCache != null) {
+                return resolvedClientIdCache;
+            }
+            String base = null;
+            if (clientId != null && clientId.trim().length() > 0) {
+                base = clientId.trim();
+            } else if (thingName != null && thingName.trim().length() > 0) {
+                base = thingName.trim();
+            }
+            if (base == null) {
+                return null;
+            }
+            resolvedClientIdCache = base + "-" + newClientIdSuffix();
+            return resolvedClientIdCache;
+        }
+    }
+
+    private static String newClientIdSuffix() {
+        long n = ThreadLocalRandom.current().nextLong() & 0xFFFFFFFFL;
+        return Long.toHexString(n);
     }
 
     public boolean isTlsEnabled() {
-        String serverUri = getServerUri();
-        return serverUri != null && serverUri.startsWith("ssl://");
+        return isTlsMqttScheme(getServerUri());
     }
 
+    /** 涓� Paho 寤鸿繛 URI锛氬畬鏁村墠缂� mqtts/mqtt/wss/ws/ssl/tcp 鍘熸牱锛涗粎涓绘満鍚嶆椂榛樿 mqtts 鎴� mqtt銆� */
     public String getServerUri() {
         if (endpoint == null || endpoint.trim().length() == 0) {
             return null;
         }
         String trimmed = endpoint.trim();
-        if (trimmed.startsWith("ssl://") || trimmed.startsWith("tcp://")) {
+        if (hasExplicitBrokerScheme(trimmed)) {
             return trimmed;
         }
         int resolvedPort = port == null ? 8883 : port;
-        return "ssl://" + trimmed + ":" + resolvedPort;
+        String scheme = resolvedPort == 1883 ? "mqtt://" : "mqtts://";
+        return scheme + trimmed + ":" + resolvedPort;
+    }
+
+    private static boolean hasExplicitBrokerScheme(String s) {
+        String lower = s.toLowerCase(Locale.ROOT);
+        return lower.startsWith("mqtts://") || lower.startsWith("mqtt://")
+                || lower.startsWith("ssl://") || lower.startsWith("tcp://")
+                || lower.startsWith("wss://") || lower.startsWith("ws://");
+    }
+
+    private static boolean isTlsMqttScheme(String serverUri) {
+        if (serverUri == null || serverUri.isEmpty()) {
+            return false;
+        }
+        int p = serverUri.indexOf("://");
+        if (p <= 0) {
+            return false;
+        }
+        String scheme = serverUri.substring(0, p).toLowerCase(Locale.ROOT);
+        return "mqtts".equals(scheme) || "ssl".equals(scheme) || "wss".equals(scheme);
     }
 }

--
Gitblit v1.9.1