Jelajahi Sumber

适配opcua历史数据的读取方式

gt 2 tahun lalu
induk
melakukan
e6ea4b5130

+ 39 - 18
chuanyi_server/src/main/java/com/judong/chuanyiserver/config/OpcAsyncTask.java

@@ -4,14 +4,18 @@ import com.judong.chuanyiserver.dao.DataSourceDao;
 import com.judong.chuanyiserver.dao.ItemGroupDao;
 import com.judong.chuanyiserver.dao.RawDataDao;
 import com.judong.chuanyiserver.entity.*;
+import com.judong.chuanyiserver.enums.DataSourceTypeEnum;
 import com.judong.chuanyiserver.enums.ResultEnum;
 import com.judong.chuanyiserver.exception.CustomException;
 import com.judong.chuanyiserver.util.*;
 import lombok.extern.slf4j.Slf4j;
 import lombok.val;
 import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
+import org.eclipse.milo.opcua.sdk.client.OpcUaSession;
 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.enumerated.TimestampsToReturn;
+import org.eclipse.milo.opcua.stack.core.types.structured.*;
 import org.openscada.opc.dcom.da.OPCSERVERSTATE;
 import org.openscada.opc.lib.da.Item;
 import org.openscada.opc.lib.da.*;
@@ -149,32 +153,49 @@ public class OpcAsyncTask {
     //异步读取OpcUa
     public void runOpcUa(ItemGroup itemGroup, DataSource dataSource) throws Exception {
         Integer id = itemGroup.getId();
+        DataSourceType dataSourceType = dataSourceDao.getDataSourceTypeById(dataSource.getId());
         OpcUaClient opcUaClient = OpcUaUtil.createClient(dataSource);
         List<com.judong.chuanyiserver.entity.Item> itemList = itemGroupDao.getItemListByGroupId(id);
-        List<NodeId> nodeIdList = OpcUaUtil.genNodeId(itemList);
         try {
             opcUaClient.connect().get();
-            Boolean flage = true;
-            while (flage) {
-                List<RawData> rawDataList = new ArrayList<>();
-                List<DataValue> valueList = opcUaClient.readValues(0.0, TimestampsToReturn.Neither, nodeIdList).get();
-                Date date = new Date();
-                for (DataValue dataValue : valueList) {
-                    StatusCode statusCode = dataValue.getStatusCode();
-                    if (Blank.isNotEmpty(statusCode)) {
-                        if (statusCode.isGood()) {
-                            Variant value = dataValue.getValue();
-                            ExpandedNodeId expandedNodeId = value.getDataType().get();
-                            rawDataList.add(new RawData(itemGroup.getDataSourceId(), expandedNodeId.getIdentifier().toString(), expandedNodeId.getType().toString(), value.getValue().toString(), date));
+            if (dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_UA_REAL.getValue())) {
+                Boolean flage = true;
+                List<NodeId> nodeIdList = OpcUaUtil.genNodeId(itemList);
+                while (flage) {
+                    List<RawData> rawDataList = new ArrayList<>();
+                    List<DataValue> valueList = opcUaClient.readValues(0.0, TimestampsToReturn.Neither, nodeIdList).get();
+                    Date date = new Date();
+                    for (DataValue dataValue : valueList) {
+                        StatusCode statusCode = dataValue.getStatusCode();
+                        if (Blank.isNotEmpty(statusCode)) {
+                            if (statusCode.isGood()) {
+                                Variant value = dataValue.getValue();
+                                ExpandedNodeId expandedNodeId = value.getDataType().get();
+                                rawDataList.add(new RawData(itemGroup.getDataSourceId(), expandedNodeId.getIdentifier().toString(), expandedNodeId.getType().toString(), value.getValue().toString(), date));
+                            }
                         }
                     }
+                    addRawDataList(id, rawDataList);
+                    Thread.sleep(itemGroup.getModeValue() * 1000);
+                    flage = (Boolean) redisUtil.get(ConstantStr.ITEM_GROUP + id);
+                    if (Blank.isEmpty(flage)) {
+                        flage = false;
+                    }
                 }
-                addRawDataList(id, rawDataList);
-                Thread.sleep(itemGroup.getModeValue() * 1000);
-                flage = (Boolean) redisUtil.get(ConstantStr.ITEM_GROUP + id);
-                if (Blank.isEmpty(flage)) {
-                    flage = false;
+            } else if (dataSourceType.getDataSourceTypeKey().equals(DataSourceTypeEnum.OPC_UA_HISTORY.getValue())) {
+                List<HistoryReadValueId> historyReadValueIdList = OpcUaUtil.genHisNodeId(itemList);
+                List<RawData> rawDataList = new ArrayList<>();
+                Date date = new Date();
+                HistoryReadResponse historyReadResponse = opcUaClient.historyRead(new ReadRawModifiedDetails(false, DateTime.MIN_VALUE, DateTime.MIN_VALUE, UInteger.MAX, false),
+                        TimestampsToReturn.Both, true, historyReadValueIdList).get();
+                HistoryReadResult[] results = historyReadResponse.getResults();
+                for (HistoryReadResult historyReadResult : results) {
+                    System.out.println(historyReadResult);
+//                    rawDataList.add(new RawData(itemGroup.getDataSourceId(), expandedNodeId.getIdentifier().toString(), expandedNodeId.getType().toString(), value.getValue().toString(), date));
                 }
+                addRawDataList(id, rawDataList);
+            } else {
+                throw new CustomException(ResultEnum.SERVER_ERROR.getRespCode(), "目前还没有此种类型的连接方式");
             }
         } catch (Exception e) {
             e.printStackTrace();

+ 8 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/controller/RawDataController.java

@@ -1,11 +1,14 @@
 package com.judong.chuanyiserver.controller;
 
 import com.judong.chuanyiserver.service.RawDataService;
+import com.judong.chuanyiserver.util.Result;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
+import java.util.List;
 
 @RestController
 @RequestMapping("rawData")
@@ -14,4 +17,9 @@ public class RawDataController {
 
     @Resource
     private RawDataService rawDataService;
+
+    @GetMapping("/getDataByTimeType")
+    public Result getDataByTimeType(List<Integer> itemGroupIdList) {
+        return rawDataService.getDataByTimeType(itemGroupIdList);
+    }
 }

+ 17 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/controller/ReportTableController.java

@@ -0,0 +1,17 @@
+package com.judong.chuanyiserver.controller;
+
+import com.judong.chuanyiserver.service.ReportTableService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+
+@RestController
+@RequestMapping("reportTable")
+@Slf4j
+public class ReportTableController {
+
+    @Resource
+    private ReportTableService reportTableService;
+}

+ 7 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/dao/ReportTableDao.java

@@ -0,0 +1,7 @@
+package com.judong.chuanyiserver.dao;
+
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface ReportTableDao {
+}

+ 1 - 1
chuanyi_server/src/main/java/com/judong/chuanyiserver/enums/ModelEnum.java

@@ -14,7 +14,7 @@ public enum ModelEnum {
     DATASOURCE("数据源","DATASOURCE"),
 
     SIGNIN("登录", "SIGNIN"), LOGOUT("退出登录", "LOGOUT"), REGISTER("注册", "REGISTER"), HOMEBYPAGE("首页", "HOMEBYPAGE"),
-    UPDATECODE("发送验证码", "UPDATECODE"), SEARCH("房源搜索", "SEARCH");
+    UPDATECODE("发送验证码", "UPDATECODE");
 
     private String value;
 

+ 6 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/service/RawDataService.java

@@ -1,4 +1,10 @@
 package com.judong.chuanyiserver.service;
 
+import com.judong.chuanyiserver.util.Result;
+
+import java.util.List;
+
 public interface RawDataService {
+
+    Result getDataByTimeType(List<Integer> itemGroupIdList);
 }

+ 4 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/service/ReportTableService.java

@@ -0,0 +1,4 @@
+package com.judong.chuanyiserver.service;
+
+public interface ReportTableService {
+}

+ 8 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/service/impl/RawDataServiceImpl.java

@@ -2,10 +2,12 @@ package com.judong.chuanyiserver.service.impl;
 
 import com.judong.chuanyiserver.dao.RawDataDao;
 import com.judong.chuanyiserver.service.RawDataService;
+import com.judong.chuanyiserver.util.Result;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
+import java.util.List;
 
 @Service
 @Transactional
@@ -13,4 +15,10 @@ public class RawDataServiceImpl implements RawDataService {
 
     @Resource
     private RawDataDao rawDataDao;
+
+    @Override
+    public Result getDataByTimeType(List<Integer> itemGroupIdList) {
+
+        return null;
+    }
 }

+ 16 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/service/impl/ReportTableServiceImpl.java

@@ -0,0 +1,16 @@
+package com.judong.chuanyiserver.service.impl;
+
+import com.judong.chuanyiserver.dao.ReportTableDao;
+import com.judong.chuanyiserver.service.ReportTableService;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.annotation.Resource;
+
+@Service
+@Transactional
+public class ReportTableServiceImpl implements ReportTableService {
+
+    @Resource
+    private ReportTableDao reportTableDao;
+}

+ 6 - 2
chuanyi_server/src/main/java/com/judong/chuanyiserver/util/MathUtil.java

@@ -12,11 +12,15 @@ public class MathUtil {
 
     /**
      * 四则混合运算
+     *
+     * @param parameter  变量代表字符串
+     * @param MathString 数学表达式字符串
+     * @param value      变量值
+     * @return
+     * @throws ParseException
      */
     public static BigDecimal quadricOperation(String parameter, String MathString, Long value) throws ParseException {
         Scope scope = new Scope();
-//        Variable a = scope.getVariable("BBC");
-//        Expression expr = Parser.parse("-2*(BBC+25)+2/7+18*2^2", scope);
         Variable a = scope.getVariable(parameter);
         Expression expr = Parser.parse(MathString, scope);
         a.setValue(value);

+ 14 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/util/OpcUaUtil.java

@@ -22,6 +22,7 @@ import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned;
 import org.eclipse.milo.opcua.stack.core.types.enumerated.IdType;
 import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
 import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
+import org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadValueId;
 
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -196,6 +197,19 @@ public class OpcUaUtil {
         return nodeIdList;
     }
 
+    public static List<HistoryReadValueId> genHisNodeId(List<Item> itemList) {
+        if (Blank.isEmpty(itemList)) {
+            return null;
+        }
+        List<HistoryReadValueId> historyReadValueIdList = new ArrayList<>();
+        for (Item item : itemList) {
+            HistoryReadValueId historyReadValueId = new HistoryReadValueId(new NodeId(item.getNodeIndex(), item.getNodeIdentifier()),
+                    null, QualifiedName.NULL_VALUE, null);
+            historyReadValueIdList.add(historyReadValueId);
+        }
+        return historyReadValueIdList;
+    }
+
     public static List<JSONObject> opcUaReadNodeIdList(DataSource dataSource, List<NodeId> nodeIdList) {
         OpcUaClient client = null;
         try {

+ 5 - 0
chuanyi_server/src/main/resources/mapper/ReportTableDao.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.judong.chuanyiserver.dao.ReportTableDao">
+
+</mapper>