feat: pushConfig支持MQTT推送,优先级高于HTTP
MQTT启用时通过EMQX推送配置到边缘端,否则回退到原有HTTP REST方式 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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<String, Object> 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<Map<String, Object>> entity = new HttpEntity<>(config, headers);
|
||||
ResponseEntity<String> 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<Map<String, Object>> entity = new HttpEntity<>(config, headers);
|
||||
ResponseEntity<String> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user