自动化立体仓库 - WMS系统
cl
2 天以前 18c51d40be82435289ba3be6bd5f8e15fdf786f7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
@ConfigurationProperties(prefix = "iot")
public class IotProperties {
 
    private boolean enabled;
 
    private String endpoint;
 
    private Integer port = 8883;
 
    private String thingName;
 
    private String clientId;
 
    private boolean cleanSession;
 
    private boolean automaticReconnect = true;
 
    private Integer keepAliveSeconds = 60;
 
    private Integer connectionTimeoutSeconds = 10;
 
    private String persistenceDir;
 
    private String caCertPath;
 
    private String clientCertPath;
 
    private String privateKeyPath;
 
    private IotTopicConfig topics = new IotTopicConfig();
 
    private Map<String, Integer> pickStationMappings = new LinkedHashMap<>();
 
    @Getter(AccessLevel.NONE)
    private volatile String resolvedClientIdCache;
 
    public String getResolvedClientId() {
        if (resolvedClientIdCache != null) {
            return resolvedClientIdCache;
        }
        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() {
        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 (hasExplicitBrokerScheme(trimmed)) {
            return trimmed;
        }
        int resolvedPort = port == null ? 8883 : port;
        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);
    }
}