|
@@ -1,42 +1,223 @@
|
|
package com.judong.chuanyiserver.service.impl;
|
|
package com.judong.chuanyiserver.service.impl;
|
|
|
|
|
|
import cn.hutool.http.HttpRequest;
|
|
import cn.hutool.http.HttpRequest;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
import com.judong.chuanyiserver.dao.ConnectDao;
|
|
import com.judong.chuanyiserver.dao.ConnectDao;
|
|
import com.judong.chuanyiserver.entity.ServerInformation;
|
|
import com.judong.chuanyiserver.entity.ServerInformation;
|
|
import com.judong.chuanyiserver.enums.ResultEnum;
|
|
import com.judong.chuanyiserver.enums.ResultEnum;
|
|
import com.judong.chuanyiserver.service.ConnectService;
|
|
import com.judong.chuanyiserver.service.ConnectService;
|
|
-import com.judong.chuanyiserver.util.Blank;
|
|
|
|
-import com.judong.chuanyiserver.util.RedisUtil;
|
|
|
|
-import com.judong.chuanyiserver.util.Result;
|
|
|
|
-import com.judong.chuanyiserver.util.UserUtil;
|
|
|
|
|
|
+import com.judong.chuanyiserver.util.*;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.jinterop.dcom.common.JIException;
|
|
|
|
+import org.openscada.opc.dcom.da.OPCSERVERSTATE;
|
|
|
|
+import org.openscada.opc.lib.common.AlreadyConnectedException;
|
|
|
|
+import org.openscada.opc.lib.common.ConnectionInformation;
|
|
|
|
+import org.openscada.opc.lib.common.NotConnectedException;
|
|
|
|
+import org.openscada.opc.lib.da.*;
|
|
|
|
+import org.openscada.opc.lib.da.browser.Branch;
|
|
|
|
+import org.openscada.opc.lib.da.browser.Leaf;
|
|
|
|
+import org.openscada.opc.lib.da.browser.TreeBrowser;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import java.net.UnknownHostException;
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.concurrent.Executors;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
@Transactional
|
|
@Transactional
|
|
|
|
+@Slf4j
|
|
public class ConnectServiceImpl implements ConnectService {
|
|
public class ConnectServiceImpl implements ConnectService {
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private ConnectDao connectDao;
|
|
private ConnectDao connectDao;
|
|
@Autowired
|
|
@Autowired
|
|
- private RedisUtil redisUtil;
|
|
|
|
|
|
+ private UserUtil userUtil;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public Result saveConnect(HttpServletRequest request, ServerInformation serverInformation) {
|
|
|
|
|
|
+ public Result testConnect(ServerInformation serverInformation) throws AlreadyConnectedException, JIException, UnknownHostException {
|
|
|
|
+ // 连接信息
|
|
|
|
+ ConnectionInformation ci = new ConnectionInformation();
|
|
|
|
+ //服务
|
|
|
|
+ Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
|
|
|
|
+ try {
|
|
|
|
+ ci.setHost(serverInformation.getIpAddress()); // 安装opc电脑IP
|
|
|
|
+ ci.setDomain(""); // 域,为空就行
|
|
|
|
+ ci.setUser(serverInformation.getIpUserName()); // 电脑上自己建好的用户名
|
|
|
|
+ ci.setPassword(serverInformation.getIpPassword()); // 用户名的密码
|
|
|
|
+ ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); // KEPServer的注册表ID,可以在“组件服务”里看到
|
|
|
|
+ // ci.setProgId("");
|
|
|
|
+
|
|
|
|
+ // 连接到服务
|
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
|
+ server.connect();
|
|
|
|
+ long finish = System.currentTimeMillis();
|
|
|
|
+ log.info("连接耗费时间为:" + (finish - start) + "毫秒");
|
|
|
|
+ if (null == server.getServerState()) {
|
|
|
|
+ return Result.no(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
|
|
|
|
+ }
|
|
|
|
+ if (OPCSERVERSTATE.OPC_STATUS_RUNNING == server.getServerState().getServerState()) {
|
|
|
|
+ return Result.ok(true);
|
|
|
|
+ }
|
|
|
|
+ } finally {
|
|
|
|
+ //关闭连接使用
|
|
|
|
+ server.dispose();
|
|
|
|
+ }
|
|
|
|
+ return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), ResultEnum.SERVER_ERROR.getRespMsg());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public synchronized Result saveConnect(HttpServletRequest request, ServerInformation serverInformation) {
|
|
String token = request.getHeader("token");
|
|
String token = request.getHeader("token");
|
|
if (Blank.isEmpty(token)) {
|
|
if (Blank.isEmpty(token)) {
|
|
return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
|
|
return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
|
|
}
|
|
}
|
|
-// System.out.println("值为:"+redisUtil.get(token));
|
|
|
|
-// return Result.no(200,"1");
|
|
|
|
- serverInformation.setUserId(UserUtil.getCurrentUser(token).getUserId());
|
|
|
|
- ServerInformation si=connectDao.getServerInformation(UserUtil.getCurrentUser(token).getUserId(),serverInformation.getIpAddress());
|
|
|
|
- if(Blank.isNotEmpty(si)){
|
|
|
|
|
|
+ String userId = userUtil.getCurrentUserId(token);
|
|
|
|
+ if (Blank.isEmpty(userId)){
|
|
|
|
+ return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
|
|
|
|
+ }
|
|
|
|
+ serverInformation.setUserId(userId);
|
|
|
|
+ ServerInformation si = connectDao.getServerInformation(userId, serverInformation.getIpAddress());
|
|
|
|
+ if (Blank.isNotEmpty(si)) {
|
|
|
|
+ return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "该用户下此ip已被使保存,请更换其他ip");
|
|
|
|
+ }
|
|
|
|
+ if (connectDao.saveConnect(serverInformation) <= 0) {
|
|
|
|
+ return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "保存连接失败");
|
|
|
|
+ }
|
|
|
|
+ return Result.ok("保存连接成功");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Result selectAllConnect(HttpServletRequest request) {
|
|
|
|
+ String token = request.getHeader("token");
|
|
|
|
+ if (Blank.isEmpty(token)) {
|
|
|
|
+ return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
|
|
|
|
+ }
|
|
|
|
+ String userId=userUtil.getCurrentUserId(token);
|
|
|
|
+ if(Blank.isEmpty(userId)){
|
|
|
|
+ return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
|
|
|
|
+ }
|
|
|
|
+ return Result.ok(connectDao.selectAllConnect(userId));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public synchronized Result editConnect(HttpServletRequest request, ServerInformation serverInformation) {
|
|
|
|
+ String token = request.getHeader("token");
|
|
|
|
+ if (Blank.isEmpty(token)) {
|
|
|
|
+ return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
|
|
|
|
+ }
|
|
|
|
+ String userId = userUtil.getCurrentUserId(token);
|
|
|
|
+ if (Blank.isEmpty(userId)){
|
|
|
|
+ return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
|
|
|
|
+ }
|
|
|
|
+ ServerInformation si = connectDao.getServerInformationEdit(serverInformation.getId(), userId, serverInformation.getIpAddress());
|
|
|
|
+ if (Blank.isNotEmpty(si)) {
|
|
return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "该用户下此ip已被使保存,请更换其他ip");
|
|
return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "该用户下此ip已被使保存,请更换其他ip");
|
|
}
|
|
}
|
|
- return Result.ok(connectDao.saveConnect(serverInformation));
|
|
|
|
|
|
+ if (connectDao.editConnect(serverInformation) <= 0) {
|
|
|
|
+ return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "编辑连接失败");
|
|
|
|
+ }
|
|
|
|
+ return Result.ok("编辑连接成功");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Result deleteConnect(HttpServletRequest request, int id) {
|
|
|
|
+ String token = request.getHeader("token");
|
|
|
|
+ if (Blank.isEmpty(token)) {
|
|
|
|
+ return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
|
|
|
|
+ }
|
|
|
|
+ String userId=userUtil.getCurrentUserId(token);
|
|
|
|
+ if (Blank.isEmpty(userId)){
|
|
|
|
+ return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
|
|
|
|
+ }
|
|
|
|
+ if (connectDao.deleteConnect(id, userId) <= 0) {
|
|
|
|
+ return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "删除连接失败");
|
|
|
|
+ }
|
|
|
|
+ return Result.ok("删除连接成功");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Result openConnect(int serverInformationId) throws AlreadyConnectedException, JIException, UnknownHostException, NotConnectedException, DuplicateGroupException, AddFailedException {
|
|
|
|
+ ServerInformation serverInformation = connectDao.getServerInformationById(serverInformationId);
|
|
|
|
+ //如果协议类型为OPCDA
|
|
|
|
+ if (serverInformation.getAgreementType() == ConstantStr.OPC_DA) {
|
|
|
|
+ ConnectionInformation ci = new ConnectionInformation();
|
|
|
|
+
|
|
|
|
+ ci.setHost(serverInformation.getIpAddress()); // 安装opc电脑IP
|
|
|
|
+ ci.setDomain(""); // 域,为空就行
|
|
|
|
+ ci.setUser(serverInformation.getIpUserName()); // 电脑上自己建好的用户名
|
|
|
|
+ ci.setPassword(serverInformation.getIpPassword()); // 用户名的密码
|
|
|
|
+ ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); // KEPServer的注册表ID,可以在“组件服务”里看到
|
|
|
|
+ // ci.setProgId("");
|
|
|
|
+
|
|
|
|
+ Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
|
|
|
|
+ server.connect();
|
|
|
|
+ Item item = null;
|
|
|
|
+ if (null == server.getServerState()) {
|
|
|
|
+ return Result.no(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
|
|
|
|
+ }
|
|
|
|
+ if (OPCSERVERSTATE.OPC_STATUS_RUNNING == server.getServerState().getServerState()) {
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
+ //获取服务器下所有ITEM列表,平面展示
|
|
|
|
+// Collection<String> items = server.getFlatBrowser().browse();
|
|
|
|
+
|
|
|
|
+ //获取服务器下所有ITEM列表,树形展示
|
|
|
|
+// TreeBrowser treeBrowser = server.getTreeBrowser();
|
|
|
|
+// Branch browse = treeBrowser.browse();
|
|
|
|
+// Collection<Branch> branches = browse.getBranches();
|
|
|
|
+// for (Branch branch : branches) {
|
|
|
|
+// Collection<Leaf> leaves = branch.getLeaves();
|
|
|
|
+// }
|
|
|
|
+ List<JSONObject> jsonObjectList = OpcDataUtil.generOpcDaTree(server);
|
|
|
|
+
|
|
|
|
+// Group group = server.addGroup();
|
|
|
|
+// Iterator<String> iterator = items.iterator();
|
|
|
|
+// while (iterator.hasNext()) {
|
|
|
|
+// item = group.addItem(iterator.next());
|
|
|
|
+// log.info("读取出来的值为:" + OpcDataUtil.getVal(item.read(true).getValue()));
|
|
|
|
+// }
|
|
|
|
+ jsonObject.put("tree", OpcDataUtil.generOpcDaTree(server));
|
|
|
|
+ return Result.ok(jsonObject);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), ResultEnum.SERVER_ERROR.getRespMsg());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Result readItemValue(int id, String itemName) throws AlreadyConnectedException, JIException, UnknownHostException, NotConnectedException, DuplicateGroupException, AddFailedException {
|
|
|
|
+ ServerInformation serverInformation = connectDao.getServerInformationById(id);
|
|
|
|
+ //如果协议类型为OPCDA
|
|
|
|
+ if (serverInformation.getAgreementType() == ConstantStr.OPC_DA) {
|
|
|
|
+ ConnectionInformation ci = new ConnectionInformation();
|
|
|
|
+
|
|
|
|
+ ci.setHost(serverInformation.getIpAddress()); // 安装opc电脑IP
|
|
|
|
+ ci.setDomain(""); // 域,为空就行
|
|
|
|
+ ci.setUser(serverInformation.getIpUserName()); // 电脑上自己建好的用户名
|
|
|
|
+ ci.setPassword(serverInformation.getIpPassword()); // 用户名的密码
|
|
|
|
+ ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); // KEPServer的注册表ID,可以在“组件服务”里看到
|
|
|
|
+ // ci.setProgId("");
|
|
|
|
+
|
|
|
|
+ Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
|
|
|
|
+ server.connect();
|
|
|
|
+ if (null == server.getServerState()) {
|
|
|
|
+ return Result.no(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
|
|
|
|
+ }
|
|
|
|
+ if (OPCSERVERSTATE.OPC_STATUS_RUNNING == server.getServerState().getServerState()) {
|
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
|
+ //获取服务器下所有ITEM列表,平面展示
|
|
|
|
+ Group group = server.addGroup();
|
|
|
|
+ Item item = group.addItem(itemName);
|
|
|
|
+ Map<String, Object> val = OpcDataUtil.getVal(item.read(true).getValue());
|
|
|
|
+ log.info("读取出来的Java类型为:" + val.get("javaType"));
|
|
|
|
+ log.info("读取出来的值为:" + val);
|
|
|
|
+ map.put("javaType", val.get("javaType"));
|
|
|
|
+ map.put("value", val);
|
|
|
|
+ return Result.ok(map);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return Result.no(ResultEnum.NOT_FOUND.getRespCode(), ResultEnum.NOT_FOUND.getRespMsg());
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|