|
@ -0,0 +1,110 @@
|
|
|
package com.yihu.jw.care.util;
|
|
|
|
|
|
import org.eclipse.paho.client.mqttv3.*;
|
|
|
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
|
|
/**
|
|
|
* Created by yeshijie on 2022/5/5.
|
|
|
*/
|
|
|
@Component
|
|
|
public class MQTTClientUtil {
|
|
|
private static Logger logger = LoggerFactory.getLogger(MQTTClientUtil.class);
|
|
|
|
|
|
public static final String HOST = "tcp://47.99.215.81:8885";
|
|
|
public static final String TOPIC1 = "00752217643114/get/request/online";
|
|
|
// public static final String TOPIC = "00752217643114/get/request/params";
|
|
|
public static final String TOPIC = "00752217643114/#";//订阅所有
|
|
|
private static final String clientid = String.valueOf(System.currentTimeMillis());;
|
|
|
private MqttClient client;
|
|
|
private MqttConnectOptions options;
|
|
|
private String userName = "admin";
|
|
|
private String passWord = "password";
|
|
|
|
|
|
private ScheduledExecutorService scheduler;
|
|
|
|
|
|
public void start() {
|
|
|
logger.info("启动监听mqtt-start");
|
|
|
try {
|
|
|
// host为主机名,clientid即连接MQTT的客户端ID,一般以唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存
|
|
|
client = new MqttClient(HOST, clientid, new MemoryPersistence());
|
|
|
// MQTT的连接设置
|
|
|
options = new MqttConnectOptions();
|
|
|
// 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
|
|
|
options.setCleanSession(true);
|
|
|
// 设置连接的用户名
|
|
|
options.setUserName(userName);
|
|
|
// 设置连接的密码
|
|
|
options.setPassword(passWord.toCharArray());
|
|
|
// 设置超时时间 单位为秒
|
|
|
options.setConnectionTimeout(10);
|
|
|
options.setAutomaticReconnect(true);//设置自动重连
|
|
|
// 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
|
|
|
options.setKeepAliveInterval(20);
|
|
|
// 设置回调
|
|
|
client.setCallback(new MqttCallback(){
|
|
|
|
|
|
@Override
|
|
|
public void connectionLost(Throwable throwable) {
|
|
|
// 连接丢失后,一般在这里面进行重连
|
|
|
throwable.printStackTrace();
|
|
|
logger.info("连接断开,可以做重连");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void messageArrived(String s, MqttMessage message) throws Exception {
|
|
|
// subscribe后得到的消息会执行到这里面
|
|
|
logger.info("接收消息主题 : " + TOPIC);
|
|
|
logger.info("接收消息Qos : " + message.getQos());
|
|
|
logger.info("接收消息内容 : " + new String(message.getPayload()));
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void deliveryComplete(IMqttDeliveryToken token) {
|
|
|
logger.info("deliveryComplete---------" + token.isComplete());
|
|
|
}
|
|
|
});
|
|
|
// MqttTopic topic = client.getTopic(TOPIC);
|
|
|
//setWill方法,如果项目中需要知道客户端是否掉线可以调用该方法。设置最终端口的通知消息
|
|
|
// options.setWill(topic, "close".getBytes(), 2, true);
|
|
|
|
|
|
client.connect(options);
|
|
|
//订阅消息
|
|
|
int[] Qos = {1};
|
|
|
String[] topic1 = {TOPIC};
|
|
|
client.subscribe(topic1, Qos);
|
|
|
|
|
|
/* // 创建消息
|
|
|
String payload = "{\n" +
|
|
|
" \"token\": 0,\n" +
|
|
|
" \"dev_type\": \"gw\", \n" +
|
|
|
" \"msg_type\": \"online\", \n" +
|
|
|
" \"content\" :[{\n" +
|
|
|
" \"action\": \"getlist\"\n" +
|
|
|
" }]\n" +
|
|
|
"}\n";
|
|
|
MqttMessage message = new MqttMessage(payload.getBytes());
|
|
|
// 设置消息的服务质量
|
|
|
message.setQos(0);
|
|
|
// 发布消息
|
|
|
client.publish(TOPIC1, message);*/
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
logger.info("启动监听mqtt-end");
|
|
|
}
|
|
|
|
|
|
// public static void main(String[] args) throws MqttException {
|
|
|
// MQTTClientUtil client = new MQTTClientUtil();
|
|
|
// client.start();
|
|
|
// logger.info("启动监听mqtt");
|
|
|
// }
|
|
|
|
|
|
}
|