Ver Fonte

适配opcUa读取数据,修改相应的后台逻辑和数据库结构

gt há 2 anos atrás
pai
commit
3694d46d03

+ 44 - 6
chuanyi_server/src/main/java/com/judong/chuanyiserver/config/OpcAsyncTask.java

@@ -6,11 +6,13 @@ import com.judong.chuanyiserver.dao.RawDataDao;
 import com.judong.chuanyiserver.entity.*;
 import com.judong.chuanyiserver.enums.ResultEnum;
 import com.judong.chuanyiserver.exception.CustomException;
-import com.judong.chuanyiserver.util.Blank;
-import com.judong.chuanyiserver.util.ConstantStr;
-import com.judong.chuanyiserver.util.KepOpcServerUtil;
-import com.judong.chuanyiserver.util.RedisUtil;
+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.stack.core.types.builtin.DataValue;
+import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
+import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
 import org.openscada.opc.dcom.da.OPCSERVERSTATE;
 import org.openscada.opc.lib.da.Item;
 import org.openscada.opc.lib.da.*;
@@ -21,6 +23,7 @@ import org.springframework.stereotype.Component;
 import javax.annotation.Resource;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.Future;
 
 import static com.judong.chuanyiserver.util.KepOpcServerUtil.getVal;
@@ -137,10 +140,45 @@ public class OpcAsyncTask {
             }
         } catch (Exception e) {
             e.printStackTrace();
+        } finally {
+            server.dispose();
             redisUtil.del(ConstantStr.ITEM_GROUP + id);
             itemGroupDao.runItemGroupById(id, ConstantStr.STOP_IT);
-        }finally {
-            server.dispose();
+        }
+    }
+
+    //异步读取OpcUa
+    public void runOpcUa(ItemGroup itemGroup, DataSource dataSource) throws Exception {
+        Integer id = itemGroup.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) {
+
+//                    rawDataList.add(new RawData(itemGroup.getDataSourceId(), key.getId(), val.get("javaType").toString(), val.get("value").toString(), date));
+                }
+                addRawDataList(id, rawDataList);
+                Thread.sleep(itemGroup.getModeValue() * 1000);
+                flage = (Boolean) redisUtil.get(ConstantStr.ITEM_GROUP + id);
+                if (Blank.isEmpty(flage)) {
+                    flage = false;
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (Blank.isNotEmpty(opcUaClient)) {
+                opcUaClient.disconnect();
+                redisUtil.del(ConstantStr.ITEM_GROUP + id);
+                itemGroupDao.runItemGroupById(id, ConstantStr.STOP_IT);
+            }
         }
     }
 

+ 1 - 3
chuanyi_server/src/main/java/com/judong/chuanyiserver/dao/ItemGroupDao.java

@@ -27,9 +27,7 @@ public interface ItemGroupDao {
 
     Integer delItemGroupById(Integer id);
 
-    List<Item> getItemByGroupId(Integer itemGroupId);
-
-    List<Item> getItemList(Integer itemGroupId);
+    List<Item> getItemListByGroupId(Integer itemGroupId);
 
     List<Item> getPublicItemList(Integer itemGroupId, List<Item> itemList);
 

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

@@ -21,4 +21,9 @@ public class Item implements Serializable {
      * 项名称
      */
     private String itemName;
+    /**
+     * opcUa特有的nodeId路径
+     */
+    private Integer nodeIndex;
+    private String nodeIndentifier;
 }

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

@@ -72,7 +72,7 @@ public class ItemGroupServiceImpl implements ItemGroupService {
                         itemGroupDao.delItemByGroupId(id);
                         return Result.ok("修改item组成功");
                     }
-                    List<Item> oldItemList = itemGroupDao.getItemList(id);
+                    List<Item> oldItemList = itemGroupDao.getItemListByGroupId(id);
                     if (Blank.isEmpty(oldItemList)) {
                         itemGroupDao.addItem(id, itemList);
                         return Result.ok("修改item组成功");
@@ -114,7 +114,7 @@ public class ItemGroupServiceImpl implements ItemGroupService {
     @Override
     public Result getItemGroupById(Integer id) {
         ItemGroup itemGroup = itemGroupDao.getItemGroupById(id);
-        List<Item> itemList = itemGroupDao.getItemByGroupId(id);
+        List<Item> itemList = itemGroupDao.getItemListByGroupId(id);
         itemGroup.setItemList(itemList);
         return Result.ok(itemGroup);
     }

+ 37 - 2
chuanyi_server/src/main/java/com/judong/chuanyiserver/util/OpcUaUtil.java

@@ -2,19 +2,23 @@ package com.judong.chuanyiserver.util;
 
 import com.alibaba.fastjson.JSONObject;
 import com.judong.chuanyiserver.entity.DataSource;
+import com.judong.chuanyiserver.entity.Item;
 import com.judong.chuanyiserver.enums.ResultEnum;
 import lombok.extern.slf4j.Slf4j;
 import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
 import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig;
 import org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider;
 import org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider;
+import org.eclipse.milo.opcua.sdk.client.model.nodes.variables.PropertyTypeNode;
 import org.eclipse.milo.opcua.sdk.client.nodes.UaNode;
 import org.eclipse.milo.opcua.stack.client.DiscoveryClient;
 import org.eclipse.milo.opcua.stack.core.Identifiers;
 import org.eclipse.milo.opcua.stack.core.UaException;
 import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
-import org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText;
+import org.eclipse.milo.opcua.stack.core.types.builtin.*;
+import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort;
 import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned;
+import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
 import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
 
 import java.nio.file.Files;
@@ -23,6 +27,7 @@ import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.function.Predicate;
 
@@ -105,7 +110,7 @@ public class OpcUaUtil {
         }
     }
 
-    public static Result opcUaGetTree(DataSource dataSource){
+    public static Result opcUaGetTree(DataSource dataSource) {
         OpcUaClient opcUaClient = null;
         try {
             opcUaClient = createClient(dataSource);
@@ -145,10 +150,40 @@ public class OpcUaUtil {
             }
             JSONObject jsonObject = new JSONObject();
             jsonObject.put("label", nd.getBrowseName().getName());
+//            jsonObject.put("nodeId", nd.getNodeId());
+            jsonObject.put("nodeIndex", nd.getNodeId().getNamespaceIndex());
+            jsonObject.put("nodeIndentifier", nd.getNodeId().getIdentifier());
+            jsonObject.put("description", nd.getDescription());
+//            PropertyTypeNode nd1 = (PropertyTypeNode) nd;
+//            nd1.getMinimumSamplingInterval();
+//            DataValue dataValue = client.readValue(0.0, TimestampsToReturn.Neither, nd.getNodeId()).get();
+//            Variant value = dataValue.getValue();
+//            Object value1 = value.getValue();
+//            Optional<ExpandedNodeId> dataType = value.getDataType();
+//            int value2 = dataType.get().getType().getValue();
+//            UShort serverPicoseconds = dataValue.getServerPicoseconds();
+//            DateTime serverTime = dataValue.getServerTime();
+//            UShort sourcePicoseconds = dataValue.getSourcePicoseconds();
+//            DateTime sourceTime = dataValue.getSourceTime();
+//            StatusCode statusCode = dataValue.getStatusCode();
+//            jsonObject.put("value",client.readValue(0.0, TimestampsToReturn.Neither, nd.getNodeId()).get());
+//            jsonObject.put("nodeId",nd.);
 //            jsonObject.put("value",client.readValue(0.0, TimestampsToReturn.Neither, nd.getNodeId()).get());
             jsonObject.put("children", generOpcUaTree(client, nd));
             jsonList.add(jsonObject);
         }
         return jsonList;
     }
+
+    public static List<NodeId> genNodeId(List<Item> itemList) {
+        if (Blank.isEmpty(itemList)) {
+            return null;
+        }
+        List<NodeId> nodeIdList = new ArrayList<>();
+        for (Item item : itemList) {
+            NodeId nodeId = new NodeId(item.getNodeIndex(), item.getNodeIndentifier());
+            nodeIdList.add(nodeId);
+        }
+        return nodeIdList;
+    }
 }

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

@@ -10,10 +10,10 @@
     </insert>
 
     <insert id="addItem">
-        insert into t_item (item_group_id, item_name)
+        insert into t_item (item_group_id, item_name,node_index,node_identifier)
         VALUES
         <foreach collection="itemList" item="item" index="index" separator=",">
-            (#{itemGroupId},#{item.itemName})
+            (#{itemGroupId},#{item.itemName},#{item.nodeIndex},#{item.nodeIndentifier})
         </foreach>
     </insert>
 
@@ -113,19 +113,13 @@
           AND group_name = #{groupName}
     </select>
 
-    <select id="getItemByGroupId" resultType="com.judong.chuanyiserver.entity.Item">
+    <select id="getItemListByGroupId" resultType="com.judong.chuanyiserver.entity.Item">
         select id, item_group_id, item_name
         from t_item
         where item_group_id = #{itemGroupId}
         order by item_name
     </select>
 
-    <select id="getItemList" resultType="com.judong.chuanyiserver.entity.Item">
-        select id, item_group_id, item_name
-        from t_item
-        where item_group_id = #{itemGroupId}
-    </select>
-
     <select id="getPublicItemList" resultType="com.judong.chuanyiserver.entity.Item">
         select id, item_group_id, item_name
         from t_item