提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.module.model.handler; |
H |
2 |
|
|
3 |
import com.alibaba.fastjson.JSON; |
|
4 |
import com.alibaba.fastjson.JSONArray; |
|
5 |
import com.alibaba.fastjson.JSONObject; |
|
6 |
import com.fasterxml.jackson.core.JsonProcessingException; |
|
7 |
import com.fasterxml.jackson.databind.ObjectMapper; |
|
8 |
import com.iailab.common.utils.PythonUtil; |
|
9 |
import com.iailab.module.mcs.dto.StModelDTO; |
|
10 |
import com.iailab.module.mcs.dto.StModelRequestLogDTO; |
|
11 |
import com.iailab.module.mcs.dto.StModelRunlogDTO; |
|
12 |
import com.iailab.module.mcs.service.*; |
|
13 |
import com.iailab.module.model.IAILModel; |
|
14 |
import com.iailab.module.model.command.MLCommand; |
|
15 |
import com.iailab.module.model.command.PyCommand; |
|
16 |
import com.iailab.module.model.sample.constructor.SampleConstructor; |
|
17 |
import com.iailab.module.model.sample.dto.SampleData; |
|
18 |
import lombok.extern.slf4j.Slf4j; |
|
19 |
import javax.annotation.Resource; |
|
20 |
import org.springframework.stereotype.Component; |
|
21 |
import org.springframework.util.CollectionUtils; |
|
22 |
|
|
23 |
import java.util.*; |
|
24 |
|
|
25 |
/** |
|
26 |
* 浮选模型处理 |
|
27 |
* |
|
28 |
* @author PanZhibao |
|
29 |
* @Description |
|
30 |
* @createTime 2023年05月16日 13:33:00 |
|
31 |
*/ |
|
32 |
@Slf4j |
|
33 |
@Component |
|
34 |
public class ModelHandler { |
|
35 |
|
|
36 |
@Resource |
|
37 |
private PyCommand pyCommand; |
|
38 |
|
|
39 |
@Resource |
|
40 |
private MLCommand mLCommand; |
|
41 |
|
|
42 |
@Resource |
|
43 |
private StModelService stModelService; |
|
44 |
|
|
45 |
@Resource |
|
46 |
private StModelSettingService stModelSettingService; |
|
47 |
|
|
48 |
@Resource |
|
49 |
private StModelRunlogService runlogService; |
|
50 |
|
|
51 |
@Resource |
|
52 |
private StModelResultService stModelResultService; |
|
53 |
|
|
54 |
@Resource |
|
55 |
private SampleConstructor sampleConstructor; |
|
56 |
|
|
57 |
@Resource |
|
58 |
private StModelRequestLogService stModelRequestLogService; |
|
59 |
|
|
60 |
/** |
|
61 |
* 运行模型 |
|
62 |
* |
|
63 |
* @param modelCode |
|
64 |
* @param runTime |
|
65 |
* @return |
|
66 |
* @throws Exception |
|
67 |
*/ |
|
68 |
public Map<String, Object> run(String modelCode, Date runTime) throws Exception { |
|
69 |
Map<String, Object> result = new HashMap<>(); |
|
70 |
Calendar calendar = Calendar.getInstance(); |
|
71 |
calendar.setTime(runTime); |
|
72 |
calendar.set(Calendar.SECOND, 0); |
|
73 |
Date dataTime = calendar.getTime(); |
|
74 |
try { |
|
75 |
// 1、查找模型 |
|
76 |
StModelDTO stModel = stModelService.getByCode(modelCode); |
|
77 |
if (stModel == null) { |
|
78 |
throw new Exception("modelCode错误!"); |
|
79 |
} |
|
80 |
|
|
81 |
// 2、构建输入参数 |
|
82 |
List<SampleData> sampleDataList = sampleConstructor.constructSample(stModel.getId(), dataTime); |
|
83 |
List<Object> params = new ArrayList<>(); |
|
84 |
if (!CollectionUtils.isEmpty(sampleDataList)) { |
|
85 |
sampleDataList.forEach(item -> { |
|
86 |
params.add(item.getMatrix()); |
|
87 |
}); |
|
88 |
} |
|
89 |
|
|
90 |
// 3、构建配置参数 |
|
91 |
Map<String, Object> settings = stModelSettingService.getByModelId(stModel.getId()); |
|
92 |
|
|
93 |
// 4、执行算法 |
|
94 |
log.info("################ modelCode=" + modelCode + " ###################"); |
|
95 |
String[] command = pyCommand.getCommand(stModel, params, settings); |
|
96 |
log.info("command=" + JSONArray.toJSONString(command)); |
|
97 |
result = PythonUtil.execPy(command); |
|
98 |
log.info("result=" + JSON.toJSONString(result)); |
|
99 |
stModelResultService.addML(stModel.getId(), result, runTime); |
|
100 |
stModelService.updateTime(stModel.getId(), runTime); |
|
101 |
|
|
102 |
// 6、插入运行日志 |
|
103 |
String jsonString = dealResult(result); |
|
104 |
this.addLog(stModel.getId(), modelCode, runTime, jsonString); |
|
105 |
} catch (Exception ex) { |
|
106 |
ex.printStackTrace(); |
|
107 |
throw ex; |
|
108 |
} |
|
109 |
return result; |
|
110 |
} |
|
111 |
|
|
112 |
public String dealResult(Map<String, Object> result) { |
|
113 |
ObjectMapper mapper = new ObjectMapper(); |
|
114 |
String jsonString = ""; |
|
115 |
try { |
|
116 |
jsonString = mapper.writeValueAsString(result); |
|
117 |
jsonString = jsonString.replace("mv_fl_kd_307", "307分流阀开度"); |
|
118 |
jsonString = jsonString.replace("mv_hj_water_valve_main_307", "315合介桶加水阀开度"); |
|
119 |
jsonString = jsonString.replace("mv_xj_water_valve_307", "333稀介桶加水阀开度"); |
|
120 |
jsonString = jsonString.replace("mv_hj_water_valve_small_307", "315合介桶密度微调阀开度"); |
|
121 |
jsonString = jsonString.replace("mv_mn_water_valve_307", "325煤泥桶加水阀开度"); |
|
122 |
|
|
123 |
jsonString = jsonString.replace("mv_fl_kd_308", "308分流阀开度"); |
|
124 |
jsonString = jsonString.replace("mv_hj_water_valve_main_308", "316合介桶加水阀开度"); |
|
125 |
jsonString = jsonString.replace("mv_xj_water_valve_308", "332稀介桶加水阀开度"); |
|
126 |
jsonString = jsonString.replace("mv_hj_water_valve_small_308", "316合介桶密度微调阀开度"); |
|
127 |
jsonString = jsonString.replace("mv_mn_water_valve_308", "325煤泥桶加水阀开度"); |
|
128 |
|
|
129 |
jsonString = jsonString.replace("mv_valve_fl_3318", "3318分流阀开度"); |
|
130 |
jsonString = jsonString.replace("mv_pump_hl_3318", "3308混料桶排水泵频率"); |
|
131 |
jsonString = jsonString.replace("mv_pump_hj_3318", "3344合介桶排水泵频率"); |
|
132 |
jsonString = jsonString.replace("mv_valve_hj_water_3318", "3344合介桶加水阀开度"); |
|
133 |
jsonString = jsonString.replace("mv_valve_jj_3318", "3344合介桶加介阀开度"); |
|
134 |
jsonString = jsonString.replace("mv_valve_xj_water_3318", "3348稀介桶加水阀开度"); |
|
135 |
jsonString = jsonString.replace("mv_pump_xj_3318", "3348稀介桶排水泵频率"); |
|
136 |
jsonString = jsonString.replace("mv_pump_mn_3318", "4402煤泥桶排水泵频率"); |
|
137 |
jsonString = jsonString.replace("mv_valve_mn_water_3318", "4402煤泥桶加水阀开度"); |
|
138 |
jsonString = jsonString.replace("target_md_3318", "3318目标合介密度"); |
|
139 |
|
|
140 |
|
|
141 |
jsonString = jsonString.replace("mv_valve_fl_3319", "3319分流阀开度"); |
|
142 |
jsonString = jsonString.replace("mv_pump_hl_3319", "3317混料桶排水泵频率"); |
|
143 |
jsonString = jsonString.replace("mv_pump_hj_3319", "3345合介桶排水泵频率"); |
|
144 |
jsonString = jsonString.replace("mv_valve_hj_water_3319", "3345合介桶加水阀开度"); |
|
145 |
jsonString = jsonString.replace("mv_valve_jj_3319", "3345合介桶加介阀开度"); |
|
146 |
jsonString = jsonString.replace("mv_valve_xj_water_3319", "3349稀介桶加水阀开度"); |
|
147 |
jsonString = jsonString.replace("mv_pump_xj_3319", "3349稀介桶排水泵频率"); |
|
148 |
jsonString = jsonString.replace("mv_pump_mn_3319", "4403煤泥桶排水泵频率"); |
|
149 |
jsonString = jsonString.replace("mv_valve_mn_water_3319", "4403煤泥桶加水阀开度"); |
|
150 |
jsonString = jsonString.replace("target_md_3319", "3319目标合介密度"); |
|
151 |
|
|
152 |
jsonString = jsonString.replace("status_first_start", "第一组风机启动信号"); |
|
153 |
jsonString = jsonString.replace("status_first_stop", "第一组风机停止信号"); |
|
154 |
jsonString = jsonString.replace("status_second_start", "第二组风机启动信号"); |
|
155 |
jsonString = jsonString.replace("status_second_stop", "第二组风机停止信号"); |
|
156 |
jsonString = jsonString.replace("mv_1_346", "346高压风机频率设定"); |
|
157 |
jsonString = jsonString.replace("mv_1_347", "347高压风机频率设定"); |
|
158 |
jsonString = jsonString.replace("mv_2_4454", "4454高压风机频率设定"); |
|
159 |
|
|
160 |
jsonString = jsonString.replace("Thickener_OF_XL", "501 浓缩池循环水池浊度预测值"); |
|
161 |
jsonString = jsonString.replace("Thickener_BF_XL", "501浓缩池出料浓度预测值"); |
|
162 |
jsonString = jsonString.replace("Thickener_H_XL", "501浓缩池污泥厚度预测值"); |
|
163 |
jsonString = jsonString.replace("Dos_yin_det1_XL", "507_1加药泵频率"); |
|
164 |
jsonString = jsonString.replace("Dos_yang_det1_XL", "508_1加药泵频率"); |
|
165 |
jsonString = jsonString.replace("Dos_yin_det2_XL", "507_2加药泵频率"); |
|
166 |
jsonString = jsonString.replace("Dos_yang_det2_XL", "508_2加药泵频率"); |
|
167 |
|
|
168 |
jsonString = jsonString.replace("Thickener_OF_QC", "浓缩池循环水池浊度预测值"); |
|
169 |
jsonString = jsonString.replace("Thickener_BF_QC", "浓缩池出料浓度预测值"); |
|
170 |
jsonString = jsonString.replace("Thickener_H_QC", "浓缩池污泥厚度预测值"); |
|
171 |
jsonString = jsonString.replace("Dos_yin_det_QC", "350加药系统频率"); |
|
172 |
jsonString = jsonString.replace("Dos_yang_det_QC", "351加药系统频率"); |
|
173 |
|
|
174 |
jsonString = jsonString.replace("UF_pumb1", "503底流泵启停"); |
|
175 |
jsonString = jsonString.replace("UF_pumb2", "502底流泵启停"); |
|
176 |
jsonString = jsonString.replace("zt4418_res", "4418启停信号"); |
|
177 |
jsonString = jsonString.replace("zt4419_res", "4419启停信号"); |
|
178 |
jsonString = jsonString.replace("zt4420_res", "4420启停信号"); |
|
179 |
jsonString = jsonString.replace("zt4421_res", "4421启停信号"); |
|
180 |
jsonString = jsonString.replace("zt4422_res", "4422启停信号"); |
|
181 |
jsonString = jsonString.replace("zt4428_res", "4428启停信号"); |
|
182 |
jsonString = jsonString.replace("zt4429_res", "4429启停信号"); |
|
183 |
jsonString = jsonString.replace("zt4430_res", "4430启停信号"); |
|
184 |
jsonString = jsonString.replace("zt4431_res", "4431启停信号"); |
|
185 |
jsonString = jsonString.replace("zt4432_res", "4432启停信号"); |
|
186 |
|
|
187 |
|
|
188 |
} catch (JsonProcessingException e) { |
|
189 |
e.printStackTrace(); |
|
190 |
} |
|
191 |
return jsonString; |
|
192 |
} |
|
193 |
|
|
194 |
/** |
|
195 |
* 调用模型 |
|
196 |
* |
|
197 |
* @param modelCode |
|
198 |
* @param runTime |
|
199 |
* @param sampleDataList |
|
200 |
* @param appKey |
|
201 |
* @return |
|
202 |
* @throws Exception |
|
203 |
*/ |
|
204 |
public Map<String, Object> run(String modelCode, Date runTime, List<double[][]> sampleDataList, String appKey) throws Exception { |
|
205 |
Map<String, Object> result = new HashMap<>(); |
|
206 |
try { |
|
207 |
// 1、查找模型 |
|
208 |
StModelDTO stModel = stModelService.getByCode(modelCode); |
|
209 |
if (stModel == null) { |
|
210 |
throw new Exception("modelCode错误!"); |
|
211 |
} |
|
212 |
|
|
213 |
// 2、构建输入参数 |
|
214 |
List<Object> params = new ArrayList<>(); |
|
215 |
if (!CollectionUtils.isEmpty(sampleDataList)) { |
|
216 |
sampleDataList.forEach(item -> { |
|
217 |
params.add(item); |
|
218 |
}); |
|
219 |
} |
|
220 |
|
|
221 |
// 3、构建配置参数 |
|
222 |
Map<String, Object> settings = stModelSettingService.getByModelId(stModel.getId()); |
|
223 |
|
|
224 |
// 4、执行算法 |
|
225 |
log.info("################ modelCode=" + modelCode + " ###################"); |
|
226 |
String[] command = pyCommand.getCommand(stModel, params, settings); |
|
227 |
log.info("command=" + JSONArray.toJSONString(command)); |
|
228 |
result = PythonUtil.execPy(command); |
|
229 |
log.info("result=" + JSON.toJSONString(result)); |
|
230 |
|
|
231 |
// 5、插入调用日志 |
|
232 |
StModelRequestLogDTO logDto = new StModelRequestLogDTO(); |
|
233 |
logDto.setModelId(stModel.getId()); |
|
234 |
logDto.setAppKey(appKey); |
|
235 |
logDto.setRequestTime(runTime); |
|
236 |
logDto.setRequestParams(JSONArray.toJSONString(sampleDataList)); |
|
237 |
logDto.setResponseResult(JSONObject.toJSONString(result)); |
|
238 |
stModelRequestLogService.save(logDto); |
|
239 |
} catch (Exception ex) { |
|
240 |
ex.printStackTrace(); |
|
241 |
throw ex; |
|
242 |
} |
|
243 |
return result; |
|
244 |
} |
|
245 |
|
|
246 |
/** |
|
247 |
* 调用模型 |
|
248 |
* |
|
249 |
* @param modelCode |
|
250 |
* @param sampleDataList |
|
251 |
* @return |
|
252 |
* @throws Exception |
|
253 |
*/ |
|
254 |
public Map<String, Object> run(String modelCode, List<double[][]> sampleDataList) throws Exception { |
|
255 |
Map<String, Object> result = new HashMap<>(); |
|
256 |
try { |
|
257 |
// 1、查找模型 |
|
258 |
StModelDTO stModel = stModelService.getByCode(modelCode); |
|
259 |
if (stModel == null) { |
|
260 |
throw new Exception("modelCode错误!"); |
|
261 |
} |
|
262 |
|
|
263 |
// 2、构建输入参数 |
|
264 |
List<Object> params = new ArrayList<>(); |
|
265 |
if (!CollectionUtils.isEmpty(sampleDataList)) { |
|
266 |
sampleDataList.forEach(item -> { |
|
267 |
params.add(item); |
|
268 |
}); |
|
269 |
} |
|
270 |
|
|
271 |
// 3、构建配置参数 |
|
272 |
Map<String, Object> settings = stModelSettingService.getByModelId(stModel.getId()); |
|
273 |
|
|
274 |
// 4、执行算法 |
|
275 |
log.info("################ modelCode=" + modelCode + " ###################"); |
|
276 |
String[] command = pyCommand.getCommand(stModel, params, settings); |
|
277 |
log.info("command=" + JSONArray.toJSONString(command)); |
|
278 |
result = PythonUtil.execPy(command); |
|
279 |
log.info("result=" + JSON.toJSONString(result)); |
|
280 |
|
|
281 |
} catch (Exception ex) { |
|
282 |
ex.printStackTrace(); |
|
283 |
throw ex; |
|
284 |
} |
|
285 |
return result; |
|
286 |
} |
|
287 |
|
|
288 |
/** |
|
289 |
* 调用模型 |
|
290 |
* |
|
291 |
* @param modelCode |
|
292 |
* @param sampleDataList |
|
293 |
* @param settings |
|
294 |
* @return |
|
295 |
* @throws Exception |
|
296 |
*/ |
|
297 |
public Map<String, Object> run(String modelCode, List<double[][]> sampleDataList, Map<String, Object> settings) throws Exception { |
|
298 |
Map<String, Object> result = new HashMap<>(); |
|
299 |
try { |
|
300 |
// 1、查找模型 |
|
301 |
StModelDTO stModel = stModelService.getByCode(modelCode); |
|
302 |
if (stModel == null) { |
|
303 |
throw new Exception("modelCode错误!"); |
|
304 |
} |
|
305 |
|
|
306 |
// 2、构建输入参数 |
|
307 |
List<Object> params = new ArrayList<>(); |
|
308 |
if (!CollectionUtils.isEmpty(sampleDataList)) { |
|
309 |
sampleDataList.forEach(item -> { |
|
310 |
params.add(item); |
|
311 |
}); |
|
312 |
} |
|
313 |
|
|
314 |
// 4、执行算法 |
|
315 |
log.info("################ modelCode=" + modelCode + " ###################"); |
|
316 |
String[] command = pyCommand.getCommand(stModel, params, settings); |
|
317 |
log.info("command=" + JSONArray.toJSONString(command)); |
|
318 |
result = PythonUtil.execPy(command); |
|
319 |
log.info("result=" + JSON.toJSONString(result)); |
|
320 |
|
|
321 |
} catch (Exception ex) { |
|
322 |
ex.printStackTrace(); |
|
323 |
throw ex; |
|
324 |
} |
|
325 |
return result; |
|
326 |
} |
|
327 |
|
|
328 |
/** |
|
329 |
* 构造IAILMDK.run()方法的newModelBean参数 |
|
330 |
* |
|
331 |
* @param stModel |
|
332 |
* @return |
|
333 |
*/ |
|
334 |
private IAILModel composeNewModelBean(StModelDTO stModel) { |
|
335 |
IAILModel newModelBean = new IAILModel(); |
|
336 |
newModelBean.setClassName(stModel.getClassName().trim()); |
|
337 |
newModelBean.setMethodName(stModel.getMethodName().trim()); |
|
338 |
//构造参数类型 |
|
339 |
String[] paArStr = stModel.getParamsStructure().trim().split(","); |
|
340 |
Class<?>[] paramsArray = new Class[paArStr.length]; |
|
341 |
for (int i = 0; i < paArStr.length; i++) { |
|
342 |
if ("[[D".equals(paArStr[i])) { |
|
343 |
paramsArray[i] = double[][].class; |
|
344 |
} else if ("Map".equals(paArStr[i]) || "java.util.HashMap".equals(paArStr[i])) { |
|
345 |
paramsArray[i] = HashMap.class; |
|
346 |
} |
|
347 |
} |
|
348 |
newModelBean.setParamsArray(paramsArray); |
|
349 |
return newModelBean; |
|
350 |
} |
|
351 |
|
|
352 |
public void addLog(String modelId, String modelCode, Date runTime, String jsonString) { |
|
353 |
StModelRunlogDTO logDto = new StModelRunlogDTO(); |
|
354 |
logDto.setModelid(modelId); |
|
355 |
logDto.setRunTime(runTime); |
|
356 |
logDto.setRunType(modelCode); |
|
357 |
logDto.setRunResult(jsonString); |
|
358 |
runlogService.add(logDto); |
|
359 |
} |
|
360 |
} |