Browse Source

Merge branch 'master' of http://116.63.33.55/git/read_opc

FinalYu 2 năm trước cách đây
mục cha
commit
ade0401da2

+ 0 - 2
chuanyi_server/src/main/java/com/judong/chuanyiserver/dao/ItemGroupDao.java

@@ -39,8 +39,6 @@ public interface ItemGroupDao {
 
     Integer updateItemDescribe(Item item);
 
-    List<Item> getTableUaByGroupId(Integer itemGroupId);
-
     List<Item> getItemListDescribe(List<Item> itemList);
 
     Item getItemByUaGroupIdName(Integer itemGroupId, String itemName);

+ 0 - 4
chuanyi_server/src/main/java/com/judong/chuanyiserver/dao/RawDataDao.java

@@ -16,10 +16,6 @@ public interface RawDataDao {
 
     List<RawData> getLineOrBarData(Integer itemGroupId, Integer remainder, Integer dataSourceId, String itemName);
 
-    List<String> getUaName(Integer itemGroupId, List<Item> itemIdList);
-
-    List<String> getKepName(Integer itemGroupId, List<Item> itemIdList);
-
     List<Map<String, Object>> getPieData(Integer itemGroupId, Integer remainder, Integer dataSourceId, List<Item> itemList);
 
     List<RawData> getTableDataEvery(Integer itemGroupId, Integer remainder, Integer dataSourceId, String itemName,String startBelongTime,String endBelongTime);

+ 0 - 1
chuanyi_server/src/main/java/com/judong/chuanyiserver/entity/Item.java

@@ -29,7 +29,6 @@ public class Item implements Serializable {
      * opcUa特有的nodeId路径
      */
     private Integer nodeIndex;
-    private String nodeIdentifier;
     private String dataType;
 
     private String describe;

+ 5 - 7
chuanyi_server/src/main/java/com/judong/chuanyiserver/service/impl/ItemGroupServiceImpl.java

@@ -235,18 +235,18 @@ public class ItemGroupServiceImpl implements ItemGroupService {
             if (dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_UA_REAL.getValue()) ||
                     dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_UA_HISTORY.getValue())) {
                 if (Blank.isEmpty(dataSource.getIpAddress(), dataSource.getIpPort(), dataSource.getIsAnonymous())) {
-                    throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "数据源ip,端口号,匿名方式都不能为空");
+                    throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "ip地址,端口号,匿名方式都不能为空");
                 }
                 if (dataSource.getIsAnonymous() == ConstantStr.NOT_ANONYMOUS) {
                     if (Blank.isEmpty(dataSource.getIpUserName(), dataSource.getIpPassword())) {
-                        throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "选择不匿名方式,需要填写用户名和密码");
+                        throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "选择不匿名方式,需要填写账户和密码");
                     }
                 }
                 //异步读取opcUA数据并保存
                 opcAsyncTask.runOpcUa(itemGroup, dataSource, map);
             } else if (dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_DA_REAL.getValue())) {
                 if (Blank.isEmpty(dataSource.getIpAddress(), dataSource.getIpUserName(), dataSource.getIpPassword())) {
-                    throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "数据源ip,用户名,密码都不能为空");
+                    throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "ip地址,账户,密码都不能为空");
                 }
                 //读取数据并保存
                 Timer timer = new Timer();
@@ -355,10 +355,8 @@ public class ItemGroupServiceImpl implements ItemGroupService {
         DataSourceType dataSourceType = dataSourceDao.getDataSourceTypeById(dataSource.getTypeId());
         List<Item> itemList = new ArrayList<>();
         if (dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_UA_REAL.getValue()) ||
-                dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_UA_HISTORY.getValue())
-        ) {
-            itemList = itemGroupDao.getTableUaByGroupId(id);
-        } else if (dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_DA_REAL.getValue()) ||
+                dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_UA_HISTORY.getValue()) ||
+                dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_DA_REAL.getValue()) ||
                 dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_AE_ALARMS.getValue()) ||
                 dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_HDA_HISTORY.getValue())) {
             itemList = itemGroupDao.getItemListByGroupId(id);

+ 9 - 6
chuanyi_server/src/main/java/com/judong/chuanyiserver/service/impl/RawDataServiceImpl.java

@@ -25,6 +25,9 @@ import java.util.Map;
 @Transactional
 public class RawDataServiceImpl implements RawDataService {
 
+    private final String timeFormate = "yyyy-MM-dd";
+    private final String timeFormate1 = "yyyy-MM-dd HH";
+
     @Resource
     private RawDataDao rawDataDao;
 
@@ -126,18 +129,18 @@ public class RawDataServiceImpl implements RawDataService {
         //未判断前端传入的时间字符串是否符合yyyy-MM-dd HH
         String startBelongTime = "";
         if (Blank.isEmpty(tableDataDto.getStartBelongTime())) {
-            startBelongTime = DateUtil.dateChangeStr(DateUtil.getYesterdayYmd(), "yyyy-MM-dd") + " 08";
+            startBelongTime = DateUtil.dateChangeStr(DateUtil.getYesterdayYmd(), timeFormate) + " 08";
         } else {
-            if (!DateUtil.isValid(tableDataDto.getStartBelongTime(), "yyyy-MM-dd HH")) {
-                throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "传入的开始归属时间格式不对,必须为yyyy-MM-dd HH");
+            if (!DateUtil.isValid(tableDataDto.getStartBelongTime(), timeFormate1)) {
+                throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "传入的开始归属时间格式不对,必须为" + timeFormate1);
             }
         }
         String endBelongTime = "";
         if (Blank.isEmpty(tableDataDto.getEndBelongTime())) {
-            endBelongTime = DateUtil.dateChangeStr(DateUtil.getTodayYmd(), "yyyy-MM-dd") + " 07";
+            endBelongTime = DateUtil.dateChangeStr(DateUtil.getTodayYmd(), timeFormate) + " 07";
         } else {
-            if (!DateUtil.isValid(tableDataDto.getEndBelongTime(), "yyyy-MM-dd HH")) {
-                throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "传入的结束归属时间格式不对,必须为yyyy-MM-dd HH");
+            if (!DateUtil.isValid(tableDataDto.getEndBelongTime(), timeFormate1)) {
+                throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "传入的结束归属时间格式不对,必须为" + timeFormate1);
             }
         }
         for (TableDataDtoCh tableDataDtoCh : tableDataDtoChList) {

+ 81 - 24
chuanyi_server/src/main/java/com/judong/chuanyiserver/task/OpcAsyncTask.java

@@ -11,13 +11,14 @@ import com.judong.chuanyiserver.task.OpcUaTimerTask;
 import com.judong.chuanyiserver.util.*;
 import lombok.extern.slf4j.Slf4j;
 import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
+import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription;
+import org.eclipse.milo.opcua.stack.core.AttributeId;
 import org.eclipse.milo.opcua.stack.core.types.builtin.*;
 import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
+import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned;
+import org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode;
 import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
-import org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResponse;
-import org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResult;
-import org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadValueId;
-import org.eclipse.milo.opcua.stack.core.types.structured.ReadRawModifiedDetails;
+import org.eclipse.milo.opcua.stack.core.types.structured.*;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.scheduling.annotation.AsyncResult;
 import org.springframework.stereotype.Component;
@@ -46,23 +47,6 @@ public class OpcAsyncTask {
     @Resource
     private RedisUtil redisUtil;
 
-    public void testA() {
-        log.info("异步任务A在执行,时间是:" + System.currentTimeMillis());
-    }
-
-    public void testB() {
-        log.info("异步任务B在执行,时间是:" + System.currentTimeMillis());
-    }
-
-    public void testXXX() {
-        Timer timer = new Timer();
-        timer.schedule(new OpcUaTimerTask(), 1000, 2000);
-    }
-
-    public Future<String> testC(String xx) {
-        return new AsyncResult(xx);
-    }
-
     //异步读取OpcUa
     public void runOpcUa(ItemGroup itemGroup, DataSource dataSource, Map<String, DataModel> map) {
         Integer id = itemGroup.getId();
@@ -125,13 +109,86 @@ public class OpcAsyncTask {
             }
         } catch (Exception e) {
             String message = e.getMessage();
+            String messageContent = "";
+            if (message.contains("Bad_ConnectionRejected")) {
+                messageContent = "ip连接不可用";
+            } else if (message.contains("Bad_UserAccessDenied")) {
+                messageContent = "用户无权执行请求的操作";
+            } else {
+                messageContent = e.getMessage();
+            }
+            redisUtil.convertAndSend(ConstantStr.ITEM_GROUP, Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "组" + itemGroup.getGroupName() + "运行异常,错误信息为:" + messageContent));
+        } finally {
+            if (Blank.isNotEmpty(opcUaClient)) {
+                opcUaClient.disconnect();
+                redisUtil.del(ConstantStr.ITEM_GROUP + id);
+                itemGroupDao.runItemGroupById(id, ConstantStr.STOP_IT);
+            }
+        }
+    }
+
+    //异步读取OpcUa
+    public void runOpcUa1(ItemGroup itemGroup, DataSource dataSource, Map<String, DataModel> map) {
+        Integer id = itemGroup.getId();
+        DataSourceType dataSourceType = dataSourceDao.getDataSourceTypeById(dataSource.getTypeId());
+        OpcUaClient opcUaClient = null;
+        List<com.judong.chuanyiserver.entity.Item> itemList = itemGroupDao.getItemListByGroupId(id);
+        try {
+            opcUaClient = OpcUaUtil.createClient(dataSource);
+            opcUaClient.connect().get();
+//            List<NodeId> nodeIdList = OpcUaUtil.genNodeId(itemList);
+            if (dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_UA_REAL.getValue())) {
+                if (itemGroup.getReadMode() == ConstantStr.ON_FREQUENCY) {
+                    //创建发布间隔为1000ms的订阅对象
+                    UaSubscription uaSubscription = opcUaClient.getSubscriptionManager().createSubscription(1000.0).get();
+                    // 监控项请求列表
+                    List<MonitoredItemCreateRequest> requests = new ArrayList<>();
+                    for (Item item : itemList) {
+                        // 创建监控的参数
+                        MonitoringParameters parameters = new MonitoringParameters(uaSubscription.nextClientHandle(),
+                                1000.0, // 取样间隔
+                                null, // 过滤器,null表示使用默认值
+                                Unsigned.uint(10), // 队列大小
+                                true // 丢弃旧的
+                        );
+                        // 创建订阅的变量, 创建监控项请 求
+                        MonitoredItemCreateRequest request = new MonitoredItemCreateRequest(
+                                new ReadValueId(new NodeId(item.getNodeIndex(), item.getItemReadName()), AttributeId.Value.uid(),
+                                        null, null),
+                                MonitoringMode.Reporting, parameters);
+                        requests.add(request);
+                    }
+                    // 创建监控项,并且注册变量值改变时候的回调函数
+                    uaSubscription.createMonitoredItems(TimestampsToReturn.Both, requests, (item, id1) -> {
+                        item.setValueConsumer((i, v) -> {
+
+                            log.info("item={}, value={}", i.getReadValueId().getNodeId(), v.getValue());
+                            log.info("时间戳={}, value={}", i.getTimestamps(), v.getValue());
+                        });
+                    }).get();
+                } else if (itemGroup.getReadMode() == ConstantStr.ON_CHANGE) {
+
+                } else if (itemGroup.getReadMode() == ConstantStr.EXCEED_SET_VALUE) {
+
+                } else if (itemGroup.getReadMode() == ConstantStr.LOWER_SET_VALUE) {
+
+                } else {
+                    throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "目前未适配其他类型的读取模式");
+                }
+            } else {
+                throw new CustomException(ResultEnum.SERVER_ERROR.getRespCode(), "目前还没有此种类型的连接方式");
+            }
+        } catch (Exception e) {
+            String message = e.getMessage();
+            String messageContent = "";
             if (message.contains("Bad_ConnectionRejected")) {
-                redisUtil.convertAndSend(ConstantStr.ITEM_GROUP, Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "组" + itemGroup.getGroupName() + "运行异常,错误信息为:" + "ip连接不可用"));
+                messageContent = "ip连接不可用";
             } else if (message.contains("Bad_UserAccessDenied")) {
-                redisUtil.convertAndSend(ConstantStr.ITEM_GROUP, Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "组" + itemGroup.getGroupName() + "运行异常,错误信息为:" + "用户无权执行请求的操作"));
+                messageContent = "用户无权执行请求的操作";
             } else {
-                redisUtil.convertAndSend(ConstantStr.ITEM_GROUP, Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "组" + itemGroup.getGroupName() + "运行异常,错误信息为:" + e.getMessage()));
+                messageContent = e.getMessage();
             }
+            redisUtil.convertAndSend(ConstantStr.ITEM_GROUP, Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "组" + itemGroup.getGroupName() + "运行异常,错误信息为:" + messageContent));
         } finally {
             if (Blank.isNotEmpty(opcUaClient)) {
                 opcUaClient.disconnect();

+ 13 - 4
chuanyi_server/src/main/java/com/judong/chuanyiserver/util/OpcUaUtil.java

@@ -113,9 +113,12 @@ public class OpcUaUtil {
             if (message.contains("Bad_UserAccessDenied")) {
                 return Result.no(ResultEnum.REQUEST_TIME_OUT.getRespCode(), "用户无权执行请求的操作");
             }
-            if (message.contains("cannot create signer")){
+            if (message.contains("cannot create signer")) {
                 return Result.no(ResultEnum.REQUEST_TIME_OUT.getRespCode(), "证书创建失败,无法创建签名者");
             }
+            if (message.contains("Bad_Timeout")){
+                return Result.no(ResultEnum.REQUEST_TIME_OUT.getRespCode(), "连接超时");
+            }
             return Result.no(ResultEnum.REQUEST_TIME_OUT.getRespCode(), e.getMessage());
         } finally {
             if (Blank.isNotEmpty(opcUaClient)) {
@@ -141,6 +144,12 @@ public class OpcUaUtil {
             if (message.contains("Bad_UserAccessDenied")) {
                 return Result.no(ResultEnum.REQUEST_TIME_OUT.getRespCode(), "用户无权执行请求的操作");
             }
+            if (message.contains("cannot create signer")) {
+                return Result.no(ResultEnum.REQUEST_TIME_OUT.getRespCode(), "证书创建失败,无法创建签名者");
+            }
+            if (message.contains("Bad_Timeout")){
+                return Result.no(ResultEnum.REQUEST_TIME_OUT.getRespCode(), "连接超时");
+            }
             return Result.no(ResultEnum.REQUEST_TIME_OUT.getRespCode(), e.getMessage());
         } finally {
             if (Blank.isNotEmpty(opcUaClient)) {
@@ -173,7 +182,7 @@ public class OpcUaUtil {
             jsonObject.put("label", nd.getBrowseName().getName());
             jsonObject.put("nodeId", nd.getNodeId());
             jsonObject.put("nodeIndex", nd.getNodeId().getNamespaceIndex());
-            jsonObject.put("nodeIdentifier", nd.getNodeId().getIdentifier());
+            jsonObject.put("itemReadName", nd.getNodeId().getIdentifier());
             jsonObject.put("dataType", nd.getNodeId().getType());
 //            jsonObject.put("value", client.readValue(0.0, TimestampsToReturn.Both, nd.getNodeId()).get());
 //            jsonObject.put("dataType", client.readValue(0.0, TimestampsToReturn.Neither, nd.getNodeId()).get().getValue().getDataType().get().getType());
@@ -212,7 +221,7 @@ public class OpcUaUtil {
         }
         List<NodeId> nodeIdList = new ArrayList<>();
         for (Item item : itemList) {
-            NodeId nodeId = new NodeId(item.getNodeIndex(), item.getNodeIdentifier());
+            NodeId nodeId = new NodeId(item.getNodeIndex(), item.getItemReadName());
             nodeIdList.add(nodeId);
         }
         return nodeIdList;
@@ -224,7 +233,7 @@ public class OpcUaUtil {
         }
         List<HistoryReadValueId> historyReadValueIdList = new ArrayList<>();
         for (Item item : itemList) {
-            HistoryReadValueId historyReadValueId = new HistoryReadValueId(new NodeId(item.getNodeIndex(), item.getNodeIdentifier()),
+            HistoryReadValueId historyReadValueId = new HistoryReadValueId(new NodeId(item.getNodeIndex(), item.getItemReadName()),
                     null, QualifiedName.NULL_VALUE, null);
             historyReadValueIdList.add(historyReadValueId);
         }

+ 3 - 16
chuanyi_server/src/main/resources/mapper/ItemGroupDao.xml

@@ -10,7 +10,7 @@
 
     <sql id="item">
         id
-        , item_group_id, item_name, item_read_name, node_index, node_identifier, data_type, `describe`, data_model_id
+        , item_group_id, item_name, item_read_name, node_index , data_type, `describe`, data_model_id
     </sql>
 
     <insert id="addItemGroup" parameterType="com.judong.chuanyiserver.entity.ItemGroup" useGeneratedKeys="true"
@@ -23,10 +23,10 @@
 
     <insert id="addItem">
         insert into t_item (item_group_id, item_name,
-        item_read_name,node_index,node_identifier,data_type,`describe`,data_model_id)
+        item_read_name,node_index ,data_type,`describe`,data_model_id)
         VALUES
         <foreach collection="itemList" item="item" index="index" separator=",">
-            (#{itemGroupId},#{item.itemName},#{item.itemReadName},#{item.nodeIndex},#{item.nodeIdentifier},#{item.dataType},#{item.describe},#{item.dataModelId})
+            (#{itemGroupId},#{item.itemName},#{item.itemReadName},#{item.nodeIndex},#{item.dataType},#{item.describe},#{item.dataModelId})
         </foreach>
     </insert>
 
@@ -132,19 +132,6 @@
         where item_group_id = #{itemGroupId}
     </select>
 
-    <select id="getTableUaByGroupId" resultType="com.judong.chuanyiserver.entity.Item">
-        select id,
-               item_group_id,
-               node_identifier as item_name,
-               node_index,
-               node_identifier,
-               data_type,
-               `describe`,
-               data_model_id
-        from t_item
-        where item_group_id = #{itemGroupId}
-    </select>
-
     <select id="getItemListDescribe" resultType="com.judong.chuanyiserver.entity.Item">
         select
         <include refid="item"/>

+ 0 - 29
chuanyi_server/src/main/resources/mapper/RawDataDao.xml

@@ -26,35 +26,6 @@
         </foreach>
     </insert>
 
-    <select id="getUaName" resultType="java.lang.String">
-        SELECT
-        COALESCE
-        ( `describe`, node_identifier ) AS `describe`
-        FROM
-        t_item
-        WHERE
-        item_group_id = #{itemGroupId}
-        AND id IN
-        <foreach collection="itemList" item="item" index="index" separator=",">
-            (#{item.id})
-        </foreach>
-        order By
-    </select>
-
-    <select id="getKepName" resultType="java.lang.String">
-        SELECT
-        COALESCE
-        ( `describe`, item_name ) AS `describe`
-        FROM
-        t_item
-        WHERE
-        item_group_id = #{itemGroupId}
-        AND id IN
-        <foreach collection="itemList" item="item" index="index" separator=",">
-            (#{item.id})
-        </foreach>
-    </select>
-
     <select id="getLineOrBarData" resultType="com.judong.chuanyiserver.entity.RawData">
         SELECT id, data_source_id, item_name, data_type, data_value, create_time
         FROM t_raw_data${remainder}