D00008Service.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. package com.miniframe.bisiness.system;
  2. import com.miniframe.constant.MFConstant;
  3. import com.miniframe.core.ExecProcessFlow;
  4. import com.miniframe.core.exception.BusinessException;
  5. import com.miniframe.core.ext.UtilTools;
  6. import com.miniframe.disaster.service.LogService;
  7. import com.miniframe.generate.business.system.model.D00008BaseModel;
  8. import com.miniframe.generate.comm.system.D_D10016_CHVALS_RECODE;
  9. import com.miniframe.generate.comm.system.D_D10016_COCODES_RECODE;
  10. import com.miniframe.model.system.*;
  11. import com.miniframe.model.system.dao.*;
  12. import com.miniframe.service.impl.FileServiceImpl;
  13. import com.miniframe.template.TemplateGenerator;
  14. import com.miniframe.template.TemplateGenerator2;
  15. import com.miniframe.tools.XIDateTimeUtils;
  16. import com.miniframe.tools.XIFileUtils;
  17. import com.miniframe.websocket.WebsocketEndPoint;
  18. import lombok.SneakyThrows;
  19. import tk.mybatis.mapper.util.StringUtil;
  20. import java.io.*;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.concurrent.CompletableFuture;
  25. /**
  26. * 基础系统,“灾情演练”逻辑处理(重新生成不覆盖)。
  27. */
  28. public class D00008Service extends D00008BaseModel implements ExecProcessFlow {
  29. private static final long serialVersionUID = -7051358269847459502L;
  30. /**
  31. * 基础系统,“灾情演练”业务核心处理
  32. */
  33. public void transExecute() throws Exception {
  34. Integer aid =this.getA_d00008().getAid();
  35. /**
  36. * totaltime: 10800 - 模拟时长
  37. * dt: 0.1 - 时间步长
  38. * dx: 10.0 -空间步长
  39. * report step: 60 -输出步长
  40. * interactionstep : 1200 -交互步长
  41. */
  42. String totaltime =this.getA_d00008().getTotaltime();
  43. String dt =this.getA_d00008().getDt();
  44. String dx =this.getA_d00008().getDx();
  45. String reportstep =this.getA_d00008().getReportstep();
  46. String interactionstep =this.getA_d00008().getInteractionstep();
  47. String acctime =this.getA_d00008().getAcctime();
  48. DAccidentMapper accidentDao = UtilTools.getBean(DAccidentMapper.class);
  49. DAccident accident= accidentDao.selectByPrimaryKey(aid);
  50. DJobMapper dJobDao = UtilTools.getBean(DJobMapper.class);
  51. String coids=this.getA_d00008().getCoids();
  52. String cocodes=this.getA_d00008().getCocodes();
  53. DJob job = CreateJob(aid, totaltime, dt, dx,
  54. reportstep, interactionstep, acctime);
  55. if(accident.getStype().equals("Fire")){
  56. if(StringUtil.isEmpty(coids)||StringUtil.isEmpty(cocodes)){
  57. throw new BusinessException("EB3100013");
  58. }
  59. job.setCoids(coids);
  60. job.setCocodes(cocodes);
  61. }else if(accident.getStype().equals("Water")){
  62. job.setCoids("-1");
  63. job.setCocodes("Height");
  64. }
  65. dJobDao.insertSelective(job);
  66. //获取最新JOB
  67. DJob newJob = getNewJob(aid, dJobDao);
  68. Integer jid =newJob.getId();
  69. //存储最新求解ID
  70. changeAccident(accidentDao, accident, jid);
  71. //创建日志
  72. LogService.createLog(accident);
  73. //获取传感器数据
  74. //TODO: 需要从第三方获取
  75. initDNodeVal(aid);
  76. //
  77. TemplateGenerator.createMonitor(aid,jid);//生成监测点文件
  78. if(accident.getStype().equals("Fire")){
  79. TemplateGenerator2.createGeometry(aid,jid);
  80. TemplateGenerator2.createFireControl(aid,jid,totaltime,dt,dx,reportstep,interactionstep,cocodes);
  81. LogService.addLog(accident,"fireControl.ftl配置生成——————————————————成功");
  82. TemplateGenerator2.createFireRunsh(aid,jid);
  83. LogService.addLog(accident,"runFile.sh配置生成——————————————————成功");
  84. TemplateGenerator2.createFireInit(aid,jid);
  85. LogService.addLog(accident,"Fire.init配置生成——————————————————成功");
  86. TemplateGenerator2.createFireEsccapeControl(aid,jid);
  87. LogService.addLog(accident,"FireEscape.control配置生成——————————————————成功");
  88. exeFire(aid,jid);
  89. }
  90. if(accident.getStype().equals("Water")){
  91. TemplateGenerator.createGeometry(aid,jid);//生成几何文件
  92. TemplateGenerator2.createWaterControl(aid,jid,totaltime,dt,dx,reportstep,interactionstep);
  93. LogService.addLog(accident,"water.control配置生成——————————————————成功");
  94. TemplateGenerator2.createWaterRunsh(aid,jid);
  95. LogService.addLog(accident,"runWater.sh配置生成——————————————————成功");
  96. TemplateGenerator2.createWaterInit(aid,jid);
  97. LogService.addLog(accident,"water.init配置生成——————————————————成功");
  98. TemplateGenerator2.createWaterEsccapeControl(aid,jid);
  99. LogService.addLog(accident,"waterEscape.control配置生成——————————————————成功");
  100. exeWater(aid,jid);
  101. }
  102. if(accident.getStype().equals("Gass")){//瓦斯爆炸
  103. DGasMapper gasMapper =UtilTools.getBean(DGasMapper.class);
  104. DGasSQLBuilder gasSb = new DGasSQLBuilder();
  105. DGasSQLBuilder.Criteria gasSc = gasSb.createCriteria();
  106. gasSc.andAidEqualTo(aid);
  107. List<DGas> gases =gasMapper.selectByExample(gasSb);
  108. if(gases.isEmpty()){
  109. throw new BusinessException("EB3000004");
  110. }
  111. for (DGas gas: gases) {
  112. TemplateGenerator.createGasControl(aid,jid,gas.getId(),totaltime,dt,dx,reportstep,interactionstep,cocodes);
  113. TemplateGenerator.createGasInit(aid,jid,gas.getId());
  114. TemplateGenerator.createGassRunsh(aid,jid,gas.getId());
  115. //文件迁移
  116. SysFileMapper sysFileMapper = UtilTools.getBean(SysFileMapper.class);
  117. SysFile gf = sysFileMapper.selectByPrimaryKey(gas.getGfid());
  118. if (gf == null) {
  119. throw new BusinessException("EB3100020");
  120. }
  121. cpFile(XIFileUtils.getRootPathStr() + MFConstant.separator + gf.getFilepath()
  122. ,TemplateGenerator.BPATH + "/" + aid + "/" + jid + "/" + "/gas" + "/" + gas.getId() + "/"+gf.getFilename());
  123. SysFile bf = sysFileMapper.selectByPrimaryKey(gas.getBfid());
  124. if (bf == null) {
  125. throw new BusinessException("EB3100021");
  126. }
  127. cpFile(XIFileUtils.getRootPathStr() + MFConstant.separator + bf.getFilepath()
  128. ,TemplateGenerator.BPATH + "/" + aid + "/" + jid + "/" + "/gas" + "/" + gas.getId() + "/"+bf.getFilename());
  129. //网格文件
  130. //执行文件
  131. // cpGssExe(TemplateGenerator.BPATH + "/" + aid + "/" + jid + "/" + "/gas" + "/" + gas.getId() );
  132. cpFile("/home/disaster/gas/bin/gas_mod.Lewis_2"
  133. ,TemplateGenerator.BPATH + "/" + aid + "/" + jid + "/" + "/gas" + "/" + gas.getId() + "/gas_mod.Lewis_2");
  134. cpFile("/home/disaster/gas/bin/reac_mod.fluent_CH4_Air_5x1"
  135. ,TemplateGenerator.BPATH + "/" + aid + "/" + jid + "/" + "/gas" + "/" + gas.getId() + "/reac_mod.fluent_CH4_Air_5x1");
  136. }
  137. }
  138. }
  139. private DJob CreateJob(Integer aid, String totaltime, String dt, String dx, String reportstep, String interactionstep, String acctime) {
  140. DJob job =new DJob();
  141. job.setAid(aid);
  142. job.setAcctime(acctime);
  143. job.setTotaltime(totaltime);
  144. job.setDt(dt);
  145. job.setDx(dx);
  146. job.setReportstep(reportstep);
  147. job.setInteractionstep(interactionstep);
  148. job.setStarttime(XIDateTimeUtils.getNowStr());
  149. job.setState("0");//未执行
  150. return job;
  151. }
  152. /**
  153. * 任务成功
  154. * @param jid
  155. * @return
  156. */
  157. private void JobSucces(Integer jid) {
  158. System.out.println("JobSucces--------start");
  159. DJobMapper dJobDao= UtilTools.getBean(DJobMapper.class);
  160. DJob job =dJobDao.selectByPrimaryKey(jid);
  161. job.setEndtime(XIDateTimeUtils.getNowStr());
  162. job.setState("1");//完成
  163. dJobDao.updateByPrimaryKey(job);
  164. System.out.println("JobSucces--------end");
  165. }
  166. /**
  167. * 任务失败
  168. * @param jid
  169. * @return
  170. */
  171. private void JobError(Integer jid) {
  172. DJobMapper dJobDao= UtilTools.getBean(DJobMapper.class);
  173. DJob job =dJobDao.selectByPrimaryKey(jid);
  174. job.setEndtime(XIDateTimeUtils.getNowStr());
  175. job.setState("-1");//任务失败
  176. dJobDao.updateByPrimaryKey(job);
  177. }
  178. private DJob getNewJob(Integer aid, DJobMapper dJobDao) {
  179. DJobSQLBuilder jsb = new DJobSQLBuilder();
  180. DJobSQLBuilder.Criteria jsc =jsb.createCriteria();
  181. jsc.andAidEqualTo(aid);
  182. jsb.setOrderByClause("starttime desc");
  183. DJob newJob= dJobDao.selectByExample(jsb).get(0);
  184. return newJob;
  185. }
  186. private void changeAccident(DAccidentMapper accidentDao, DAccident accident, Integer jid) {
  187. accident.setJid(jid);
  188. accidentDao.updateByPrimaryKey(accident);
  189. }
  190. private void initDNodeVal(Integer aid) {
  191. DNodeValMapper dnvm= UtilTools.getBean(DNodeValMapper.class);
  192. DNodeValSQLBuilder sb =new DNodeValSQLBuilder();
  193. DNodeValSQLBuilder.Criteria sc =sb.createCriteria();
  194. sc.andAidEqualTo(aid);
  195. dnvm.deleteByExample(sb);
  196. DNodeMapper dnm = UtilTools.getBean(DNodeMapper.class);
  197. List<DNode> nlist = dnm.selectAll();
  198. for (DNode dn:nlist) {
  199. DNodeVal nv = new DNodeVal();
  200. nv.setAid(aid);
  201. nv.setNid(dn.getId());
  202. nv.setNname(dn.getName());
  203. nv.setVal1(0.0f);
  204. nv.setVal2(300.0f);
  205. nv.setVal3(0.0f);
  206. nv.setVal4(0.0f);
  207. nv.setNtype(dn.getNtype());
  208. dnvm.insertSelective(nv);
  209. }
  210. }
  211. public void cpGssExe(String gassDir) throws IOException {
  212. Runtime runtime = Runtime.getRuntime();
  213. Process p =null;
  214. String shell ="cp -f /home/disaster/gas/bin/* "+ gassDir;
  215. System.out.println(shell);
  216. runtime.exec(shell);
  217. }
  218. public void cpFile(String file1Path,String file2Path) throws IOException {
  219. Runtime runtime = Runtime.getRuntime();
  220. Process p =null;
  221. String shell ="cp -f "+file1Path+" "+ file2Path;
  222. System.out.println(shell);
  223. runtime.exec(shell);
  224. }
  225. public void saveMonitorValue(Integer aid,Integer jid) throws FileNotFoundException, BusinessException {
  226. DJobMapper dJobDao= UtilTools.getBean(DJobMapper.class);
  227. DJob job =dJobDao.selectByPrimaryKey(jid);
  228. Integer stepNum = Integer.valueOf(job.getTotaltime())/Integer.valueOf(job.getReportstep());
  229. DAccidentMapper accidentDao = UtilTools.getBean(DAccidentMapper.class);
  230. DAccident accident= accidentDao.selectByPrimaryKey(aid);
  231. List<DChecknodeVal> nVals =new ArrayList<>();
  232. List<DChecknodeValcode> nValCodes =new ArrayList<>();
  233. for (int step = 1; step <=stepNum ; step++) {
  234. String outPath="";
  235. String fileName ="";
  236. if(accident.getStype().endsWith("Fire")){
  237. outPath= TemplateGenerator.BPATH+"/"+aid+"/"+accident.getJid()+"/fire/out";
  238. fileName ="monitorValue"+step;
  239. }
  240. if(accident.getStype().endsWith("Water")){
  241. outPath=TemplateGenerator.BPATH+"/"+aid+"/"+accident.getJid()+"/water/out";
  242. fileName ="monitorValue"+step;
  243. }
  244. try{
  245. FileReader fileReader = new FileReader(outPath+"/"+fileName);
  246. BufferedReader reader = new BufferedReader(fileReader);
  247. String line;
  248. int linNum =1;
  249. while ((line = reader.readLine()) != null) {
  250. String[] t = line.trim().split(",");
  251. if(linNum==1){
  252. if(nValCodes.isEmpty()){
  253. DChecknodeValcode nValCode = new DChecknodeValcode();
  254. nValCode.setJid(jid);
  255. nValCode.setAid(aid);
  256. nValCode.setValCode1(t.length>1?t[1]:null);
  257. nValCode.setValCode2(t.length>2?t[2]:null);
  258. nValCode.setValCode3(t.length>3?t[3]:null);
  259. nValCode.setValCode4(t.length>4?t[4]:null);
  260. nValCode.setValCode5(t.length>5?t[5]:null);
  261. nValCode.setValCode6(t.length>6?t[6]:null);
  262. nValCodes.add(nValCode);
  263. }
  264. }else {
  265. DChecknodeVal chval=new DChecknodeVal();
  266. chval.setJid(jid);
  267. chval.setAid(aid);
  268. chval.setStep(step);
  269. chval.setChname(t.length>0?t[0]:"0");
  270. chval.setV1(t.length>1?t[1]:"0");
  271. chval.setV2(t.length>2?t[2]:"0");
  272. chval.setV3(t.length>3?t[3]:"0");
  273. chval.setV4(t.length>4?t[4]:"0");
  274. chval.setV5(t.length>5?t[5]:"0");
  275. chval.setV6(t.length>6?t[6]:"0");
  276. nVals.add(chval);
  277. }
  278. linNum++;
  279. }
  280. fileReader.close();
  281. reader.close();
  282. }catch (Exception e){
  283. System.out.println(e);
  284. throw new BusinessException("EB3100014");
  285. }
  286. }
  287. DChecknodeValMapper valMapper =UtilTools.getBean(DChecknodeValMapper.class);
  288. DChecknodeValcodeMapper valcodeMapper =UtilTools.getBean(DChecknodeValcodeMapper.class);
  289. for (DChecknodeVal nVal:nVals) {
  290. valMapper.insertSelective(nVal);
  291. }
  292. for (DChecknodeValcode nValCode:nValCodes) {
  293. valcodeMapper.insertSelective(nValCode);
  294. }
  295. }
  296. //异步执行
  297. @SneakyThrows
  298. public void exeWater(Integer aid,Integer jid) throws Exception{
  299. CompletableFuture<Void> cf = CompletableFuture.supplyAsync(()->{
  300. try {
  301. LogService.addLog(aid,jid,"Water","求解——————————————————开始");
  302. Runtime runtime = Runtime.getRuntime();
  303. Process p =null;
  304. p= runtime.exec("sh "+TemplateGenerator.BPATH+"/"+aid+"/"+jid+"/water"+"/"+"runWater.sh");
  305. InputStream fis = p.getInputStream();
  306. InputStreamReader isr = new InputStreamReader(fis);
  307. BufferedReader br = new BufferedReader(isr);
  308. String line = null;
  309. while ((line = br.readLine()) != null) {
  310. LogService.addLog(aid,jid,"Water",line);
  311. System.out.println(line);
  312. }
  313. saveMonitorValue(aid,jid);
  314. JobSucces(jid);
  315. LogService.addLog(aid,jid,"Water","求解——————————————————成功");
  316. } catch (IOException | BusinessException e) {
  317. LogService.addLog(aid,jid,"Water","求解——————————————————失败");
  318. JobError(jid);
  319. e.printStackTrace();
  320. }
  321. return null;
  322. });
  323. }
  324. //异步执行
  325. @SneakyThrows
  326. public void exeFire(Integer aid,Integer jid) throws Exception{
  327. CompletableFuture<Void> cf = CompletableFuture.supplyAsync(()->{
  328. try {
  329. Runtime runtime = Runtime.getRuntime();
  330. Process p =null;
  331. LogService.addLog(aid,jid,"Fire","求解——————————————————开始");
  332. p= runtime.exec("sh "+TemplateGenerator.BPATH+"/"+
  333. aid+"/"+jid+"/fire"+"/"+"runFile.sh");
  334. InputStream fis = p.getInputStream();
  335. InputStreamReader isr = new InputStreamReader(fis);
  336. BufferedReader br = new BufferedReader(isr);
  337. String line = null;
  338. while ((line = br.readLine()) != null) {
  339. LogService.addLog(aid,jid,"Fire",line);
  340. System.out.println(line);
  341. }
  342. //监测点数据保存
  343. saveMonitorValue(aid,jid);
  344. LogService.addLog(aid,jid,"Fire","求解——————————————————成功");
  345. JobSucces(jid);
  346. } catch (IOException | BusinessException e) {
  347. LogService.addLog(aid,jid,"Fire","求解——————————————————失败");
  348. e.printStackTrace();
  349. JobError(jid);
  350. }
  351. return null;
  352. });
  353. }
  354. /**
  355. *
  356. *
  357. *
  358. *
  359. * 基础系统,“灾情演练”业务前处理
  360. */
  361. public void preTransFlow() throws Exception {
  362. this.validater();
  363. }
  364. /**
  365. * 基础系统,“灾情演练”业务后处理
  366. */
  367. public void afterTransFlow() throws Exception {
  368. }
  369. /**
  370. * 基础系统,“灾情演练”逻辑入口处理方法
  371. */
  372. @SuppressWarnings("rawtypes")
  373. @Override
  374. public Map execute(Map vars) throws Exception {
  375. this.setTransMap(vars);
  376. preTransFlow();// 执行业务开始的规则检查和校验
  377. transExecute();// 执行核心业务段
  378. afterTransFlow();// 执行核心逻辑完成后的收尾逻辑
  379. return this.getTransMap();
  380. }
  381. }