package com.fzzy.mqtt;
|
|
import lombok.extern.slf4j.Slf4j;
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
|
@Slf4j
|
@Service
|
public class MqttPublishService {
|
|
|
@Resource
|
private MqttProperties mqttProperties;
|
|
private static MqttClient client;
|
|
|
public void init() throws MqttException {
|
//String topic = "/device/hx-weigh-big-01/91511424746940066Y001_91511424746940066Y0010000_002_004_002_001/message/property/report";
|
String username = mqttProperties.getUsername();
|
String password = mqttProperties.getPassword();
|
String clientid = mqttProperties.getClientId();
|
String broker = mqttProperties.getHost();
|
//String content = " { \"headers\":{ \"productId\":\"hx-weigh-big-01\", \"keepOnlineTimeoutSeconds\":600, \"keepOnline\":true, \"deviceName\":\"地磅称重\" }, \"messageType\":\"REPORT_PROPERTY\", \"deviceId\":\"91511424746940066Y001_91511424746940066Y0010000_002_004_002_001\", \"properties\":{ \"weightInfo\":\"{\\\"exceed\\\":false,\\\"grossWeight\\\":3000.0,\\\"netWeight\\\":3000.0,\\\"static\\\":false,\\\"tareWeight\\\":3000.0,\\\"weightUnit\\\":\\\"KG\\\"}\" }, \"timestamp\":1698336020044 }";
|
int qos = 0;
|
try {
|
client = new MqttClient(broker, clientid, new MemoryPersistence());
|
// 连接参数
|
MqttConnectOptions options = new MqttConnectOptions();
|
// 设置用户名和密码
|
options.setUserName(username);
|
options.setPassword(password.toCharArray());
|
options.setConnectionTimeout(60);
|
options.setKeepAliveInterval(60);
|
// 连接
|
client.connect(options);
|
|
} catch (MqttException e) {
|
throw new RuntimeException(e);
|
}
|
|
}
|
|
public void publishMsg(String topic, String content) {
|
|
// String topic = "/device/hx-weigh-big-01/91511424746940066Y001_91511424746940066Y0010000_002_004_002_001/message/property/report";
|
//String content = " { \"headers\":{ \"productId\":\"hx-weigh-big-01\", \"keepOnlineTimeoutSeconds\":600, \"keepOnline\":true, \"deviceName\":\"地磅称重\" }, \"messageType\":\"REPORT_PROPERTY\", \"deviceId\":\"91511424746940066Y001_91511424746940066Y0010000_002_004_002_001\", \"properties\":{ \"weightInfo\":\"{\\\"exceed\\\":false,\\\"grossWeight\\\":3000.0,\\\"netWeight\\\":3000.0,\\\"static\\\":false,\\\"tareWeight\\\":3000.0,\\\"weightUnit\\\":\\\"KG\\\"}\" }, \"timestamp\":1698336020044 }";
|
int qos = mqttProperties.getQos();
|
|
try {
|
// 创建消息并设置 QoS
|
MqttMessage message = new MqttMessage(content.getBytes());
|
message.setQos(qos);
|
// 发布消息
|
client.publish(topic, message);
|
|
|
log.info("------------Message published-------------");
|
log.info("topic: " + topic);
|
log.info("message content: " + content);
|
|
// 关闭连接
|
//client.disconnect();
|
// 关闭客户端
|
//client.close();
|
} catch (MqttException e) {
|
throw new RuntimeException(e);
|
}
|
}
|
}
|