|
@@ -1,14 +1,18 @@
|
|
|
package com.sckj.opc.dataservice;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
import com.baosight.hdsdk.HDConnectionFactory;
|
|
|
import com.baosight.hdsdk.HDDataProvider;
|
|
|
import com.baosight.hdsdk.HDServerFactory;
|
|
|
+import com.baosight.hdsdk.HDTagManager;
|
|
|
+import com.baosight.hdsdk.domain.data.HDBasicTag;
|
|
|
import com.baosight.hdsdk.domain.data.HDDataConnection;
|
|
|
import com.baosight.hdsdk.domain.data.HDDataServer;
|
|
|
import com.baosight.hdsdk.domain.data.HDRecord;
|
|
|
import com.baosight.hdsdk.exception.HDSdkException;
|
|
|
import com.google.common.eventbus.AsyncEventBus;
|
|
|
+import com.sckj.common.exception.OperateException;
|
|
|
import com.sckj.common.manager.ScheduledTaskManager;
|
|
|
import com.sckj.opc.dto.HdTagDTO;
|
|
|
import com.sckj.opc.entity.OPCServer;
|
|
@@ -19,7 +23,6 @@ import com.sckj.opc.service.THdTagServiceImpl;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
-import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.PreDestroy;
|
|
@@ -46,29 +49,80 @@ public class HDServiceImpl {
|
|
|
@Resource
|
|
|
private AsyncEventBus asyncEventBus;
|
|
|
|
|
|
- @Resource(name = "taskExecutor")
|
|
|
- ThreadPoolTaskExecutor taskExecutor;
|
|
|
-
|
|
|
-
|
|
|
//定时器
|
|
|
@Resource
|
|
|
private ScheduledTaskManager scheduledTaskManager;
|
|
|
|
|
|
private ConcurrentHashMap<Long, HDDataConnection> mOPCDaClientMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
+ //存储ihd标记的定时任务
|
|
|
private Set<String> taskNameSet = new HashSet<>();
|
|
|
|
|
|
+ static {
|
|
|
+ boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
|
|
|
+
|
|
|
+ System.setProperty("java.library.path", isWindows ? "D:\\project\\xiha\\iron\\ihd\\win64_3.9.2_hdSDK3\\hdsdk\\executable_x64" : "/home/taphole/hdsdk/linux");
|
|
|
+ try {
|
|
|
+ java.lang.reflect.Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
|
|
|
+ fieldSysPath.setAccessible(true);
|
|
|
+ fieldSysPath.set(null, null);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ final String soDir = System.getProperty("java.library.path");
|
|
|
+
|
|
|
+ System.out.println("hdsdk path >>> java.library.path:" + soDir);
|
|
|
+
|
|
|
+ if (isWindows) {
|
|
|
+ System.loadLibrary("hdOS");
|
|
|
+ System.loadLibrary("hdNetClient");
|
|
|
+ System.loadLibrary("hdDNAPI");
|
|
|
+ System.loadLibrary("hdKingAPI");
|
|
|
+ System.loadLibrary("hdProcComm");
|
|
|
+ System.loadLibrary("hdDataSDK");
|
|
|
+ } else {
|
|
|
+ final String SO_EXT = ".so.1.0.0";
|
|
|
+ System.load(soDir + "/" + "libhdOS" + SO_EXT);
|
|
|
+ System.load(soDir + "/" + "libhdNetClient" + SO_EXT);
|
|
|
+ System.load(soDir + "/" + "libhdKingAPI" + SO_EXT);
|
|
|
+ System.load(soDir + "/" + "libhdProcComm" + SO_EXT);
|
|
|
+ System.load(soDir + "/" + "libhdDNAPI" + SO_EXT);
|
|
|
+ System.load(soDir + "/" + "libhdDataSDK" + SO_EXT);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试连接
|
|
|
+ */
|
|
|
+ public void testConnection(OPCServer opcServer) throws HDSdkException {
|
|
|
+ HDDataServer server = HDServerFactory.getHDDataServer(opcServer.getIp(), opcServer.getPort(), "", opcServer.getPort());
|
|
|
+ HDDataConnection dataConn = null;
|
|
|
+ try {
|
|
|
+ dataConn = HDConnectionFactory.getHDDataConnection(server);
|
|
|
+ dataConn.loginToServer(opcServer.getUsername(), opcServer.getPassword());
|
|
|
+ System.out.println(dataConn);
|
|
|
+ } finally {
|
|
|
+ if (dataConn != null) {
|
|
|
+ log.info("ihdk connection close");
|
|
|
+ dataConn.dispose();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获得基础的连接信息
|
|
|
*/
|
|
|
- public HDDataConnection createServer(OPCServer opcServer) {
|
|
|
+ public HDDataConnection createConnection(OPCServer opcServer) {
|
|
|
return mOPCDaClientMap.computeIfAbsent(opcServer.getId(), id -> {
|
|
|
HDDataServer server = HDServerFactory.getHDDataServer(opcServer.getIp(), opcServer.getPort(), "", opcServer.getPort());
|
|
|
HDDataConnection dataConn = null;
|
|
|
try {
|
|
|
dataConn = HDConnectionFactory.getHDDataConnection(server);
|
|
|
dataConn.loginToServer(opcServer.getUsername(), opcServer.getPassword());
|
|
|
- dataConn.dispose();
|
|
|
} catch (HDSdkException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -77,36 +131,40 @@ public class HDServiceImpl {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public Object createSubscription(OPCServer opcServer, THdTag hdTag) {
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
- if (ObjectUtils.isNotEmpty(hdTag)) {
|
|
|
- scheduledTaskManager.addTask(hdTag.getTagName(), 0, hdTag.getPeriod(), TimeUnit.SECONDS, () -> {
|
|
|
- HDDataConnection connection = createServer(opcServer);
|
|
|
- HDDataProvider dp = new HDDataProvider(connection);
|
|
|
- try {
|
|
|
- HDRecord hdRecord = dp.querySnapshotByTagID(hdTag.getTagId());
|
|
|
- asyncEventBus.post(hdRecord);
|
|
|
- } catch (HDSdkException e) {
|
|
|
- e.printStackTrace();
|
|
|
- log.info("{}订阅异常:{}", hdTag.getTagName(), e.getMessage());
|
|
|
- }
|
|
|
- });
|
|
|
+ private void createSubscription(OPCServer opcServer, THdTag hdTag) {
|
|
|
+ if (ObjectUtils.isEmpty(opcServer) || ObjectUtils.isEmpty(hdTag)) {
|
|
|
+ throw new OperateException("未获取到服务器信息或未获取到标记信号");
|
|
|
}
|
|
|
- return sb;
|
|
|
+ scheduledTaskManager.addTask(hdTag.getTagName(), 0, hdTag.getPeriod(), TimeUnit.SECONDS, () -> {
|
|
|
+ HDDataConnection connection = createConnection(opcServer);
|
|
|
+ HDDataProvider dp = new HDDataProvider(connection);
|
|
|
+ try {
|
|
|
+ HDRecord hdRecord = dp.querySnapshotByTagID(hdTag.getTagId());
|
|
|
+ log.info("{}:{}", hdTag.getTagName(), hdRecord.toString());
|
|
|
+ asyncEventBus.post(hdRecord);
|
|
|
+ } catch (HDSdkException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.info("{}订阅异常:{}", hdTag.getTagName(), e.getMessage());
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- public Object subscribe(THdTag hdTag) {
|
|
|
- try {
|
|
|
- HdTagDTO opcPointDTO = hdTagService.selectInfoWithServer(hdTag);
|
|
|
- QueryWrapper<HdTagDTO> pointQueryWrapper = new QueryWrapper<>();
|
|
|
- pointQueryWrapper.lambda().eq(HdTagDTO::getStatus, "1");
|
|
|
- OPCServer opcServer = new OPCServer();
|
|
|
- BeanUtils.copyProperties(opcPointDTO, opcServer);
|
|
|
- return createSubscription(opcServer, opcPointDTO);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ /***
|
|
|
+ * 订阅指定节点
|
|
|
+ * @param hdTag
|
|
|
+ */
|
|
|
+ public void subscribe(THdTag hdTag) {
|
|
|
+ HdTagDTO opcPointDTO = hdTagService.selectInfoWithServer(hdTag);
|
|
|
+ if (ObjectUtils.isEmpty(opcPointDTO)) {
|
|
|
+ throw new OperateException("未查询到该节点数据");
|
|
|
}
|
|
|
- return null;
|
|
|
+
|
|
|
+ QueryWrapper<HdTagDTO> pointQueryWrapper = new QueryWrapper<>();
|
|
|
+ pointQueryWrapper.lambda().eq(HdTagDTO::getStatus, "1");
|
|
|
+ OPCServer opcServer = new OPCServer();
|
|
|
+ BeanUtils.copyProperties(opcPointDTO, opcServer);
|
|
|
+ opcServer.setId(opcPointDTO.getOpcServerId());
|
|
|
+ createSubscription(opcServer, opcPointDTO);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -137,7 +195,7 @@ public class HDServiceImpl {
|
|
|
serverQueryWrapper.lambda().eq(OPCServer::getStatus, "1");
|
|
|
List<OPCServer> opcServerList = opcServerService.list(serverQueryWrapper);
|
|
|
if (ObjectUtils.isEmpty(opcServerList)) {
|
|
|
- return;
|
|
|
+ throw new OperateException("没有可用的服务器!");
|
|
|
}
|
|
|
for (OPCServer opcServer : opcServerList) {
|
|
|
QueryWrapper<THdTag> pointQueryWrapper = new QueryWrapper<>();
|
|
@@ -156,25 +214,91 @@ public class HDServiceImpl {
|
|
|
|
|
|
/**
|
|
|
* 读取节点数据
|
|
|
- * <p>
|
|
|
- * identifier也可以通过UaExpert客户端去查询,这个值=通道名称.设备名称.标记名称
|
|
|
*
|
|
|
- * @param opcPoint
|
|
|
+ * @param hdTag
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public HDRecord readNodeValue(THdTag opcPoint) throws Exception {
|
|
|
- HdTagDTO opcPointDTO = hdTagService.selectInfoWithServer(opcPoint);
|
|
|
+ public HDRecord readTagValue(THdTag hdTag) throws Exception {
|
|
|
+ HdTagDTO opcPointDTO = hdTagService.selectInfoWithServer(hdTag);
|
|
|
if (ObjectUtils.isEmpty(opcPointDTO)) {
|
|
|
return null;
|
|
|
}
|
|
|
OPCServer opcServer = new OPCServer();
|
|
|
BeanUtils.copyProperties(opcPointDTO, opcServer);
|
|
|
- HDDataConnection connection = createServer(opcServer);
|
|
|
+ opcServer.setId(opcPointDTO.getOpcServerId());
|
|
|
+ HDDataConnection connection = createConnection(opcServer);
|
|
|
HDDataProvider dp = new HDDataProvider(connection);
|
|
|
- HDRecord hdRecord = dp.querySnapshotByTagID(opcPoint.getTagId());
|
|
|
+ HDRecord hdRecord = dp.querySnapshotByTagID(hdTag.getTagId());
|
|
|
return hdRecord;
|
|
|
}
|
|
|
|
|
|
+ /***
|
|
|
+ * 根据tagName刷新TagId
|
|
|
+ */
|
|
|
+ public void refreshTagIds() throws HDSdkException {
|
|
|
+ QueryWrapper<OPCServer> serverQueryWrapper = new QueryWrapper<>();
|
|
|
+ serverQueryWrapper.lambda().eq(OPCServer::getType, "3");
|
|
|
+ List<OPCServer> opcServerList = opcServerService.list(serverQueryWrapper);
|
|
|
+ if (ObjectUtils.isEmpty(opcServerList)) {
|
|
|
+ throw new OperateException("没有可用的服务器!");
|
|
|
+ }
|
|
|
+ for (OPCServer opcServer : opcServerList) {
|
|
|
+ HDDataConnection connection = createConnection(opcServer);
|
|
|
+ if (null == connection) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ QueryWrapper<THdTag> pointQueryWrapper = new QueryWrapper<>();
|
|
|
+ List<THdTag> opcPointList = hdTagService.list(pointQueryWrapper);
|
|
|
+ if (ObjectUtils.isNotEmpty(opcPointList)) {
|
|
|
+ for (THdTag opcPoint : opcPointList) {
|
|
|
+ log.info(opcPoint.getTagName());
|
|
|
+ HDTagManager tagManager = connection.getTagManager();
|
|
|
+ HDBasicTag basicTag = tagManager.getBasicTagByName(opcPoint.getTagName());
|
|
|
+ if (ObjectUtils.isEmpty(basicTag)) {
|
|
|
+ log.info("{}未查询到数据", opcPoint.getTagName());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ UpdateWrapper<THdTag> updateWrapper = new UpdateWrapper<>();
|
|
|
+ updateWrapper.lambda()
|
|
|
+ .set(THdTag::getTagId, basicTag.getId())
|
|
|
+ .set(THdTag::getTagType, basicTag.getTagDataType().name())
|
|
|
+ .eq(THdTag::getId, opcPoint.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (HDSdkException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } finally {
|
|
|
+ connection.dispose();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 根据Tag名称获取普通类型Tag对象
|
|
|
+ */
|
|
|
+ public HDBasicTag getBasicTagByName(THdTag hdTag) throws HDSdkException {
|
|
|
+ HDBasicTag basicTag = null;
|
|
|
+ HdTagDTO opcPointDTO = hdTagService.selectInfoWithServer(hdTag);
|
|
|
+ if (ObjectUtils.isEmpty(opcPointDTO)) {
|
|
|
+ return basicTag;
|
|
|
+ }
|
|
|
+ OPCServer opcServer = new OPCServer();
|
|
|
+ BeanUtils.copyProperties(opcPointDTO, opcServer);
|
|
|
+ opcServer.setId(opcPointDTO.getOpcServerId());
|
|
|
+ HDDataConnection connection = null;
|
|
|
+ try {
|
|
|
+ connection = createConnection(opcServer);
|
|
|
+ HDTagManager tm = connection.getTagManager();
|
|
|
+ basicTag = tm.getBasicTagByName(opcPointDTO.getTagName());
|
|
|
+ } finally {
|
|
|
+ if (connection != null) {
|
|
|
+ connection.dispose();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return basicTag;
|
|
|
+ }
|
|
|
+
|
|
|
@PreDestroy
|
|
|
public void releaseServer() {
|
|
|
if (ObjectUtils.isNotEmpty(mOPCDaClientMap)) {
|
|
@@ -188,4 +312,5 @@ public class HDServiceImpl {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
}
|