|
|
|
@ -2,6 +2,7 @@ package com.inspect.mqtt; |
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.eclipse.paho.client.mqttv3.MqttCallback; |
|
|
|
import org.eclipse.paho.client.mqttv3.MqttClient; |
|
|
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; |
|
|
|
@ -11,6 +12,7 @@ import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Service |
|
|
|
public class MQTTUtil implements IMQTTUtil { |
|
|
|
@Value("${mqtt.ip}") |
|
|
|
@ -32,17 +34,19 @@ public class MQTTUtil implements IMQTTUtil { |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
public void mqttUtil() { |
|
|
|
log.info("mqttUtil start!!!"); |
|
|
|
} |
|
|
|
|
|
|
|
public void subscribe(String[] topicFilters, int[] qos) { |
|
|
|
try { |
|
|
|
this.client.subscribe(topicFilters, qos); |
|
|
|
log.info("subscribe topicFilters: {}, qos: {}", topicFilters, qos); |
|
|
|
client.subscribe(topicFilters, qos); |
|
|
|
} catch (MqttException e) { |
|
|
|
System.out.println("reason: " + e.getReasonCode()); |
|
|
|
System.out.println("msg: " + e.getMessage()); |
|
|
|
System.out.println("loc: " + e.getLocalizedMessage()); |
|
|
|
System.out.println("cause: " + e.getCause()); |
|
|
|
System.out.println("exception: " + e); |
|
|
|
log.info("reason: {}", e.getReasonCode()); |
|
|
|
log.info("msg: {}", e.getMessage()); |
|
|
|
log.info("loc: {}", e.getLocalizedMessage()); |
|
|
|
log.info("cause: {}", e.getCause()); |
|
|
|
log.info("exception: {}", e.getMessage()); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
@ -51,15 +55,15 @@ public class MQTTUtil implements IMQTTUtil { |
|
|
|
public void connect(String broker, String clientId, String userName, String password, MqttCallback callback) { |
|
|
|
MemoryPersistence persistence = new MemoryPersistence(); |
|
|
|
try { |
|
|
|
this.client = new MqttClient(broker, clientId, persistence); |
|
|
|
client = new MqttClient(broker, clientId, persistence); |
|
|
|
MqttConnectOptions connOpts = new MqttConnectOptions(); |
|
|
|
connOpts.setUserName(userName); |
|
|
|
connOpts.setPassword(password.toCharArray()); |
|
|
|
connOpts.setCleanSession(true); |
|
|
|
this.client.setCallback(callback); |
|
|
|
System.out.println("Connecting to broker: " + broker); |
|
|
|
this.client.connect(connOpts); |
|
|
|
System.out.println("Connected"); |
|
|
|
client.setCallback(callback); |
|
|
|
log.info("Connecting to mqtt broker: {}", broker); |
|
|
|
client.connect(connOpts); |
|
|
|
log.info("Connected to mqtt broker"); |
|
|
|
} catch (MqttException e) { |
|
|
|
System.out.println("reason: " + e.getReasonCode()); |
|
|
|
System.out.println("msg: " + e.getMessage()); |
|
|
|
@ -73,8 +77,9 @@ public class MQTTUtil implements IMQTTUtil { |
|
|
|
|
|
|
|
public void disconnect() { |
|
|
|
try { |
|
|
|
this.client.disconnect(); |
|
|
|
this.client.close(); |
|
|
|
log.info("Disconnect mqtt broker"); |
|
|
|
client.disconnect(); |
|
|
|
client.close(); |
|
|
|
} catch (MqttException e) { |
|
|
|
System.out.println("reason: " + e.getReasonCode()); |
|
|
|
System.out.println("msg: " + e.getMessage()); |
|
|
|
@ -93,7 +98,8 @@ public class MQTTUtil implements IMQTTUtil { |
|
|
|
try { |
|
|
|
MqttMessage message = new MqttMessage(content.getBytes()); |
|
|
|
message.setQos(qos); |
|
|
|
this.client.publish(pubTopic, message); |
|
|
|
log.info("Publish mqtt broker"); |
|
|
|
client.publish(pubTopic, message); |
|
|
|
return true; |
|
|
|
} catch (MqttException e) { |
|
|
|
System.out.println("reason: " + e.getReasonCode()); |
|
|
|
|