优化设备注册接口代码结构

This commit is contained in:
wulin 2023-09-18 20:08:23 +08:00
parent 501205c445
commit 36523de816

View File

@ -59,45 +59,52 @@ public class DeviceController {
return Mono.error(ex);
}
//设备类型是否匹配暂时不做限制
//验签
String wifiMd5 = MD5.create().digestHex(wifiMac).toLowerCase();
String btMd5 = MD5.create().digestHex(btMac).toLowerCase();
String md5 = MD5.create().digestHex(wifiMd5 + btMd5 + type + time + key).toLowerCase();
if (md5.equals(signature)) {
//
DeviceInfoRequest request = new DeviceInfoRequest();
request.setWifiMac(wifiMac);
request.setBtMac(btMac);
Mono<DeviceInfoEntity> mono = deviceInfoService.selectDeviceInfoByRequest(request);
return mono.defaultIfEmpty(new DeviceInfoEntity()).flatMap(entity -> {
if(entity.getId() == null){
DeviceTypeEnum entryTypeEnum = DeviceTypeEnum.getEnumWithCode(type);
entity = new DeviceInfoEntity();
entity.setWifiMac(wifiMac);
entity.setBtMac(btMac);
entity.setBatchId(1l);
entity.setName(entryTypeEnum.getName());
entity.setDeviceType(type);
entity.setKey( com.qiuguo.iot.base.utils.StringUtils.getRandomStr(10));//生成key
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyMMddHHmmss");
entity.setSn("QG" + entryTypeEnum.getSn() +df.format(LocalDateTime.now()) + StringUtils.getRandomStr(3));
deviceInfoService.insertDeviceInfo(entity);
}else{
entity.setKey( com.qiuguo.iot.base.utils.StringUtils.getRandomStr(10));//重新生成Key
deviceInfoService.updateDeviceInfoById(entity);
}
return Mono.just(entity);
}).map(o -> {
DeviceInitResp resp = new DeviceInitResp();
resp.setKey(o.getKey());
resp.setSn(o.getSn());
return resp;
});
DeviceTypeEnum entryTypeEnum = DeviceTypeEnum.getEnumWithCode(type);
if(entryTypeEnum == null){
BusinessException ex = new BusinessException("不支持的设备类型");
return Mono.error(ex);
}
//验签失败
BusinessException ex = new BusinessException("验签失败");
return Mono.error(ex);
//验签
String wifiMd5 = MD5.create().digestHex(wifiMac);
String btMd5 = MD5.create().digestHex(btMac);
String md5 = MD5.create().digestHex(wifiMd5 + btMd5 + type + time + key);
if (!md5.equals(signature.toLowerCase())) {
//
//验签失败
BusinessException ex = new BusinessException("验签失败");
return Mono.error(ex);
}
DeviceInfoRequest request = new DeviceInfoRequest();
request.setWifiMac(wifiMac);
request.setBtMac(btMac);
Mono<DeviceInfoEntity> mono = deviceInfoService.selectDeviceInfoByRequest(request);
return mono.defaultIfEmpty(new DeviceInfoEntity()).flatMap(entity -> {
if(entity.getId() == null){
entity = new DeviceInfoEntity();
entity.setWifiMac(wifiMac);
entity.setBtMac(btMac);
entity.setBatchId(1l);
entity.setName(entryTypeEnum.getName());
entity.setDeviceType(type);
entity.setKey( com.qiuguo.iot.base.utils.StringUtils.getRandomStr(10));//生成key
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyMMddHHmmss");
entity.setSn("QG" + entryTypeEnum.getSn() +df.format(LocalDateTime.now()) + StringUtils.getRandomStr(3));
deviceInfoService.insertDeviceInfo(entity);
}else{
entity.setKey( com.qiuguo.iot.base.utils.StringUtils.getRandomStr(10));//重新生成Key
deviceInfoService.updateDeviceInfoById(entity);
}
return Mono.just(entity);
}).map(o -> {
DeviceInitResp resp = new DeviceInitResp();
resp.setKey(o.getKey());
resp.setSn(o.getSn());
return resp;
});
}
}