Преглед изворни кода

通道配置,添加接口查询出未分配的树节点

gt пре 2 година
родитељ
комит
6c8bdf7f3e

+ 18 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/controller/ChannelSettingController.java

@@ -18,6 +18,12 @@ public class ChannelSettingController {
     @Resource
     private ChannelSettingService channelSettingService;
 
+    /**
+     * 保存通道配置
+     *
+     * @param channelSetting
+     * @return
+     */
     @PostMapping("/channelSetting")
     public Result channelSetting(@RequestBody ChannelSetting channelSetting) {
         if (Blank.isEmpty(channelSetting, channelSetting.getServerId(), channelSetting.getChannelName())) {
@@ -26,6 +32,12 @@ public class ChannelSettingController {
         return channelSettingService.channelSetting(channelSetting);
     }
 
+    /**
+     * 通过服务器id和通道名称获取通道的配置信息
+     *
+     * @param channelSetting
+     * @return
+     */
     @GetMapping("/getChannelSetting")
     public Result getChannelSetting(@RequestBody ChannelSetting channelSetting) {
         if (Blank.isEmpty(channelSetting, channelSetting.getServerId(), channelSetting.getChannelName())) {
@@ -34,6 +46,12 @@ public class ChannelSettingController {
         return channelSettingService.getChannelSetting(channelSetting.getServerId(), channelSetting.getChannelName());
     }
 
+    /**
+     * 通过服务器id获取此服务器下未分配的树节点
+     *
+     * @param serverId
+     * @return
+     */
     @GetMapping("/getConnectTree")
     public Result getConnectTree(Integer serverId) {
         if (Blank.isEmpty(serverId)) {

+ 2 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/dao/ChannelSettingDao.java

@@ -17,4 +17,6 @@ public interface ChannelSettingDao {
     Integer updateChannelSetting(ChannelSetting channelSetting);
 
     List<String> getConfigList(int serverId);
+
+    List<ChannelSetting> getChannelListNoConfig(Integer serverId);
 }

+ 6 - 2
chuanyi_server/src/main/java/com/judong/chuanyiserver/service/impl/ChannelSettingServiceImpl.java

@@ -64,8 +64,12 @@ public class ChannelSettingServiceImpl implements ChannelSettingService {
         if (serverInformation.getConnectMode() == ConnectModeEnum.KingOPCServer.getValue()) {
             if (KepOpcServerUtil.validationServerInformation(serverInformation)) {
                 JSONObject jsonObject = new JSONObject();
-                List<JSONObject> jsonObjectList = KepOpcServerUtil.opcReadItemTree(serverInformation);
-                jsonObject.put("tree", jsonObjectList);
+                List<ChannelSetting> channelSettings = channelSettingDao.getChannelListNoConfig(serverId);
+                if (Blank.isNotEmpty(channelSettings)) {
+                    jsonObject.put("tree", KepOpcServerUtil.opcNoConfigTree(serverInformation, channelSettings));
+                } else {
+                    jsonObject.put("tree", KepOpcServerUtil.opcReadItemTree(serverInformation));
+                }
                 return Result.ok(jsonObject);
             }
             return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());

+ 99 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/util/KepOpcServerUtil.java

@@ -2,6 +2,7 @@ package com.judong.chuanyiserver.util;
 
 import com.alibaba.fastjson.JSONObject;
 import com.judong.chuanyiserver.config.KepOpcServerPoolFactory;
+import com.judong.chuanyiserver.entity.ChannelSetting;
 import com.judong.chuanyiserver.entity.ServerInformation;
 import com.judong.chuanyiserver.enums.ConnectModeEnum;
 import com.judong.chuanyiserver.enums.ResultEnum;
@@ -184,6 +185,26 @@ public class KepOpcServerUtil {
         throw new CustomException(ResultEnum.SERVER_ERROR.getRespCode(), ResultEnum.SERVER_ERROR.getRespMsg());
     }
 
+    public static List<JSONObject> opcNoConfigTree(ServerInformation serverInformation, List<ChannelSetting> channelSettings) {
+        try {
+            String opcServerDaPoolKey = KepOpcServerUtil.generateOpcPoolKey(serverInformation);
+            if (KepOpcServerUtil.validationKey(opcServerDaPoolKey)) {
+                Server server = KepOpcServerUtil.getServer(opcServerDaPoolKey);
+                if (null == server.getServerState()) {
+                    throw new CustomException(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
+                }
+                if (OPCSERVERSTATE.OPC_STATUS_RUNNING == server.getServerState().getServerState()) {
+                    List<JSONObject> jsonObjectList = generOpcNoConfigTree(server, channelSettings);
+                    KepOpcServerUtil.returnServer(opcServerDaPoolKey, server);
+                    return jsonObjectList;
+                }
+            }
+        } catch (Exception e) {
+            throw new CustomException(ResultEnum.REQUEST_TIME_OUT.getRespCode(), e.getMessage());
+        }
+        throw new CustomException(ResultEnum.SERVER_ERROR.getRespCode(), ResultEnum.SERVER_ERROR.getRespMsg());
+    }
+
     public static Result closeConnect(ServerInformation serverInformation) {
         String opcServerDaPoolKey = KepOpcServerUtil.generateOpcPoolKey(serverInformation);
         if (KepOpcServerUtil.validationKey(opcServerDaPoolKey)) {
@@ -277,6 +298,84 @@ public class KepOpcServerUtil {
         return jsonObjectList;
     }
 
+    public static List<JSONObject> generOpcNoConfigTree(Server server, List<ChannelSetting> channelSettings) throws JIException, UnknownHostException {
+        List<JSONObject> jsonList = new ArrayList<>();
+        //获取服务器下所有ITEM列表,树形展示
+        TreeBrowser treeBrowser = server.getTreeBrowser();
+        Branch browse = treeBrowser.browse();
+        if (Blank.isEmpty(browse)) {
+            return null;
+        }
+        Collection<Branch> branches = browse.getBranches();
+        for (Branch branch : branches) {
+            Iterator<ChannelSetting> iterator = channelSettings.iterator();
+            if (Blank.isNotEmpty(branch.getBranches())) {
+                String branchName = branch.getName();
+                Boolean flage = true;
+                while (iterator.hasNext()) {
+                    if (iterator.next().getChannelName().equals(branchName)) {
+                        flage = false;
+                        iterator.remove();
+                    }
+                }
+                if (flage) {
+                    JSONObject jsonObject = new JSONObject();
+                    jsonObject.put("label", branchName);
+                    jsonObject.put("children", recursionNoConfigChildren(branch.getBranches(), channelSettings, branchName));
+                    jsonList.add(jsonObject);
+                }
+            }
+        }
+        return jsonList;
+    }
+
+    public static List<JSONObject> recursionNoConfigChildren(Collection<Branch> branchCollection, List<ChannelSetting> channelSettings, String branchName) {
+        List<JSONObject> jsonList = new ArrayList<>();
+        for (Branch branch : branchCollection) {
+            Iterator<ChannelSetting> iterator = channelSettings.iterator();
+            String newBranchName = branchName + "." + branch.getName();
+            Boolean flage = true;
+            while (iterator.hasNext()) {
+                if (iterator.next().getChannelName().equals(newBranchName)) {
+                    flage = false;
+                    iterator.remove();
+                }
+            }
+            if (flage) {
+                JSONObject jsonObject = new JSONObject();
+                jsonObject.put("label", branch.getName());
+                if (Blank.isNotEmpty(branch.getBranches())) {
+                    jsonObject.put("children", recursionNoConfigChildren(branch.getBranches(), channelSettings, newBranchName));
+                } else {
+                    jsonObject.put("children", generNoConfigLeaf(branch.getLeaves(), channelSettings, newBranchName));
+                }
+                jsonList.add(jsonObject);
+            }
+        }
+        return jsonList;
+    }
+
+    public static List<JSONObject> generNoConfigLeaf(Collection<Leaf> leaves, List<ChannelSetting> channelSettings, String branchName) {
+        List<JSONObject> jsonObjectList = new ArrayList<>();
+        for (Leaf leaf : leaves) {
+            Iterator<ChannelSetting> iterator = channelSettings.iterator();
+            String newBranchName = branchName + "." + leaf.getName();
+            Boolean flage = true;
+            while (iterator.hasNext()) {
+                if (iterator.next().getChannelName().equals(newBranchName)) {
+                    flage = false;
+                }
+            }
+            if (flage) {
+                JSONObject jsonObject = new JSONObject();
+                jsonObject.put("label", leaf.getName());
+                jsonObjectList.add(jsonObject);
+            }
+        }
+        return jsonObjectList;
+    }
+
+
     /**
      * 通过itemid获取响应的值
      *

+ 16 - 0
chuanyi_server/src/main/resources/mapper/ChannelSettingDao.xml

@@ -61,4 +61,20 @@
               order by create_time) tcs
     </select>
 
+    <select id="getChannelListNoConfig" resultType="com.judong.chuanyiserver.entity.ChannelSetting">
+        select id,
+               server_id,
+               channel_name,
+               channel_map_name,
+               data_type,
+               read_mechanism,
+               read_policy,
+               policy_value,
+               configuration_name,
+               create_time
+        from t_channel_setting
+        where server_id = #{serverId}
+          and configuration_name is null
+    </select>
+
 </mapper>