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

View File

@ -4,6 +4,7 @@ import org.jeecg.modules.mqtt.service.MqttService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@ -14,6 +15,7 @@ import java.util.Map;
@RestController
@RequestMapping("/api/mqtt")
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "mqtt", name = "enabled", havingValue = "true")
public class MqttController {
@Autowired(required = false)
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.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import java.util.HashMap;
@ -18,6 +19,7 @@ import java.util.concurrent.CompletableFuture;
@Slf4j
@Service
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "mqtt", name = "enabled", havingValue = "true")
public class MqttService {
private final MqttClient mqttClient;