From 36523de816460f77b8e4748e93031a35957432bf Mon Sep 17 00:00:00 2001 From: wulin Date: Mon, 18 Sep 2023 20:08:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AE=BE=E5=A4=87=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E6=8E=A5=E5=8F=A3=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/device/DeviceController.java | 83 ++++++++++--------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/device/DeviceController.java b/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/device/DeviceController.java index e9ab1a3..f2327ee 100644 --- a/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/device/DeviceController.java +++ b/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/device/DeviceController.java @@ -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 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 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; + }); + } }