潘志宝
3 天以前 268c71b4154956daddfb7cd17d5baade9e59d46d
提交 | 用户 | 时间
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             }
b2aca2 101             Calendar calendar = Calendar.getInstance();
D 102             calendar.setTime(reqDTO.getPredictTime());
103             calendar.set(Calendar.MILLISECOND, 0);
104             calendar.set(Calendar.SECOND, 0);
105             reqDTO.setPredictTime(calendar.getTime());
7fd198 106             log.info("预测参数:" + JSON.toJSONString(reqDTO));
107             MdkPredictModuleRespDTO result = new MdkPredictModuleRespDTO();
108             result.setPredictTime(reqDTO.getPredictTime());
109             result.setModuleType(reqDTO.getModuleType());
110             List<DmModuleEntity> moduleList = dmModuleService.getModuleByModuleType(reqDTO.getModuleType());
111             log.info("预测计算开始: " + System.currentTimeMillis());
112             for (DmModuleEntity module : moduleList) {
113                 int intervalTime = 0;
114                 if (module.getPredicttime() != null) {
115                     intervalTime = (int) (reqDTO.getPredictTime().getTime() - module.getPredicttime().getTime()) / (1000 * 60);
116                 }
117                 List<ItemVO> predictItemList = mmPredictItemService.getByModuleId(module.getId());
07890e 118                 Map<String, PredictResultVO> predictResultMap = new HashMap<>(predictItemList.size());
D 119                 // 分组,先运行normal预测项,再将结果传递给merge预测项
120                 List<ItemVO> normalItems = predictItemList.stream().filter(e -> e.getItemType().equals("NormalItem")).collect(Collectors.toList());
121                 if (!CollectionUtils.isEmpty(normalItems)) {
ac52ae 122                     predictModuleHandler.predict(normalItems, reqDTO.getPredictTime(), intervalTime, predictResultMap);
fde993 123                     List<ItemVO> mergeItems = predictItemList.stream().filter(e -> e.getItemType().equals("MergeItem")).collect(Collectors.toList());
D 124                     if (!CollectionUtils.isEmpty(mergeItems)) {
ac52ae 125                         predictModuleHandler.predict(mergeItems, reqDTO.getPredictTime(), intervalTime, predictResultMap);
07890e 126                     }
D 127                 }
4f1717 128                 // 更新Module时间
129                 dmModuleService.updatePredictTime(module.getId(), reqDTO.getPredictTime());
130                 if (reqDTO.getIsResult() == null || !reqDTO.getIsResult()) {
131                     return resp;
132                 }
133                 for (Map.Entry<String, PredictResultVO> entry : predictResultMap.entrySet()) {
134                     MdkPredictItemRespDTO itemResp = new MdkPredictItemRespDTO();
135                     itemResp.setItemId(entry.getKey());
136                     itemResp.setPredictTime(reqDTO.getPredictTime());
137                     Map<String, List<MdkPredictDataDTO>> itemPredictData = new HashMap<>();
138
139                     Map<String, List<DataValueVO>> predictLists = predictResultHandler.convertToPredictData2(entry.getValue());
140                     for (Map.Entry<String, List<DataValueVO>> dataListEntry : predictLists.entrySet()) {
141                         List<MdkPredictDataDTO> predictData = dataListEntry.getValue().stream().map(t -> {
142                             MdkPredictDataDTO dto1 = new MdkPredictDataDTO();
143                             dto1.setDataTime(t.getDataTime());
144                             dto1.setDataValue(t.getDataValue());
145                             return dto1;
146                         }).collect(Collectors.toList());
147                         itemPredictData.put(dataListEntry.getKey(), predictData);
148                     }
149                     itemResp.setPredictData(itemPredictData);
150                     predictItemRespMap.put(entry.getKey(), itemResp);
151                 }
7fd198 152             }
153             log.info("预测计算结束: " + System.currentTimeMillis());
154         } catch (Exception ex) {
148842 155             ex.printStackTrace();
156             return resp;
7fd198 157         }
158         resp.setPredictItemRespMap(predictItemRespMap);
148842 159         return resp;
7fd198 160     }
161
162     /**
163      * 单个预测
164      *
165      * @param reqDTO
166      * @return
167      */
168     @Override
148842 169     public MdkPredictItemRespDTO predictItem(MdkPredictReqDTO reqDTO) {
7fd198 170         MdkPredictItemRespDTO resp = new MdkPredictItemRespDTO();
9162d9 171         try {
1178da 172
D 173             ItemVO itemByItemNo = mmPredictItemService.getItemByItemNo(reqDTO.getItemNo());
174             List<ItemVO> predictItemList = new ArrayList<>();
175             predictItemList.add(itemByItemNo);
176             Map<String, PredictResultVO> predictResultMap = new HashMap<>(predictItemList.size());
ac52ae 177             predictModuleHandler.predict(predictItemList, reqDTO.getPredictTime(), 0, predictResultMap);
1178da 178
D 179             Map<String, List<MdkPredictDataDTO>> itemPredictData = new HashMap<>();
180
181             Map<String, List<DataValueVO>> predictLists = predictResultHandler.convertToPredictData2(predictResultMap.get(reqDTO.getItemNo()));
182             for (Map.Entry<String, List<DataValueVO>> dataListEntry : predictLists.entrySet()) {
183                 List<MdkPredictDataDTO> predictData = dataListEntry.getValue().stream().map(t -> {
184                     MdkPredictDataDTO dto1 = new MdkPredictDataDTO();
185                     dto1.setDataTime(t.getDataTime());
186                     dto1.setDataValue(t.getDataValue());
187                     return dto1;
188                 }).collect(Collectors.toList());
189                 itemPredictData.put(dataListEntry.getKey(), predictData);
9162d9 190             }
1178da 191             resp.setItemId(reqDTO.getItemNo());
9162d9 192             resp.setPredictTime(reqDTO.getPredictTime());
1178da 193             resp.setPredictData(itemPredictData);
D 194         } catch (Exception e) {
195             throw new RuntimeException(e);
9162d9 196         }
197
148842 198         return resp;
7fd198 199     }
200
201     /**
202      * 预测调整
203      *
204      * @param reqDTO
205      * @return
206      */
207     @Override
148842 208     public Boolean predictAutoAdjust(MdkPredictReqDTO reqDTO) {
7fd198 209
210
148842 211         return true;
7fd198 212     }
213
214     /**
215      * 执行调度模型
216      *
217      * @param reqDTO
218      * @return
219      */
220     @Override
148842 221     public MdkScheduleRespDTO doSchedule(MdkScheduleReqDTO reqDTO) {
7fd198 222         MdkScheduleRespDTO resp = new MdkScheduleRespDTO();
054fb9 223         resp.setScheduleCode(reqDTO.getScheduleCode());
224         resp.setScheduleTime(reqDTO.getScheduleTime());
225         try {
226             log.info("调度计算开始: " + System.currentTimeMillis());
bab433 227             log.info("reqDTO=" + JSON.toJSONString(reqDTO));
81ce77 228             ScheduleResultVO scheduleResult = scheduleModelHandler.doSchedule(reqDTO.getScheduleCode(), reqDTO.getScheduleTime(),
229                     reqDTO.getDynamicDataLength(), reqDTO.getDynamicSettings());
b2bb7d 230             resp.setStatusCode(scheduleResult.getResultCode());
054fb9 231             resp.setResult(scheduleResult.getResult());
ac52ae 232             stScheduleRecordService.create(scheduleResult);
b2bb7d 233             stScheduleSchemeService.updateTime(scheduleResult.getSchemeId(), scheduleResult.getScheduleTime(), scheduleResult.getResultCode());
054fb9 234             log.info("预测计算结束: " + System.currentTimeMillis());
235         } catch (Exception ex) {
236             log.info("调度计算异常: " + System.currentTimeMillis());
ac52ae 237             ex.printStackTrace();
148842 238             return resp;
054fb9 239         }
148842 240         return resp;
7fd198 241     }
95066d 242
268c71 243     /**
244      * 执行调度模型
245      *
246      * @param reqDTO
247      * @return
248      */
249     @Override
250     public MdkScheduleRespDTO runSchedule(MdkScheduleReqDTO reqDTO) {
251         MdkScheduleRespDTO resp = new MdkScheduleRespDTO();
252         resp.setScheduleCode(reqDTO.getScheduleCode());
253         resp.setScheduleTime(reqDTO.getScheduleTime());
254         String catchKey = "ScheduleResult:" + reqDTO.getScheduleCode();
255         try {
256             if (redisTemplate.hasKey(catchKey)) {
257                 log.info("查找调度结果缓存: " + catchKey);
258                 return JSON.parseObject(JSONObject.toJSONString(redisTemplate.opsForValue().get(catchKey)), MdkScheduleRespDTO.class);
259             }
260             log.info("调度计算开始: " + System.currentTimeMillis());
261             log.info("reqDTO=" + JSON.toJSONString(reqDTO));
262             ScheduleResultVO scheduleResult = scheduleModelHandler.doSchedule(reqDTO.getScheduleCode(), reqDTO.getScheduleTime(),
263                     reqDTO.getDynamicDataLength(), reqDTO.getDynamicSettings());
264             resp.setStatusCode(scheduleResult.getResultCode());
265             resp.setResult(scheduleResult.getResult());
266             redisTemplate.opsForValue().set(catchKey, JSON.toJSONString(resp), offset, TimeUnit.SECONDS);
267             stScheduleSchemeService.updateTime(scheduleResult.getSchemeId(), scheduleResult.getScheduleTime(), scheduleResult.getResultCode());
268             log.info("预测计算结束: " + System.currentTimeMillis());
269         } catch (Exception ex) {
270             log.info("调度计算异常: " + System.currentTimeMillis());
271             ex.printStackTrace();
272             return resp;
273         }
274         return resp;
275     }
276
95066d 277     @Override
D 278     public Boolean scheduleModelOut(MdkScheduleRespDTO dto) {
279         String modelId = stScheduleSchemeService.getByCode(dto.getScheduleCode()).getModelId();
2b47c5 280         Map<String, Object> result = dto.getResult();
D 281         List<StScheduleModelOutDTO> list = stScheduleModelOutService.list(modelId);
282         try {
283             for (StScheduleModelOutDTO stScheduleModelOutDTO : list) {
284                 double value = 0;
285                 //判断点位是否下发
286                 if (stScheduleModelOutDTO.getIsWrite().equals(IsWriteEnum.NOTWRITE.value())) {
287                     continue;
288                 }
289                 //返回结果是否存在
290                 if (result.get(stScheduleModelOutDTO.getResultKey()) == null) {
291                     log.error(result.get(stScheduleModelOutDTO.getResultKey()) + "resultKey匹配失败");
292                     continue;
293                 }
294                 Object resultValue = result.get(stScheduleModelOutDTO.getResultKey());
295                 //判断解析方式
296                 ModelOutResultType modelOutResultType = ModelOutResultType.getEumByCode(stScheduleModelOutDTO.getResultType());
297                 switch (modelOutResultType) {
298                     case D:
299                         value = (Double) resultValue;
300                         break;
301                     case D1:
302                         ArrayList<Double> doubleList = (ArrayList<Double>) resultValue;
303                         double[] array1 = new double[doubleList.size()];
304                         for (int i = 0; i < doubleList.size(); i++) {
305                             array1[i] = doubleList.get(i);
95066d 306                         }
2b47c5 307                         if (stScheduleModelOutDTO.getResultPort() < array1.length) {
D 308                             value = array1[stScheduleModelOutDTO.getResultPort()];
309                         } else {
310                             log.error(result.get(stScheduleModelOutDTO.getResultKey()) + "下角标超限");
311                         }
312                         break;
313                     case D2:
314                         ArrayList<ArrayList<Double>> doubleListList = (ArrayList<ArrayList<Double>>) resultValue;
315                         double[][] array2 = new double[doubleListList.size()][];
316                         for (int i = 0; i < doubleListList.size(); i++) {
317                             ArrayList<Double> doubleList2 = doubleListList.get(i);
318                             array2[i] = new double[doubleList2.size()];
319                             for (int j = 0; j < doubleList2.size(); j++) {
320                                 array2[i][j] = doubleList2.get(j);
321                             }
322                         }
323                         if (stScheduleModelOutDTO.getResultPort() < array2.length && stScheduleModelOutDTO.getResultIndex() < array2[stScheduleModelOutDTO.getResultPort()].length) {
324                             value = array2[stScheduleModelOutDTO.getResultPort()][stScheduleModelOutDTO.getResultIndex()];
325                         } else {
326                             log.error(result.get(stScheduleModelOutDTO.getResultKey()) + "下标超限");
327                         }
328                         break;
329                 }
330                 //下发到point点位
331                 ApiPointValueWriteDTO ApiPointValueWriteDTO = new ApiPointValueWriteDTO();
332                 ApiPointValueWriteDTO.setPointNo(stScheduleModelOutDTO.getPointNo());
333                 ApiPointValueWriteDTO.setValue(value);
334                 if (!dataPointApi.writePointRealValue(ApiPointValueWriteDTO)) {
335                     log.error(result.get(stScheduleModelOutDTO.getResultKey()) + "下发数据异常");
336                 }
337             }
338         } catch (Exception ex) {
95066d 339             log.error("下发数据异常");
D 340             ex.printStackTrace();
341         }
342         return true;
343     }
7fd198 344 }