dengzedong
昨天 e691b98e02153c9c439d7f5acb468924572d75d9
提交 | 用户 | 时间
7fd198 1 package com.iailab.module.model.api;
2
3 import com.alibaba.fastjson.JSON;
268c71 4 import com.alibaba.fastjson.JSONObject;
95066d 5 import com.iailab.module.data.api.point.DataPointApi;
D 6 import com.iailab.module.data.api.point.dto.ApiPointValueWriteDTO;
7 import com.iailab.module.model.api.mcs.dto.StScheduleModelOutDTO;
7fd198 8 import com.iailab.module.model.api.mdk.MdkApi;
9 import com.iailab.module.model.api.mdk.dto.*;
2b47c5 10 import com.iailab.module.model.common.enums.IsWriteEnum;
D 11 import com.iailab.module.model.common.enums.ModelOutResultType;
12 import com.iailab.module.model.common.enums.OutResultType;
7fd198 13 import com.iailab.module.model.mcs.pre.entity.DmModuleEntity;
14 import com.iailab.module.model.mcs.pre.service.DmModuleService;
15 import com.iailab.module.model.mcs.pre.service.MmPredictItemService;
95066d 16 import com.iailab.module.model.mcs.sche.service.StScheduleModelOutService;
ac52ae 17 import com.iailab.module.model.mcs.sche.service.StScheduleRecordService;
18 import com.iailab.module.model.mcs.sche.service.StScheduleSchemeService;
7fd198 19 import com.iailab.module.model.mdk.predict.PredictModuleHandler;
9162d9 20 import com.iailab.module.model.mdk.predict.PredictResultHandler;
054fb9 21 import com.iailab.module.model.mdk.schedule.ScheduleModelHandler;
9162d9 22 import com.iailab.module.model.mdk.vo.DataValueVO;
7fd198 23 import com.iailab.module.model.mdk.vo.ItemVO;
24 import com.iailab.module.model.mdk.vo.PredictResultVO;
054fb9 25 import com.iailab.module.model.mdk.vo.ScheduleResultVO;
7fd198 26 import lombok.extern.slf4j.Slf4j;
27 import org.springframework.beans.factory.annotation.Autowired;
268c71 28 import org.springframework.data.redis.core.RedisTemplate;
9162d9 29 import org.springframework.util.CollectionUtils;
7fd198 30 import org.springframework.validation.annotation.Validated;
31 import org.springframework.web.bind.annotation.RestController;
32
b2aca2 33 import java.util.*;
268c71 34 import java.util.concurrent.TimeUnit;
7fd198 35 import java.util.stream.Collectors;
2b47c5 36
D 37 import static com.iailab.module.model.common.enums.ModelOutResultType.D;
7fd198 38
39 /**
40  * @author PanZhibao
41  * @Description
42  * @createTime 2024年08月26日
43  */
44 @Slf4j
45 @RestController
46 @Validated
47 public class MdkApiImpl implements MdkApi {
48
49     @Autowired
50     private DmModuleService dmModuleService;
51
52     @Autowired
53     private MmPredictItemService mmPredictItemService;
54
55     @Autowired
56     private PredictModuleHandler predictModuleHandler;
9162d9 57
58     @Autowired
59     private PredictResultHandler predictResultHandler;
054fb9 60
61     @Autowired
62     private ScheduleModelHandler scheduleModelHandler;
ac52ae 63
64     @Autowired
65     private StScheduleRecordService stScheduleRecordService;
66
67     @Autowired
68     private StScheduleSchemeService stScheduleSchemeService;
95066d 69
D 70     @Autowired
71     private StScheduleModelOutService stScheduleModelOutService;
72
73     @Autowired
74     private DataPointApi dataPointApi;
268c71 75
76     @Autowired
77     private RedisTemplate<String, Object> redisTemplate;
78
79     public static final long offset = 60 * 3L;
7fd198 80
81     /**
82      * 按模块预测
83      *
84      * @param reqDTO
85      * @return
86      */
87     @Override
148842 88     public MdkPredictModuleRespDTO predictModule(MdkPredictReqDTO reqDTO) {
7fd198 89         MdkPredictModuleRespDTO resp = new MdkPredictModuleRespDTO();
69bd5e 90         resp.setPredictTime(reqDTO.getPredictTime());
D 91         resp.setModuleType(reqDTO.getModuleType());
92
7fd198 93         Map<String, MdkPredictItemRespDTO> predictItemRespMap = new HashMap<>();
94         try {
95             if (reqDTO.getPredictTime() == null) {
96                 throw new Exception("PredictTime不能为空");
97             }
98             if (reqDTO.getModuleType() == null) {
99                 throw new Exception("ModuleType不能为空");
100             }
e691b9 101
7fd198 102             log.info("预测参数:" + JSON.toJSONString(reqDTO));
103             List<DmModuleEntity> moduleList = dmModuleService.getModuleByModuleType(reqDTO.getModuleType());
104             log.info("预测计算开始: " + System.currentTimeMillis());
105             for (DmModuleEntity module : moduleList) {
106                 int intervalTime = 0;
107                 if (module.getPredicttime() != null) {
108                     intervalTime = (int) (reqDTO.getPredictTime().getTime() - module.getPredicttime().getTime()) / (1000 * 60);
109                 }
110                 List<ItemVO> predictItemList = mmPredictItemService.getByModuleId(module.getId());
07890e 111                 Map<String, PredictResultVO> predictResultMap = new HashMap<>(predictItemList.size());
D 112                 // 分组,先运行normal预测项,再将结果传递给merge预测项
113                 List<ItemVO> normalItems = predictItemList.stream().filter(e -> e.getItemType().equals("NormalItem")).collect(Collectors.toList());
114                 if (!CollectionUtils.isEmpty(normalItems)) {
ac52ae 115                     predictModuleHandler.predict(normalItems, reqDTO.getPredictTime(), intervalTime, predictResultMap);
fde993 116                     List<ItemVO> mergeItems = predictItemList.stream().filter(e -> e.getItemType().equals("MergeItem")).collect(Collectors.toList());
D 117                     if (!CollectionUtils.isEmpty(mergeItems)) {
ac52ae 118                         predictModuleHandler.predict(mergeItems, reqDTO.getPredictTime(), intervalTime, predictResultMap);
07890e 119                     }
D 120                 }
4f1717 121                 // 更新Module时间
122                 dmModuleService.updatePredictTime(module.getId(), reqDTO.getPredictTime());
123                 if (reqDTO.getIsResult() == null || !reqDTO.getIsResult()) {
124                     return resp;
125                 }
126                 for (Map.Entry<String, PredictResultVO> entry : predictResultMap.entrySet()) {
127                     MdkPredictItemRespDTO itemResp = new MdkPredictItemRespDTO();
128                     itemResp.setItemId(entry.getKey());
129                     itemResp.setPredictTime(reqDTO.getPredictTime());
130                     Map<String, List<MdkPredictDataDTO>> itemPredictData = new HashMap<>();
131
132                     Map<String, List<DataValueVO>> predictLists = predictResultHandler.convertToPredictData2(entry.getValue());
133                     for (Map.Entry<String, List<DataValueVO>> dataListEntry : predictLists.entrySet()) {
134                         List<MdkPredictDataDTO> predictData = dataListEntry.getValue().stream().map(t -> {
135                             MdkPredictDataDTO dto1 = new MdkPredictDataDTO();
136                             dto1.setDataTime(t.getDataTime());
137                             dto1.setDataValue(t.getDataValue());
138                             return dto1;
139                         }).collect(Collectors.toList());
140                         itemPredictData.put(dataListEntry.getKey(), predictData);
141                     }
142                     itemResp.setPredictData(itemPredictData);
143                     predictItemRespMap.put(entry.getKey(), itemResp);
144                 }
7fd198 145             }
146             log.info("预测计算结束: " + System.currentTimeMillis());
147         } catch (Exception ex) {
148842 148             ex.printStackTrace();
149             return resp;
7fd198 150         }
151         resp.setPredictItemRespMap(predictItemRespMap);
148842 152         return resp;
7fd198 153     }
154
155     /**
156      * 单个预测
157      *
158      * @param reqDTO
159      * @return
160      */
161     @Override
148842 162     public MdkPredictItemRespDTO predictItem(MdkPredictReqDTO reqDTO) {
7fd198 163         MdkPredictItemRespDTO resp = new MdkPredictItemRespDTO();
9162d9 164         try {
1178da 165
D 166             ItemVO itemByItemNo = mmPredictItemService.getItemByItemNo(reqDTO.getItemNo());
167             List<ItemVO> predictItemList = new ArrayList<>();
168             predictItemList.add(itemByItemNo);
169             Map<String, PredictResultVO> predictResultMap = new HashMap<>(predictItemList.size());
ac52ae 170             predictModuleHandler.predict(predictItemList, reqDTO.getPredictTime(), 0, predictResultMap);
1178da 171
D 172             Map<String, List<MdkPredictDataDTO>> itemPredictData = new HashMap<>();
173
174             Map<String, List<DataValueVO>> predictLists = predictResultHandler.convertToPredictData2(predictResultMap.get(reqDTO.getItemNo()));
175             for (Map.Entry<String, List<DataValueVO>> dataListEntry : predictLists.entrySet()) {
176                 List<MdkPredictDataDTO> predictData = dataListEntry.getValue().stream().map(t -> {
177                     MdkPredictDataDTO dto1 = new MdkPredictDataDTO();
178                     dto1.setDataTime(t.getDataTime());
179                     dto1.setDataValue(t.getDataValue());
180                     return dto1;
181                 }).collect(Collectors.toList());
182                 itemPredictData.put(dataListEntry.getKey(), predictData);
9162d9 183             }
1178da 184             resp.setItemId(reqDTO.getItemNo());
9162d9 185             resp.setPredictTime(reqDTO.getPredictTime());
1178da 186             resp.setPredictData(itemPredictData);
D 187         } catch (Exception e) {
188             throw new RuntimeException(e);
9162d9 189         }
190
148842 191         return resp;
7fd198 192     }
193
194     /**
195      * 预测调整
196      *
197      * @param reqDTO
198      * @return
199      */
200     @Override
148842 201     public Boolean predictAutoAdjust(MdkPredictReqDTO reqDTO) {
7fd198 202
203
148842 204         return true;
7fd198 205     }
206
207     /**
208      * 执行调度模型
209      *
210      * @param reqDTO
211      * @return
212      */
213     @Override
148842 214     public MdkScheduleRespDTO doSchedule(MdkScheduleReqDTO reqDTO) {
7fd198 215         MdkScheduleRespDTO resp = new MdkScheduleRespDTO();
054fb9 216         resp.setScheduleCode(reqDTO.getScheduleCode());
217         resp.setScheduleTime(reqDTO.getScheduleTime());
218         try {
219             log.info("调度计算开始: " + System.currentTimeMillis());
bab433 220             log.info("reqDTO=" + JSON.toJSONString(reqDTO));
81ce77 221             ScheduleResultVO scheduleResult = scheduleModelHandler.doSchedule(reqDTO.getScheduleCode(), reqDTO.getScheduleTime(),
222                     reqDTO.getDynamicDataLength(), reqDTO.getDynamicSettings());
b2bb7d 223             resp.setStatusCode(scheduleResult.getResultCode());
054fb9 224             resp.setResult(scheduleResult.getResult());
ac52ae 225             stScheduleRecordService.create(scheduleResult);
b2bb7d 226             stScheduleSchemeService.updateTime(scheduleResult.getSchemeId(), scheduleResult.getScheduleTime(), scheduleResult.getResultCode());
054fb9 227             log.info("预测计算结束: " + System.currentTimeMillis());
228         } catch (Exception ex) {
229             log.info("调度计算异常: " + System.currentTimeMillis());
ac52ae 230             ex.printStackTrace();
148842 231             return resp;
054fb9 232         }
148842 233         return resp;
7fd198 234     }
95066d 235
268c71 236     /**
237      * 执行调度模型
238      *
239      * @param reqDTO
240      * @return
241      */
242     @Override
243     public MdkScheduleRespDTO runSchedule(MdkScheduleReqDTO reqDTO) {
244         MdkScheduleRespDTO resp = new MdkScheduleRespDTO();
d322fe 245         if (reqDTO.getScheduleTime() == null) {
246             Calendar calendar = Calendar.getInstance();
247             calendar.set(Calendar.MILLISECOND, 0);
248             calendar.set(Calendar.SECOND, 0);
f853b0 249             reqDTO.setScheduleTime(calendar.getTime());
d322fe 250         }
f853b0 251         resp.setScheduleCode(reqDTO.getScheduleCode());
252         resp.setScheduleTime(reqDTO.getScheduleTime());
268c71 253         String catchKey = "ScheduleResult:" + reqDTO.getScheduleCode();
254         try {
255             if (redisTemplate.hasKey(catchKey)) {
256                 log.info("查找调度结果缓存: " + catchKey);
e14de7 257                 return JSON.parseObject(redisTemplate.opsForValue().get(catchKey).toString(), MdkScheduleRespDTO.class);
268c71 258             }
259             log.info("调度计算开始: " + System.currentTimeMillis());
260             log.info("reqDTO=" + JSON.toJSONString(reqDTO));
261             ScheduleResultVO scheduleResult = scheduleModelHandler.doSchedule(reqDTO.getScheduleCode(), reqDTO.getScheduleTime(),
262                     reqDTO.getDynamicDataLength(), reqDTO.getDynamicSettings());
263             resp.setStatusCode(scheduleResult.getResultCode());
264             resp.setResult(scheduleResult.getResult());
265             redisTemplate.opsForValue().set(catchKey, JSON.toJSONString(resp), offset, TimeUnit.SECONDS);
266             stScheduleSchemeService.updateTime(scheduleResult.getSchemeId(), scheduleResult.getScheduleTime(), scheduleResult.getResultCode());
267             log.info("预测计算结束: " + System.currentTimeMillis());
268         } catch (Exception ex) {
269             log.info("调度计算异常: " + System.currentTimeMillis());
270             ex.printStackTrace();
271             return resp;
272         }
273         return resp;
274     }
275
95066d 276     @Override
D 277     public Boolean scheduleModelOut(MdkScheduleRespDTO dto) {
278         String modelId = stScheduleSchemeService.getByCode(dto.getScheduleCode()).getModelId();
2b47c5 279         Map<String, Object> result = dto.getResult();
D 280         List<StScheduleModelOutDTO> list = stScheduleModelOutService.list(modelId);
281         try {
282             for (StScheduleModelOutDTO stScheduleModelOutDTO : list) {
283                 double value = 0;
284                 //判断点位是否下发
285                 if (stScheduleModelOutDTO.getIsWrite().equals(IsWriteEnum.NOTWRITE.value())) {
286                     continue;
287                 }
288                 //返回结果是否存在
289                 if (result.get(stScheduleModelOutDTO.getResultKey()) == null) {
290                     log.error(result.get(stScheduleModelOutDTO.getResultKey()) + "resultKey匹配失败");
291                     continue;
292                 }
293                 Object resultValue = result.get(stScheduleModelOutDTO.getResultKey());
294                 //判断解析方式
295                 ModelOutResultType modelOutResultType = ModelOutResultType.getEumByCode(stScheduleModelOutDTO.getResultType());
296                 switch (modelOutResultType) {
297                     case D:
298                         value = (Double) resultValue;
299                         break;
300                     case D1:
301                         ArrayList<Double> doubleList = (ArrayList<Double>) resultValue;
302                         double[] array1 = new double[doubleList.size()];
303                         for (int i = 0; i < doubleList.size(); i++) {
304                             array1[i] = doubleList.get(i);
95066d 305                         }
2b47c5 306                         if (stScheduleModelOutDTO.getResultPort() < array1.length) {
D 307                             value = array1[stScheduleModelOutDTO.getResultPort()];
308                         } else {
309                             log.error(result.get(stScheduleModelOutDTO.getResultKey()) + "下角标超限");
310                         }
311                         break;
312                     case D2:
313                         ArrayList<ArrayList<Double>> doubleListList = (ArrayList<ArrayList<Double>>) resultValue;
314                         double[][] array2 = new double[doubleListList.size()][];
315                         for (int i = 0; i < doubleListList.size(); i++) {
316                             ArrayList<Double> doubleList2 = doubleListList.get(i);
317                             array2[i] = new double[doubleList2.size()];
318                             for (int j = 0; j < doubleList2.size(); j++) {
319                                 array2[i][j] = doubleList2.get(j);
320                             }
321                         }
322                         if (stScheduleModelOutDTO.getResultPort() < array2.length && stScheduleModelOutDTO.getResultIndex() < array2[stScheduleModelOutDTO.getResultPort()].length) {
323                             value = array2[stScheduleModelOutDTO.getResultPort()][stScheduleModelOutDTO.getResultIndex()];
324                         } else {
325                             log.error(result.get(stScheduleModelOutDTO.getResultKey()) + "下标超限");
326                         }
327                         break;
328                 }
329                 //下发到point点位
330                 ApiPointValueWriteDTO ApiPointValueWriteDTO = new ApiPointValueWriteDTO();
331                 ApiPointValueWriteDTO.setPointNo(stScheduleModelOutDTO.getPointNo());
332                 ApiPointValueWriteDTO.setValue(value);
333                 if (!dataPointApi.writePointRealValue(ApiPointValueWriteDTO)) {
334                     log.error(result.get(stScheduleModelOutDTO.getResultKey()) + "下发数据异常");
335                 }
336             }
337         } catch (Exception ex) {
95066d 338             log.error("下发数据异常");
D 339             ex.printStackTrace();
340         }
341         return true;
342     }
7fd198 343 }