D00008Service.java 11 KB

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