D00008Service.java 12 KB

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