避免mqtt未开启时,类报错

This commit is contained in:
zy 2026-03-31 15:40:25 +08:00
parent 56bd4dd4bd
commit d771c1ba5c
3 changed files with 14 additions and 5 deletions

View File

@ -736,11 +736,16 @@ public class IotCommonP3ServiceImpl {
public boolean sendMqttDeviceQuery(SurvDeviceDeploy deploy, List<String> variables) { public boolean sendMqttDeviceQuery(SurvDeviceDeploy deploy, List<String> variables) {
boolean result = true; boolean result = true;
log.error("===========预发送指令========={}============",variables.size()); try{
if(deploy!=null && variables!=null && !variables.isEmpty()){ log.error("===========预发送指令========={}============", variables.size());
QueryCmd queryCmd = LhIotUtil.ConstructCmd(variables); if (deploy != null && variables != null && !variables.isEmpty()) {
String cmdStr = JSONObject.toJSONString(queryCmd); QueryCmd queryCmd = LhIotUtil.ConstructCmd(variables);
mqttService.sendMessage(deploy.getDeviceIotUrl(),cmdStr); String cmdStr = JSONObject.toJSONString(queryCmd);
mqttService.sendMessage(deploy.getDeviceIotUrl(), cmdStr);
}
}catch (Exception e){
//抛出未注入时的报错
e.printStackTrace();
} }
return result; return result;
} }

View File

@ -4,6 +4,7 @@ import org.jeecg.modules.mqtt.service.MqttService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -14,6 +15,7 @@ import java.util.Map;
@RestController @RestController
@RequestMapping("/api/mqtt") @RequestMapping("/api/mqtt")
@RequiredArgsConstructor @RequiredArgsConstructor
@ConditionalOnProperty(prefix = "mqtt", name = "enabled", havingValue = "true")
public class MqttController { public class MqttController {
@Autowired(required = false) @Autowired(required = false)
private final MqttService mqttService; private final MqttService mqttService;

View File

@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.HashMap; import java.util.HashMap;
@ -18,6 +19,7 @@ import java.util.concurrent.CompletableFuture;
@Slf4j @Slf4j
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@ConditionalOnProperty(prefix = "mqtt", name = "enabled", havingValue = "true")
public class MqttService { public class MqttService {
private final MqttClient mqttClient; private final MqttClient mqttClient;