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