mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-26 17:13:54 +08:00
update:细化版本对比 (#1004)
This commit is contained in:
+60
-18
@@ -120,7 +120,8 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
|
|||||||
|
|
||||||
if (deviceById == null || deviceById.getAutoUpdate() != 0) {
|
if (deviceById == null || deviceById.getAutoUpdate() != 0) {
|
||||||
String type = deviceReport.getBoard() == null ? null : deviceReport.getBoard().getType();
|
String type = deviceReport.getBoard() == null ? null : deviceReport.getBoard().getType();
|
||||||
DeviceReportRespDTO.Firmware firmware = buildFirmwareInfo(type);
|
DeviceReportRespDTO.Firmware firmware = buildFirmwareInfo(type,
|
||||||
|
deviceReport.getApplication() == null ? null : deviceReport.getApplication().getVersion());
|
||||||
response.setFirmware(firmware);
|
response.setFirmware(firmware);
|
||||||
} else {
|
} else {
|
||||||
response.setFirmware(null);
|
response.setFirmware(null);
|
||||||
@@ -147,10 +148,16 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
|
|||||||
|
|
||||||
response.setWebsocket(websocket);
|
response.setWebsocket(websocket);
|
||||||
|
|
||||||
if (deviceById != null) { // 如果设备存在,则更新上次连接时间
|
if (deviceById != null) {
|
||||||
|
// 如果设备存在,则更新上次连接时间
|
||||||
deviceById.setLastConnectedAt(new Date());
|
deviceById.setLastConnectedAt(new Date());
|
||||||
|
if (deviceReport.getApplication() != null
|
||||||
|
&& StringUtils.isNotBlank(deviceReport.getApplication().getVersion())) {
|
||||||
|
deviceById.setAppVersion(deviceReport.getApplication().getVersion());
|
||||||
|
}
|
||||||
deviceDao.updateById(deviceById);
|
deviceDao.updateById(deviceById);
|
||||||
} else { // 如果设备不存在,则生成激活码
|
} else {
|
||||||
|
// 如果设备不存在,则生成激活码
|
||||||
DeviceReportRespDTO.Activation code = buildActivation(macAddress, deviceReport);
|
DeviceReportRespDTO.Activation code = buildActivation(macAddress, deviceReport);
|
||||||
response.setActivation(code);
|
response.setActivation(code);
|
||||||
}
|
}
|
||||||
@@ -299,33 +306,68 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
|
|||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DeviceReportRespDTO.Firmware buildFirmwareInfo(String type) {
|
private DeviceReportRespDTO.Firmware buildFirmwareInfo(String type, String currentVersion) {
|
||||||
if (StringUtils.isBlank(type)) {
|
if (StringUtils.isBlank(type)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if (StringUtils.isBlank(currentVersion)) {
|
||||||
|
currentVersion = "0.0.0";
|
||||||
|
}
|
||||||
|
|
||||||
OtaEntity ota = otaService.getLatestOta(type);
|
OtaEntity ota = otaService.getLatestOta(type);
|
||||||
DeviceReportRespDTO.Firmware firmware = new DeviceReportRespDTO.Firmware();
|
DeviceReportRespDTO.Firmware firmware = new DeviceReportRespDTO.Firmware();
|
||||||
String downloadUrl = null;
|
String downloadUrl = null;
|
||||||
|
|
||||||
if (ota != null) {
|
if (ota != null) {
|
||||||
|
// 如果设备没有版本信息,或者OTA版本比设备版本新,则返回下载地址
|
||||||
String otaUrl = sysParamsService.getValue(Constant.SERVER_OTA, true);
|
if (compareVersions(ota.getVersion(), currentVersion) > 0) {
|
||||||
if (StringUtils.isBlank(otaUrl) || otaUrl.equals("null")) {
|
String otaUrl = sysParamsService.getValue(Constant.SERVER_OTA, true);
|
||||||
log.error("OTA地址未配置,请登录智控台,在参数管理找到【server.ota】配置");
|
if (StringUtils.isBlank(otaUrl) || otaUrl.equals("null")) {
|
||||||
// 尝试从请求中获取
|
log.error("OTA地址未配置,请登录智控台,在参数管理找到【server.ota】配置");
|
||||||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
|
// 尝试从请求中获取
|
||||||
.getRequest();
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
|
||||||
otaUrl = request.getRequestURL().toString();
|
.getRequestAttributes())
|
||||||
|
.getRequest();
|
||||||
|
otaUrl = request.getRequestURL().toString();
|
||||||
|
}
|
||||||
|
// 将URL中的/ota/替换为/otaMag/download/
|
||||||
|
String uuid = UUID.randomUUID().toString();
|
||||||
|
redisUtils.set(RedisKeys.getOtaIdKey(uuid), ota.getId());
|
||||||
|
downloadUrl = otaUrl.replace("/ota/", "/otaMag/download/") + uuid;
|
||||||
}
|
}
|
||||||
// 将URL中的/ota/替换为/otaMag/download/
|
|
||||||
String uuid = UUID.randomUUID().toString();
|
|
||||||
redisUtils.set(RedisKeys.getOtaIdKey(uuid), ota.getId());
|
|
||||||
downloadUrl = otaUrl.replace("/ota/", "/otaMag/download/") + uuid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
firmware.setVersion(ota == null ? null : ota.getVersion());
|
firmware.setVersion(ota == null ? currentVersion : ota.getVersion());
|
||||||
firmware.setUrl(downloadUrl);
|
firmware.setUrl(downloadUrl == null ? "" : downloadUrl);
|
||||||
return firmware;
|
return firmware;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 比较两个版本号
|
||||||
|
*
|
||||||
|
* @param version1 版本1
|
||||||
|
* @param version2 版本2
|
||||||
|
* @return 如果version1 > version2返回1,version1 < version2返回-1,相等返回0
|
||||||
|
*/
|
||||||
|
private static int compareVersions(String version1, String version2) {
|
||||||
|
if (version1 == null || version2 == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] v1Parts = version1.split("\\.");
|
||||||
|
String[] v2Parts = version2.split("\\.");
|
||||||
|
|
||||||
|
int length = Math.max(v1Parts.length, v2Parts.length);
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
int v1 = i < v1Parts.length ? Integer.parseInt(v1Parts[i]) : 0;
|
||||||
|
int v2 = i < v2Parts.length ? Integer.parseInt(v2Parts[i]) : 0;
|
||||||
|
|
||||||
|
if (v1 > v2) {
|
||||||
|
return 1;
|
||||||
|
} else if (v1 < v2) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user