智控台设备地址归一化,拒绝时统一回 400

设备上报的地址写法有带冒号、连字符、无分隔符、大写多种,入库比对却是
按原文精确匹配,写法对不上就查不到设备,结果是不发凭证也不发固件,
只留一行日志。在接口入口统一归一成小写带冒号,后续查库、签名、
拼订阅地址、算缓存键都用同一个值。

校验不通过时两端回的状态码也不一样,改成都回 400,固件按状态码判断
才不会走偏。另外设备没登记又没带版本信息时本来会 500,补上判空。
This commit is contained in:
hrz 2026-10-22 02:24:39 +00:00
parent 340c3698d2
commit ed4bbff6cb
2 changed files with 41 additions and 5 deletions

View file

@ -3,6 +3,7 @@ package xiaozhi.modules.device.controller;
import java.nio.charset.StandardCharsets;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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 = "Client-Id", description = "客户端标识", required = false, in = ParameterIn.HEADER) @RequestHeader(value = "Client-Id", required = false) String clientId) {
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)) {
clientId = deviceId;
}
boolean macAddressValid = isMacAddressValid(deviceId);
// 设备Id和Mac地址应是一致的, 并且必须需要application字段
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));
}
@ -94,19 +96,50 @@ public class OTAController {
@SneakyThrows
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.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String json = objectMapper.writeValueAsString(deviceReportRespDTO);
byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8);
return ResponseEntity
.ok()
.status(status)
.contentType(MediaType.APPLICATION_JSON)
.contentLength(jsonBytes.length)
.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
* @return

View file

@ -198,7 +198,10 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
// 设备未绑定,则返回当前上传的固件信息(不更新)以此兼容旧固件版本
if (deviceById == null) {
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);
response.setFirmware(firmware);
} else {