优化设备注册接口代码结构
This commit is contained in:
parent
501205c445
commit
36523de816
@ -59,45 +59,52 @@ public class DeviceController {
|
|||||||
return Mono.error(ex);
|
return Mono.error(ex);
|
||||||
}
|
}
|
||||||
//设备类型是否匹配暂时不做限制
|
//设备类型是否匹配暂时不做限制
|
||||||
//验签
|
DeviceTypeEnum entryTypeEnum = DeviceTypeEnum.getEnumWithCode(type);
|
||||||
String wifiMd5 = MD5.create().digestHex(wifiMac).toLowerCase();
|
if(entryTypeEnum == null){
|
||||||
String btMd5 = MD5.create().digestHex(btMac).toLowerCase();
|
BusinessException ex = new BusinessException("不支持的设备类型");
|
||||||
String md5 = MD5.create().digestHex(wifiMd5 + btMd5 + type + time + key).toLowerCase();
|
return Mono.error(ex);
|
||||||
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;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
//验签失败
|
//验签
|
||||||
BusinessException ex = new BusinessException("验签失败");
|
String wifiMd5 = MD5.create().digestHex(wifiMac);
|
||||||
return Mono.error(ex);
|
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;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user