| | |
| | | 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 { |
| | | |
| | | private static MqttClient client ; |
| | | |
| | | @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 = "admin"; |
| | | String password = "admin123321"; |
| | | String clientid = "FZZY-gateway"; |
| | | String broker = "tcp://127.0.0.1:1883"; |
| | | 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 { |
| | |
| | | } |
| | | |
| | | } |
| | | 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 = 0; |
| | | public void publishMsg(String topic, String content) { |
| | | |
| | | try { |
| | | // 创建消息并设置 QoS |
| | | MqttMessage message = new MqttMessage(content.getBytes()); |
| | | message.setQos(qos); |
| | | // 发布消息 |
| | | client.publish(topic, message); |
| | | System.out.println("Message published"); |
| | | System.out.println("topic: " + topic); |
| | | System.out.println("message content: " + content); |
| | | // 关闭连接 |
| | | //client.disconnect(); |
| | | // 关闭客户端 |
| | | //client.close(); |
| | | } catch (MqttException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | // 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); |
| | | } |
| | | } |
| | | } |