mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-25 04:29:51 +01:00
智控台设备地址归一化,拒绝时统一回 400
设备上报的地址写法有带冒号、连字符、无分隔符、大写多种,入库比对却是 按原文精确匹配,写法对不上就查不到设备,结果是不发凭证也不发固件, 只留一行日志。在接口入口统一归一成小写带冒号,后续查库、签名、 拼订阅地址、算缓存键都用同一个值。 校验不通过时两端回的状态码也不一样,改成都回 400,固件按状态码判断 才不会走偏。另外设备没登记又没带版本信息时本来会 500,补上判空。
This commit is contained in:
parent
340c3698d2
commit
ed4bbff6cb
2 changed files with 41 additions and 5 deletions
|
|
@ -3,6 +3,7 @@ package xiaozhi.modules.device.controller;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
|
@ -46,15 +47,16 @@ public class OTAController {
|
||||||
@Parameter(name = "Device-Id", description = "设备唯一标识", required = true, in = ParameterIn.HEADER) @RequestHeader("Device-Id") String deviceId,
|
@Parameter(name = "Device-Id", description = "设备唯一标识", required = true, in = ParameterIn.HEADER) @RequestHeader("Device-Id") String deviceId,
|
||||||
@Parameter(name = "Client-Id", description = "客户端标识", required = false, in = ParameterIn.HEADER) @RequestHeader(value = "Client-Id", required = false) String clientId) {
|
@Parameter(name = "Client-Id", description = "客户端标识", required = false, in = ParameterIn.HEADER) @RequestHeader(value = "Client-Id", required = false) String clientId) {
|
||||||
if (StringUtils.isBlank(deviceId)) {
|
if (StringUtils.isBlank(deviceId)) {
|
||||||
return createResponse(DeviceReportRespDTO.createError("Device ID is required"));
|
return createResponse(DeviceReportRespDTO.createError("Device ID is required"), HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
deviceId = normalizeDeviceId(deviceId);
|
||||||
if (StringUtils.isBlank(clientId)) {
|
if (StringUtils.isBlank(clientId)) {
|
||||||
clientId = deviceId;
|
clientId = deviceId;
|
||||||
}
|
}
|
||||||
boolean macAddressValid = isMacAddressValid(deviceId);
|
boolean macAddressValid = isMacAddressValid(deviceId);
|
||||||
// 设备Id和Mac地址应是一致的, 并且必须需要application字段
|
// 设备Id和Mac地址应是一致的, 并且必须需要application字段
|
||||||
if (!macAddressValid) {
|
if (!macAddressValid) {
|
||||||
return createResponse(DeviceReportRespDTO.createError("Invalid device ID"));
|
return createResponse(DeviceReportRespDTO.createError("Invalid device ID"), HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
return createResponse(deviceService.checkDeviceActive(deviceId, clientId, deviceReportReqDTO));
|
return createResponse(deviceService.checkDeviceActive(deviceId, clientId, deviceReportReqDTO));
|
||||||
}
|
}
|
||||||
|
|
@ -94,19 +96,50 @@ public class OTAController {
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
private ResponseEntity<String> createResponse(DeviceReportRespDTO deviceReportRespDTO) {
|
private ResponseEntity<String> createResponse(DeviceReportRespDTO deviceReportRespDTO) {
|
||||||
|
return createResponse(deviceReportRespDTO, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
private ResponseEntity<String> createResponse(DeviceReportRespDTO deviceReportRespDTO, HttpStatus status) {
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||||
String json = objectMapper.writeValueAsString(deviceReportRespDTO);
|
String json = objectMapper.writeValueAsString(deviceReportRespDTO);
|
||||||
byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8);
|
byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8);
|
||||||
return ResponseEntity
|
return ResponseEntity
|
||||||
.ok()
|
.status(status)
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.contentLength(jsonBytes.length)
|
.contentLength(jsonBytes.length)
|
||||||
.body(json);
|
.body(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 简单判断mac地址是否有效(非严格)
|
* 把设备上报的 Device-Id 归一成小写冒号分隔的 MAC 形态。
|
||||||
|
* 入库存的是用户在智控台填的原始字符串,查询、签名、topic、缓存键
|
||||||
|
* 统一用归一后的值,否则同一台设备换个写法就会被当成没登记
|
||||||
|
*
|
||||||
|
* @param deviceId
|
||||||
|
* @return 归一后的 Device-Id;不是合法 MAC 时原样返回,交给后续校验拒绝
|
||||||
|
*/
|
||||||
|
private static String normalizeDeviceId(String deviceId) {
|
||||||
|
if (StringUtils.isBlank(deviceId)) {
|
||||||
|
return deviceId;
|
||||||
|
}
|
||||||
|
String hex = deviceId.toLowerCase().replace(":", "").replace("-", "").replace("_", "");
|
||||||
|
if (hex.length() != 12) {
|
||||||
|
return deviceId;
|
||||||
|
}
|
||||||
|
StringBuilder mac = new StringBuilder(17);
|
||||||
|
for (int i = 0; i < 12; i += 2) {
|
||||||
|
if (i > 0) {
|
||||||
|
mac.append(':');
|
||||||
|
}
|
||||||
|
mac.append(hex, i, i + 2);
|
||||||
|
}
|
||||||
|
return mac.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断 mac 地址是否有效,收紧为严格的十六进制 MAC 形态
|
||||||
*
|
*
|
||||||
* @param macAddress
|
* @param macAddress
|
||||||
* @return
|
* @return
|
||||||
|
|
|
||||||
|
|
@ -198,7 +198,10 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
|
||||||
// 设备未绑定,则返回当前上传的固件信息(不更新)以此兼容旧固件版本
|
// 设备未绑定,则返回当前上传的固件信息(不更新)以此兼容旧固件版本
|
||||||
if (deviceById == null) {
|
if (deviceById == null) {
|
||||||
DeviceReportRespDTO.Firmware firmware = new DeviceReportRespDTO.Firmware();
|
DeviceReportRespDTO.Firmware firmware = new DeviceReportRespDTO.Firmware();
|
||||||
firmware.setVersion(deviceReport.getApplication().getVersion());
|
String currentVersion = deviceReport != null && deviceReport.getApplication() != null
|
||||||
|
? deviceReport.getApplication().getVersion()
|
||||||
|
: null;
|
||||||
|
firmware.setVersion(currentVersion);
|
||||||
firmware.setUrl(Constant.INVALID_FIRMWARE_URL);
|
firmware.setUrl(Constant.INVALID_FIRMWARE_URL);
|
||||||
response.setFirmware(firmware);
|
response.setFirmware(firmware);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue