| | |
| | | */ |
| | | public void connect() throws MqttException { |
| | | if (client != null && client.isConnected()) { |
| | | System.out.println("MQTT客户端已连接,ClientId: " + clientId); |
| | | return; |
| | | } |
| | | |
| | |
| | | |
| | | // 执行连接 |
| | | client.connect(options); |
| | | System.out.println("MQTT连接成功!ClientId: " + clientId + ", 服务器: " + host + ", 主题: " + topic); |
| | | |
| | | // 启动连接监控线程 |
| | | startConnectionMonitor(); |
| | |
| | | connect(); |
| | | } |
| | | client.subscribe(topic, qos); |
| | | System.out.println("已订阅主题: " + topic + ", QoS: " + qos); |
| | | } |
| | | |
| | | /** |
| | |
| | | Thread.sleep(5000); // 每5秒检查一次 |
| | | |
| | | if (client != null && !client.isConnected()) { |
| | | System.out.println("检测到MQTT连接断开,尝试重连... ClientId: " + clientId); |
| | | try { |
| | | // 尝试重新连接 |
| | | if (!client.isConnected()) { |
| | |
| | | // 重连成功后重新订阅 |
| | | if (client.isConnected()) { |
| | | client.subscribe(topic, qos); |
| | | System.out.println("连接监控:重连成功并重新订阅主题: " + topic); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | |
| | | connectionMonitorThread.setDaemon(true); |
| | | connectionMonitorThread.setName("MQTT-ConnectionMonitor-" + clientId); |
| | | connectionMonitorThread.start(); |
| | | System.out.println("已启动MQTT连接监控线程: " + clientId); |
| | | } |
| | | |
| | | /** |
| | |
| | | public static boolean connectMQTT() { |
| | | // 防止重复重连 |
| | | if (isReconnecting) { |
| | | System.out.println("MQTT正在重连中,跳过本次重连请求"); |
| | | return false; |
| | | } |
| | | |
| | | // 检查是否已经连接 |
| | | if (areClientsConnected()) { |
| | | System.out.println("MQTT客户端已连接,无需重复连接"); |
| | | return true; |
| | | } |
| | | |
| | |
| | | if (gpsClient != null && gpsClient.isConnected()) { |
| | | gpsClient.subscribe(); |
| | | gpsSuccess = true; |
| | | System.out.println("GPS主题MQTT连接并订阅成功"); |
| | | } |
| | | } catch (MqttException e) { |
| | | System.err.println("GPS主题MQTT连接失败: " + e.getMessage()); |
| | |
| | | if (responseClient != null && responseClient.isConnected()) { |
| | | responseClient.subscribe(); |
| | | responseSuccess = true; |
| | | System.out.println("响应主题MQTT连接并订阅成功"); |
| | | } |
| | | } catch (MqttException e) { |
| | | System.err.println("响应主题MQTT连接失败: " + e.getMessage()); |
| | |
| | | } |
| | | |
| | | if (gpsSuccess && responseSuccess) { |
| | | System.out.println("所有MQTT主题连接并订阅成功!"); |
| | | return true; |
| | | } else if (gpsSuccess || responseSuccess) { |
| | | System.out.println("部分MQTT主题连接成功"); |
| | | return true; |
| | | } else { |
| | | System.err.println("所有MQTT主题连接失败"); |
| | |
| | | public static boolean connectMQTT(String host, String deviceId, String userEmail) { |
| | | // 防止重复重连 |
| | | if (isReconnecting) { |
| | | System.out.println("MQTT正在重连中,跳过本次重连请求"); |
| | | return false; |
| | | } |
| | | |
| | | // 检查是否已经连接 |
| | | if (areClientsConnected()) { |
| | | System.out.println("MQTT客户端已连接,无需重复连接"); |
| | | return true; |
| | | } |
| | | |
| | |
| | | if (gpsClient != null && gpsClient.isConnected()) { |
| | | gpsClient.subscribe(); |
| | | gpsSuccess = true; |
| | | System.out.println("GPS主题MQTT连接并订阅成功"); |
| | | } |
| | | } catch (MqttException e) { |
| | | System.err.println("GPS主题MQTT连接失败: " + e.getMessage()); |
| | |
| | | if (responseClient != null && responseClient.isConnected()) { |
| | | responseClient.subscribe(); |
| | | responseSuccess = true; |
| | | System.out.println("响应主题MQTT连接并订阅成功"); |
| | | } |
| | | } catch (MqttException e) { |
| | | System.err.println("响应主题MQTT连接失败: " + e.getMessage()); |
| | |
| | | } |
| | | |
| | | if (gpsSuccess && responseSuccess) { |
| | | System.out.println("所有MQTT主题连接并订阅成功!"); |
| | | return true; |
| | | } else if (gpsSuccess || responseSuccess) { |
| | | System.out.println("部分MQTT主题连接成功"); |
| | | return true; |
| | | } else { |
| | | System.err.println("所有MQTT主题连接失败"); |
| | |
| | | Client mqttClient = new Client(host, topic, clientId); |
| | | mqttClient.connect(); |
| | | mqttClient.subscribe(qos); |
| | | System.out.println("MQTT客户端创建并订阅成功,主题: " + topic + ", ClientId: " + clientId); |
| | | return mqttClient; |
| | | } catch (MqttException e) { |
| | | System.err.println("MQTT客户端创建失败: " + e.getMessage() + ", 主题: " + topic); |
| | |
| | | try { |
| | | if (gpsClient != null) { |
| | | gpsClient.close(); |
| | | System.out.println("GPS主题MQTT连接已断开"); |
| | | gpsClient = null; |
| | | } |
| | | if (responseClient != null) { |
| | | responseClient.close(); |
| | | System.out.println("响应主题MQTT连接已断开"); |
| | | responseClient = null; |
| | | } |
| | | } catch (Exception e) { |