Browse Source

修复 设备添加或修改时候检测设备订阅topic是否相同,相同的topic只允许存在一个设备号

zhaiyifei 1 year ago
parent
commit
b44bfb1717

+ 1 - 1
src/main/java/com/yunfeiyun/agmp/iotm/web/controller/IotDeviceclassController.java

@@ -43,7 +43,7 @@ public class IotDeviceclassController extends BaseController
      * 创建设备时查询设备类型列表
      */
     @PreAuthorize("@ss.hasPermi('iot:deviceclass:create:list')")
-    @GetMapping("/create/list")
+    @GetMapping("/usable/list")
     public TableDataInfo listByCreateDev(TosDeviceclass reqVo) {
         startPage();
         List<TosDeviceclass> list = tosDeviceclassService.selectTosDeviceclassListByCreateDev(reqVo);

+ 1 - 1
src/main/java/com/yunfeiyun/agmp/iotm/web/controller/IotDevicetypeController.java

@@ -45,7 +45,7 @@ public class IotDevicetypeController extends BaseController
      * 创建编辑设备时候查询设备类型列表
      */
     @PreAuthorize("@ss.hasPermi('iot:devicetype:list')")
-    @GetMapping("/create/list")
+    @GetMapping("/usable/list")
     public TableDataInfo listByCreateDev(TosDevicetype tosDevicetype) {
         startPage();
         List<IotDevicetypeListResVo> list = tosDevicetypeService.listByCreateDev(tosDevicetype);

+ 1 - 7
src/main/java/com/yunfeiyun/agmp/iotm/web/service/IIotDeviceService.java

@@ -70,12 +70,6 @@ public interface IIotDeviceService {
 
     List<IotDeviceListResVo> selectIotDeviceListByType(IotDeviceListReqVo reqVo);
 
-    /**
-     * 检查设备基础列表
-     *
-     * @param iotDevice 设备基础
-     * @return 设备基础集合
-     */
-    public List<IotDevice> checkIotDeviceList(IotDevice iotDevice);
+    public String checkIotDeviceExist(String devtypeBid, String[] devCode);
 
 }

+ 33 - 35
src/main/java/com/yunfeiyun/agmp/iotm/web/service/impl/IotDeviceServiceImpl.java

@@ -5,6 +5,7 @@ import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
 import com.yunfeiyun.agmp.common.utils.DateUtils;
 import com.yunfeiyun.agmp.common.utils.SecurityUtils;
 import com.yunfeiyun.agmp.common.utils.StringUtils;
+import com.yunfeiyun.agmp.iot.common.constant.mqtt.IotMqttConstant;
 import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
 import com.yunfeiyun.agmp.iot.common.domain.IotDeviceconn;
 import com.yunfeiyun.agmp.iot.common.enums.IotDeviceDelStatusEnum;
@@ -32,10 +33,7 @@ import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
 import java.math.BigDecimal;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
 
 /**
  * 设备基础Service业务层处理
@@ -111,20 +109,16 @@ public class IotDeviceServiceImpl implements IIotDeviceService
         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);
+        }
+
         IotDevice selectIotDevice = new IotDevice();
         selectIotDevice.setDevtypeBid(devtypeBid);
 
-        List<IotDevice> iotDeviceList = checkIotDeviceList(selectIotDevice);
-        Set<String> devCodeSet = new HashSet<>();
-        for (IotDevice iotDevice : iotDeviceList) {
-            devCodeSet.add(iotDevice.getDevtypeBid() + iotDevice.getDevCode());
-        }
         List<IotDevice> insertIotDeviceList = new ArrayList<>();
         for (String devCode : devCodeList) {
-            if(devCodeSet.contains(devtypeBid + devCode)){
-                throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"该设备已存在:"+devCode);
-            }
-
             IotDevice iotDevice = new IotDevice();
             iotDevice.setDevBid(iotDevice.getUUId());
             iotDevice.setDevtypeBid(devtypeBid);
@@ -198,20 +192,15 @@ public class IotDeviceServiceImpl implements IIotDeviceService
             throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备连接不存在");
         }
 
-        IotDevice selectIotDevice = new IotDevice();
-        selectIotDevice.setDevCode(devCode);
-        selectIotDevice.setDevtypeBid(newIotDeviceconn.getDevtypeBid());
-
-        List<IotDevice> iotDeviceList = checkIotDeviceList(selectIotDevice);
-        Set<String> devSet = new HashSet<>();
-        for (IotDevice item : iotDeviceList) {
-            devSet.add(item.getFirmBid() + item.getDevtypeBid() + item.getDevCode());
-        }
-        String devKey = iotDevice.getFirmBid() + iotDevice.getDevtypeBid() + iotDevice.getDevCode();
-        devSet.remove(devKey);
-        if(!devSet.isEmpty()){
-            throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"该设备编码已存在");
+        String devtypeBid = newIotDeviceconn.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);
+            }
         }
+
         IotDevice updateIotDevice = new IotDevice();
         updateIotDevice.setDevBid(iotDevice.getDevBid());
         updateIotDevice.setDevName(devName);
@@ -385,15 +374,24 @@ public class IotDeviceServiceImpl implements IIotDeviceService
         return iotDeviceMapper.selectIotDeviceListByType(reqVo);
     }
 
-    /**
-     * 检查设备基础列表
-     *
-     * @param iotDevice 设备基础
-     * @return 设备基础集合
-     */
     @Override
-    public List<IotDevice> checkIotDeviceList(IotDevice iotDevice) {
-        return selectIotDeviceList(iotDevice, false);
+    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){
+            devCodeSet.add(iotDevice.getDevtypeBid() + iotDevice.getDevCode());
+            devTopicSet.add(IotMqttConstant.getReportTopicByDevtype(iotDevice.getDevtypeBid()));
+        }
+        String topic = IotMqttConstant.getReportTopicByDevtype(devtypeBid);
+        for(String code : devCode){
+            String key = devtypeBid + code;
+            // 同一个mqtt Topic只能有一个设备
+            // 同一个设备类型下不能有重复的设备编号
+            if(devTopicSet.contains(topic) || devCodeSet.contains(key)){
+                return code;
+            }
+        }
+        return null;
     }
-
 }