package com.zt.life.modules.mainPart.async;
import com.alibaba.fastjson.JSONObject;
import com.zt.common.servlet.Result;
import com.zt.life.modules.mainPart.taskReliability.model.ReliaSimLibResult;
import com.zt.life.modules.mainPart.taskReliability.model.SimulatAssess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
@Component
public class PythonLib {
private static final Logger logger = LoggerFactory.getLogger(PythonLib.class);
@Value("${spring.redis.host}")
private String redisHost;
@Value("${spring.redis.port}")
private String redisPort;
@Value("${data.reliaSimLib.mainPy}")
private String reliaSimMain;
@Value("${data.reliaSimLib.resultHome}")
private String resultHome;
// private Long taskId = 123L;
// private Long taskId = 124L;
// private Long taskId = 100L; // =》 model1.xml1
private String taskType = "calcreq";
@Autowired
private RedisTemplate redisTemplate;
public Result callPython(SimulatAssess simulatAssess) {
Result result = null;
InputStream is = null;
BufferedReader br = null;
try {
setParamToRedis(simulatAssess);
Process process = null;
// String command = "python D:\\work\\python\\relia_calc.py";
String command = "python " + reliaSimMain;
command += " -ip " + redisHost + " -port " + redisPort;
command += " -taskType " + taskType + " -taskId " + simulatAssess.getId().toString();
logger.info("cmd命令为:" + command);
if(System.getProperty("os.name").toLowerCase().indexOf("windows") > -1){
process = Runtime.getRuntime().exec(new String[]{"cmd", "/c", command});
}else if(System.getProperty("os.name").toLowerCase().indexOf("linux") > -1){
process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", command});
}else{
throw new Exception("暂不支持该操作系统,进行启动python计算!");
}
is = process.getInputStream();
// 以命令行方式调用python时,返回的结果是utf-8编码
br = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line = br.readLine();
logger.info("python返回结果:" + line);
// int exitCode = process.waitFor();
// if (exitCode == 0) {
logger.info("启动python计算成功");
if (line != null) {
ReliaSimLibResult rtn = JSONObject.parseObject(line, ReliaSimLibResult.class);
if ("0".equals(rtn.getCode())) {
result = Result.ok();
} else {
String errorMsg = rtn.getErrorMsg();
throw new RuntimeException("启动python计算失败: errorMsg=" + errorMsg);
}
}
// } else {
// logger.error("启动python计算失败: exitCode=" + exitCode);
// throw new RuntimeException("启动python计算失败: exitCode=" + exitCode);
// }
} catch (Exception e) {
logger.error("启动python计算时发生Exception:", e);
e.printStackTrace();
result = Result.error(e.getMessage());
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return result;
}
/*
public String getPythonCalcResult() {
String key = taskId.toString() + "calcprog";
logger.info("redis key:" + key);
String progress = (String)redisTemplate.opsForValue().get(key);
if (progress==null) progress = "0";
return progress;
}
*/
private void setParamToRedis(SimulatAssess simulatAssess) {
String key = simulatAssess.getId().toString() + taskType;
logger.info("redis key:" + key);
String xmlfile = "\n" +
"\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
"\t\t\t\t \n" +
"\t\t\t\t\t \n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
" \n" +
"\t\t\t\t\t\t \n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\n" +
" \n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
" \n" +
"\t\t\t\t\t\t \n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t \n" +
"\t\t\t\t\t\n" +
" \n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t \n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
"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\t\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\n" +
"\t\t\t\t\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
"";
/*
String xmlfile = "\n" +
"\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
"\t\t\t\t \n" +
"\t\t\t\t\t \n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
" \n" +
"\t\t\t\t\t\t \n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\n" +
" \n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
" \n" +
"\t\t\t\t\t\t \n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t \n" +
"\t\t\t\t\t\n" +
"\t\t\t\t\n" +
"\t\t\t\t \n" +
"\t\t\t\t\t\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\n" +
"\t\t\t\t\t\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\n" +
"\t\t\t\t\n" +
" \n" +
" \n" +
" \n" +
" \n" +
"\t\t\t\t \n" +
"\t\t\t\t\t \n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
" \n" +
"\t\t\t\t\t\t \n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\n" +
" \n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
" \n" +
"\t\t\t\t\t\t \n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t \n" +
"\t\t\t\t\t\n" +
"\t\t\t\t\n" +
"\t\t\t\t \n" +
"\t\t\t\t\t\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\n" +
"\t\t\t\t\t\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\n" +
"\t\t\t\t\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
"";
*/
JSONObject jsonObject = new JSONObject();
/*
Long simulate_times = 10000L;
Long timeslice = 10L;
jsonObject.put("xmlfile", xmlfile);
jsonObject.put("simulate_times", simulate_times);
jsonObject.put("seed", 1000);
jsonObject.put("timeslice", 1);
// jsonObject.put("timeslice", timeslice/60.0);
// jsonObject.put("timeslice", timeslice);
*/
jsonObject.put("xmlfile", xmlfile);
jsonObject.put("simulate_times", simulatAssess.getSimulatFrequency());
jsonObject.put("seed", 1000);
jsonObject.put("timeslice", simulatAssess.getSamplPeriod()/60.0);
jsonObject.put("result_home", resultHome);
redisTemplate.opsForValue().set(key, jsonObject.toJSONString());
}
}