Pārlūkot izejas kodu

初步完成数据附属属性和基本属性的分开存储

zhoupeng 1 gadu atpakaļ
vecāks
revīzija
a78b4daf3b

+ 7 - 0
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/dao/RawDataDao.java

@@ -1,5 +1,6 @@
 package com.example.opc_da.dao;
 
+import com.example.opc_common.entity.AttachRawData;
 import com.example.opc_common.entity.CursorRawData;
 import com.example.opc_common.entity.Item;
 import com.example.opc_common.entity.RawData;
@@ -50,4 +51,10 @@ public interface RawDataDao {
     List<CursorRawData> getTableCursorData(Integer itemGroupId, Integer dataSourceId, String itemName, String sqlCurrentYmdh);
 
     List<String> getCursorYmdh(Integer itemGroupId, Integer dataSourceId, String itemName, String startBelongTime, String endBelongTime);
+
+    AttachRawData getAttachRawData(AttachRawData attachRawData);
+
+    Integer addAttachRawData(AttachRawData attachRawData);
+
+    Integer updateAttachRawData(AttachRawData attachRawData);
 }

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

@@ -65,9 +65,14 @@ public class OpcAsyncTask {
         }
     }
 
-    public synchronized void addAttachRawData(AttachRawData attachRawData){
-        if (Blank.isNotEmpty(attachRawData)){
-
+    public synchronized void addAttachRawData(AttachRawData attachRawData) {
+        if (Blank.isNotEmpty(attachRawData)) {
+            AttachRawData attachRawData1 = rawDataDao.getAttachRawData(attachRawData);
+            if (Blank.isEmpty(attachRawData1)) {
+                rawDataDao.addAttachRawData(attachRawData);
+            } else {
+                rawDataDao.updateAttachRawData(attachRawData);
+            }
         }
     }
 

+ 43 - 8
chaunyi_opc/opc_da/src/main/resources/mapper/RawDataDao.xml

@@ -12,6 +12,11 @@
         , item_group_id, data_source_id, item_name, data_type, org_data_value, data_value, data_value_time, data_index, value_belong_time, is_record, create_time
     </sql>
 
+    <sql id="attachRawData">
+        id
+        , item_group_id, data_source_id, item_name, data_type, org_data_value, data_value, data_value_time, create_time
+    </sql>
+
     <insert id="addRawData">
         insert into t_raw_data${remainder}
         (item_group_id, data_source_id, item_name, data_type, org_data_value, data_value, data_value_time, data_index,
@@ -33,6 +38,26 @@
                 #{dataValueTime}, #{dataIndex}, #{valueBelongTime}, #{isRecord}, #{createTime})
     </insert>
 
+    <insert id="addAttachRawData">
+        insert into t_attach_raw_data
+        (item_group_id, data_source_id, item_name, data_type, org_data_value, data_value, data_value_time,
+         create_time)
+            VALUE (#{itemGroupId}, #{dataSourceId}, #{itemName}, #{dataType},
+            #{orgDataValue}, #{dataValue},
+            #{dataValueTime}, #{createTime})
+    </insert>
+
+    <update id="updateAttachRawData">
+        update t_attach_raw_data
+        set data_type=#{dataType},
+            org_data_value=#{orgDataValue},
+            data_value=#{dataValue},
+            data_value_time=#{dataValueTime}
+        where item_group_id = #{itemGroupId}
+          and data_source_id = #{dataSourceId}
+          and item_name = #{itemName}
+    </update>
+
     <delete id="delCursorRawDataList">
         delete
         from t_raw_data
@@ -236,16 +261,26 @@
     </select>
 
     <select id="getCursorYmdh" resultType="java.lang.String">
-        select
-        value_belong_time
+        select value_belong_time
         from t_raw_data
         WHERE item_group_id = #{itemGroupId}
-        AND data_source_id = #{dataSourceId}
-        AND item_name = (SELECT item_read_name FROM t_item WHERE item_group_id = #{itemGroupId} AND item_name =
-        #{itemName})
-        AND value_belong_time BETWEEN #{startBelongTime} AND #{endBelongTime}
-        GROUP BY
-        value_belong_time
+          AND data_source_id = #{dataSourceId}
+          AND item_name = (SELECT item_read_name
+                           FROM t_item
+                           WHERE item_group_id = #{itemGroupId}
+                             AND item_name =
+                                 #{itemName})
+          AND value_belong_time BETWEEN #{startBelongTime} AND #{endBelongTime}
+        GROUP BY value_belong_time
         ORDER BY value_belong_time
     </select>
+
+    <select id="getAttachRawData" resultType="com.example.opc_common.entity.AttachRawData">
+        select
+        <include refid="attachRawData"/>
+        from t_attach_raw_data
+        where item_group_id=#{itemGroupId}
+        and data_source_id=#{dataSourceId}
+        and item_name=#{itemName}
+    </select>
 </mapper>

+ 2 - 0
chaunyi_opc/opc_ua/src/main/java/com/example/opc_ua/dao/ItemGroupDao.java

@@ -18,4 +18,6 @@ public interface ItemGroupDao {
     Integer stopItemGroupById(Integer id, Integer runState);
 
     List<ItemGroup> getAllItemGroup(String userId);
+
+    List<Item> getBasicItemList(Integer itemGroupId, Integer itemType);
 }

+ 7 - 0
chaunyi_opc/opc_ua/src/main/java/com/example/opc_ua/dao/RawDataDao.java

@@ -1,5 +1,6 @@
 package com.example.opc_ua.dao;
 
+import com.example.opc_common.entity.AttachRawData;
 import com.example.opc_common.entity.CursorRawData;
 import com.example.opc_common.entity.Item;
 import com.example.opc_common.entity.RawData;
@@ -31,4 +32,10 @@ public interface RawDataDao {
     List<CursorRawData> getEventDataList(Integer itemGroupId, Integer dataSourceId, Integer itemId, List<Long> indexList);
 
     Integer delEventByIndexList(Integer itemGroupId, Integer dataSourceId, Long dataIndex);
+
+    AttachRawData getAttachRawData(AttachRawData attachRawData);
+
+    Integer addAttachRawData(AttachRawData attachRawData);
+
+    Integer updateAttachRawData(AttachRawData attachRawData);
 }

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

@@ -52,6 +52,17 @@ public class OpcAsyncTask {
         }
     }
 
+    public void addAttachRawData(AttachRawData attachRawData) {
+        if (Blank.isNotEmpty(attachRawData)) {
+            AttachRawData attachRawData1 = rawDataDao.getAttachRawData(attachRawData);
+            if (Blank.isEmpty(attachRawData1)) {
+                rawDataDao.addAttachRawData(attachRawData);
+            } else {
+                rawDataDao.updateAttachRawData(attachRawData);
+            }
+        }
+    }
+
     /**
      * 此接口,只有事件驱动任务才会调用,其他地方不调用
      *

+ 130 - 94
chaunyi_opc/opc_ua/src/main/java/com/example/opc_ua/task/OpcUaTask.java

@@ -1,11 +1,14 @@
 package com.example.opc_ua.task;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.example.opc_common.entity.*;
 import com.example.opc_common.enums.ResultEnum;
 import com.example.opc_common.exception.CustomException;
 import com.example.opc_common.util.Blank;
 import com.example.opc_common.util.ConstantStr;
 import com.example.opc_common.util.DateUtil;
+import com.example.opc_common.util.MathUtil;
 import com.example.opc_ua.dao.DataModelDao;
 import com.example.opc_ua.dao.ItemGroupDao;
 import com.example.opc_ua.dao.MessageNoticeDao;
@@ -14,11 +17,15 @@ import com.example.opc_ua.util.OpcUaUtil;
 import com.example.opc_ua.util.RedisUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
+import org.eclipse.milo.opcua.stack.core.types.builtin.*;
+import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
+import java.math.BigDecimal;
 import java.util.*;
+import java.util.stream.Collectors;
 
 @Slf4j
 @Component
@@ -62,108 +69,137 @@ public class OpcUaTask {
         Date startDate = DateUtil.strYmdhmsChangeDate(startTime);
         Date endDate = DateUtil.strYmdhmsChangeDate(endTime);
 
-        Boolean flage = false;
         //获取组中所有标签的数据模型,并转换为map<itemName,DataModel>
-        List<Item> allItemList = itemGroupDao.getItemListByGroupId(id);
-        itemGroup.setItemList(allItemList);
-        if (Blank.isNotEmpty(allItemList)) {
-            for (Item item : allItemList) {
-                if (Blank.isNotEmpty(item.getDataModelId())) {
-                    flage = true;
-                    break;
+        List<Item> basicItemList = itemGroupDao.getBasicItemList(id, ConstantStr.BASIC_ATTRIBUTES);
+        List<DataModel> dmBasicItemList = dataModelDao.getDmListByItemList(basicItemList);
+        Map<String, DataModel> basicMap = DataModel.genMap(basicItemList, dmBasicItemList);
+
+        if (Blank.isNotEmpty(basicItemList)) {
+            Timer timer = new Timer();
+            OpcUaClient opcUaClient = null;
+            try {
+                opcUaClient = OpcUaUtil.createClient(dataSource);
+                opcUaClient.connect().get();
+            } catch (Exception e) {
+                messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                        itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行停止",
+                        OpcUaUtil.genException(e.getMessage()),
+                        ConstantStr.NO_READ));
+
+                itemGroupDao.stopItemGroupById(id, ConstantStr.EXCEPT_STOP_UP);
+                if (Blank.isNotEmpty(opcUaClient)) {
+                    opcUaClient.disconnect();
                 }
+                if (Blank.isNotEmpty(timer)) {
+                    timer.cancel();
+                }
+                cronTaskRegister.removeCronTask(cronId);
+            }
+            if (readMode == ConstantStr.ON_FREQUENCY) {
+                timer.schedule(new OpcUaFrequencyTask(
+                                redisUtil,
+                                opcAsyncTask,
+                                cronId,
+                                cronTaskRegister,
+                                itemGroupDao,
+                                messageNoticeDao,
+                                opcUaClient,
+                                basicItemList,
+                                basicMap,
+                                itemGroup,
+                                dataSource,
+                                timer,
+                                timeFormat,
+                                endDate.getTime()),
+                        startDate, (int) (Math.round(itemGroup.getModeValue() * 1000)));
+            } else if (readMode == ConstantStr.ON_CHANGE) {
+                timer.schedule(new OpcUaChangeTask(
+                                redisUtil,
+                                opcAsyncTask,
+                                cronId,
+                                cronTaskRegister,
+                                itemGroupDao,
+                                messageNoticeDao,
+                                opcUaClient,
+                                basicItemList,
+                                basicMap,
+                                itemGroup,
+                                dataSource,
+                                timer,
+                                timeFormat,
+                                endDate.getTime()),
+                        startDate, 500);
+            } else if (readMode == ConstantStr.EXCEED_SET_VALUE) {
+                timer.schedule(new OpcUaExceedTask(redisUtil,
+                                opcAsyncTask,
+                                cronId,
+                                cronTaskRegister,
+                                itemGroupDao,
+                                messageNoticeDao,
+                                opcUaClient,
+                                basicItemList,
+                                basicMap,
+                                itemGroup,
+                                dataSource,
+                                timer,
+                                timeFormat,
+                                endDate.getTime()),
+                        startDate, 500);
+            } else {
+                cronTaskRegister.removeCronTask(cronId);
+                throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "目前未适配此种采样模式");
             }
         }
-        Map<String, DataModel> map = new HashMap<>();
-        if (flage) {
-            List<DataModel> dmListByItemList = dataModelDao.getDmListByItemList(allItemList);
-            if (Blank.isNotEmpty(dmListByItemList)) {
-                for (int i = 0; i < allItemList.size(); i++) {
-                    for (DataModel dm : dmListByItemList) {
-                        if (dm.getId().equals(allItemList.get(i).getDataModelId())) {
-                            map.put(allItemList.get(i).getItemReadName(), dm);
-                            break;
+        List<Item> attachItemList = itemGroupDao.getBasicItemList(id, ConstantStr.ATTACH_ATTRIBUTES);
+        List<DataModel> dmAttachItemList = dataModelDao.getDmListByItemList(attachItemList);
+        Map<String, DataModel> attachMap = DataModel.genMap(attachItemList, dmAttachItemList);
+        if (Blank.isNotEmpty(attachItemList)) {
+            OpcUaClient opcUaClient = null;
+            try {
+                opcUaClient = OpcUaUtil.createClient(dataSource);
+                opcUaClient.connect().get();
+                for (Item item : attachItemList) {
+                    NodeId n = new NodeId(item.getNodeIndex(), item.getItemReadName());
+                    String itemName = item.getItemReadName();
+                    DataModel dm = attachMap.get(itemName);
+                    JSONObject jsonObject = new JSONObject();
+                    DataValue dataValue = opcUaClient.readValue(0.0, TimestampsToReturn.Both, n).get();
+                    StatusCode statusCode = dataValue.getStatusCode();
+                    jsonObject.put("quality", statusCode.isGood());
+                    Object value = dataValue.getValue().getValue();
+                    Date time = dataValue.getServerTime().getJavaDate();
+                    String javaType = OpcUaUtil.getValType(dataValue);
+                    jsonObject.put("dataType", javaType);
+                    if (javaType.toLowerCase().equals("boolean")) {
+                        AttachRawData attachRawData = new AttachRawData(
+                                id, dataSource.getId(), itemName, javaType, value.toString(), value.toString(), DateUtil.dateChangeStrYmdhmss(time), new Date()
+                        );
+                        opcAsyncTask.addAttachRawData(attachRawData);
+                    } else {
+                        try {
+                            BigDecimal bigDecimal = JSON.parseObject(value.toString(), BigDecimal.class);
+                            AttachRawData attachRawData = new AttachRawData(
+                                    id, dataSource.getId(), itemName, javaType, bigDecimal.toString(),
+                                    (Blank.isNotEmpty(dm) ? MathUtil.quadricOperation(dm.getMathParameter(), dm.getOperationRule(), bigDecimal) : bigDecimal).toString(),
+                                    DateUtil.dateChangeStrYmdhmss(time), new Date()
+                            );
+                            opcAsyncTask.addAttachRawData(attachRawData);
+                        } catch (Exception e) {
+                            AttachRawData attachRawData = new AttachRawData(
+                                    id, dataSource.getId(), itemName, javaType, value.toString(), value.toString(), DateUtil.dateChangeStrYmdhmss(time), new Date()
+                            );
+                            opcAsyncTask.addAttachRawData(attachRawData);
                         }
                     }
                 }
+            } catch (Exception e) {
+                e.printStackTrace();
+                throw new CustomException(ResultEnum.REQUEST_TIME_OUT.getRespCode(), OpcUaUtil.genException(e.getMessage()));
+            } finally {
+                if (Blank.isNotEmpty(opcUaClient)) {
+                    opcUaClient.disconnect();
+                }
             }
         }
-
-        List<Item> itemList = itemGroupDao.getItemListByGroupId(id);
-        OpcUaClient opcUaClient = null;
-        Timer timer = new Timer();
-        try {
-            opcUaClient = OpcUaUtil.createClient(dataSource);
-            opcUaClient.connect().get();
-        } catch (Exception e) {
-            messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
-                    itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行停止",
-                    OpcUaUtil.genException(e.getMessage()),
-                    ConstantStr.NO_READ));
-
-            itemGroupDao.stopItemGroupById(id, ConstantStr.EXCEPT_STOP_UP);
-            if (Blank.isNotEmpty(opcUaClient)) {
-                opcUaClient.disconnect();
-            }
-            if (Blank.isNotEmpty(timer)) {
-                timer.cancel();
-            }
-            cronTaskRegister.removeCronTask(cronId);
-        }
-        if (readMode == ConstantStr.ON_FREQUENCY) {
-            timer.schedule(new OpcUaFrequencyTask(
-                            redisUtil,
-                            opcAsyncTask,
-                            cronId,
-                            cronTaskRegister,
-                            itemGroupDao,
-                            messageNoticeDao,
-                            opcUaClient,
-                            itemList,
-                            map,
-                            itemGroup,
-                            dataSource,
-                            timer,
-                            timeFormat,
-                            endDate.getTime()),
-                    startDate, (int) (Math.round(itemGroup.getModeValue() * 1000)));
-        } else if (readMode == ConstantStr.ON_CHANGE) {
-            timer.schedule(new OpcUaChangeTask(
-                            redisUtil,
-                            opcAsyncTask,
-                            cronId,
-                            cronTaskRegister,
-                            itemGroupDao,
-                            messageNoticeDao,
-                            opcUaClient,
-                            itemList,
-                            map,
-                            itemGroup,
-                            dataSource,
-                            timer,
-                            timeFormat,
-                            endDate.getTime()),
-                    startDate, 500);
-        } else if (readMode == ConstantStr.EXCEED_SET_VALUE) {
-            timer.schedule(new OpcUaExceedTask(redisUtil,
-                            opcAsyncTask,
-                            cronId,
-                            cronTaskRegister,
-                            itemGroupDao,
-                            messageNoticeDao,
-                            opcUaClient,
-                            itemList,
-                            map,
-                            itemGroup,
-                            dataSource,
-                            timer,
-                            timeFormat,
-                            endDate.getTime()),
-                    startDate, 500);
-        } else {
-            cronTaskRegister.removeCronTask(cronId);
-            throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "目前未适配此种采样模式");
-        }
-
     }
 }

+ 19 - 0
chaunyi_opc/opc_ua/src/main/resources/mapper/ItemGroupDao.xml

@@ -38,6 +38,25 @@
         </if>
     </select>
 
+    <select id="getBasicItemList" resultType="com.example.opc_common.entity.Item">
+        select ti.id,
+               ti.item_group_id,
+               ti.item_name,
+               ti.item_read_name,
+               ti.item_type,
+               ti.node_index,
+               ti.data_type,
+               ti.`describe`,
+               ti.data_model_id,
+               ti.is_driver_item,
+               tdm.operation_rule
+        FROM t_item ti
+                 LEFT JOIN t_data_model tdm ON ti.data_model_id = tdm.id
+        where ti.item_group_id = #{itemGroupId}
+          and ti.item_type = #{itemType}
+        order by is_driver_item DESC, item_name
+    </select>
+
     <update id="runItemGroupById">
         update t_item_group
         set run_state=#{runState},

+ 34 - 0
chaunyi_opc/opc_ua/src/main/resources/mapper/RawDataDao.xml

@@ -12,6 +12,11 @@
         , item_group_id, data_source_id, item_name, data_type, org_data_value, data_value, data_value_time, data_index, value_belong_time, is_record, create_time
     </sql>
 
+    <sql id="attachRawData">
+        id
+        , item_group_id, data_source_id, item_name, data_type, org_data_value, data_value, data_value_time, create_time
+    </sql>
+
     <insert id="addRawData">
         insert into t_raw_data${remainder}
         (item_group_id, data_source_id, item_name, data_type, org_data_value, data_value, data_value_time, data_index,
@@ -33,6 +38,26 @@
                 #{dataValueTime}, #{dataIndex}, #{valueBelongTime}, #{isRecord}, #{createTime})
     </insert>
 
+    <insert id="addAttachRawData">
+        insert into t_attach_raw_data
+        (item_group_id, data_source_id, item_name, data_type, org_data_value, data_value, data_value_time,
+         create_time)
+            VALUE (#{itemGroupId}, #{dataSourceId}, #{itemName}, #{dataType},
+            #{orgDataValue}, #{dataValue},
+            #{dataValueTime}, #{createTime})
+    </insert>
+
+    <update id="updateAttachRawData">
+        update t_attach_raw_data
+        set data_type=#{dataType},
+            org_data_value=#{orgDataValue},
+            data_value=#{dataValue},
+            data_value_time=#{dataValueTime}
+        where item_group_id = #{itemGroupId}
+          and data_source_id = #{dataSourceId}
+          and item_name = #{itemName}
+    </update>
+
     <delete id="delEventRawDataList">
         delete
         from t_event_raw_data
@@ -152,4 +177,13 @@
         </if>
     </select>
 
+    <select id="getAttachRawData" resultType="com.example.opc_common.entity.AttachRawData">
+        select
+        <include refid="attachRawData"/>
+        from t_attach_raw_data
+        where item_group_id=#{itemGroupId}
+        and data_source_id=#{dataSourceId}
+        and item_name=#{itemName}
+    </select>
+
 </mapper>