Bläddra i källkod

opcUa读取数据,遍历树形节点,返回重要的节点信息

gt 2 år sedan
förälder
incheckning
54bbe5c4bc

+ 1 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/dao/DataSourceDao.java

@@ -5,6 +5,7 @@ import com.judong.chuanyiserver.entity.DataSourceType;
 import org.springframework.stereotype.Repository;
 
 import java.util.List;
+import java.util.Map;
 
 @Repository
 public interface DataSourceDao {

+ 7 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/entity/DataSource.java

@@ -53,4 +53,11 @@ public class DataSource implements Serializable {
      * 创建时间
      */
     private String createTime;
+
+    /**
+     * 虚拟字段
+     */
+    private String dataSourceTypeName;
+    private String dataSourceTypeKey;
+    private String dataSourceTypeDescribe;
 }

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

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

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

@@ -115,7 +115,9 @@ public class ItemGroupServiceImpl implements ItemGroupService {
     public Result getItemGroupById(Integer id) {
         ItemGroup itemGroup = itemGroupDao.getItemGroupById(id);
         List<Item> itemList = itemGroupDao.getItemListByGroupId(id);
-        itemGroup.setItemList(itemList);
+        if (Blank.isNotEmpty(itemList)){
+            itemGroup.setItemList(itemList);
+        }
         return Result.ok(itemGroup);
     }
 

+ 15 - 5
chuanyi_server/src/main/java/com/judong/chuanyiserver/util/OpcUaUtil.java

@@ -18,6 +18,7 @@ import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
 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.IdType;
 import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
 import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
 
@@ -150,14 +151,23 @@ public class OpcUaUtil {
             }
             JSONObject jsonObject = new JSONObject();
             jsonObject.put("label", nd.getBrowseName().getName());
-//            jsonObject.put("nodeId", nd.getNodeId());
+            jsonObject.put("nodeId", nd.getNodeId());
             jsonObject.put("nodeIndex", nd.getNodeId().getNamespaceIndex());
-            jsonObject.put("nodeIndentifier", nd.getNodeId().getIdentifier());
-            jsonObject.put("dataType", client.readValue(0.0, TimestampsToReturn.Neither, nd.getNodeId()).get().getValue().getDataType().get().getType());
+            jsonObject.put("nodeIdentifier", 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());
             jsonObject.put("description", nd.getDescription());
 //            PropertyTypeNode nd1 = (PropertyTypeNode) nd;
 //            nd1.getMinimumSamplingInterval();
-//            DataValue dataValue = client.readValue(0.0, TimestampsToReturn.Neither, nd.getNodeId()).get();
+            DataValue dataValue = client.readValue(0.0, TimestampsToReturn.Both, nd.getNodeId()).get();
+            StatusCode statusCode = dataValue.getStatusCode();
+            if (Blank.isNotEmpty(statusCode)) {
+                if (statusCode.isGood()) {
+                    jsonObject.put("value", dataValue.getValue().getValue());
+                }
+            }
+
 //            Variant value = dataValue.getValue();
 //            Object value1 = value.getValue();
 //            Optional<ExpandedNodeId> dataType = value.getDataType();
@@ -182,7 +192,7 @@ public class OpcUaUtil {
         }
         List<NodeId> nodeIdList = new ArrayList<>();
         for (Item item : itemList) {
-            NodeId nodeId = new NodeId(item.getNodeIndex(), item.getNodeIndentifier());
+            NodeId nodeId = new NodeId(item.getNodeIndex(), item.getNodeIdentifier());
             nodeIdList.add(nodeId);
         }
         return nodeIdList;

+ 16 - 12
chuanyi_server/src/main/resources/mapper/DataSourceDao.xml

@@ -59,18 +59,22 @@
     </select>
 
     <select id="getAllDataSource" resultType="com.judong.chuanyiserver.entity.DataSource">
-        select id,
-               type_id,
-               user_id,
-               data_source_name,
-               data_source_describe,
-               ip_address,
-               ip_port,
-               ip_user_name,
-               ip_password,
-               is_anonymous,
-               create_time
-        from t_data_source
+        select tds.id,
+               tds.type_id,
+               tds.user_id,
+               tds.data_source_name,
+               tds.data_source_describe,
+               tds.ip_address,
+               tds.ip_port,
+               tds.ip_user_name,
+               tds.ip_password,
+               tds.is_anonymous,
+               tds.create_time,
+               tdst.data_source_type_name,
+               tdst.data_source_type_key,
+               tdst.data_source_type_describe
+        from t_data_source tds
+                 LEFT JOIN t_data_source_type tdst ON tds.type_id = tdst.id
         where user_id = #{userId}
         order by create_time
     </select>

+ 1 - 1
chuanyi_server/src/main/resources/mapper/ItemGroupDao.xml

@@ -13,7 +13,7 @@
         insert into t_item (item_group_id, item_name,node_index,node_identifier,data_type)
         VALUES
         <foreach collection="itemList" item="item" index="index" separator=",">
-            (#{itemGroupId},#{item.itemName},#{item.nodeIndex},#{item.nodeIndentifier},#{item.dataType})
+            (#{itemGroupId},#{item.itemName},#{item.nodeIndex},#{item.nodeIdentifier},#{item.dataType})
         </foreach>
     </insert>