Просмотр исходного кода

新增设备时过滤去除设备序列号中的所有的换行符,回车符,制表符,空格

liuyaowen 1 год назад
Родитель
Сommit
1c413bf49b

+ 48 - 50
src/main/java/com/yunfeiyun/agmp/iotm/web/service/impl/IotDeviceServiceImpl.java

@@ -43,8 +43,7 @@ import java.util.*;
  */
 @Service
 @Slf4j
-public class IotDeviceServiceImpl implements IIotDeviceService
-{
+public class IotDeviceServiceImpl implements IIotDeviceService {
     @Autowired
     private IotDeviceMapper iotDeviceMapper;
 
@@ -77,41 +76,42 @@ public class IotDeviceServiceImpl implements IIotDeviceService
         BigDecimal devLngalign = reqVo.getDevLngalign();
         BigDecimal devLatalign = reqVo.getDevLatalign();
 
-        if(devCodeList == null || devCodeList.length == 0){
-            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"设备编码不能为空");
+        if (devCodeList == null || devCodeList.length == 0) {
+            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备编码不能为空");
         }
         Set<String> tempDevCodeSet = new HashSet<>();
-        for(String devCode : devCodeList){
-            if(StringUtils.isEmpty(devCode)){
-                throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"设备编码不能为空");
+        for (String devCode : devCodeList) {
+            if (StringUtils.isEmpty(devCode)) {
+                throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备编码不能为空");
             }
+            devCode = devCode.replaceAll("[\r\n\t ]", "");
             tempDevCodeSet.add(devCode);
         }
-        if(tempDevCodeSet.size() != devCodeList.length){
-            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"设备编码不能重复");
+        if (tempDevCodeSet.size() != devCodeList.length) {
+            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备编码不能重复");
         }
 
         IotDeviceconn iotDeviceconn = iotDeviceconnService.selectIotDeviceconnByBid(devconnBid);
-        if(iotDeviceconn == null){
+        if (iotDeviceconn == null) {
             throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备连接不存在");
         }
         String devName = iotDeviceconn.getDevtypeName();
-        if(devCodeList.length == 1){
+        if (devCodeList.length == 1) {
             devName = reqVo.getDevName();
             devLngalign = null;
             devLatalign = null;
         }
 
-        if(StringUtils.isEmpty(devName) ){
-            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"设备名称不能为空");
+        if (StringUtils.isEmpty(devName)) {
+            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备名称不能为空");
         }
 
         String firmBid = iotDeviceconn.getFirmBid();
         String devtypeBid = iotDeviceconn.getDevtypeBid();
 
         String devCodeExist = checkIotDeviceExist(devtypeBid, devCodeList);
-        if(StringUtils.isNotEmpty(devCodeExist) ){
-            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"该设备已存在:"+devCodeExist);
+        if (StringUtils.isNotEmpty(devCodeExist)) {
+            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "该设备已存在:" + devCodeExist);
         }
 
         List<IotDevice> insertIotDeviceList = new ArrayList<>();
@@ -158,7 +158,7 @@ public class IotDeviceServiceImpl implements IIotDeviceService
         List<IotDeviceListResVo> iotDeviceListResVoList = new ArrayList<>();
         for (IotDevice iotDevice : data) {
             IotDeviceListResVo iotDeviceListResVo = new IotDeviceListResVo();
-            BeanUtils.copyProperties(iotDevice,iotDeviceListResVo);
+            BeanUtils.copyProperties(iotDevice, iotDeviceListResVo);
             iotDeviceListResVoList.add(iotDeviceListResVo);
         }
         tableDataInfo.setData(iotDeviceListResVoList);
@@ -174,28 +174,28 @@ public class IotDeviceServiceImpl implements IIotDeviceService
         BigDecimal devLngalign = reqVo.getDevLngalign();
         BigDecimal devLatalign = reqVo.getDevLatalign();
 
-        if(StringUtils.isEmpty(devName) || StringUtils.isEmpty(devCode)){
-            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"参数不能为空");
+        if (StringUtils.isEmpty(devName) || StringUtils.isEmpty(devCode)) {
+            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "参数不能为空");
         }
 
         IotDevice iotDevice = selectIotDeviceByDevBid(devBid);
-        if(iotDevice == null){
-            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"设备不存在");
+        if (iotDevice == null) {
+            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备不存在");
         }
 
         IotDeviceconn oldIotDeviceconn = iotDeviceconnService.selectIotDeviceconnByBid(iotDevice.getDevconnBid());
 
         IotDeviceconn newIotDeviceconn = iotDeviceconnService.selectIotDeviceconnByBid(devconnBid);
-        if(newIotDeviceconn == null){
+        if (newIotDeviceconn == null) {
             throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备连接不存在");
         }
 
         String devtypeBid = newIotDeviceconn.getDevtypeBid();
-        if(!(devCode.equals(iotDevice.getDevCode()) && devtypeBid.equals(iotDevice.getDevtypeBid()))){
+        if (!(devCode.equals(iotDevice.getDevCode()) && devtypeBid.equals(iotDevice.getDevtypeBid()))) {
             String[] devCodeList = {devCode};
             String devCodeExist = checkIotDeviceExist(devtypeBid, devCodeList);
-            if(StringUtils.isNotEmpty(devCodeExist) ){
-                throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"该设备已存在:"+devCodeExist);
+            if (StringUtils.isNotEmpty(devCodeExist)) {
+                throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "该设备已存在:" + devCodeExist);
             }
         }
 
@@ -222,12 +222,12 @@ public class IotDeviceServiceImpl implements IIotDeviceService
     public int modifyNameIotDevice(IotDeviceModifyReqVo reqVo) {
         String devBid = reqVo.getDevBid();
         String devName = reqVo.getDevName();
-        if(StringUtils.isEmpty(devName)){
-            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"参数不能为空");
+        if (StringUtils.isEmpty(devName)) {
+            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "参数不能为空");
         }
         IotDevice iotDevice = selectIotDeviceByDevBid(devBid);
-        if(iotDevice == null){
-            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"设备不存在");
+        if (iotDevice == null) {
+            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备不存在");
         }
         IotDevice updateIotDevice = new IotDevice();
         updateIotDevice.setDevBid(iotDevice.getDevBid());
@@ -241,8 +241,8 @@ public class IotDeviceServiceImpl implements IIotDeviceService
     @Override
     public int removeIotDevice(String devBid) {
         IotDevice iotDevice = selectIotDeviceByDevBid(devBid);
-        if(iotDevice == null){
-            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"设备不存在");
+        if (iotDevice == null) {
+            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备不存在");
         }
         return deleteIotDeviceByDevBid(iotDevice);
     }
@@ -254,8 +254,7 @@ public class IotDeviceServiceImpl implements IIotDeviceService
      * @return 设备基础
      */
     @Override
-    public IotDevice selectIotDeviceByDevBid(String devBid)
-    {
+    public IotDevice selectIotDeviceByDevBid(String devBid) {
         return iotDeviceMapper.selectIotDeviceByDevBid(devBid);
     }
 
@@ -278,9 +277,9 @@ public class IotDeviceServiceImpl implements IIotDeviceService
      */
     public List<IotDevice> selectIotDeviceList(IotDevice iotDevice, boolean isTid) {
         // 生产环境
-        if("0".equals(runMode) && !isTid){
+        if ("0".equals(runMode) && !isTid) {
             iotDevice.setTid(null);
-        }else{
+        } else {
             iotDevice.setTid(SecurityUtils.getTid());
         }
         return iotDeviceMapper.selectIotDeviceList(iotDevice);
@@ -300,7 +299,7 @@ public class IotDeviceServiceImpl implements IIotDeviceService
         int status = iotDeviceMapper.insertIotDeviceByBatch(iotDeviceList);
         // 同步新增设备到TOS
         // 获取连接 订阅设备
-        for(IotDevice iotDevice : iotDeviceList){
+        for (IotDevice iotDevice : iotDeviceList) {
             sendToTosMsgService.sendIotDeviceInsertMsg(iotDevice);
             sendToIotsMsgService.sendIotDeviceInsertMsg(iotDevice);
         }
@@ -311,13 +310,13 @@ public class IotDeviceServiceImpl implements IIotDeviceService
                 for (IotDevice iotDevice : iotDeviceList) {
                     try {
                         iotDeviceRefreshService.refresh(iotDevice.getDevBid());
-                        log.info("创建设备刷新成功: " + iotDevice);
+                        IotDeviceServiceImpl.log.info("创建设备刷新成功: " + iotDevice);
                     } catch (Exception e) {
-                        log.error("创建设备刷新失败: " + iotDevice, e);
+                        IotDeviceServiceImpl.log.error("创建设备刷新失败: " + iotDevice, e);
                     }
                 }
             } catch (Exception e) {
-                log.error("创建设备刷新失败: " + iotDeviceList, e);
+                IotDeviceServiceImpl.log.error("创建设备刷新失败: " + iotDeviceList, e);
             }
         }).start();
 
@@ -331,12 +330,12 @@ public class IotDeviceServiceImpl implements IIotDeviceService
      * @return 结果
      */
     @Override
-    public int updateIotDevice(IotDevice iotDevice, IotDeviceconn oldDeviceconn, IotDeviceconn newDeviceconn){
+    public int updateIotDevice(IotDevice iotDevice, IotDeviceconn oldDeviceconn, IotDeviceconn newDeviceconn) {
         IotDevice oldDevice = iotDeviceMapper.selectIotDeviceByDevBid(iotDevice.getDevBid());
         int result = iotDeviceMapper.updateIotDevice(iotDevice);
         // 同步修改设备到TOS
         sendToTosMsgService.sendIotDeviceUpdateMsg(iotDevice);
-        if(oldDeviceconn != null && newDeviceconn != null){
+        if (oldDeviceconn != null && newDeviceconn != null) {
             // 解除旧连接订阅 串行
             // 订阅新连接 串行
             IotDeviceEditMqModel iotDeviceEditMqModel = new IotDeviceEditMqModel();
@@ -348,7 +347,6 @@ public class IotDeviceServiceImpl implements IIotDeviceService
     }
 
 
-
     /**
      * 删除设备基础信息
      *
@@ -373,23 +371,23 @@ public class IotDeviceServiceImpl implements IIotDeviceService
     }
 
     @Override
-    public String checkIotDeviceExist(String devtypeBid, String[] devCode){
+    public String checkIotDeviceExist(String devtypeBid, String[] devCode) {
         List<IotDevice> iotDeviceList = selectIotDeviceList(new IotDevice(), false);
         Set<String> devCodeSet = new HashSet<>();
         Set<String> devTopicSet = new HashSet<>();
-        for(IotDevice iotDevice : iotDeviceList){
+        for (IotDevice iotDevice : iotDeviceList) {
             devCodeSet.add(iotDevice.getDevtypeBid() + iotDevice.getDevCode());
             String topic = IotMqttConstant.getReportTopicByDevtype(iotDevice.getDevtypeBid());
-            if(StringUtils.isNotEmpty(topic)){
+            if (StringUtils.isNotEmpty(topic)) {
                 devTopicSet.add(IotMqttConstant.getReportTopicByDevtype(iotDevice.getDevtypeBid()) + iotDevice.getDevCode());
             }
         }
         String topic = IotMqttConstant.getReportTopicByDevtype(devtypeBid);
-        for(String code : devCode){
+        for (String code : devCode) {
             String key = devtypeBid + code;
             // 同一个mqtt Topic只能有一个设备
             // 同一个设备类型下不能有重复的设备编号
-            if(devTopicSet.contains(topic + code) || devCodeSet.contains(key)){
+            if (devTopicSet.contains(topic + code) || devCodeSet.contains(key)) {
                 return code;
             }
         }
@@ -400,12 +398,12 @@ public class IotDeviceServiceImpl implements IIotDeviceService
     public int updateIotDeviceExtInfo(IotDevice iotDevice) {
         String devBid = iotDevice.getDevBid();
         String extInfo = iotDevice.getExtInfo();
-        if(StringUtils.isEmpty(devBid) || StringUtils.isEmpty(extInfo)){
-            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"参数不合法");
+        if (StringUtils.isEmpty(devBid) || StringUtils.isEmpty(extInfo)) {
+            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "参数不合法");
         }
         IotDevice updateIotDevice = selectIotDeviceByDevBid(devBid);
-        if(updateIotDevice == null){
-            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"设备不存在");
+        if (updateIotDevice == null) {
+            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备不存在");
         }
         updateIotDevice.setExtInfo(iotDevice.getExtInfo());
         updateIotDevice.setDevUpdateddate(DateUtils.dateTimeNow());