|
@@ -3,6 +3,7 @@ package com.judong.chuanyiserver.util;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.judong.chuanyiserver.config.OpcServerDaPoolFactory;
|
|
|
import com.judong.chuanyiserver.entity.ServerInformation;
|
|
|
+import com.judong.chuanyiserver.enums.ConnectModeEnum;
|
|
|
import com.judong.chuanyiserver.enums.ResultEnum;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
|
|
@@ -23,10 +24,10 @@ import java.util.*;
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
/**
|
|
|
- * OPCDa协议工具类
|
|
|
+ * KepOpcServer协议工具类
|
|
|
*/
|
|
|
@Slf4j
|
|
|
-public class OpcServerDaUtil {
|
|
|
+public class KepOpcServerUtil {
|
|
|
|
|
|
/**
|
|
|
* 通过ip,user,password生成对应的连接池键
|
|
@@ -34,14 +35,16 @@ public class OpcServerDaUtil {
|
|
|
* @param ip
|
|
|
* @param user
|
|
|
* @param password
|
|
|
+ * @param clsid
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String generateOpcPoolKey(String ip, String user, String password) {
|
|
|
+ public static String generateOpcPoolKey(String ip, String user, String password, String clsid) {
|
|
|
StringBuffer buffer = new StringBuffer("");
|
|
|
buffer.append("{\"ip\":\"");
|
|
|
buffer.append(ip + "\",\"user\":\"");
|
|
|
buffer.append(user + "\",\"password\":\"");
|
|
|
- buffer.append(password + "\"}");
|
|
|
+ buffer.append(password + "\",\"clsid\":\"");
|
|
|
+ buffer.append(clsid + "\"}");
|
|
|
return buffer.toString();
|
|
|
}
|
|
|
|
|
@@ -55,11 +58,12 @@ public class OpcServerDaUtil {
|
|
|
if (Blank.isEmpty(key)) {
|
|
|
return false;
|
|
|
}
|
|
|
- JSONObject jsonObject = (JSONObject) JSONObject.parse(key);
|
|
|
- String ip = (String) jsonObject.get("ip");
|
|
|
- String user = (String) jsonObject.get("user");
|
|
|
- String password = (String) jsonObject.get("password");
|
|
|
- if (Blank.isEmpty(ip) || Blank.isEmpty(user) || Blank.isEmpty(password)) {
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(key);
|
|
|
+ String ip = jsonObject.getString("ip");
|
|
|
+ String user = jsonObject.getString("user");
|
|
|
+ String password = jsonObject.getString("password");
|
|
|
+ String clsid = jsonObject.getString("clsid");
|
|
|
+ if (Blank.isEmpty(ip, user, password, clsid)) {
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
@@ -95,7 +99,7 @@ public class OpcServerDaUtil {
|
|
|
return OpcServerDaPoolFactory.getPool();
|
|
|
}
|
|
|
|
|
|
- public static Result opcDaTestConnect(ServerInformation serverInformation) throws AlreadyConnectedException, JIException, UnknownHostException {
|
|
|
+ public static Result opcTestConnect(ServerInformation serverInformation) throws AlreadyConnectedException, JIException, UnknownHostException {
|
|
|
// 连接信息
|
|
|
ConnectionInformation ci = new ConnectionInformation();
|
|
|
//服务
|
|
@@ -105,7 +109,7 @@ public class OpcServerDaUtil {
|
|
|
ci.setDomain(""); // 域,为空就行
|
|
|
ci.setUser(serverInformation.getIpUserName()); // 电脑上自己建好的用户名
|
|
|
ci.setPassword(serverInformation.getIpPassword()); // 用户名的密码
|
|
|
- ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); // KEPServer的注册表ID,可以在“组件服务”里看到
|
|
|
+ ci.setClsid(ConnectModeEnum.KEPOPCSERVER.getName()); // KEPServer的注册表ID,可以在“组件服务”里看到
|
|
|
// ci.setProgId("");
|
|
|
|
|
|
// 连接到服务
|
|
@@ -133,17 +137,17 @@ public class OpcServerDaUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static Result opcDaReadItemTree(ServerInformation serverInformation) throws Exception {
|
|
|
- String opcServerDaPoolKey = OpcServerDaUtil.generateOpcPoolKey(serverInformation.getIpAddress(), serverInformation.getIpUserName(), RSAUtil.decrypt(serverInformation.getIpPassword(), "utf-8"));
|
|
|
- if (OpcServerDaUtil.validationKey(opcServerDaPoolKey)) {
|
|
|
- Server server = OpcServerDaUtil.getServer(opcServerDaPoolKey);
|
|
|
+ public static Result opcReadItemTree(ServerInformation serverInformation) throws Exception {
|
|
|
+ String opcServerDaPoolKey = KepOpcServerUtil.generateOpcPoolKey(serverInformation.getIpAddress(), serverInformation.getIpUserName(), RSAUtil.decrypt(serverInformation.getIpPassword(), "utf-8"), ConnectModeEnum.KEPOPCSERVER.getName());
|
|
|
+ if (KepOpcServerUtil.validationKey(opcServerDaPoolKey)) {
|
|
|
+ Server server = KepOpcServerUtil.getServer(opcServerDaPoolKey);
|
|
|
if (null == server.getServerState()) {
|
|
|
return Result.no(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
|
|
|
}
|
|
|
if (OPCSERVERSTATE.OPC_STATUS_RUNNING == server.getServerState().getServerState()) {
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
- jsonObject.put("tree", OpcServerDaUtil.generOpcDaTree(server));
|
|
|
- OpcServerDaUtil.returnServer(opcServerDaPoolKey, server);
|
|
|
+ jsonObject.put("tree", KepOpcServerUtil.generOpcTree(server));
|
|
|
+ KepOpcServerUtil.returnServer(opcServerDaPoolKey, server);
|
|
|
return Result.ok(jsonObject);
|
|
|
}
|
|
|
}
|
|
@@ -158,10 +162,10 @@ public class OpcServerDaUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static Result opcDaReadItemValue(ServerInformation serverInformation, String itemName) throws Exception {
|
|
|
- String opcServerDaPoolKey = OpcServerDaUtil.generateOpcPoolKey(serverInformation.getIpAddress(), serverInformation.getIpUserName(), RSAUtil.decrypt(serverInformation.getIpPassword(), "utf-8"));
|
|
|
- if (OpcServerDaUtil.validationKey(opcServerDaPoolKey)) {
|
|
|
- Server server = OpcServerDaUtil.getServer(opcServerDaPoolKey);
|
|
|
+ public static Result opcReadItemValue(ServerInformation serverInformation, String itemName) throws Exception {
|
|
|
+ String opcServerDaPoolKey = KepOpcServerUtil.generateOpcPoolKey(serverInformation.getIpAddress(), serverInformation.getIpUserName(), RSAUtil.decrypt(serverInformation.getIpPassword(), "utf-8"), ConnectModeEnum.KEPOPCSERVER.getName());
|
|
|
+ if (KepOpcServerUtil.validationKey(opcServerDaPoolKey)) {
|
|
|
+ Server server = KepOpcServerUtil.getServer(opcServerDaPoolKey);
|
|
|
if (null == server.getServerState()) {
|
|
|
return Result.no(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
|
|
|
}
|
|
@@ -172,7 +176,7 @@ public class OpcServerDaUtil {
|
|
|
Map<String, Object> value = getVal(item.read(true).getValue());
|
|
|
jsonObject.put("javaType", value.get("javaType"));
|
|
|
jsonObject.put("value", value);
|
|
|
- OpcServerDaUtil.returnServer(opcServerDaPoolKey, server);
|
|
|
+ KepOpcServerUtil.returnServer(opcServerDaPoolKey, server);
|
|
|
return Result.ok(jsonObject);
|
|
|
}
|
|
|
}
|
|
@@ -184,7 +188,7 @@ public class OpcServerDaUtil {
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
- public static List<JSONObject> generOpcDaTree(Server server) throws JIException, UnknownHostException {
|
|
|
+ public static List<JSONObject> generOpcTree(Server server) throws JIException, UnknownHostException {
|
|
|
List<JSONObject> jsonList = new ArrayList<>();
|
|
|
//获取服务器下所有ITEM列表,树形展示
|
|
|
TreeBrowser treeBrowser = server.getTreeBrowser();
|