update:ota接口验证通过 (#675)

This commit is contained in:
hrz
2025-04-06 02:09:49 +08:00
committed by GitHub
parent 2f2884e991
commit 5655362a00
17 changed files with 42 additions and 75 deletions
+1 -1
View File
@@ -92,5 +92,5 @@ nohup java -jar xiaozhi-esp32-api.jar --spring.profiles.activate=dev
# 接口文档
启动后打开:http://localhost:8002/xiaozhi-esp32-api/doc.html
启动后打开:http://localhost:8002/xiaozhi/doc.html
@@ -8,6 +8,6 @@ public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
System.out.println("http://localhost:8002/xiaozhi-esp32-api/api/v1/doc.html");
System.out.println("http://localhost:8002/xiaozhi/doc.html");
}
}
@@ -19,7 +19,6 @@ import xiaozhi.common.redis.RedisKeys;
import xiaozhi.common.redis.RedisUtils;
import xiaozhi.common.user.UserDetail;
import xiaozhi.common.utils.Result;
import xiaozhi.modules.device.dto.DeviceBindDTO;
import xiaozhi.modules.device.dto.DeviceRegisterDTO;
import xiaozhi.modules.device.dto.DeviceUnBindDTO;
import xiaozhi.modules.device.entity.DeviceEntity;
@@ -39,13 +38,7 @@ public class DeviceController {
@Operation(summary = "绑定设备")
@RequiresPermissions("sys:role:normal")
public Result<Void> bindDevice(@PathVariable String agentId, @PathVariable String deviceCode) {
String macAddress = (String) redisUtils.get(RedisKeys.getDeviceCaptchaKey(deviceCode));
if (StringUtils.isBlank(macAddress)) {
return new Result<Void>().error(ErrorCode.DEVICE_CAPTCHA_ERROR);
}
Long user = SecurityUser.getUser().getId();
DeviceBindDTO deviceBindDTO = new DeviceBindDTO(macAddress, user, agentId);
deviceService.bindDevice(deviceBindDTO);
deviceService.deviceActivation(agentId, deviceCode);
return new Result<>();
}
@@ -5,12 +5,10 @@ import java.nio.charset.StandardCharsets;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonInclude;
@@ -22,7 +20,6 @@ import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import xiaozhi.common.utils.Result;
import xiaozhi.modules.device.dto.DeviceReportReqDTO;
import xiaozhi.modules.device.dto.DeviceReportRespDTO;
import xiaozhi.modules.device.service.DeviceService;
@@ -31,7 +28,7 @@ import xiaozhi.modules.device.utils.NetworkUtil;
@Tag(name = "设备管理", description = "OTA 相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/ota")
@RequestMapping("/ota/")
public class OTAController {
private final DeviceService deviceService;
@@ -55,12 +52,6 @@ public class OTAController {
return createResponse(deviceService.checkDeviceActive(macAddress, deviceId, clientId, deviceReportReqDTO));
}
@Operation(summary = "设备激活")
@GetMapping("/activation")
public Result<Boolean> deviceActivation(@RequestParam String code) {
return new Result<Boolean>().ok(deviceService.deviceActivation(code));
}
@SneakyThrows
private ResponseEntity<String> createResponse(DeviceReportRespDTO deviceReportRespDTO) {
ObjectMapper objectMapper = new ObjectMapper();
@@ -9,7 +9,7 @@ import lombok.Setter;
@Schema(description = "设备OTA检测版本返回体,包含激活码要求")
public class DeviceReportRespDTO {
@Schema(description = "服务器时间")
private ServerTime serverTime;
private ServerTime server_time;
@Schema(description = "激活码")
private Activation activation;
@@ -56,6 +56,6 @@ public class DeviceReportRespDTO {
private String timeZone;
@Schema(description = "时区偏移量,单位为分钟")
private Integer timezoneOffset;
private Integer timezone_offset;
}
}
@@ -3,7 +3,6 @@ package xiaozhi.modules.device.service;
import java.util.List;
import xiaozhi.common.page.PageData;
import xiaozhi.modules.device.dto.DeviceBindDTO;
import xiaozhi.modules.device.dto.DevicePageUserDTO;
import xiaozhi.modules.device.dto.DeviceReportReqDTO;
import xiaozhi.modules.device.dto.DeviceReportRespDTO;
@@ -23,11 +22,6 @@ public interface DeviceService {
DeviceReportRespDTO checkDeviceActive(String macAddress, String deviceId, String clientId,
DeviceReportReqDTO deviceReport);
/**
* 绑定设备
*/
DeviceEntity bindDevice(DeviceBindDTO deviceHeader);
/**
* 获取用户指定智能体的设备列表,
*/
@@ -41,7 +35,7 @@ public interface DeviceService {
/**
* 设备激活
*/
Boolean deviceActivation(String activationCode);
Boolean deviceActivation(String agentId, String activationCode);
/**
* 删除此用户的所有设备
@@ -27,7 +27,6 @@ import xiaozhi.common.user.UserDetail;
import xiaozhi.common.utils.ConvertUtils;
import xiaozhi.common.utils.DateUtils;
import xiaozhi.modules.device.dao.DeviceDao;
import xiaozhi.modules.device.dto.DeviceBindDTO;
import xiaozhi.modules.device.dto.DevicePageUserDTO;
import xiaozhi.modules.device.dto.DeviceReportReqDTO;
import xiaozhi.modules.device.dto.DeviceReportRespDTO;
@@ -66,7 +65,7 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
}
@Override
public Boolean deviceActivation(String activationCode) {
public Boolean deviceActivation(String agentId, String activationCode) {
if (StringUtils.isBlank(activationCode)) {
throw new RenException("激活码不能为空");
}
@@ -103,6 +102,7 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
DeviceEntity deviceEntity = new DeviceEntity();
deviceEntity.setId(deviceId);
deviceEntity.setBoard(board);
deviceEntity.setAgentId(agentId);
deviceEntity.setAppVersion(appVersion);
deviceEntity.setMacAddress(macAddress);
deviceEntity.setUserId(user.getId());
@@ -123,13 +123,13 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
public DeviceReportRespDTO checkDeviceActive(String macAddress, String deviceId, String clientId,
DeviceReportReqDTO deviceReport) {
DeviceReportRespDTO response = new DeviceReportRespDTO();
response.setServerTime(buildServerTime());
response.setServer_time(buildServerTime());
// todo: 此处是固件信息,目前是针对固件上传上来的版本号再返回回去
// 在未来开发了固件更新功能,需要更换此处代码,
// 或写定时任务定期请求虾哥的OTA,获取最新的版本讯息保存到服务内
DeviceReportRespDTO.Firmware firmware = new DeviceReportRespDTO.Firmware();
firmware.setVersion(deviceReport.getApplication().getVersion());
firmware.setUrl("http://localhost:8002/xiaozhi-esp32-api/api/v1/ota/download");
firmware.setUrl("http://localhost:8002/xiaozhi/ota/download");
response.setFirmware(firmware);
DeviceEntity deviceById = getDeviceById(deviceId);
@@ -178,19 +178,6 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
return response;
}
@Override
public DeviceEntity bindDevice(DeviceBindDTO dto) {
// 查看是否已经被绑定
DeviceEntity deviceEntity = baseDao
.selectOne(new LambdaQueryWrapper<DeviceEntity>().eq(DeviceEntity::getMacAddress, dto.getMacAddress()));
if (deviceEntity != null) {
throw new RenException("设备已绑定");
}
deviceEntity = ConvertUtils.sourceToTarget(dto, DeviceEntity.class);
baseDao.insert(deviceEntity);
return deviceEntity;
}
@Override
public List<DeviceEntity> getUserDevices(Long userId, String agentId) {
QueryWrapper<DeviceEntity> wrapper = new QueryWrapper<>();
@@ -251,7 +238,7 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
TimeZone tz = TimeZone.getDefault();
serverTime.setTimestamp(Instant.now().toEpochMilli());
serverTime.setTimeZone(tz.getID());
serverTime.setTimezoneOffset(tz.getOffset(System.currentTimeMillis()) / (60 * 1000));
serverTime.setTimezone_offset(tz.getOffset(System.currentTimeMillis()) / (60 * 1000));
return serverTime;
}
}
@@ -7,7 +7,7 @@ server:
min-spare: 30
port: 8002
servlet:
context-path: /xiaozhi-esp32-api/api/v1
context-path: /xiaozhi
session:
cookie:
http-only: true
@@ -26,6 +26,8 @@ spring:
enabled: true
main:
allow-bean-definition-overriding: true
liquibase:
enabled: false
knife4j:
enable: true