update:设置同类型OTA固件,只保留一个

This commit is contained in:
hrz
2025-04-25 09:48:25 +08:00
parent d39c595828
commit d638e512c9
4 changed files with 66 additions and 37 deletions
@@ -124,22 +124,7 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
response.setServer_time(buildServerTime());
String type = deviceReport.getBoard() == null ? null : deviceReport.getBoard().getType();
OtaEntity ota = otaService.getLatestOta(type);
String downloadUrl = null;
if (ota != null) {
// 获取当前请求的URL
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
String requestUrl = request.getRequestURL().toString();
// 将URL中的/ota/替换为/otaMag/download/
String uuid = UUID.randomUUID().toString();
redisUtils.set(RedisKeys.getOtaIdKey(uuid), ota.getId());
downloadUrl = requestUrl.replace("/ota/", "/otaMag/download/") + uuid;
}
DeviceReportRespDTO.Firmware firmware = new DeviceReportRespDTO.Firmware();
firmware.setVersion(ota == null ? null : ota.getVersion());
firmware.setUrl(downloadUrl);
DeviceReportRespDTO.Firmware firmware = buildFirmwareInfo(type, macAddress);
response.setFirmware(firmware);
// 添加WebSocket配置
@@ -315,4 +300,29 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
}
return code;
}
private DeviceReportRespDTO.Firmware buildFirmwareInfo(String type, String macAddress) {
if (StringUtils.isBlank(type)) {
return null;
}
OtaEntity ota = otaService.getLatestOta(type);
DeviceReportRespDTO.Firmware firmware = new DeviceReportRespDTO.Firmware();
String downloadUrl = null;
if (ota != null) {
// 获取当前请求的URL
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
String requestUrl = request.getRequestURL().toString();
// 将URL中的/ota/替换为/otaMag/download/
String uuid = UUID.randomUUID().toString();
redisUtils.set(RedisKeys.getOtaIdKey(uuid), ota.getId());
downloadUrl = requestUrl.replace("/ota/", "/otaMag/download/") + uuid;
}
firmware.setVersion(ota == null ? null : ota.getVersion());
firmware.setUrl(downloadUrl);
return firmware;
}
}
@@ -1,6 +1,8 @@
package xiaozhi.modules.device.service.impl;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
@@ -38,17 +40,17 @@ public class OtaServiceImpl extends BaseServiceImpl<OtaDao, OtaEntity> implement
@Override
public void update(OtaEntity entity) {
// 检查是否存在相同名称、类型和版本的固件(排除当前记录)
// 检查是否存在相同类型和版本的固件(排除当前记录)
QueryWrapper<OtaEntity> queryWrapper = new QueryWrapper<OtaEntity>()
.eq("firmware_name", entity.getFirmwareName())
.eq("type", entity.getType())
.eq("version", entity.getVersion())
.ne("id", entity.getId()); // 排除当前记录
if (baseDao.selectCount(queryWrapper) > 0) {
throw new RuntimeException("已存在相同名称、类型和版本的固件,请修改后重试");
throw new RuntimeException("已存在相同类型和版本的固件,请修改后重试");
}
entity.setUpdateDate(new Date());
baseDao.updateById(entity);
}
@@ -59,16 +61,16 @@ public class OtaServiceImpl extends BaseServiceImpl<OtaDao, OtaEntity> implement
@Override
public boolean save(OtaEntity entity) {
// 检查是否存在相同名称、类型和版本的固件
QueryWrapper<OtaEntity> queryWrapper = new QueryWrapper<OtaEntity>()
.eq("firmware_name", entity.getFirmwareName())
.eq("type", entity.getType())
.eq("version", entity.getVersion());
if (baseDao.selectCount(queryWrapper) > 0) {
throw new RuntimeException("已存在相同名称、类型和版本的固件,请勿重复添加");
.eq("type", entity.getType());
// 同类固件只保留最新的一条
List<OtaEntity> otaList = baseDao.selectList(queryWrapper);
if (otaList != null && otaList.size() > 0) {
OtaEntity otaBefore = otaList.getFirst();
entity.setId(otaBefore.getId());
baseDao.updateById(entity);
return true;
}
return baseDao.insert(entity) > 0;
}