自动化立体仓库 - WMS系统
cl
3 天以前 18c51d40be82435289ba3be6bd5f8e15fdf786f7
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);
    }
}