|
@@ -1,7 +1,6 @@
|
|
|
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;
|
|
@@ -15,6 +14,7 @@ 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.OPCData;
|
|
|
import com.sckj.opc.entity.OPCServer;
|
|
|
import com.sckj.opc.entity.THdTag;
|
|
|
import com.sckj.opc.service.OPCDataServiceImpl;
|
|
@@ -24,9 +24,12 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.PreDestroy;
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Date;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
@@ -99,16 +102,25 @@ public class HDServiceImpl {
|
|
|
* 测试连接
|
|
|
*/
|
|
|
public void testConnection(OPCServer opcServer) throws HDSdkException {
|
|
|
+ if (ObjectUtils.isEmpty(opcServer)
|
|
|
+ || ObjectUtils.isEmpty(opcServer.getIp())
|
|
|
+ || ObjectUtils.isEmpty(opcServer.getPort())
|
|
|
+ || ObjectUtils.isEmpty(opcServer.getUsername())
|
|
|
+ || ObjectUtils.isEmpty(opcServer.getPassword())
|
|
|
+ ) {
|
|
|
+ throw new OperateException(String.format("HDSDK服务器信息:【ip:%s,port:%s,username:%s,password:%s】", opcServer.getIp(), opcServer.getPort(), opcServer.getUsername(), opcServer.getPassword()));
|
|
|
+ }
|
|
|
+ log.info("hdsdk【ip:{},port:{},username:{},password:{}】", opcServer.getIp(), opcServer.getPort(), opcServer.getUsername(), opcServer.getPassword());
|
|
|
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);
|
|
|
+ log.info("hdsdk connection info:{}", dataConn.toString());
|
|
|
} finally {
|
|
|
if (dataConn != null) {
|
|
|
- log.info("ihdk connection close");
|
|
|
dataConn.dispose();
|
|
|
+ log.info("hdsdk connection close");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -116,35 +128,69 @@ public class HDServiceImpl {
|
|
|
/**
|
|
|
* 获得基础的连接信息
|
|
|
*/
|
|
|
- public HDDataConnection createConnection(OPCServer opcServer) {
|
|
|
+ private synchronized HDDataConnection createConnection(OPCServer opcServer) {
|
|
|
+ if (ObjectUtils.isEmpty(opcServer)
|
|
|
+ || ObjectUtils.isEmpty(opcServer.getIp())
|
|
|
+ || ObjectUtils.isEmpty(opcServer.getPort())
|
|
|
+ || ObjectUtils.isEmpty(opcServer.getUsername())
|
|
|
+ || ObjectUtils.isEmpty(opcServer.getPassword())
|
|
|
+ ) {
|
|
|
+ throw new OperateException(String.format("HDSDK服务器信息:【ip:%s,port:%s,username:%s,password:%s】", opcServer.getIp(), opcServer.getPort(), opcServer.getUsername(), opcServer.getPassword()));
|
|
|
+ }
|
|
|
return mOPCDaClientMap.computeIfAbsent(opcServer.getId(), id -> {
|
|
|
+ log.info("hdsdk【ip:{},port:{},username:{},password:{}】", opcServer.getIp(), opcServer.getPort(), opcServer.getUsername(), opcServer.getPassword());
|
|
|
HDDataServer server = HDServerFactory.getHDDataServer(opcServer.getIp(), opcServer.getPort(), "", opcServer.getPort());
|
|
|
HDDataConnection dataConn = null;
|
|
|
try {
|
|
|
+ log.info("hdsdk start connection:{}", LocalDateTime.now());
|
|
|
dataConn = HDConnectionFactory.getHDDataConnection(server);
|
|
|
+ log.info("hdsdk init success:{}", LocalDateTime.now());
|
|
|
dataConn.loginToServer(opcServer.getUsername(), opcServer.getPassword());
|
|
|
- } catch (HDSdkException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.info("hdsdk connected success:{}", LocalDateTime.now());
|
|
|
+ return dataConn;
|
|
|
+ } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+ if (dataConn != null) {
|
|
|
+ try {
|
|
|
+ dataConn.dispose();
|
|
|
+ log.info("ihsdk connection close");
|
|
|
+ } catch (HDSdkException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new OperateException("ihdsdk connection failed : " + e.getMessage());
|
|
|
}
|
|
|
- return dataConn;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
|
|
|
private void createSubscription(OPCServer opcServer, THdTag hdTag) {
|
|
|
if (ObjectUtils.isEmpty(opcServer) || ObjectUtils.isEmpty(hdTag)) {
|
|
|
- throw new OperateException("未获取到服务器信息或未获取到标记信号");
|
|
|
+ throw new OperateException("未获取到服务器或标记信息");
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isEmpty(hdTag.getTagId()) || ObjectUtils.isEmpty(hdTag.getTagName()) || ObjectUtils.isEmpty(hdTag.getPeriod())) {
|
|
|
+ log.warn("subscription point => tagId:{},tagName{},period{}",hdTag.getTagId(),hdTag.getTagName(),hdTag.getPeriod());
|
|
|
+ throw new OperateException("tagId 、tagName、period不能为空");
|
|
|
}
|
|
|
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());
|
|
|
+ final HDDataConnection connection = createConnection(opcServer);
|
|
|
+ HDDataProvider dp = new HDDataProvider(connection);
|
|
|
+ HDRecord record = dp.querySnapshotByTagID(hdTag.getTagId());
|
|
|
+
|
|
|
+ OPCData build = OPCData.builder()
|
|
|
+ .data(record.getValueStr())
|
|
|
+ .pointName(hdTag.getTagName())
|
|
|
+ .belongTagID(record.getBelongTagID())
|
|
|
+ .sourceTime(new Date(record.getSecond() * 1000))
|
|
|
+ .serverTime(new Date(record.getSecond() * 1000))
|
|
|
+ .serverType(opcServer.getType())
|
|
|
+ .build();
|
|
|
+ asyncEventBus.post(build);
|
|
|
+ log.info("{}({})获取数据成功,数据:{}", hdTag.getTagName(),hdTag.getTagDesc(), record);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // e.printStackTrace();
|
|
|
+ log.info("{}({})获取数据异常:{}", hdTag.getTagName(),hdTag.getTagDesc(), e.getMessage());
|
|
|
}
|
|
|
});
|
|
|
}
|
|
@@ -154,17 +200,16 @@ public class HDServiceImpl {
|
|
|
* @param hdTag
|
|
|
*/
|
|
|
public void subscribe(THdTag hdTag) {
|
|
|
- HdTagDTO opcPointDTO = hdTagService.selectInfoWithServer(hdTag);
|
|
|
- if (ObjectUtils.isEmpty(opcPointDTO)) {
|
|
|
- throw new OperateException("未查询到该节点数据");
|
|
|
+ HdTagDTO hdTagDTO = hdTagService.selectInfoWithServer(hdTag);
|
|
|
+ if (ObjectUtils.isEmpty(hdTagDTO)) {
|
|
|
+ throw new OperateException(hdTag.getTagName() + "未查询到信息");
|
|
|
}
|
|
|
-
|
|
|
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);
|
|
|
+ BeanUtils.copyProperties(hdTagDTO, opcServer);
|
|
|
+ opcServer.setId(hdTagDTO.getOpcServerId());
|
|
|
+ createSubscription(opcServer, hdTagDTO);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -195,7 +240,7 @@ public class HDServiceImpl {
|
|
|
serverQueryWrapper.lambda().eq(OPCServer::getStatus, "1");
|
|
|
List<OPCServer> opcServerList = opcServerService.list(serverQueryWrapper);
|
|
|
if (ObjectUtils.isEmpty(opcServerList)) {
|
|
|
- throw new OperateException("没有可用的服务器!");
|
|
|
+ throw new OperateException("没有可用的服务器,请确认服务器是否被禁用!");
|
|
|
}
|
|
|
for (OPCServer opcServer : opcServerList) {
|
|
|
QueryWrapper<THdTag> pointQueryWrapper = new QueryWrapper<>();
|
|
@@ -204,7 +249,7 @@ public class HDServiceImpl {
|
|
|
if (ObjectUtils.isNotEmpty(opcPointList)) {
|
|
|
log.info("start point list:");
|
|
|
for (THdTag opcPoint : opcPointList) {
|
|
|
- log.info(opcPoint.getTagName());
|
|
|
+ // log.info(opcPoint.getTagName());
|
|
|
createSubscription(opcServer, opcPoint);
|
|
|
}
|
|
|
}
|
|
@@ -217,71 +262,157 @@ public class HDServiceImpl {
|
|
|
*
|
|
|
* @param hdTag
|
|
|
* @throws Exception
|
|
|
+ *
|
|
|
+ *{
|
|
|
+ * "code": 200,
|
|
|
+ * "msg": "成功",
|
|
|
+ * "data": {
|
|
|
+ * "second": 1748263901,
|
|
|
+ * "microSecond": 32,
|
|
|
+ * "quality": 192,
|
|
|
+ * "dataType": "FLOAT32",
|
|
|
+ * "valueStr": "0.0",
|
|
|
+ * "belongTagID": 180772,
|
|
|
+ * "timeStampStr": "2025-05-26 20:51:41:032"
|
|
|
+ * }
|
|
|
+ * }
|
|
|
+ *
|
|
|
*/
|
|
|
public HDRecord readTagValue(THdTag hdTag) throws Exception {
|
|
|
HdTagDTO opcPointDTO = hdTagService.selectInfoWithServer(hdTag);
|
|
|
if (ObjectUtils.isEmpty(opcPointDTO)) {
|
|
|
- return null;
|
|
|
+ throw new OperateException(hdTag.getTagName() + "未查询到信息");
|
|
|
}
|
|
|
OPCServer opcServer = new OPCServer();
|
|
|
BeanUtils.copyProperties(opcPointDTO, opcServer);
|
|
|
opcServer.setId(opcPointDTO.getOpcServerId());
|
|
|
- HDDataConnection connection = createConnection(opcServer);
|
|
|
- HDDataProvider dp = new HDDataProvider(connection);
|
|
|
- HDRecord hdRecord = dp.querySnapshotByTagID(hdTag.getTagId());
|
|
|
+ HDRecord hdRecord = null;
|
|
|
+ try {
|
|
|
+ HDDataConnection connection = createConnection(opcServer);
|
|
|
+ HDDataProvider dp = new HDDataProvider(connection);
|
|
|
+ hdRecord = dp.querySnapshotByTagID(opcPointDTO.getTagId());
|
|
|
+ } catch (HDSdkException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
return hdRecord;
|
|
|
}
|
|
|
|
|
|
/***
|
|
|
* 根据tagName刷新TagId
|
|
|
*/
|
|
|
+ @Transactional(rollbackFor = {Exception.class})
|
|
|
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("没有可用的服务器!");
|
|
|
+ throw new OperateException("没有可用的服务器,请确认服务器是否被禁用!");
|
|
|
}
|
|
|
+ StringBuilder sb = new StringBuilder("订阅信息:\n");
|
|
|
for (OPCServer opcServer : opcServerList) {
|
|
|
- HDDataConnection connection = createConnection(opcServer);
|
|
|
+ HDDataConnection connection = null;
|
|
|
+ try {
|
|
|
+ connection = createConnection(opcServer);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
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());
|
|
|
+ sb.append("服务器地址:"+opcServer.getIp()+"\n");
|
|
|
+// try {
|
|
|
+ QueryWrapper<THdTag> pointQueryWrapper = new QueryWrapper<>();
|
|
|
+ pointQueryWrapper.lambda().eq(THdTag::getStatus, "1");
|
|
|
+ List<THdTag> opcPointList = hdTagService.list(pointQueryWrapper);
|
|
|
+ if (ObjectUtils.isNotEmpty(opcPointList)) {
|
|
|
+ for (THdTag opcPoint : opcPointList) {
|
|
|
+// log.info(opcPoint.getTagName());
|
|
|
+ HDBasicTag basicTag = null;
|
|
|
+ try {
|
|
|
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());
|
|
|
+ basicTag = tagManager.getBasicTagByName(opcPoint.getTagName());
|
|
|
+ } catch (HDSdkException e) {
|
|
|
+ log.error("{},{}", opcPoint.getTagName(), e.getMessage());
|
|
|
}
|
|
|
+ if (ObjectUtils.isEmpty(basicTag)) {
|
|
|
+// log.info("{}未查询到信息", opcPoint.getTagName());
|
|
|
+ sb.append("编号:"+opcPoint.getId()+",tageId:"+basicTag.getId()+",tageName:"+opcPoint.getTagName()+",tageType:"+basicTag.getTagDataType().name()+"\n");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+// log.info("{}查询到信息:TagId:{},TagType:{},Id:{}", opcPoint.getTagName(), basicTag.getId(), basicTag.getTagDataType().name(), opcPoint.getId());
|
|
|
+ THdTag newTHdTag = new THdTag();
|
|
|
+ newTHdTag.setId(opcPoint.getId());
|
|
|
+ newTHdTag.setTagId(basicTag.getId());
|
|
|
+ newTHdTag.setTagType(basicTag.getTagDataType().name());
|
|
|
+ hdTagService.updateById(newTHdTag);
|
|
|
}
|
|
|
- } catch (HDSdkException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- } finally {
|
|
|
- connection.dispose();
|
|
|
}
|
|
|
+ log.info("刷新信息失败:{}",sb.toString());
|
|
|
+// } catch (HDSdkException e) {
|
|
|
+// throw new RuntimeException(e);
|
|
|
+// }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/***
|
|
|
* 根据Tag名称获取普通类型Tag对象
|
|
|
+ * {
|
|
|
+ * "code": 200,
|
|
|
+ * "msg": "成功",
|
|
|
+ * "data": {
|
|
|
+ * "name": "BF4_1_IRONNOTCH_TAPPING",
|
|
|
+ * "desc": "四高炉1号铁口出铁中",
|
|
|
+ * "id": 180772,
|
|
|
+ * "hdServer": {
|
|
|
+ * "serverIp": "10.3.231.3",
|
|
|
+ * "serverPort": 5673,
|
|
|
+ * "serverBakIp": "",
|
|
|
+ * "serverBakPort": 5673
|
|
|
+ * },
|
|
|
+ * "tagDataType": "FLOAT32",
|
|
|
+ * "tagClass": "BASIC",
|
|
|
+ * "archiving": true,
|
|
|
+ * "scan": true,
|
|
|
+ * "changeDate": 1740730555,
|
|
|
+ * "changer": "admin",
|
|
|
+ * "creationDate": 1740730555,
|
|
|
+ * "creator": "admin",
|
|
|
+ * "excMaxTime": 300,
|
|
|
+ * "excDev": 0,
|
|
|
+ * "compDev": 0,
|
|
|
+ * "compMaxTime": 0,
|
|
|
+ * "compType": 0,
|
|
|
+ * "recAccess": 53,
|
|
|
+ * "recOperateGroup": "admin",
|
|
|
+ * "recOwnerGroup": "admin",
|
|
|
+ * "tagAccess": 53,
|
|
|
+ * "tagOperateGroup": "admin",
|
|
|
+ * "tagOwnerGroup": "admin",
|
|
|
+ * "span": 100,
|
|
|
+ * "minValue": 0,
|
|
|
+ * "digitalSet": "DEFAULT",
|
|
|
+ * "engUnits": "",
|
|
|
+ * "instrumentPoint": "AOD25606.PV",
|
|
|
+ * "instrumentPointType": 0,
|
|
|
+ * "param1": 0,
|
|
|
+ * "param2": 0,
|
|
|
+ * "param3": 0,
|
|
|
+ * "param4": 0,
|
|
|
+ * "param5": 0,
|
|
|
+ * "deviceName": "Yokogawa.ExaopcDACS1.1",
|
|
|
+ * "collectorID": 25,
|
|
|
+ * "collectorName": "BF4",
|
|
|
+ * "deviceID": 23,
|
|
|
+ * "scanGroupID": 253,
|
|
|
+ * "scanGroupName": "BF4_ZNCT"
|
|
|
+ * }
|
|
|
+ * }
|
|
|
*/
|
|
|
public HDBasicTag getBasicTagByName(THdTag hdTag) throws HDSdkException {
|
|
|
HDBasicTag basicTag = null;
|
|
|
HdTagDTO opcPointDTO = hdTagService.selectInfoWithServer(hdTag);
|
|
|
if (ObjectUtils.isEmpty(opcPointDTO)) {
|
|
|
- return basicTag;
|
|
|
+ throw new OperateException(hdTag.getTagName() + "未查询到信息");
|
|
|
}
|
|
|
OPCServer opcServer = new OPCServer();
|
|
|
BeanUtils.copyProperties(opcPointDTO, opcServer);
|
|
@@ -291,10 +422,8 @@ public class HDServiceImpl {
|
|
|
connection = createConnection(opcServer);
|
|
|
HDTagManager tm = connection.getTagManager();
|
|
|
basicTag = tm.getBasicTagByName(opcPointDTO.getTagName());
|
|
|
- } finally {
|
|
|
- if (connection != null) {
|
|
|
- connection.dispose();
|
|
|
- }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
return basicTag;
|
|
|
}
|