From dfb9d6fd06c50f0e129688ad9d166d031bbb68bb Mon Sep 17 00:00:00 2001 From: 16337 <1633794139@qq.com> Date: Wed, 4 Feb 2026 14:44:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20pushConfig=E6=94=AF=E6=8C=81MQTT?= =?UTF-8?q?=E6=8E=A8=E9=80=81=EF=BC=8C=E4=BC=98=E5=85=88=E7=BA=A7=E9=AB=98?= =?UTF-8?q?=E4=BA=8EHTTP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MQTT启用时通过EMQX推送配置到边缘端,否则回退到原有HTTP REST方式 Co-Authored-By: Claude Opus 4.5 --- .../service/impl/AiConfigServiceImpl.java | 54 +++++++++++++------ 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiConfigServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiConfigServiceImpl.java index e6b20e948..c74b90d8e 100644 --- a/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiConfigServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiConfigServiceImpl.java @@ -4,11 +4,13 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.genersoft.iot.vmp.aiot.bean.AiAlgorithm; import com.genersoft.iot.vmp.aiot.bean.AiRoi; import com.genersoft.iot.vmp.aiot.bean.AiRoiAlgoBind; +import com.genersoft.iot.vmp.aiot.config.AiMqttConfig; import com.genersoft.iot.vmp.aiot.config.AiServiceConfig; import com.genersoft.iot.vmp.aiot.dao.AiAlgorithmMapper; import com.genersoft.iot.vmp.aiot.dao.AiRoiAlgoBindMapper; import com.genersoft.iot.vmp.aiot.dao.AiRoiMapper; import com.genersoft.iot.vmp.aiot.service.IAiConfigService; +import com.genersoft.iot.vmp.aiot.service.MqttService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; @@ -33,6 +35,12 @@ public class AiConfigServiceImpl implements IAiConfigService { @Autowired private AiServiceConfig aiServiceConfig; + @Autowired + private AiMqttConfig mqttConfig; + + @Autowired + private MqttService mqttService; + private final ObjectMapper objectMapper = new ObjectMapper(); @Override @@ -77,25 +85,37 @@ public class AiConfigServiceImpl implements IAiConfigService { @Override public void pushConfig(String cameraId) { - if (!aiServiceConfig.isEnabled()) { - log.warn("AI服务未启用,跳过推送"); - throw new RuntimeException("AI服务未启用,请在配置中设置ai.service.enabled=true"); - } Map config = exportConfig(cameraId); - try { - RestTemplate restTemplate = new RestTemplate(); - String url = aiServiceConfig.getUrl() + "/api/config/receive"; - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); - HttpEntity> entity = new HttpEntity<>(config, headers); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class); - if (!response.getStatusCode().is2xxSuccessful()) { - throw new RuntimeException("推送失败,边缘端返回: " + response.getStatusCode()); + + if (mqttConfig.isEnabled()) { + // MQTT推送优先 + try { + String configJson = objectMapper.writeValueAsString(config); + mqttService.pushConfig(cameraId, configJson); + log.info("MQTT配置推送成功: cameraId={}", cameraId); + } catch (Exception e) { + log.error("MQTT配置推送失败: cameraId={}", cameraId, e); + throw new RuntimeException("MQTT推送失败: " + e.getMessage()); } - log.info("配置推送成功: cameraId={}", cameraId); - } catch (Exception e) { - log.error("配置推送失败: cameraId={}", cameraId, e); - throw new RuntimeException("推送失败: " + e.getMessage()); + } else if (aiServiceConfig.isEnabled()) { + // HTTP REST推送 + try { + RestTemplate restTemplate = new RestTemplate(); + String url = aiServiceConfig.getUrl() + "/api/config/receive"; + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity> entity = new HttpEntity<>(config, headers); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class); + if (!response.getStatusCode().is2xxSuccessful()) { + throw new RuntimeException("推送失败,边缘端返回: " + response.getStatusCode()); + } + log.info("HTTP配置推送成功: cameraId={}", cameraId); + } catch (Exception e) { + log.error("HTTP配置推送失败: cameraId={}", cameraId, e); + throw new RuntimeException("推送失败: " + e.getMessage()); + } + } else { + throw new RuntimeException("AI配置推送未启用,请设置ai.mqtt.enabled=true或ai.service.enabled=true"); } } }