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.AiAlgorithm;
|
||||||
import com.genersoft.iot.vmp.aiot.bean.AiRoi;
|
import com.genersoft.iot.vmp.aiot.bean.AiRoi;
|
||||||
import com.genersoft.iot.vmp.aiot.bean.AiRoiAlgoBind;
|
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.config.AiServiceConfig;
|
||||||
import com.genersoft.iot.vmp.aiot.dao.AiAlgorithmMapper;
|
import com.genersoft.iot.vmp.aiot.dao.AiAlgorithmMapper;
|
||||||
import com.genersoft.iot.vmp.aiot.dao.AiRoiAlgoBindMapper;
|
import com.genersoft.iot.vmp.aiot.dao.AiRoiAlgoBindMapper;
|
||||||
import com.genersoft.iot.vmp.aiot.dao.AiRoiMapper;
|
import com.genersoft.iot.vmp.aiot.dao.AiRoiMapper;
|
||||||
import com.genersoft.iot.vmp.aiot.service.IAiConfigService;
|
import com.genersoft.iot.vmp.aiot.service.IAiConfigService;
|
||||||
|
import com.genersoft.iot.vmp.aiot.service.MqttService;
|
||||||
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.http.*;
|
import org.springframework.http.*;
|
||||||
@@ -33,6 +35,12 @@ public class AiConfigServiceImpl implements IAiConfigService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private AiServiceConfig aiServiceConfig;
|
private AiServiceConfig aiServiceConfig;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AiMqttConfig mqttConfig;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MqttService mqttService;
|
||||||
|
|
||||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -77,11 +85,20 @@ public class AiConfigServiceImpl implements IAiConfigService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void pushConfig(String cameraId) {
|
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);
|
Map<String, Object> config = exportConfig(cameraId);
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
} else if (aiServiceConfig.isEnabled()) {
|
||||||
|
// HTTP REST推送
|
||||||
try {
|
try {
|
||||||
RestTemplate restTemplate = new RestTemplate();
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
String url = aiServiceConfig.getUrl() + "/api/config/receive";
|
String url = aiServiceConfig.getUrl() + "/api/config/receive";
|
||||||
@@ -92,10 +109,13 @@ public class AiConfigServiceImpl implements IAiConfigService {
|
|||||||
if (!response.getStatusCode().is2xxSuccessful()) {
|
if (!response.getStatusCode().is2xxSuccessful()) {
|
||||||
throw new RuntimeException("推送失败,边缘端返回: " + response.getStatusCode());
|
throw new RuntimeException("推送失败,边缘端返回: " + response.getStatusCode());
|
||||||
}
|
}
|
||||||
log.info("配置推送成功: cameraId={}", cameraId);
|
log.info("HTTP配置推送成功: cameraId={}", cameraId);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("配置推送失败: cameraId={}", cameraId, e);
|
log.error("HTTP配置推送失败: cameraId={}", cameraId, e);
|
||||||
throw new RuntimeException("推送失败: " + e.getMessage());
|
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