|
@@ -2,9 +2,11 @@ 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;
|
|
|
+import com.judong.chuanyiserver.exception.CustomException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
|
|
|
import org.jinterop.dcom.common.JIException;
|
|
@@ -163,25 +165,44 @@ public class KepOpcServerUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static Result opcReadItemTree(ServerInformation serverInformation) {
|
|
|
+ public static List<JSONObject> opcReadItemTree(ServerInformation serverInformation) {
|
|
|
try {
|
|
|
String opcServerDaPoolKey = KepOpcServerUtil.generateOpcPoolKey(serverInformation);
|
|
|
if (KepOpcServerUtil.validationKey(opcServerDaPoolKey)) {
|
|
|
Server server = KepOpcServerUtil.getServer(opcServerDaPoolKey);
|
|
|
if (null == server.getServerState()) {
|
|
|
- return Result.no(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
|
|
|
+ throw new CustomException(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
|
|
|
}
|
|
|
if (OPCSERVERSTATE.OPC_STATUS_RUNNING == server.getServerState().getServerState()) {
|
|
|
- JSONObject jsonObject = new JSONObject();
|
|
|
- jsonObject.put("tree", generOpcTree(server));
|
|
|
+ List<JSONObject> jsonObjectList = generOpcTree(server);
|
|
|
KepOpcServerUtil.returnServer(opcServerDaPoolKey, server);
|
|
|
- return Result.ok(jsonObject);
|
|
|
+ return jsonObjectList;
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- return Result.no(ResultEnum.REQUEST_TIME_OUT.getRespCode(), e.getMessage());
|
|
|
+ throw new CustomException(ResultEnum.REQUEST_TIME_OUT.getRespCode(), e.getMessage());
|
|
|
}
|
|
|
- return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), ResultEnum.SERVER_ERROR.getRespMsg());
|
|
|
+ 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) {
|
|
@@ -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获取响应的值
|
|
|
*
|