package com.dy.pipIrrApp.workOrder.mqtt; import lombok.extern.slf4j.Slf4j; import org.eclipse.paho.client.mqttv3.MqttClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author ZhuBaoMin * @date 2024-11-16 11:32 * @LastEditTime 2024-11-16 11:32 * @Description */ @Slf4j @RestController @RequestMapping(path = "mqtt") public class TestController { @GetMapping("/mqtt/{msg}") public String testSendMqttMsg(@PathVariable("msg") String msg){ log.info("消息内容:{}.", msg); MqttClient mqttClient = MqttClientConnectorPool.connectMQTT(); MqttMsgSender sender = new MqttMsgSender(); String content = "{" + " \"deviceNo\": \"" + msg + "\"," + " \"val\": 232.5" + "}"; String topic = "workOrder"; int qos = 1; if (null != mqttClient){ sender.sendMessage(mqttClient, topic, content, qos); } else { log.info("MqttClient为空,无法发送!"); return "失败!"; } return "成功!"; } }