houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.iailab.module.job.task;
 
import com.alibaba.fastjson.JSONObject;
import com.iailab.common.constant.BusinessConstant;
import com.iailab.module.data.api.IFeignDataApi;
import com.iailab.module.data.dto.FeignWritePointValueDTO;
import com.iailab.module.infra.api.config.ConfigApi;
import com.iailab.module.mcs.dto.StModelDTO;
import com.iailab.module.mcs.dto.StModelOutDTO;
import com.iailab.module.mcs.service.StModelOutService;
import com.iailab.module.mcs.service.StModelService;
import com.iailab.module.model.handler.ModelHandler;
import com.iailab.module.watch.WatchConstant;
import com.iailab.module.watch.WatchDog;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
 
/**
 * 旋流器分选控制
 * ZX2
 * 5 0/1 * * * ?
 */
@Component("modelSeparationTask")
public class ModelSeparationTask implements ITask {
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    private final String MODEL_CODE_3318 = "separation_control3318";
 
    private final String MODEL_CODE_3319 = "separation_control3319";
 
    private final String mParamCode = "isDev";
 
    private final List<String> MODEL_CODE_LIST = new ArrayList<>();
 
    private final String ALM_POINT = WatchConstant.CONTROLLER_ALM_2;
 
    @Resource
    private StModelService stModelService;
 
    @Resource
    private ModelHandler modelHandler;
 
    @Resource
    private IFeignDataApi feignDataApi;
 
    @Resource
    private ConfigApi configApi;
 
    @Resource
    private StModelOutService stModelOutService;
 
    @Resource
    private WatchDog watchDog;
 
    private final String MV_VALVE_JJ = "mv_valve_jj";
 
    private int writeDelayTime = 20;
 
    private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
 
    ModelSeparationTask() {
        MODEL_CODE_LIST.add(MODEL_CODE_3318);
        MODEL_CODE_LIST.add(MODEL_CODE_3319);
    }
 
    @Override
    public void run(String params) {
        String isDev = configApi.queryConfigByCode(mParamCode);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.SECOND, -5); // 滞后5s
        logger.info("ModelSeparationTask定时任务正在执行,参数为:{}", params);
        if (BusinessConstant.DEV_Y.equals(isDev)) {
            logger.info("模型采用开发模型");
        } else {
            logger.info("模型采用生产模型");
        }
        try {
            if (!BusinessConstant.DEV_Y.equals(isDev)) {
                // 1、判断通讯是否正常
                if (!watchDog.isConnect(WatchConstant.ZX2_STATUS)) {
                    logger.info("watchDog.isConnect=false");
                    return;
                }
 
                // 2、判断控制器状态
                if (!watchDog.isUse(WatchConstant.CONTROLLER_ONSTS_2)) {
                    logger.info("将反馈值写入下发值,无扰动切换");
                    MODEL_CODE_LIST.forEach(item -> {
                        this.writeBySetValue(item);
                    });
                    return;
                }
                logger.info("控制器启动");
            }
 
            // 3、运行模型
            Map<String, Map<String, Object>> allCommonResult = new HashMap<>();
            for (int i = 0; i < MODEL_CODE_LIST.size(); i++) {
                StModelDTO stModel = stModelService.getByCode(MODEL_CODE_LIST.get(i));
                if (stModel == null) {
                    logger.info("model为空!");
                    return;
                }
                logger.info("运行模型:" + stModel.getModelCode());
                Map<String, Object> modelCommonResult = modelHandler.run(stModel.getModelCode(), calendar.getTime());
                allCommonResult.put(MODEL_CODE_LIST.get(i), modelCommonResult);
            }
 
            // 4、下发数据
            if (!BusinessConstant.DEV_Y.equals(isDev)) {
                for (int i = 0; i < MODEL_CODE_LIST.size(); i++) {
                    if (!writeToDcs(allCommonResult.get(MODEL_CODE_LIST.get(i)), MODEL_CODE_LIST.get(i))) {
                        throw new Exception(MODEL_CODE_LIST.get(i) + "下发数据异常");
                    }
                }
            }
        } catch (Exception ex) {
            logger.error("ModelSeparationTask运行异常");
            ex.printStackTrace();
        }
        logger.info("ModelSeparationTask运行完成");
    }
 
    /**
     * 数据下发
     *
     * @param data
     * @param modelCode
     */
    private boolean writeToDcs(Map<String, Object> data, String modelCode) {
        String code = data.get(BusinessConstant.MODELE_RESULT_STATUS).toString();
 
        if (BusinessConstant.MODEL_R_ERROR.equals(code.substring(0,1))) {
            logger.warn("code=" + code + ",结果异常,不下发数据!恢复手动");
            watchDog.stopControllerSts(WatchConstant.CONTROLLER_ONSTS_2);
            return false;
        } else if (BusinessConstant.MODEL_R_WARN.equals(code.substring(0,1))) {
            logger.warn("code=" + code + ",结果警告");
            FeignWritePointValueDTO writeAlm = new FeignWritePointValueDTO();
            writeAlm.setPointCode(ALM_POINT);
            writeAlm.setPointValue(BusinessConstant.STATUS_FALSE);
            feignDataApi.setPointValue(writeAlm);
        }
 
        logger.info("下发的数据=" + JSONObject.toJSONString(data));
        List<StModelOutDTO> outPoints = stModelOutService.getOutPoint(modelCode);
        if (CollectionUtils.isEmpty(outPoints)) {
            logger.info("outPoints 为空!");
            return true;
        }
        outPoints.forEach(pointItem -> {
            FeignWritePointValueDTO writePointValue = new FeignWritePointValueDTO();
            writePointValue.setPointCode(pointItem.getPointNo());
            writePointValue.setPointValue(new BigDecimal(data.get(pointItem.getResultKey()).toString()));
            feignDataApi.setPointValue(writePointValue);
 
            if (pointItem.getResultKey().contains(MV_VALVE_JJ)) {
                executor.schedule(() -> {
                    try {
                        logger.info("20秒后加介阀开度复位0!");
                        FeignWritePointValueDTO fw = new FeignWritePointValueDTO();
                        fw.setPointCode(pointItem.getPointNo());
                        fw.setPointValue(BigDecimal.ZERO);
                        feignDataApi.setPointValue(fw);
                        logger.info("复位完成!");
                    } catch (Exception ex) {
                        logger.info("复位失败!");
                        ex.printStackTrace();
                    }
                }, writeDelayTime, TimeUnit.SECONDS);
            }
        });
        return true;
    }
 
    /**
     * 无扰模式
     * 反馈写入下发点位
     */
    private void writeBySetValue(String modelCode) {
        logger.info("=====writeBySetValue Start=======");
        List<StModelOutDTO> outPoints = stModelOutService.getOutPoint(modelCode);
        if (CollectionUtils.isEmpty(outPoints)) {
            return;
        }
        outPoints.forEach(item -> {
            if (StringUtils.isNotBlank(item.getPointNo()) && StringUtils.isNotBlank(item.getDisturbancePointNo())) {
                List<String> points = new ArrayList<>();
                points.add(item.getDisturbancePointNo());
                Map<String, Object> value = feignDataApi.getCurrentValue(points);
                FeignWritePointValueDTO writePointValue = new FeignWritePointValueDTO();
                writePointValue.setPointCode(item.getPointNo());
                writePointValue.setPointValue(value.get(item.getDisturbancePointNo()));
                feignDataApi.setPointValue(writePointValue);
            }
        });
    }
}