Bläddra i källkod

修改opcua和da异步任务,组装数据接口错误

zhoupeng 2 år sedan
förälder
incheckning
2f219bbaad

+ 1 - 0
chaunyi_opc/opc_common/src/main/java/com/example/opc_common/entity/RawData.java

@@ -57,6 +57,7 @@ public class RawData implements Serializable {
     }
 
     public RawData(Integer itemGroupId, Integer dataSourceId, String itemName, String dataType, String dataValue, String dataValueTime, String valueBelongTime, Date createTime) {
+        this.itemGroupId = itemGroupId;
         this.dataSourceId = dataSourceId;
         this.itemName = itemName;
         this.dataType = dataType;

+ 10 - 10
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/task/OpcAsyncTask.java

@@ -88,8 +88,8 @@ public class OpcAsyncTask {
                 String itemName = rawData1.getItemName();
                 String dataType = rawData1.getDataType();
                 if (dataType.equals("boolean")) {
-                    List<Boolean> dataList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), Boolean[].class)) : new ArrayList<>();
-                    List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class)) : new ArrayList<>();
+                    List<Boolean> dataList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), Boolean[].class))) : new ArrayList<>();
+                    List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class))) : new ArrayList<>();
                     for (RawData rawData : rawDataList) {
                         dataList.add(JSON.parseObject(rawData.getDataValue(), Boolean.class));
                         dataTimeList.add(rawData.getDataValueTime());
@@ -98,9 +98,9 @@ public class OpcAsyncTask {
                             JSON.toJSONString(dataTimeList), sqlCurrentYmdh, new Date());
                     rawDataDao.addRawData(remainder, rawData);
                 } else {
-                    List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class)) : new ArrayList<>();
+                    List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class))) : new ArrayList<>();
                     try {
-                        List<BigDecimal> dataList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), BigDecimal[].class)) : new ArrayList<>();
+                        List<BigDecimal> dataList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), BigDecimal[].class))) : new ArrayList<>();
                         for (RawData rawData : rawDataList) {
                             BigDecimal bigDecimal = JSON.parseObject(rawData.getDataValue(), BigDecimal.class);
                             dataList.add(bigDecimal);
@@ -110,7 +110,7 @@ public class OpcAsyncTask {
                                 JSON.toJSONString(dataTimeList), sqlCurrentYmdh, new Date());
                         rawDataDao.addRawData(remainder, rawData);
                     } catch (Exception e) {
-                        List<String> dataList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), String[].class)) : new ArrayList<>();
+                        List<String> dataList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), String[].class))) : new ArrayList<>();
                         for (RawData rawData : rawDataList) {
                             dataList.add(rawData.getDataValue());
                             dataTimeList.add(rawData.getDataValueTime());
@@ -144,8 +144,8 @@ public class OpcAsyncTask {
                     String itemName = rawData1.getItemName();
                     String dataType = rawData1.getDataType();
                     if (dataType.equals("boolean")) {
-                        List<Boolean> dataList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), Boolean[].class)) : new ArrayList<>();
-                        List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class)) : new ArrayList<>();
+                        List<Boolean> dataList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), Boolean[].class))) : new ArrayList<>();
+                        List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class))) : new ArrayList<>();
                         for (RawData rawData : rawDataList) {
                             dataList.add(JSON.parseObject(rawData.getDataValue(), Boolean.class));
                             dataTimeList.add(rawData.getDataValueTime());
@@ -154,9 +154,9 @@ public class OpcAsyncTask {
                                 JSON.toJSONString(dataTimeList), sqlCurrentYmdh, new Date());
                         rawDataDao.addRawData(remainder, rawData);
                     } else {
-                        List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class)) : new ArrayList<>();
+                        List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class))) : new ArrayList<>();
                         try {
-                            List<BigDecimal> dataList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), BigDecimal[].class)) : new ArrayList<>();
+                            List<BigDecimal> dataList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), BigDecimal[].class))) : new ArrayList<>();
                             for (RawData rawData : rawDataList) {
                                 BigDecimal bigDecimal = JSON.parseObject(rawData.getDataValue(), BigDecimal.class);
                                 dataList.add(bigDecimal);
@@ -166,7 +166,7 @@ public class OpcAsyncTask {
                                     JSON.toJSONString(dataTimeList), sqlCurrentYmdh, new Date());
                             rawDataDao.addRawData(remainder, rawData);
                         } catch (Exception e) {
-                            List<String> dataList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), String[].class)) : new ArrayList<>();
+                            List<String> dataList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), String[].class))) : new ArrayList<>();
                             for (RawData rawData : rawDataList) {
                                 dataList.add(rawData.getDataValue());
                                 dataTimeList.add(rawData.getDataValueTime());

+ 7 - 1
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/util/OpcDaUtil.java

@@ -412,8 +412,10 @@ public class OpcDaUtil {
                 org.openscada.opc.lib.da.Item[] itemArr = new org.openscada.opc.lib.da.Item[itemSet.size()];
                 itemSet.toArray(itemArr);
                 Map<org.openscada.opc.lib.da.Item, ItemState> resultMap = group.read(true, itemArr);
+                System.out.println("---------");
                 for (org.openscada.opc.lib.da.Item key : resultMap.keySet()) {
                     String keyId = key.getId();
+                    System.out.println(keyId);
                     for (Item item : itemList) {
                         String itemReadName = item.getItemReadName();
                         DataModel dm = dmMap.get(itemReadName);
@@ -447,9 +449,13 @@ public class OpcDaUtil {
                     }
                 }
                 jsonObjectList.stream()
-                        .sorted(Comparator.comparing((JSONObject jsonObject) -> jsonObject.getInteger("eventMode")).reversed()
+                        .sorted(Comparator.comparing((JSONObject jsonObject) -> jsonObject.getInteger("eventMode"), Comparator.nullsFirst(Integer::compareTo).reversed())
                                 .thenComparing(jsonObject -> jsonObject.getString("itemName")
                                 )).collect(Collectors.toList());
+//                jsonObjectList.stream()
+//                        .sorted(Comparator.comparing((JSONObject jsonObject) -> jsonObject.getString("itemName"))
+//                                .thenComparing(jsonObject -> jsonObject.getInteger("eventMode"), Comparator.nullsFirst(Integer::compareTo).reversed()
+//                                )).collect(Collectors.toList());
                 return jsonObjectList;
             }
         } catch (Exception e) {

+ 19 - 16
chaunyi_opc/opc_da/src/main/resources/mapper/RawDataDao.xml

@@ -31,28 +31,30 @@
         insert into t_raw_data
         (item_group_id, data_source_id, item_name, data_type, data_value, data_value_time, value_belong_time,
          create_time)
-        VALUES (#{rawData.itemGroupId}, #{rawData.dataSourceId}, #{rawData.itemName}, #{rawData.dataType},
-                #{rawData.dataValue},
-                #{rawData.dataValueTime}, #{rawData.valueBelongTime}, #{rawData.createTime})
+        VALUES (#{itemGroupId}, #{dataSourceId}, #{itemName}, #{dataType},
+                #{dataValue},
+                #{dataValueTime}, #{valueBelongTime}, #{createTime})
     </insert>
 
     <insert id="addEventRawData">
         insert into t_event_raw_data
         (item_group_id, data_source_id, item_name, data_type, data_value, data_value_time, value_belong_time,
          create_time)
-        VALUES (#{rawData.itemGroupId}, #{rawData.dataSourceId}, #{rawData.itemName}, #{rawData.dataType},
-                #{rawData.dataValue},
-                #{rawData.dataValueTime}, #{rawData.valueBelongTime}, #{rawData.createTime})
+        VALUES (#{itemGroupId}, #{dataSourceId}, #{itemName}, #{dataType},
+                #{dataValue},
+                #{dataValueTime}, #{valueBelongTime}, #{createTime})
     </insert>
 
     <delete id="delRawDataList">
         delete
         from t_raw_data
         where value_belong_time = #{valueBelongTime}
-        and
-        <foreach collection="rawDataList" item="rawData" index="index" separator=",">
-            (#{rawData.id})
-        </foreach>
+        <if test="rawDataList!= null and rawDataList.size() >0">
+            AND id IN
+            <foreach collection="rawDataList" item="rawData" separator="," open="(" close=")">
+                #{rawData.id}
+            </foreach>
+        </if>
     </delete>
 
     <delete id="delEventRawDataList">
@@ -120,12 +122,13 @@
     </select>
 
     <select id="getTempRawDataList" resultType="com.example.opc_common.entity.RawData">
-        select data_type, data_value
+        select
+        <include refid="rawData"/>
         from t_raw_data
         where item_group_id = #{item.itemGroupId}
-          and data_souerce_id = #{dataSourceId}
-          and item_name = #{item.itemReadName}
-          and value_belong_time = #{valueBelongTime}
+        and data_source_id = #{dataSourceId}
+        and item_name = #{item.itemReadName}
+        and value_belong_time = #{valueBelongTime}
         order by data_value_time desc
     </select>
 
@@ -134,7 +137,7 @@
         <include refid="rawData"/>
         from t_raw_data${remainder}
         where item_group_id = #{item.itemGroupId}
-        and data_souerce_id = #{dataSourceId}
+        and data_source_id = #{dataSourceId}
         and item_name = #{item.itemReadName}
         and value_belong_time = #{valueBelongTime}
     </select>
@@ -144,7 +147,7 @@
         <include refid="rawData"/>
         from t_evnet_raw_data
         where item_group_id = #{item.itemGroupId}
-        and data_souerce_id = #{dataSourceId}
+        and data_source_id = #{dataSourceId}
         and item_name = #{item.itemReadName}
         and value_belong_time = #{valueBelongTime}
     </select>

+ 10 - 10
chaunyi_opc/opc_ua/src/main/java/com/example/opc_ua/task/OpcAsyncTask.java

@@ -80,8 +80,8 @@ public class OpcAsyncTask {
                 String itemName = rawData1.getItemName();
                 String dataType = rawData1.getDataType();
                 if (dataType.equals("boolean")) {
-                    List<Boolean> dataList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), Boolean[].class)) : new ArrayList<>();
-                    List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class)) : new ArrayList<>();
+                    List<Boolean> dataList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), Boolean[].class))) : new ArrayList<>();
+                    List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class))) : new ArrayList<>();
                     for (RawData rawData : rawDataList) {
                         dataList.add(JSON.parseObject(rawData.getDataValue(), Boolean.class));
                         dataTimeList.add(rawData.getDataValueTime());
@@ -90,9 +90,9 @@ public class OpcAsyncTask {
                             JSON.toJSONString(dataTimeList), sqlCurrentYmdh, new Date());
                     rawDataDao.addRawData(remainder, rawData);
                 } else {
-                    List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class)) : new ArrayList<>();
+                    List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class))) : new ArrayList<>();
                     try {
-                        List<BigDecimal> dataList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), BigDecimal[].class)) : new ArrayList<>();
+                        List<BigDecimal> dataList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), BigDecimal[].class))) : new ArrayList<>();
                         for (RawData rawData : rawDataList) {
                             BigDecimal bigDecimal = JSON.parseObject(rawData.getDataValue(), BigDecimal.class);
                             dataList.add(bigDecimal);
@@ -102,7 +102,7 @@ public class OpcAsyncTask {
                                 JSON.toJSONString(dataTimeList), sqlCurrentYmdh, new Date());
                         rawDataDao.addRawData(remainder, rawData);
                     } catch (Exception e) {
-                        List<String> dataList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), String[].class)) : new ArrayList<>();
+                        List<String> dataList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), String[].class))) : new ArrayList<>();
                         for (RawData rawData : rawDataList) {
                             dataList.add(rawData.getDataValue());
                             dataTimeList.add(rawData.getDataValueTime());
@@ -136,8 +136,8 @@ public class OpcAsyncTask {
                     String itemName = rawData1.getItemName();
                     String dataType = rawData1.getDataType();
                     if (dataType.equals("boolean")) {
-                        List<Boolean> dataList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), Boolean[].class)) : new ArrayList<>();
-                        List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class)) : new ArrayList<>();
+                        List<Boolean> dataList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), Boolean[].class))) : new ArrayList<>();
+                        List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class))) : new ArrayList<>();
                         for (RawData rawData : rawDataList) {
                             dataList.add(JSON.parseObject(rawData.getDataValue(), Boolean.class));
                             dataTimeList.add(rawData.getDataValueTime());
@@ -146,9 +146,9 @@ public class OpcAsyncTask {
                                 JSON.toJSONString(dataTimeList), sqlCurrentYmdh, new Date());
                         rawDataDao.addRawData(remainder, rawData);
                     } else {
-                        List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class)) : new ArrayList<>();
+                        List<String> dataTimeList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValueTime(), String[].class))) : new ArrayList<>();
                         try {
-                            List<BigDecimal> dataList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), BigDecimal[].class)) : new ArrayList<>();
+                            List<BigDecimal> dataList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), BigDecimal[].class))) : new ArrayList<>();
                             for (RawData rawData : rawDataList) {
                                 BigDecimal bigDecimal = JSON.parseObject(rawData.getDataValue(), BigDecimal.class);
                                 dataList.add(bigDecimal);
@@ -158,7 +158,7 @@ public class OpcAsyncTask {
                                     JSON.toJSONString(dataTimeList), sqlCurrentYmdh, new Date());
                             rawDataDao.addRawData(remainder, rawData);
                         } catch (Exception e) {
-                            List<String> dataList = Blank.isNotEmpty(oldRawData) ? Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), String[].class)) : new ArrayList<>();
+                            List<String> dataList = Blank.isNotEmpty(oldRawData) ? new ArrayList<>(Arrays.asList(JSON.parseObject(oldRawData.getDataValue(), String[].class))) : new ArrayList<>();
                             for (RawData rawData : rawDataList) {
                                 dataList.add(rawData.getDataValue());
                                 dataTimeList.add(rawData.getDataValueTime());

+ 1 - 1
chaunyi_opc/opc_ua/src/main/java/com/example/opc_ua/util/OpcUaUtil.java

@@ -280,7 +280,7 @@ public class OpcUaUtil {
                 jsonObjectList.add(jsonObject);
             }
             jsonObjectList.stream()
-                    .sorted(Comparator.comparing((JSONObject jsonObject) -> jsonObject.getInteger("eventMode")).reversed()
+                    .sorted(Comparator.comparing((JSONObject jsonObject) -> jsonObject.getInteger("eventMode"), Comparator.nullsFirst(Integer::compareTo)).reversed()
                             .thenComparing(jsonObject -> jsonObject.getString("itemName")
                             )).collect(Collectors.toList());
             return jsonObjectList;

+ 19 - 16
chaunyi_opc/opc_ua/src/main/resources/mapper/RawDataDao.xml

@@ -31,28 +31,30 @@
         insert into t_raw_data
         (item_group_id, data_source_id, item_name, data_type, data_value, data_value_time, value_belong_time,
          create_time)
-        VALUES (#{rawData.itemGroupId}, #{rawData.dataSourceId}, #{rawData.itemName}, #{rawData.dataType},
-                #{rawData.dataValue},
-                #{rawData.dataValueTime}, #{rawData.valueBelongTime}, #{rawData.createTime})
+        VALUES (#{itemGroupId}, #{dataSourceId}, #{itemName}, #{dataType},
+                #{dataValue},
+                #{dataValueTime}, #{valueBelongTime}, #{createTime})
     </insert>
 
     <insert id="addEventRawData">
         insert into t_event_raw_data
         (item_group_id, data_source_id, item_name, data_type, data_value, data_value_time, value_belong_time,
          create_time)
-        VALUES (#{rawData.itemGroupId}, #{rawData.dataSourceId}, #{rawData.itemName}, #{rawData.dataType},
-                #{rawData.dataValue},
-                #{rawData.dataValueTime}, #{rawData.valueBelongTime}, #{rawData.createTime})
+        VALUES (#{itemGroupId}, #{dataSourceId}, #{itemName}, #{dataType},
+                #{dataValue},
+                #{dataValueTime}, #{valueBelongTime}, #{createTime})
     </insert>
 
     <delete id="delRawDataList">
         delete
         from t_raw_data
         where value_belong_time = #{valueBelongTime}
-        and
-        <foreach collection="rawDataList" item="rawData" index="index" separator=",">
-            (#{rawData.id})
-        </foreach>
+        <if test="rawDataList!= null and rawDataList.size() >0">
+            AND id IN
+            <foreach collection="rawDataList" item="rawData" separator="," open="(" close=")">
+                #{rawData.id}
+            </foreach>
+        </if>
     </delete>
 
     <delete id="delEventRawDataList">
@@ -69,18 +71,19 @@
         <include refid="rawData"/>
         from t_raw_data${remainder}
         where item_group_id = #{item.itemGroupId}
-        and data_souerce_id = #{dataSourceId}
+        and data_source_id = #{dataSourceId}
         and item_name = #{item.itemReadName}
         and value_belong_time = #{valueBelongTime}
     </select>
 
     <select id="getTempRawDataList" resultType="com.example.opc_common.entity.RawData">
-        select data_type, data_value
+        select
+        <include refid="rawData"/>
         from t_raw_data
         where item_group_id = #{item.itemGroupId}
-          and data_souerce_id = #{dataSourceId}
-          and item_name = #{item.itemReadName}
-          and value_belong_time = #{valueBelongTime}
+        and data_source_id = #{dataSourceId}
+        and item_name = #{item.itemReadName}
+        and value_belong_time = #{valueBelongTime}
         order by data_value_time desc
     </select>
 
@@ -89,7 +92,7 @@
         <include refid="rawData"/>
         from t_evnet_raw_data
         where item_group_id = #{item.itemGroupId}
-        and data_souerce_id = #{dataSourceId}
+        and data_source_id = #{dataSourceId}
         and item_name = #{item.itemReadName}
         and value_belong_time = #{valueBelongTime}
     </select>