Bläddra i källkod

Merge branch 'master' of http://116.63.33.55/git/read_opc

FinalYu 1 år sedan
förälder
incheckning
561af69cc8
20 ändrade filer med 961 tillägg och 158 borttagningar
  1. 9 1
      chaunyi_opc/opc_common/src/main/java/com/example/opc_common/entity/DataModel.java
  2. 8 0
      chaunyi_opc/opc_common/src/main/java/com/example/opc_common/entity/Item.java
  3. 3 0
      chaunyi_opc/opc_common/src/main/java/com/example/opc_common/util/ConstantStr.java
  4. 4 4
      chaunyi_opc/opc_da/src/main/java/com/example/opc_da/controller/DataModelController.java
  5. 15 0
      chaunyi_opc/opc_da/src/main/java/com/example/opc_da/controller/ItemGroupController.java
  6. 2 0
      chaunyi_opc/opc_da/src/main/java/com/example/opc_da/dao/DataSourceDao.java
  7. 7 0
      chaunyi_opc/opc_da/src/main/java/com/example/opc_da/dao/ItemGroupDao.java
  8. 2 0
      chaunyi_opc/opc_da/src/main/java/com/example/opc_da/service/ItemGroupService.java
  9. 21 0
      chaunyi_opc/opc_da/src/main/java/com/example/opc_da/service/impl/DataModelServiceImpl.java
  10. 56 1
      chaunyi_opc/opc_da/src/main/java/com/example/opc_da/service/impl/DataSourceServiceImpl.java
  11. 27 7
      chaunyi_opc/opc_da/src/main/java/com/example/opc_da/service/impl/ItemGroupServiceImpl.java
  12. 53 45
      chaunyi_opc/opc_da/src/main/java/com/example/opc_da/task/OpcDaChangeTask.java
  13. 310 0
      chaunyi_opc/opc_da/src/main/java/com/example/opc_da/task/OpcDaChangeTask1.java
  14. 8 19
      chaunyi_opc/opc_da/src/main/java/com/example/opc_da/task/OpcDaExceedTask.java
  15. 84 68
      chaunyi_opc/opc_da/src/main/java/com/example/opc_da/task/OpcDaFrequencyTask.java
  16. 293 0
      chaunyi_opc/opc_da/src/main/java/com/example/opc_da/task/OpcDaFrequencyTask1.java
  17. 1 2
      chaunyi_opc/opc_da/src/main/java/com/example/opc_da/util/OpcDaUtil.java
  18. 7 4
      chaunyi_opc/opc_da/src/main/resources/mapper/DataModelDao.xml
  19. 7 0
      chaunyi_opc/opc_da/src/main/resources/mapper/DataSourceDao.xml
  20. 44 7
      chaunyi_opc/opc_da/src/main/resources/mapper/ItemGroupDao.xml

+ 9 - 1
chaunyi_opc/opc_common/src/main/java/com/example/opc_common/entity/DataModel.java

@@ -1,6 +1,7 @@
 package com.example.opc_common.entity;
 
 import com.example.opc_common.util.Blank;
+import com.example.opc_common.util.ConstantStr;
 import lombok.Data;
 
 import java.io.Serializable;
@@ -18,7 +19,10 @@ public class DataModel implements Serializable {
      * 主键id
      */
     private Integer id;
-
+    /**
+     * 模型类型,1为,替换那个,0位四则运算
+     */
+    private Integer modelType = ConstantStr.MATH_OPERATION;
     /**
      * 数学参数
      */
@@ -28,6 +32,10 @@ public class DataModel implements Serializable {
      */
     private String operationRule;
     /**
+     * 替换值
+     */
+    private String replacingValue;
+    /**
      * 用户id
      */
     private String userId;

+ 8 - 0
chaunyi_opc/opc_common/src/main/java/com/example/opc_common/entity/Item.java

@@ -62,5 +62,13 @@ public class Item implements Serializable {
      * 虚拟数据源名称,返回给前端的
      */
     private String dataSourceName;
+    /**
+     * 附属属性原始值
+     */
+    private String orgDataValue;
+    /**
+     * 附属属性计算值
+     */
+    private String dataValue;
 
 }

+ 3 - 0
chaunyi_opc/opc_common/src/main/java/com/example/opc_common/util/ConstantStr.java

@@ -141,5 +141,8 @@ public class ConstantStr {
     //消息通知状态,0未读,1已读
     public static final Integer NO_READ = 0;
     public static final Integer ALREADY_READ = 1;
+    //
+    public static final Integer MATH_OPERATION = 0;
+    public static final Integer VALUE_REPLACE = 1;
 
 }

+ 4 - 4
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/controller/DataModelController.java

@@ -33,8 +33,8 @@ public class DataModelController {
     @PostMapping("/addDataModel")
     @WebLog(ServerEnum = ServerEnum.CLIENT, ModelEnum = ModelEnum.DATAMODEL, OperationEnum = OperationEnum.ADD)
     public Result addDataModel(@RequestBody DataModel dataModel) {
-        if (Blank.isEmpty(dataModel, dataModel.getRemark(), dataModel.getMathParameter(), dataModel.getOperationRule())) {
-            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "名称不能为空,数学参数和四则混合运算表达式不能为空");
+        if (Blank.isEmpty(dataModel, dataModel.getRemark(), dataModel.getOperationRule())) {
+            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "名称不能为空,四则混合运算表达式不能为空");
         }
         return dataModelService.addDataModel(dataModel);
     }
@@ -48,8 +48,8 @@ public class DataModelController {
     @PostMapping("/updateDataModel")
     @WebLog(ServerEnum = ServerEnum.CLIENT, ModelEnum = ModelEnum.DATAMODEL, OperationEnum = OperationEnum.UPDATE)
     public Result updateDataModel(@RequestBody DataModel dataModel) {
-        if (Blank.isEmpty(dataModel, dataModel.getId(), dataModel.getRemark(), dataModel.getMathParameter(), dataModel.getOperationRule())) {
-            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "名称不能为空,数学参数和四则混合运算表达式不能为空");
+        if (Blank.isEmpty(dataModel, dataModel.getId(), dataModel.getRemark(), dataModel.getOperationRule())) {
+            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "名称不能为空,四则混合运算表达式不能为空");
         }
         return dataModelService.updateDataModel(dataModel);
     }

+ 15 - 0
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/controller/ItemGroupController.java

@@ -159,6 +159,21 @@ public class ItemGroupController {
     }
 
     /**
+     * 通过数据组id和数据项id增加
+     *
+     * @param itemGroup
+     * @return
+     */
+    @PostMapping("/addItemList")
+    @WebLog(ServerEnum = ServerEnum.CLIENT, ModelEnum = ModelEnum.DATAGROUP, OperationEnum = OperationEnum.UPDATE)
+    public Result addItemList(@RequestBody ItemGroup itemGroup) {
+        if (Blank.isEmpty(itemGroup, itemGroup.getId(), itemGroup.getItemList())) {
+            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
+        }
+        return itemGroupService.addItemList(itemGroup);
+    }
+
+    /**
      * 通过id获取数据组的itemList的详细信息
      *
      * @param id

+ 2 - 0
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/dao/DataSourceDao.java

@@ -52,4 +52,6 @@ public interface DataSourceDao {
     DataSourceDriver getDataSourceDriverByid(Integer id);
 
     DataSourceDriver getDataSourceDriverByclsId(String clsId);
+
+    List<DataSource> getDataSourceByclsId(String clsId);
 }

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

@@ -1,5 +1,6 @@
 package com.example.opc_da.dao;
 
+import com.example.opc_common.entity.DataSource;
 import com.example.opc_common.entity.Item;
 import com.example.opc_common.entity.ItemGroup;
 import org.springframework.stereotype.Repository;
@@ -60,4 +61,10 @@ public interface ItemGroupDao {
     List<Item> getAttributesItemList(Integer itemGroupId, Integer itemType);
 
     List<ItemGroup> getAllItemGroup1(String userId, Integer readMode);
+
+    List<ItemGroup> getIgListByDsId(List<DataSource> dataSourceList);
+
+    List<Item> getItemListByGidList(List<ItemGroup> itemGroupList);
+
+    Integer updateItemTypeList(String strArr,String replaceStr);
 }

+ 2 - 0
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/service/ItemGroupService.java

@@ -30,4 +30,6 @@ public interface ItemGroupService {
     Result batchSetExpresses(ItemGroup itemGroup);
 
     Result itemDataQuery(Integer id, Integer valueType, String startTime, String endTime);
+
+    Result addItemList(ItemGroup itemGroup);
 }

+ 21 - 0
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/service/impl/DataModelServiceImpl.java

@@ -6,6 +6,7 @@ import com.example.opc_common.entity.Item;
 import com.example.opc_common.entity.ItemGroup;
 import com.example.opc_common.enums.ResultEnum;
 import com.example.opc_common.util.Blank;
+import com.example.opc_common.util.ConstantStr;
 import com.example.opc_common.util.MathUtil;
 import com.example.opc_common.util.Result;
 import com.example.opc_da.dao.DataModelDao;
@@ -33,6 +34,16 @@ public class DataModelServiceImpl implements DataModelService {
     public synchronized Result addDataModel(DataModel dataModel) {
         String userId = userUtil.getCurrentUserId();
         String remark = dataModel.getRemark();
+        Integer modelType = dataModel.getModelType();
+        if (modelType.equals(ConstantStr.VALUE_REPLACE)) {
+            if (Blank.isEmpty(dataModel.getReplacingValue())) {
+                return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "选择替换类型时,替换值不能为空");
+            }
+        } else {
+            if (Blank.isEmpty(dataModel.getMathParameter())) {
+                return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "数学参数不能为空");
+            }
+        }
         DataModel exitDataModel = dataModelDao.getDataModelByRemark(userId, remark);
         if (Blank.isNotEmpty(exitDataModel)) {
             return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "已经存在此名称的数据模型了,请修改名称");
@@ -49,6 +60,16 @@ public class DataModelServiceImpl implements DataModelService {
         Integer id = dataModel.getId();
         String userId = userUtil.getCurrentUserId();
         String remark = dataModel.getRemark();
+        Integer modelType = dataModel.getModelType();
+        if (modelType.equals(ConstantStr.VALUE_REPLACE)) {
+            if (Blank.isEmpty(dataModel.getReplacingValue())) {
+                return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "选择替换类型时,替换值不能为空");
+            }
+        } else {
+            if (Blank.isEmpty(dataModel.getMathParameter())) {
+                return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "数学参数不能为空");
+            }
+        }
         DataModel exitDataModel = dataModelDao.getDataModelByRemarkNoId(id, userId, remark);
         if (Blank.isNotEmpty(exitDataModel)) {
             return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "已经存在此名称的数据模型了,请修改名称");

+ 56 - 1
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/service/impl/DataSourceServiceImpl.java

@@ -227,7 +227,7 @@ public class DataSourceServiceImpl implements DataSourceService {
             Method getValue = clazz.getMethod("getValue");
             Map<String, String> map = new HashMap<>();
             for (Object o : objects) {
-                map.put(getName.invoke(o).toString(), getValue.invoke(o).toString());
+                map.put(getName.invoke(o).toString(), getValue.invoke(o).toString().toLowerCase());
             }
             return Result.ok(map);
         } catch (NoSuchMethodException e) {
@@ -263,6 +263,33 @@ public class DataSourceServiceImpl implements DataSourceService {
                 Integer id = dataSourceDriver.getId();
                 dataSourceDao.addDriverBasicItemList(id, driverBasicItemList);
             }
+            List<DataSource> dataSourceList = dataSourceDao.getDataSourceByclsId(dataSourceDriver.getClsId());
+            if (Blank.isNotEmpty(dataSourceList)) {
+                List<ItemGroup> itemGroupList = itemGroupDao.getIgListByDsId(dataSourceList);
+                if (Blank.isNotEmpty(itemGroupList)) {
+                    for (ItemGroup itemGroup : itemGroupList) {
+                        Integer runState = itemGroup.getRunState();
+                        if (runState.equals(ConstantStr.START_UP)) {
+                            throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "此数据源配置的数据组,存在处于运行中,不能修改");
+                        }
+                    }
+                    List<Item> itemList = itemGroupDao.getItemListByGidList(itemGroupList);
+                    itemList = OpcDaUtil.genBasicDa(itemList,
+                            Blank.isEmpty(dataSourceDriver) ? null : dataSourceDriver);
+                    if (Blank.isNotEmpty(itemList)) {
+                        String strArr = "";
+                        int size = itemList.size();
+                        for (int i = 0; i < size; i++) {
+                            Item item = itemList.get(i);
+                            strArr += item.getId() + "," + item.getItemType();
+                            if (i != size - 1) {
+                                strArr += "-";
+                            }
+                        }
+                        itemGroupDao.updateItemTypeList(strArr, "-");
+                    }
+                }
+            }
             return Result.ok("新增驱动配置成功");
         } else {
             throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "目前未适配此种类型的驱动");
@@ -286,6 +313,34 @@ public class DataSourceServiceImpl implements DataSourceService {
             if (Blank.isNotEmpty(driverBasicItemList)) {
                 dataSourceDao.addDriverBasicItemList(id, driverBasicItemList);
             }
+            DataSourceDriver newDataSourceDriver = dataSourceDao.getDataSourceDriverByid(dataSourceDriver.getId());
+            List<DataSource> dataSourceList = dataSourceDao.getDataSourceByclsId(clsId);
+            if (Blank.isNotEmpty(dataSourceList)) {
+                List<ItemGroup> itemGroupList = itemGroupDao.getIgListByDsId(dataSourceList);
+                if (Blank.isNotEmpty(itemGroupList)) {
+                    for (ItemGroup itemGroup : itemGroupList) {
+                        Integer runState = itemGroup.getRunState();
+                        if (runState.equals(ConstantStr.START_UP)) {
+                            throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "此数据源配置的数据组,存在处于运行中,不能修改");
+                        }
+                    }
+                    List<Item> itemList = itemGroupDao.getItemListByGidList(itemGroupList);
+                    itemList = OpcDaUtil.genBasicDa(itemList,
+                            Blank.isEmpty(newDataSourceDriver) ? null : newDataSourceDriver);
+                    if (Blank.isNotEmpty(itemList)) {
+                        String strArr = "";
+                        int size = itemList.size();
+                        for (int i = 0; i < size; i++) {
+                            Item item = itemList.get(i);
+                            strArr += item.getId() + "," + item.getItemType();
+                            if (i != size - 1) {
+                                strArr += "-";
+                            }
+                        }
+                        itemGroupDao.updateItemTypeList(strArr, "-");
+                    }
+                }
+            }
             return Result.ok("修改驱动配置成功");
         } else {
             throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "目前未适配此种类型的驱动");

+ 27 - 7
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/service/impl/ItemGroupServiceImpl.java

@@ -107,9 +107,9 @@ public class ItemGroupServiceImpl implements ItemGroupService {
                 DataSourceDriver dataSourceDriver = null;
                 if (isOpcDa) {
                     String clsId = dataSource.getClsId();
-                    dataSourceDriver = dataSourceDao.getDataSourceDriverByclsId(clsId.toUpperCase());
+                    dataSourceDriver = dataSourceDao.getDataSourceDriverByclsId(clsId);
                 }
-                itemGroup.setItemList(OpcDaUtil.genBasicDa(dataSource, itemList,
+                itemGroup.setItemList(OpcDaUtil.genBasicDa(itemList,
                         Blank.isEmpty(dataSourceDriver) ? null : dataSourceDriver));
                 if (readMode == ConstantStr.EXCEED_SET_VALUE) {
                     int count = 0;
@@ -145,8 +145,8 @@ public class ItemGroupServiceImpl implements ItemGroupService {
                 if (Blank.isNotEmpty(itemList)) {
                     if (isOpcDa) {
                         String clsId = dataSource.getClsId();
-                        DataSourceDriver dataSourceDriver = dataSourceDao.getDataSourceDriverByclsId(clsId.toUpperCase());
-                        itemList = OpcDaUtil.genBasicDa(dataSource, itemList,
+                        DataSourceDriver dataSourceDriver = dataSourceDao.getDataSourceDriverByclsId(clsId);
+                        itemList = OpcDaUtil.genBasicDa(itemList,
                                 Blank.isEmpty(dataSourceDriver) ? null : dataSourceDriver);
                     }
                     if (readMode == ConstantStr.EXCEED_SET_VALUE) {
@@ -169,15 +169,15 @@ public class ItemGroupServiceImpl implements ItemGroupService {
                     DataSourceDriver dataSourceDriver = null;
                     if (isOpcDa) {
                         String clsId = dataSource.getClsId();
-                        dataSourceDriver = dataSourceDao.getDataSourceDriverByclsId(clsId.toUpperCase());
-                        itemList = OpcDaUtil.genBasicDa(dataSource, itemList,
+                        dataSourceDriver = dataSourceDao.getDataSourceDriverByclsId(clsId);
+                        itemList = OpcDaUtil.genBasicDa(itemList,
                                 Blank.isEmpty(dataSourceDriver) ? null : dataSourceDriver);
                     }
                     //删除已经删除的项
                     itemGroupDao.delItemNoPub(id, itemList);
                     //得到没有删除的项
                     List<Item> noDelItemList = itemGroupDao.getPublicItemList(id, itemList);
-                    noDelItemList = OpcDaUtil.genBasicDa(dataSource, noDelItemList,
+                    noDelItemList = OpcDaUtil.genBasicDa(noDelItemList,
                             Blank.isEmpty(dataSourceDriver) ? null : dataSourceDriver);
                     if (Blank.isNotEmpty(noDelItemList)) {
                         List<Item> newItemList = new ArrayList<>();
@@ -492,4 +492,24 @@ public class ItemGroupServiceImpl implements ItemGroupService {
         }
         return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "所选数据项是附属属性,不支持,请重新选择数据项");
     }
+
+    @Override
+    public Result addItemList(ItemGroup itemGroup) {
+        Integer id = itemGroup.getId();
+        List<Item> itemList = itemGroup.getItemList();
+        DataSourceDriver dataSourceDriver = null;
+        ItemGroup oldItemGroup = itemGroupDao.getItemGroupById(id);
+        Integer dataSourceId = oldItemGroup.getDataSourceId();
+        DataSource dataSource = dataSourceDao.getDataSourceById(dataSourceId);
+        DataSourceType dataSourceType = dataSourceDao.getDataSourceTypeById(dataSource.getTypeId());
+        Boolean isOpcDa = OpcUtil.isOpcDa(dataSourceType.getDataSourceTypeKey());
+        if (isOpcDa) {
+            String clsId = dataSource.getClsId();
+            dataSourceDriver = dataSourceDao.getDataSourceDriverByclsId(clsId);
+        }
+        itemGroup.setItemList(OpcDaUtil.genBasicDa(itemList,
+                Blank.isEmpty(dataSourceDriver) ? null : dataSourceDriver));
+        itemGroupDao.addItem(id, itemList);
+        return Result.ok("添加数据组成功");
+    }
 }

+ 53 - 45
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/task/OpcDaChangeTask.java

@@ -94,20 +94,9 @@ public class OpcDaChangeTask extends TimerTask {
                             ConstantStr.NO_READ));
                     Timer listenerTimer = new Timer();
                     listenerTimer.schedule(new TimerTask() {
-                        private int sum = 0;
-
                         @Override
                         public void run() {
                             try {
-                                sum++;
-                                if (sum > 3) {
-                                    redisUtil.set(ConstantStr.ITEM_GROUP + id, false);
-                                    messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
-                                            itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接失败",
-                                            "3次服务重新连接失败",
-                                            ConstantStr.NO_READ));
-                                    listenerTimer.cancel();
-                                }
                                 server.connect();
                                 if (Blank.isNotEmpty(server.getServerState()) && server.getServerState().getServerState() == OPCSERVERSTATE.OPC_STATUS_RUNNING) {
                                     messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
@@ -117,16 +106,16 @@ public class OpcDaChangeTask extends TimerTask {
                                     listenerTimer.cancel();
                                 }
                             } catch (Exception e) {
-                                redisUtil.set(ConstantStr.ITEM_GROUP + id, false);
-                                String message = OpcDaUtil.genException(e.getMessage());
-                                messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
-                                        itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接失败",
-                                        message,
-                                        ConstantStr.NO_READ));
-                                listenerTimer.cancel();
+//                                redisUtil.set(ConstantStr.ITEM_GROUP + id, false);
+//                                String message = OpcDaUtil.genException(e.getMessage());
+//                                messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+//                                        itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接失败",
+//                                        message,
+//                                        ConstantStr.NO_READ));
+//                                listenerTimer.cancel();
                             }
                         }
-                    }, 10000);
+                    }, 60000);
                 }
             });
         } catch (Exception e) {
@@ -150,7 +139,6 @@ public class OpcDaChangeTask extends TimerTask {
                     private BigDecimal oldValue;
                     private String sqlCurrentYmdh = "";
                     private Long index = 0L;
-                    private Long timeStamp = 0L;
 
                     @Override
                     public void changed(Item item1, ItemState itemState) {
@@ -163,49 +151,69 @@ public class OpcDaChangeTask extends TimerTask {
                             Object value = val.get("value");
                             //值对应取值的时间
                             Date time = itemState.getTimestamp().getTime();
-                            long time1 = time.getTime();
                             String currentYmdh = DateUtil.dateChangeStr(time, timeFormat);
                             String currentYmdhmss = DateUtil.dateChangeStrYmdhmss(time);
                             if (Blank.isEmpty(sqlCurrentYmdh)) {
                                 sqlCurrentYmdh = currentYmdh;
-                                timeStamp = time1;
                                 redisUtil.set(ConstantStr.VALUE_BELONG_TIME + id, currentYmdh, ConstantStr.TWO_HOUR);
                             } else {
-                                //如果上次时间戳和这次时间戳相等,说明值未发生变化,不需要进行下面的操作
-                                if (timeStamp == time1) {
-                                    return;
-                                }
                                 if (!sqlCurrentYmdh.equals(currentYmdh)) {
                                     //组装相应的原始数据
                                     opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
                                     redisUtil.set(ConstantStr.VALUE_BELONG_TIME + id, currentYmdh, ConstantStr.TWO_HOUR);
                                     sqlCurrentYmdh = currentYmdh;
-                                    timeStamp = time1;
                                 }
                             }
+                            String valueStr = value.toString();
                             if (javaType.toLowerCase().equals("boolean")) {
-                                Boolean dmData = JSON.parseObject(value.toString(), Boolean.class);
-                                CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(dmData), JSON.toJSONString(dmData),
-                                        currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
-                                opcAsyncTask.addCursorRawData(cursorRawData);
-                            } else {
-                                try {
-                                    BigDecimal bigDecimal = JSON.parseObject(value.toString(), BigDecimal.class);
-                                    BigDecimal dmData = Blank.isNotEmpty(dm) ?
-                                            MathUtil.quadricOperation(dm.getMathParameter(), dm.getOperationRule(), bigDecimal) :
-                                            bigDecimal;
-                                    CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
-                                            currentYmdhmss, index, currentYmdh,
-                                            Blank.isEmpty(oldValue) ? ConstantStr.IS_RECORD : MathUtil.isMeetChange(oldValue,
-                                                    dmData, new BigDecimal(modeValue), readModeType), new Date());
+                                Boolean data = JSON.parseObject(valueStr, Boolean.class);
+                                if (Blank.isNotEmpty(dm) && dm.getModelType().equals(ConstantStr.VALUE_REPLACE) && dm.getOperationRule().equals(valueStr)) {
+                                    CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(data), dm.getReplacingValue(),
+                                            currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
                                     opcAsyncTask.addCursorRawData(cursorRawData);
-                                    oldValue = dmData;
-                                } catch (Exception e) {
-                                    String valueStr = value.toString();
-                                    CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, valueStr, "".equals(valueStr) ? ConstantStr.STRING_EMPTY : valueStr,
+                                } else {
+                                    CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(data), JSON.toJSONString(data),
                                             currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
                                     opcAsyncTask.addCursorRawData(cursorRawData);
                                 }
+                            } else {
+                                try {
+                                    BigDecimal bigDecimal = JSON.parseObject(valueStr, BigDecimal.class);
+                                    if (Blank.isNotEmpty(dm) && dm.getModelType().equals(ConstantStr.VALUE_REPLACE) && dm.getOperationRule().equals(valueStr)) {
+                                        CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), dm.getReplacingValue(),
+                                                currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
+                                        opcAsyncTask.addCursorRawData(cursorRawData);
+                                    } else {
+                                        BigDecimal dmData = Blank.isNotEmpty(dm) ?
+                                                MathUtil.quadricOperation(dm.getMathParameter(), dm.getOperationRule(), bigDecimal) :
+                                                bigDecimal;
+                                        CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
+                                                currentYmdhmss, index, currentYmdh,
+                                                Blank.isEmpty(oldValue) ? ConstantStr.IS_RECORD : MathUtil.isMeetChange(oldValue,
+                                                        dmData, new BigDecimal(modeValue), readModeType), new Date());
+                                        opcAsyncTask.addCursorRawData(cursorRawData);
+                                        oldValue = dmData;
+                                    }
+//                                    BigDecimal dmData = Blank.isNotEmpty(dm) ?
+//                                            MathUtil.quadricOperation(dm.getMathParameter(), dm.getOperationRule(), bigDecimal) :
+//                                            bigDecimal;
+//                                    CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
+//                                            currentYmdhmss, index, currentYmdh,
+//                                            Blank.isEmpty(oldValue) ? ConstantStr.IS_RECORD : MathUtil.isMeetChange(oldValue,
+//                                                    dmData, new BigDecimal(modeValue), readModeType), new Date());
+//                                    opcAsyncTask.addCursorRawData(cursorRawData);
+//                                    oldValue = dmData;
+                                } catch (Exception e) {
+                                    if (Blank.isNotEmpty(dm) && dm.getModelType().equals(ConstantStr.VALUE_REPLACE) && dm.getOperationRule().equals(valueStr)) {
+                                        CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, valueStr, dm.getReplacingValue(),
+                                                currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
+                                        opcAsyncTask.addCursorRawData(cursorRawData);
+                                    } else {
+                                        CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, valueStr, "".equals(valueStr) ? ConstantStr.STRING_EMPTY : valueStr,
+                                                currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
+                                        opcAsyncTask.addCursorRawData(cursorRawData);
+                                    }
+                                }
                             }
                         } catch (Exception e) {
                             //执行组装数据库的原始数据

+ 310 - 0
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/task/OpcDaChangeTask1.java

@@ -0,0 +1,310 @@
+package com.example.opc_da.task;
+
+import com.alibaba.fastjson.JSON;
+import com.example.opc_common.entity.*;
+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_da.dao.ItemGroupDao;
+import com.example.opc_da.dao.MessageNoticeDao;
+import com.example.opc_da.dynamicSchedule.CronTaskRegister;
+import com.example.opc_da.util.OpcDaUtil;
+import com.example.opc_da.util.RedisUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.openscada.opc.dcom.da.OPCSERVERSTATE;
+import org.openscada.opc.lib.da.Item;
+import org.openscada.opc.lib.da.*;
+
+import java.math.BigDecimal;
+import java.util.*;
+
+@Slf4j
+public class OpcDaChangeTask1 extends TimerTask {
+
+    private final RedisUtil redisUtil;
+
+    private final OpcAsyncTask opcAsyncTask;
+
+    private final String cronId;
+
+    private final CronTaskRegister cronTaskRegister;
+
+    private final ItemGroupDao itemGroupDao;
+
+    private final MessageNoticeDao messageNoticeDao;
+
+    private final Timer timer;
+
+    private final ItemGroup itemGroup;
+
+    private final DataSource dataSource;
+
+    private final Map<String, DataModel> map;
+
+    private final List<com.example.opc_common.entity.Item> itemList;
+
+    private final String timeFormat;
+
+    private final Long endTime;
+
+    public OpcDaChangeTask1(RedisUtil redisUtil,
+                            OpcAsyncTask opcAsyncTask,
+                            String cronId,
+                            CronTaskRegister cronTaskRegister,
+                            ItemGroupDao itemGroupDao,
+                            MessageNoticeDao messageNoticeDao,
+                            Timer timer,
+                            ItemGroup itemGroup,
+                            DataSource dataSource,
+                            Map<String, DataModel> map,
+                            List<com.example.opc_common.entity.Item> itemList,
+                            String timeFormat,
+                            Long endTime) {
+        this.redisUtil = redisUtil;
+        this.opcAsyncTask = opcAsyncTask;
+        this.cronId = cronId;
+        this.cronTaskRegister = cronTaskRegister;
+        this.itemGroupDao = itemGroupDao;
+        this.messageNoticeDao = messageNoticeDao;
+        this.timer = timer;
+        this.itemGroup = itemGroup;
+        this.dataSource = dataSource;
+        this.map = map;
+        this.itemList = itemList;
+        this.timeFormat = timeFormat;
+        this.endTime = endTime;
+    }
+
+    @Override
+    public void run() {
+        Server server = OpcDaUtil.createServer(dataSource);
+        Integer id = itemGroup.getId();
+        Integer readModeType = itemGroup.getReadModeType();
+        Double modeValue = itemGroup.getModeValue();
+        Integer dataSourceId = dataSource.getId();
+        SyncAccess access = null;
+        try {
+            server.connect();
+            server.addStateListener(connected -> {
+                if (!connected) {
+                    messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                            itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务断开",
+                            "服务断开,马山进行重新连接",
+                            ConstantStr.NO_READ));
+                    Timer listenerTimer = new Timer();
+                    listenerTimer.schedule(new TimerTask() {
+                        private int sum = 0;
+
+                        @Override
+                        public void run() {
+                            try {
+                                sum++;
+                                if (sum > 3) {
+                                    redisUtil.set(ConstantStr.ITEM_GROUP + id, false);
+                                    messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                                            itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接失败",
+                                            "3次服务重新连接失败",
+                                            ConstantStr.NO_READ));
+                                    listenerTimer.cancel();
+                                }
+                                server.connect();
+                                if (Blank.isNotEmpty(server.getServerState()) && server.getServerState().getServerState() == OPCSERVERSTATE.OPC_STATUS_RUNNING) {
+                                    messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                                            itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接成功",
+                                            "服务重新连接成功",
+                                            ConstantStr.NO_READ));
+                                    listenerTimer.cancel();
+                                }
+                            } catch (Exception e) {
+                                redisUtil.set(ConstantStr.ITEM_GROUP + id, false);
+                                String message = OpcDaUtil.genException(e.getMessage());
+                                messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                                        itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接失败",
+                                        message,
+                                        ConstantStr.NO_READ));
+                                listenerTimer.cancel();
+                            }
+                        }
+                    }, 10000);
+                }
+            });
+        } catch (Exception e) {
+            String message = OpcDaUtil.genException(e.getMessage());
+            messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                    itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行失败",
+                    message,
+                    ConstantStr.NO_READ));
+            if (Blank.isNotEmpty(server)) {
+                server.dispose();
+            }
+            itemGroupDao.stopItemGroupById(itemGroup.getId(), ConstantStr.EXCEPT_STOP_UP);
+            cronTaskRegister.removeCronTask(cronId);
+            timer.cancel();
+        }
+        try {
+            access = new SyncAccess(server, 500);
+            for (com.example.opc_common.entity.Item item : itemList) {
+                String itemId = item.getItemReadName();
+                access.addItem(itemId, new DataCallback() {
+                    private BigDecimal oldValue;
+                    private String sqlCurrentYmdh = "";
+                    private Long index = 0L;
+                    private Long timeStamp = 0L;
+
+                    @Override
+                    public void changed(Item item1, ItemState itemState) {
+                        index++;
+                        try {
+                            Map<String, Object> val = OpcDaUtil.getVal(itemState.getValue());
+                            DataModel dm = map.get(itemId);
+                            //读取的值
+                            String javaType = String.valueOf(val.get("javaType"));
+                            Object value = val.get("value");
+                            //值对应取值的时间
+                            Date time = itemState.getTimestamp().getTime();
+                            long time1 = time.getTime();
+                            String currentYmdh = DateUtil.dateChangeStr(time, timeFormat);
+                            String currentYmdhmss = DateUtil.dateChangeStrYmdhmss(time);
+                            if (Blank.isEmpty(sqlCurrentYmdh)) {
+                                sqlCurrentYmdh = currentYmdh;
+                                timeStamp = time1;
+                                redisUtil.set(ConstantStr.VALUE_BELONG_TIME + id, currentYmdh, ConstantStr.TWO_HOUR);
+                            } else {
+                                //如果上次时间戳和这次时间戳相等,说明值未发生变化,不需要进行下面的操作
+                                if (timeStamp == time1) {
+                                    return;
+                                }
+                                if (!sqlCurrentYmdh.equals(currentYmdh)) {
+                                    //组装相应的原始数据
+                                    opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
+                                    redisUtil.set(ConstantStr.VALUE_BELONG_TIME + id, currentYmdh, ConstantStr.TWO_HOUR);
+                                    sqlCurrentYmdh = currentYmdh;
+                                    timeStamp = time1;
+                                }
+                            }
+                            if (javaType.toLowerCase().equals("boolean")) {
+                                Boolean dmData = JSON.parseObject(value.toString(), Boolean.class);
+                                CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(dmData), JSON.toJSONString(dmData),
+                                        currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
+                                opcAsyncTask.addCursorRawData(cursorRawData);
+                            } else {
+                                try {
+                                    BigDecimal bigDecimal = JSON.parseObject(value.toString(), BigDecimal.class);
+                                    BigDecimal dmData = Blank.isNotEmpty(dm) ?
+                                            MathUtil.quadricOperation(dm.getMathParameter(), dm.getOperationRule(), bigDecimal) :
+                                            bigDecimal;
+                                    CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
+                                            currentYmdhmss, index, currentYmdh,
+                                            Blank.isEmpty(oldValue) ? ConstantStr.IS_RECORD : MathUtil.isMeetChange(oldValue,
+                                                    dmData, new BigDecimal(modeValue), readModeType), new Date());
+                                    opcAsyncTask.addCursorRawData(cursorRawData);
+                                    oldValue = dmData;
+                                } catch (Exception e) {
+                                    String valueStr = value.toString();
+                                    CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, valueStr, "".equals(valueStr) ? ConstantStr.STRING_EMPTY : valueStr,
+                                            currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
+                                    opcAsyncTask.addCursorRawData(cursorRawData);
+                                }
+                            }
+                        } catch (Exception e) {
+                            //执行组装数据库的原始数据
+                            opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
+                            redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
+
+                            messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                                    itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行停止",
+                                    e.getMessage(),
+                                    ConstantStr.NO_READ));
+                            if (Blank.isNotEmpty(server)) {
+                                server.dispose();
+                            }
+                            itemGroupDao.stopItemGroupById(id, ConstantStr.EXCEPT_STOP_UP);
+                            cronTaskRegister.removeCronTask(cronId);
+                            timer.cancel();
+                        }
+                    }
+                });
+            }
+            access.bind();
+            while (true) {
+                Boolean flage = (Boolean) redisUtil.get(ConstantStr.ITEM_GROUP + id);
+                if (Blank.isEmpty(flage)) {
+                    flage = false;
+                }
+                if (!access.isActive()) {
+                    //执行组装数据库的数据,以及生成驱动报表
+                    String sqlCurrentYmdh = String.valueOf(redisUtil.get(ConstantStr.VALUE_BELONG_TIME + id));
+                    opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
+                    redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
+                    messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                            itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行失败",
+                            "服务异常停止了",
+                            ConstantStr.NO_READ));
+                    if (Blank.isNotEmpty(access)) {
+                        access.clear();
+                    }
+                    if (Blank.isNotEmpty(server)) {
+                        server.dispose();
+                    }
+                    itemGroupDao.stopItemGroupById(id, ConstantStr.EXCEPT_STOP_UP);
+                    cronTaskRegister.removeCronTask(cronId);
+                    redisUtil.del(ConstantStr.ITEM_GROUP + id);
+                    timer.cancel();
+                    break;
+                }
+                if (!flage) {
+                    //执行组装数据库的数据,以及生成驱动报表
+                    String sqlCurrentYmdh = String.valueOf(redisUtil.get(ConstantStr.VALUE_BELONG_TIME + id));
+                    opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
+                    redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
+                    if (Blank.isNotEmpty(access)) {
+                        access.clear();
+                    }
+                    if (Blank.isNotEmpty(server)) {
+                        server.dispose();
+                    }
+                    cronTaskRegister.removeCronTask(cronId);
+                    redisUtil.del(ConstantStr.ITEM_GROUP + id);
+                    timer.cancel();
+                    break;
+                }
+                if (System.currentTimeMillis() >= endTime) {
+                    //执行组装数据库的数据,以及生成驱动报表
+                    String sqlCurrentYmdh = String.valueOf(redisUtil.get(ConstantStr.VALUE_BELONG_TIME + id));
+                    opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
+                    redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
+
+                    if (Blank.isNotEmpty(access)) {
+                        access.clear();
+                    }
+                    if (Blank.isNotEmpty(server)) {
+                        server.dispose();
+                    }
+                    timer.cancel();
+                    break;
+                }
+            }
+        } catch (Exception e) {
+            Boolean flage = (Boolean) redisUtil.get(ConstantStr.ITEM_GROUP + id);
+            if (Blank.isEmpty(flage)) {
+                flage = false;
+            }
+            if (!flage) {
+                //执行组装数据库的数据,以及生成驱动报表
+                String sqlCurrentYmdh = String.valueOf(redisUtil.get(ConstantStr.VALUE_BELONG_TIME + id));
+                opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
+                redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
+                if (Blank.isNotEmpty(access)) {
+                    access.clear();
+                }
+                if (Blank.isNotEmpty(server)) {
+                    server.dispose();
+                }
+                itemGroupDao.stopItemGroupById(itemGroup.getId(), ConstantStr.EXCEPT_STOP_UP);
+                cronTaskRegister.removeCronTask(cronId);
+                timer.cancel();
+            }
+        }
+    }
+}

+ 8 - 19
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/task/OpcDaExceedTask.java

@@ -92,20 +92,9 @@ public class OpcDaExceedTask extends TimerTask {
                             ConstantStr.NO_READ));
                     Timer listenerTimer = new Timer();
                     listenerTimer.schedule(new TimerTask() {
-                        private int sum = 0;
-
                         @Override
                         public void run() {
                             try {
-                                sum++;
-                                if (sum > 3) {
-                                    redisUtil.set(ConstantStr.ITEM_GROUP + id, false);
-                                    messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
-                                            itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接失败",
-                                            "3次服务重新连接失败",
-                                            ConstantStr.NO_READ));
-                                    listenerTimer.cancel();
-                                }
                                 server.connect();
                                 if (Blank.isNotEmpty(server.getServerState()) && server.getServerState().getServerState() == OPCSERVERSTATE.OPC_STATUS_RUNNING) {
                                     messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
@@ -115,16 +104,16 @@ public class OpcDaExceedTask extends TimerTask {
                                     listenerTimer.cancel();
                                 }
                             } catch (Exception e) {
-                                redisUtil.set(ConstantStr.ITEM_GROUP + id, false);
-                                String message = OpcDaUtil.genException(e.getMessage());
-                                messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
-                                        itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接失败",
-                                        message,
-                                        ConstantStr.NO_READ));
-                                listenerTimer.cancel();
+//                                redisUtil.set(ConstantStr.ITEM_GROUP + id, false);
+//                                String message = OpcDaUtil.genException(e.getMessage());
+//                                messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+//                                        itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接失败",
+//                                        message,
+//                                        ConstantStr.NO_READ));
+//                                listenerTimer.cancel();
                             }
                         }
-                    }, 10000);
+                    }, 60000);
                 }
             });
         }catch (Exception e){

+ 84 - 68
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/task/OpcDaFrequencyTask.java

@@ -12,7 +12,6 @@ import com.example.opc_da.dynamicSchedule.CronTaskRegister;
 import com.example.opc_da.util.OpcDaUtil;
 import com.example.opc_da.util.RedisUtil;
 import lombok.extern.slf4j.Slf4j;
-import org.jinterop.dcom.common.JIException;
 import org.openscada.opc.dcom.da.OPCSERVERSTATE;
 import org.openscada.opc.lib.da.Item;
 import org.openscada.opc.lib.da.*;
@@ -82,7 +81,7 @@ public class OpcDaFrequencyTask extends TimerTask {
         Server server = OpcDaUtil.createServer(dataSource);
         Integer id = itemGroup.getId();
         Integer dataSourceId = dataSource.getId();
-        Timer chTimer = new Timer();
+        SyncAccess access = null;
         try {
             server.connect();
             server.addStateListener(connected -> {
@@ -93,20 +92,9 @@ public class OpcDaFrequencyTask extends TimerTask {
                             ConstantStr.NO_READ));
                     Timer listenerTimer = new Timer();
                     listenerTimer.schedule(new TimerTask() {
-                        private int sum = 0;
-
                         @Override
                         public void run() {
                             try {
-                                sum++;
-                                if (sum > 3) {
-                                    redisUtil.set(ConstantStr.ITEM_GROUP + id, false);
-                                    messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
-                                            itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接失败",
-                                            "3次服务重新连接失败",
-                                            ConstantStr.NO_READ));
-                                    listenerTimer.cancel();
-                                }
                                 server.connect();
                                 if (Blank.isNotEmpty(server.getServerState()) && server.getServerState().getServerState() == OPCSERVERSTATE.OPC_STATUS_RUNNING) {
                                     messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
@@ -116,16 +104,16 @@ public class OpcDaFrequencyTask extends TimerTask {
                                     listenerTimer.cancel();
                                 }
                             } catch (Exception e) {
-                                redisUtil.set(ConstantStr.ITEM_GROUP + id, false);
-                                String message = OpcDaUtil.genException(e.getMessage());
-                                messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
-                                        itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接失败",
-                                        message,
-                                        ConstantStr.NO_READ));
-                                listenerTimer.cancel();
+//                                redisUtil.set(ConstantStr.ITEM_GROUP + id, false);
+//                                String message = OpcDaUtil.genException(e.getMessage());
+//                                messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+//                                        itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接失败",
+//                                        message,
+//                                        ConstantStr.NO_READ));
+//                                listenerTimer.cancel();
                             }
                         }
-                    }, 10000);
+                    }, 60000);
                 }
             });
         } catch (Exception e) {
@@ -139,37 +127,21 @@ public class OpcDaFrequencyTask extends TimerTask {
             }
             itemGroupDao.stopItemGroupById(itemGroup.getId(), ConstantStr.EXCEPT_STOP_UP);
             cronTaskRegister.removeCronTask(cronId);
-            chTimer.cancel();
             timer.cancel();
         }
         try {
-            Group group = server.addGroup();
-            List<String> itemStrList = new ArrayList<>();
+            access = new SyncAccess(server, (int) (Math.round(itemGroup.getModeValue() * 1000)));
+//            AccessBase access = new Async20Access(server, (int) (itemGroup.getModeValue() * 1000), true);
             for (com.example.opc_common.entity.Item item : itemList) {
-                itemStrList.add(item.getItemReadName());
-            }
-            String[] items = itemStrList.toArray(new String[]{});
-            Map<String, Item> itemResult = group.addItems(items);
-            Set itemSet = new HashSet(itemResult.values());
-            org.openscada.opc.lib.da.Item[] itemArr = new org.openscada.opc.lib.da.Item[itemSet.size()];
-            itemSet.toArray(itemArr);
+                String itemId = item.getItemReadName();
+                access.addItem(itemId, new DataCallback() {
 
-            chTimer.schedule(new TimerTask() {
-                private String sqlCurrentYmdh = "";
-                private Long index = 0L;
+                    private String sqlCurrentYmdh = "";
+                    private Long index = 0L;
 
-                @Override
-                public void run() {
-                    index++;
-                    Map<Item, ItemState> resultMap = null;
-                    try {
-                        resultMap = group.read(true, itemArr);
-                    } catch (JIException e) {
-                        throw new RuntimeException(e);
-                    }
-                    for (Item item : resultMap.keySet()) {
-                        String itemId = item.getId();
-                        ItemState itemState = resultMap.get(item);
+                    @Override
+                    public void changed(Item item1, ItemState itemState) {
+                        index++;
                         try {
                             Map<String, Object> val = OpcDaUtil.getVal(itemState.getValue());
                             DataModel dm = map.get(itemId);
@@ -191,29 +163,47 @@ public class OpcDaFrequencyTask extends TimerTask {
                                     sqlCurrentYmdh = currentYmdh;
                                 }
                             }
+                            String valueStr = value.toString();
                             if (javaType.toLowerCase().equals("boolean")) {
                                 //存数据
-                                Boolean dmData = JSON.parseObject(value.toString(), Boolean.class);
-                                CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(dmData), JSON.toJSONString(dmData),
-                                        currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
-                                opcAsyncTask.addCursorRawData(cursorRawData);
-                            } else {
-                                try {
-                                    BigDecimal bigDecimal = JSON.parseObject(value.toString(), BigDecimal.class);
-                                    BigDecimal dmData = Blank.isNotEmpty(dm) ?
-                                            MathUtil.quadricOperation(dm.getMathParameter(), dm.getOperationRule(), bigDecimal) :
-                                            bigDecimal;
-                                    CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
+                                Boolean data = JSON.parseObject(valueStr, Boolean.class);
+                                if (Blank.isNotEmpty(dm) && dm.getModelType().equals(ConstantStr.VALUE_REPLACE) && dm.getOperationRule().equals(valueStr)) {
+                                    CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(data), dm.getReplacingValue(),
                                             currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
                                     opcAsyncTask.addCursorRawData(cursorRawData);
-                                } catch (Exception e) {
-                                    String valueStr = value.toString();
-                                    CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, valueStr, "".equals(valueStr) ? ConstantStr.STRING_EMPTY : valueStr,
+                                } else {
+                                    CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(data), JSON.toJSONString(data),
                                             currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
                                     opcAsyncTask.addCursorRawData(cursorRawData);
                                 }
+                            } else {
+                                try {
+                                    BigDecimal bigDecimal = JSON.parseObject(valueStr, BigDecimal.class);
+                                    if (Blank.isNotEmpty(dm) && dm.getModelType().equals(ConstantStr.VALUE_REPLACE) && dm.getOperationRule().equals(valueStr)) {
+                                        CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), dm.getReplacingValue(),
+                                                currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
+                                        opcAsyncTask.addCursorRawData(cursorRawData);
+                                    } else {
+                                        BigDecimal dmData = Blank.isNotEmpty(dm) ?
+                                                MathUtil.quadricOperation(dm.getMathParameter(), dm.getOperationRule(), bigDecimal) :
+                                                bigDecimal;
+                                        CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
+                                                currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
+                                        opcAsyncTask.addCursorRawData(cursorRawData);
+                                    }
+                                } catch (Exception e) {
+                                    if (Blank.isNotEmpty(dm) && dm.getModelType().equals(ConstantStr.VALUE_REPLACE) && dm.getOperationRule().equals(valueStr)) {
+                                        CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, valueStr, dm.getReplacingValue(),
+                                                currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
+                                        opcAsyncTask.addCursorRawData(cursorRawData);
+                                    } else {
+                                        CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, valueStr, "".equals(valueStr) ? ConstantStr.STRING_EMPTY : valueStr,
+                                                currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
+                                        opcAsyncTask.addCursorRawData(cursorRawData);
+                                    }
+                                }
                             }
-                        } catch (JIException e) {
+                        } catch (Exception e) {
                             //执行组装数据库的数据,以及生成驱动报表
                             opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
                             redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
@@ -228,29 +218,51 @@ public class OpcDaFrequencyTask extends TimerTask {
                             itemGroupDao.stopItemGroupById(id, ConstantStr.EXCEPT_STOP_UP);
                             cronTaskRegister.removeCronTask(cronId);
                             redisUtil.del(ConstantStr.ITEM_GROUP + id);
-                            chTimer.cancel();
                             timer.cancel();
                         }
                     }
-                }
-            }, new Date(), (int) (Math.round(itemGroup.getModeValue() * 1000)));
-
+                });
+            }
+            access.bind();
             while (true) {
                 Boolean flage = (Boolean) redisUtil.get(ConstantStr.ITEM_GROUP + id);
                 if (Blank.isEmpty(flage)) {
                     flage = false;
                 }
+                if (!access.isActive()) {
+                    //执行组装数据库的数据,以及生成驱动报表
+                    String sqlCurrentYmdh = String.valueOf(redisUtil.get(ConstantStr.VALUE_BELONG_TIME + id));
+                    opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
+                    redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
+                    messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                            itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行失败",
+                            "服务异常停止了",
+                            ConstantStr.NO_READ));
+                    if (Blank.isNotEmpty(access)) {
+                        access.clear();
+                    }
+                    if (Blank.isNotEmpty(server)) {
+                        server.dispose();
+                    }
+                    itemGroupDao.stopItemGroupById(id, ConstantStr.EXCEPT_STOP_UP);
+                    cronTaskRegister.removeCronTask(cronId);
+                    redisUtil.del(ConstantStr.ITEM_GROUP + id);
+                    timer.cancel();
+                    break;
+                }
                 if (!flage) {
                     //执行组装数据库的数据,以及生成驱动报表
                     String sqlCurrentYmdh = String.valueOf(redisUtil.get(ConstantStr.VALUE_BELONG_TIME + id));
                     opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
                     redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
+                    if (Blank.isNotEmpty(access)) {
+                        access.clear();
+                    }
                     if (Blank.isNotEmpty(server)) {
                         server.dispose();
                     }
                     cronTaskRegister.removeCronTask(cronId);
                     redisUtil.del(ConstantStr.ITEM_GROUP + id);
-                    chTimer.cancel();
                     timer.cancel();
                     break;
                 }
@@ -260,10 +272,12 @@ public class OpcDaFrequencyTask extends TimerTask {
                     opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
                     redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
 
+                    if (Blank.isNotEmpty(access)) {
+                        access.clear();
+                    }
                     if (Blank.isNotEmpty(server)) {
                         server.dispose();
                     }
-                    chTimer.cancel();
                     timer.cancel();
                     break;
                 }
@@ -278,12 +292,14 @@ public class OpcDaFrequencyTask extends TimerTask {
                 String sqlCurrentYmdh = String.valueOf(redisUtil.get(ConstantStr.VALUE_BELONG_TIME + id));
                 opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
                 redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
+                if (Blank.isNotEmpty(access)) {
+                    access.clear();
+                }
                 if (Blank.isNotEmpty(server)) {
                     server.dispose();
                 }
                 itemGroupDao.stopItemGroupById(itemGroup.getId(), ConstantStr.EXCEPT_STOP_UP);
                 cronTaskRegister.removeCronTask(cronId);
-                chTimer.cancel();
                 timer.cancel();
             }
         }

+ 293 - 0
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/task/OpcDaFrequencyTask1.java

@@ -0,0 +1,293 @@
+package com.example.opc_da.task;
+
+import com.alibaba.fastjson.JSON;
+import com.example.opc_common.entity.*;
+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_da.dao.ItemGroupDao;
+import com.example.opc_da.dao.MessageNoticeDao;
+import com.example.opc_da.dynamicSchedule.CronTaskRegister;
+import com.example.opc_da.util.OpcDaUtil;
+import com.example.opc_da.util.RedisUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.jinterop.dcom.common.JIException;
+import org.openscada.opc.dcom.da.OPCSERVERSTATE;
+import org.openscada.opc.lib.da.Group;
+import org.openscada.opc.lib.da.Item;
+import org.openscada.opc.lib.da.ItemState;
+import org.openscada.opc.lib.da.Server;
+
+import java.math.BigDecimal;
+import java.util.*;
+
+@Slf4j
+public class OpcDaFrequencyTask1 extends TimerTask {
+
+    private final RedisUtil redisUtil;
+
+    private final OpcAsyncTask opcAsyncTask;
+
+    private final String cronId;
+
+    private final CronTaskRegister cronTaskRegister;
+
+    private final ItemGroupDao itemGroupDao;
+
+    private final MessageNoticeDao messageNoticeDao;
+
+    private final Timer timer;
+
+    private final ItemGroup itemGroup;
+
+    private final DataSource dataSource;
+
+    private final Map<String, DataModel> map;
+
+    private final List<com.example.opc_common.entity.Item> itemList;
+
+    private final String timeFormat;
+
+    private final Long endTime;
+
+    public OpcDaFrequencyTask1(RedisUtil redisUtil,
+                               OpcAsyncTask opcAsyncTask,
+                               String cronId,
+                               CronTaskRegister cronTaskRegister,
+                               ItemGroupDao itemGroupDao,
+                               MessageNoticeDao messageNoticeDao,
+                               Timer timer,
+                               ItemGroup itemGroup,
+                               DataSource dataSource,
+                               Map<String, DataModel> map,
+                               List<com.example.opc_common.entity.Item> itemList,
+                               String timeFormat,
+                               Long endTime) {
+        this.redisUtil = redisUtil;
+        this.opcAsyncTask = opcAsyncTask;
+        this.cronId = cronId;
+        this.cronTaskRegister = cronTaskRegister;
+        this.itemGroupDao = itemGroupDao;
+        this.messageNoticeDao = messageNoticeDao;
+        this.timer = timer;
+        this.itemGroup = itemGroup;
+        this.dataSource = dataSource;
+        this.map = map;
+        this.itemList = itemList;
+        this.timeFormat = timeFormat;
+        this.endTime = endTime;
+    }
+
+    @Override
+    public void run() {
+        Server server = OpcDaUtil.createServer(dataSource);
+        Integer id = itemGroup.getId();
+        Integer dataSourceId = dataSource.getId();
+        Timer chTimer = new Timer();
+        try {
+            server.connect();
+            server.addStateListener(connected -> {
+                if (!connected) {
+                    messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                            itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务断开",
+                            "服务断开,马山进行重新连接",
+                            ConstantStr.NO_READ));
+                    Timer listenerTimer = new Timer();
+                    listenerTimer.schedule(new TimerTask() {
+                        private int sum = 0;
+
+                        @Override
+                        public void run() {
+                            try {
+                                sum++;
+                                if (sum > 3) {
+                                    redisUtil.set(ConstantStr.ITEM_GROUP + id, false);
+                                    messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                                            itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接失败",
+                                            "3次服务重新连接失败",
+                                            ConstantStr.NO_READ));
+                                    listenerTimer.cancel();
+                                }
+                                server.connect();
+                                if (Blank.isNotEmpty(server.getServerState()) && server.getServerState().getServerState() == OPCSERVERSTATE.OPC_STATUS_RUNNING) {
+                                    messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                                            itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接成功",
+                                            "服务重新连接成功",
+                                            ConstantStr.NO_READ));
+                                    listenerTimer.cancel();
+                                }
+                            } catch (Exception e) {
+                                redisUtil.set(ConstantStr.ITEM_GROUP + id, false);
+                                String message = OpcDaUtil.genException(e.getMessage());
+                                messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                                        itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接失败",
+                                        message,
+                                        ConstantStr.NO_READ));
+                                listenerTimer.cancel();
+                            }
+                        }
+                    }, 10000);
+                }
+            });
+        } catch (Exception e) {
+            String message = OpcDaUtil.genException(e.getMessage());
+            messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                    itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行失败",
+                    message,
+                    ConstantStr.NO_READ));
+            if (Blank.isNotEmpty(server)) {
+                server.dispose();
+            }
+            itemGroupDao.stopItemGroupById(itemGroup.getId(), ConstantStr.EXCEPT_STOP_UP);
+            cronTaskRegister.removeCronTask(cronId);
+            chTimer.cancel();
+            timer.cancel();
+        }
+        try {
+            Group group = server.addGroup();
+            List<String> itemStrList = new ArrayList<>();
+            for (com.example.opc_common.entity.Item item : itemList) {
+                itemStrList.add(item.getItemReadName());
+            }
+            String[] items = itemStrList.toArray(new String[]{});
+            Map<String, Item> itemResult = group.addItems(items);
+            Set itemSet = new HashSet(itemResult.values());
+            Item[] itemArr = new Item[itemSet.size()];
+            itemSet.toArray(itemArr);
+
+            chTimer.schedule(new TimerTask() {
+                private String sqlCurrentYmdh = "";
+                private Long index = 0L;
+
+                @Override
+                public void run() {
+                    index++;
+                    Map<Item, ItemState> resultMap = null;
+                    try {
+                        resultMap = group.read(true, itemArr);
+                    } catch (JIException e) {
+                        throw new RuntimeException(e);
+                    }
+                    for (Item item : resultMap.keySet()) {
+                        String itemId = item.getId();
+                        ItemState itemState = resultMap.get(item);
+                        try {
+                            Map<String, Object> val = OpcDaUtil.getVal(itemState.getValue());
+                            DataModel dm = map.get(itemId);
+                            //读取的值
+                            String javaType = String.valueOf(val.get("javaType"));
+                            Object value = val.get("value");
+                            //值对应取值的时间
+                            Date time = itemState.getTimestamp().getTime();
+                            String currentYmdh = DateUtil.dateChangeStr(time, timeFormat);
+                            String currentYmdhmss = DateUtil.dateChangeStrYmdhmss(time);
+                            if (Blank.isEmpty(sqlCurrentYmdh)) {
+                                sqlCurrentYmdh = currentYmdh;
+                                redisUtil.set(ConstantStr.VALUE_BELONG_TIME + id, currentYmdh, ConstantStr.TWO_HOUR);
+                            } else {
+                                if (!sqlCurrentYmdh.equals(currentYmdh)) {
+                                    //组装相应的原始数据
+                                    opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
+                                    redisUtil.set(ConstantStr.VALUE_BELONG_TIME + id, currentYmdh, ConstantStr.TWO_HOUR);
+                                    sqlCurrentYmdh = currentYmdh;
+                                }
+                            }
+                            if (javaType.toLowerCase().equals("boolean")) {
+                                //存数据
+                                Boolean dmData = JSON.parseObject(value.toString(), Boolean.class);
+                                CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(dmData), JSON.toJSONString(dmData),
+                                        currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
+                                opcAsyncTask.addCursorRawData(cursorRawData);
+                            } else {
+                                try {
+                                    BigDecimal bigDecimal = JSON.parseObject(value.toString(), BigDecimal.class);
+                                    BigDecimal dmData = Blank.isNotEmpty(dm) ?
+                                            MathUtil.quadricOperation(dm.getMathParameter(), dm.getOperationRule(), bigDecimal) :
+                                            bigDecimal;
+                                    CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
+                                            currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
+                                    opcAsyncTask.addCursorRawData(cursorRawData);
+                                } catch (Exception e) {
+                                    String valueStr = value.toString();
+                                    CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, valueStr, "".equals(valueStr) ? ConstantStr.STRING_EMPTY : valueStr,
+                                            currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
+                                    opcAsyncTask.addCursorRawData(cursorRawData);
+                                }
+                            }
+                        } catch (JIException e) {
+                            //执行组装数据库的数据,以及生成驱动报表
+                            opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
+                            redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
+
+                            messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
+                                    itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行失败",
+                                    e.getMessage(),
+                                    ConstantStr.NO_READ));
+                            if (Blank.isNotEmpty(server)) {
+                                server.dispose();
+                            }
+                            itemGroupDao.stopItemGroupById(id, ConstantStr.EXCEPT_STOP_UP);
+                            cronTaskRegister.removeCronTask(cronId);
+                            redisUtil.del(ConstantStr.ITEM_GROUP + id);
+                            chTimer.cancel();
+                            timer.cancel();
+                        }
+                    }
+                }
+            }, new Date(), (int) (Math.round(itemGroup.getModeValue() * 1000)));
+
+            while (true) {
+                Boolean flage = (Boolean) redisUtil.get(ConstantStr.ITEM_GROUP + id);
+                if (Blank.isEmpty(flage)) {
+                    flage = false;
+                }
+                if (!flage) {
+                    //执行组装数据库的数据,以及生成驱动报表
+                    String sqlCurrentYmdh = String.valueOf(redisUtil.get(ConstantStr.VALUE_BELONG_TIME + id));
+                    opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
+                    redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
+                    if (Blank.isNotEmpty(server)) {
+                        server.dispose();
+                    }
+                    cronTaskRegister.removeCronTask(cronId);
+                    redisUtil.del(ConstantStr.ITEM_GROUP + id);
+                    chTimer.cancel();
+                    timer.cancel();
+                    break;
+                }
+                if (System.currentTimeMillis() >= endTime) {
+                    //执行组装数据库的数据,以及生成驱动报表
+                    String sqlCurrentYmdh = String.valueOf(redisUtil.get(ConstantStr.VALUE_BELONG_TIME + id));
+                    opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
+                    redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
+
+                    if (Blank.isNotEmpty(server)) {
+                        server.dispose();
+                    }
+                    chTimer.cancel();
+                    timer.cancel();
+                    break;
+                }
+            }
+        } catch (Exception e) {
+            Boolean flage = (Boolean) redisUtil.get(ConstantStr.ITEM_GROUP + id);
+            if (Blank.isEmpty(flage)) {
+                flage = false;
+            }
+            if (!flage) {
+                //执行组装数据库的数据,以及生成驱动报表
+                String sqlCurrentYmdh = String.valueOf(redisUtil.get(ConstantStr.VALUE_BELONG_TIME + id));
+                opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
+                redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
+                if (Blank.isNotEmpty(server)) {
+                    server.dispose();
+                }
+                itemGroupDao.stopItemGroupById(itemGroup.getId(), ConstantStr.EXCEPT_STOP_UP);
+                cronTaskRegister.removeCronTask(cronId);
+                chTimer.cancel();
+                timer.cancel();
+            }
+        }
+    }
+}

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

@@ -327,12 +327,11 @@ public class OpcDaUtil {
      * 生成是基础属性还是附属属性,如果在数据源驱动配置中,未有这个驱动配置,则此数据源下的所有项均为基础属性
      * 如果有这个配置,根据是否配置了这个驱动的基础项来判断,如果和配置服务,则为基础属性,否则为附属属性
      *
-     * @param dataSource
      * @param itemList
      * @param dataSourceDriver
      * @return
      */
-    public static List<Item> genBasicDa(DataSource dataSource, List<Item> itemList,
+    public static List<Item> genBasicDa(List<Item> itemList,
                                         DataSourceDriver dataSourceDriver) {
         if (Blank.isNotEmpty(dataSourceDriver)) {
             List<DriverBasicItem> driverBasicItemList = dataSourceDriver.getDriverBasicItemList();

+ 7 - 4
chaunyi_opc/opc_da/src/main/resources/mapper/DataModelDao.xml

@@ -4,7 +4,7 @@
 
     <sql id="dataModel">
         id
-        ,math_parameter, operation_rule, user_id, remark, create_time
+        , model_type, math_parameter, operation_rule, replacing_value, user_id, remark, create_time
     </sql>
 
     <sql id="item">
@@ -13,14 +13,17 @@
     </sql>
 
     <insert id="addDataModel">
-        insert into t_data_model (math_parameter, operation_rule, user_id, remark, create_time)
-            VALUE (#{mathParameter}, #{operationRule}, #{userId}, #{remark}, now())
+        insert into t_data_model (model_type, math_parameter, operation_rule, replacing_value, user_id, remark,
+                                  create_time)
+            VALUE (#{modelType},#{mathParameter}, #{operationRule},#{replacingValue}, #{userId}, #{remark}, now())
     </insert>
 
     <update id="updateDataModel">
         update t_data_model
-        set math_parameter=#{mathParameter},
+        set model_type=#{modelType},
+            math_parameter=#{mathParameter},
             operation_rule = #{operationRule},
+            replacing_value = #{replacingValue},
             remark=#{remark}
         where id = #{id}
     </update>

+ 7 - 0
chaunyi_opc/opc_da/src/main/resources/mapper/DataSourceDao.xml

@@ -219,4 +219,11 @@
         from t_data_source_driver
         where cls_id=#{clsId}
     </select>
+
+    <select id="getDataSourceByclsId" resultType="com.example.opc_common.entity.DataSource">
+        select
+        <include refid="dataSource"/>
+        from t_data_source
+        where cls_id = #{clsId}
+    </select>
 </mapper>

+ 44 - 7
chaunyi_opc/opc_da/src/main/resources/mapper/ItemGroupDao.xml

@@ -83,6 +83,12 @@
           and item_group_id = #{itemGroupId}
     </update>
 
+    <update id="updateItemTypeList" statementType="CALLABLE">
+        {
+            call updateItemTypeList(#{strArr}, #{replaceStr})
+            }
+    </update>
+
     <delete id="delItemByGroupId">
         delete
         from t_item
@@ -153,7 +159,8 @@
     </select>
 
     <select id="getItemListByGroupId" resultType="com.example.opc_common.entity.Item">
-        select ti.id,
+        SELECT
+        ti.id,
         ti.item_group_id,
         ti.item_name,
         ti.item_read_name,
@@ -163,14 +170,24 @@
         ti.`describe`,
         ti.data_model_id,
         ti.is_driver_item,
-        tdm.operation_rule
-        FROM t_item ti
+        tdm.operation_rule,
+        tard.org_data_value,
+        tard.data_value
+        FROM
+        t_item ti
+        LEFT JOIN t_item_group tig ON ti.item_group_id = tig.id
         LEFT JOIN t_data_model tdm ON ti.data_model_id = tdm.id
-        where ti.item_group_id = #{itemGroupId}
-        <if test="itemType != null and itemType != ''">
-            and ti.item_type = #{itemType}
+        LEFT JOIN t_attach_raw_data tard ON ti.item_group_id = tard.item_group_id
+        AND tig.data_source_id = tard.data_source_id
+        AND ti.item_read_name = tard.item_name
+        WHERE
+        ti.item_group_id = #{itemGroupId}
+        <if test="itemType != null">
+            and ti.item_type = #{itemType,jdbcType=INTEGER}
         </if>
-        order by ti.is_driver_item DESC, ti.item_name
+        ORDER BY
+        ti.is_driver_item DESC,
+        ti.item_name
     </select>
 
     <select id="getPublicItemList" resultType="com.example.opc_common.entity.Item">
@@ -267,4 +284,24 @@
         </if>
     </select>
 
+    <select id="getIgListByDsId" resultType="com.example.opc_common.entity.ItemGroup">
+        select
+        <include refid="itemGroup"/>
+        from t_item_group
+        where data_source_id in
+        <foreach collection="dataSourceList" item="dataSource" separator="," open="(" close=")">
+            #{dataSource.id}
+        </foreach>
+    </select>
+
+    <select id="getItemListByGidList" resultType="com.example.opc_common.entity.Item">
+        select
+        <include refid="item"/>
+        from t_item
+        where item_group_id in
+        <foreach collection="itemGroupList" item="itemGroup" separator="," open="(" close=")">
+            #{itemGroup.id}
+        </foreach>
+    </select>
+
 </mapper>