package com.miniframe.bisiness.system; import com.miniframe.constant.MFConstant; import com.miniframe.core.ExecProcessFlow; import com.miniframe.core.exception.BusinessException; import com.miniframe.core.ext.UtilTools; import com.miniframe.disaster.service.LogService; import com.miniframe.generate.business.system.model.D00008BaseModel; import com.miniframe.generate.comm.system.D_D10016_CHVALS_RECODE; import com.miniframe.generate.comm.system.D_D10016_COCODES_RECODE; import com.miniframe.model.system.*; import com.miniframe.model.system.dao.*; import com.miniframe.service.impl.FileServiceImpl; import com.miniframe.template.TemplateGenerator; import com.miniframe.template.TemplateGenerator2; import com.miniframe.tools.XIDateTimeUtils; import com.miniframe.tools.XIFileUtils; import com.miniframe.websocket.WebsocketEndPoint; import lombok.SneakyThrows; import tk.mybatis.mapper.util.StringUtil; import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; /** * 基础系统,“灾情演练”逻辑处理(重新生成不覆盖)。 */ public class D00008Service extends D00008BaseModel implements ExecProcessFlow { private static final long serialVersionUID = -7051358269847459502L; /** * 基础系统,“灾情演练”业务核心处理 */ public void transExecute() throws Exception { Integer aid =this.getA_d00008().getAid(); /** * totaltime: 10800 - 模拟时长 * dt: 0.1 - 时间步长 * dx: 10.0 -空间步长 * report step: 60 -输出步长 * interactionstep : 1200 -交互步长 */ String totaltime =this.getA_d00008().getTotaltime(); String dt =this.getA_d00008().getDt(); String dx =this.getA_d00008().getDx(); String reportstep =this.getA_d00008().getReportstep(); String interactionstep =this.getA_d00008().getInteractionstep(); String acctime =this.getA_d00008().getAcctime(); DAccidentMapper accidentDao = UtilTools.getBean(DAccidentMapper.class); DAccident accident= accidentDao.selectByPrimaryKey(aid); DJobMapper dJobDao = UtilTools.getBean(DJobMapper.class); String coids=this.getA_d00008().getCoids(); String cocodes=this.getA_d00008().getCocodes(); DJob job = CreateJob(aid, totaltime, dt, dx, reportstep, interactionstep, acctime); if(accident.getStype().equals("Fire")){ if(StringUtil.isEmpty(coids)||StringUtil.isEmpty(cocodes)){ throw new BusinessException("EB3100013"); } job.setCoids(coids); job.setCocodes(cocodes); }else if(accident.getStype().equals("Water")){ job.setCoids("-1"); job.setCocodes("Height"); } dJobDao.insertSelective(job); //获取最新JOB DJob newJob = getNewJob(aid, dJobDao); Integer jid =newJob.getId(); //存储最新求解ID changeAccident(accidentDao, accident, jid); //创建日志 LogService.createLog(accident); //获取传感器数据 //TODO: 需要从第三方获取 initDNodeVal(aid); // TemplateGenerator.createMonitor(aid,jid);//生成监测点文件 if(accident.getStype().equals("Fire")){ TemplateGenerator2.createGeometry(aid,jid); TemplateGenerator2.createFireControl(aid,jid,totaltime,dt,dx,reportstep,interactionstep,cocodes); LogService.addLog(accident,"fireControl.ftl配置生成——————————————————成功"); TemplateGenerator2.createFireRunsh(aid,jid); LogService.addLog(accident,"runFile.sh配置生成——————————————————成功"); TemplateGenerator2.createFireInit(aid,jid); LogService.addLog(accident,"Fire.init配置生成——————————————————成功"); TemplateGenerator2.createFireEsccapeControl(aid,jid); LogService.addLog(accident,"FireEscape.control配置生成——————————————————成功"); exeFire(aid,jid); } if(accident.getStype().equals("Water")){ TemplateGenerator.createGeometry(aid,jid);//生成几何文件 TemplateGenerator2.createWaterControl(aid,jid,totaltime,dt,dx,reportstep,interactionstep); LogService.addLog(accident,"water.control配置生成——————————————————成功"); TemplateGenerator2.createWaterRunsh(aid,jid); LogService.addLog(accident,"runWater.sh配置生成——————————————————成功"); TemplateGenerator2.createWaterInit(aid,jid); LogService.addLog(accident,"water.init配置生成——————————————————成功"); TemplateGenerator2.createWaterEsccapeControl(aid,jid); LogService.addLog(accident,"waterEscape.control配置生成——————————————————成功"); exeWater(aid,jid); } if(accident.getStype().equals("Gass")){//瓦斯爆炸 DGasMapper gasMapper =UtilTools.getBean(DGasMapper.class); DGasSQLBuilder gasSb = new DGasSQLBuilder(); DGasSQLBuilder.Criteria gasSc = gasSb.createCriteria(); gasSc.andAidEqualTo(aid); List gases =gasMapper.selectByExample(gasSb); if(gases.isEmpty()){ throw new BusinessException("EB3000004"); } for (DGas gas: gases) { TemplateGenerator.createGasControl(aid,jid,gas.getId(),totaltime,dt,dx,reportstep,interactionstep,cocodes); TemplateGenerator.createGasInit(aid,jid,gas.getId()); TemplateGenerator.createGassRunsh(aid,jid,gas.getId()); //文件迁移 SysFileMapper sysFileMapper = UtilTools.getBean(SysFileMapper.class); SysFile gf = sysFileMapper.selectByPrimaryKey(gas.getGfid()); if (gf == null) { throw new BusinessException("EB3100020"); } cpFile(XIFileUtils.getRootPathStr() + MFConstant.separator + gf.getFilepath() ,TemplateGenerator.BPATH + "/" + aid + "/" + jid + "/" + "/gas" + "/" + gas.getId() + "/"+gf.getFilename()); SysFile bf = sysFileMapper.selectByPrimaryKey(gas.getBfid()); if (bf == null) { throw new BusinessException("EB3100021"); } cpFile(XIFileUtils.getRootPathStr() + MFConstant.separator + bf.getFilepath() ,TemplateGenerator.BPATH + "/" + aid + "/" + jid + "/" + "/gas" + "/" + gas.getId() + "/"+bf.getFilename()); //网格文件 //执行文件 // cpGssExe(TemplateGenerator.BPATH + "/" + aid + "/" + jid + "/" + "/gas" + "/" + gas.getId() ); cpFile("/home/disaster/gas/bin/gas_mod.Lewis_2" ,TemplateGenerator.BPATH + "/" + aid + "/" + jid + "/" + "/gas" + "/" + gas.getId() + "/gas_mod.Lewis_2"); cpFile("/home/disaster/gas/bin/reac_mod.fluent_CH4_Air_5x1" ,TemplateGenerator.BPATH + "/" + aid + "/" + jid + "/" + "/gas" + "/" + gas.getId() + "/reac_mod.fluent_CH4_Air_5x1"); } } } private DJob CreateJob(Integer aid, String totaltime, String dt, String dx, String reportstep, String interactionstep, String acctime) { DJob job =new DJob(); job.setAid(aid); job.setAcctime(acctime); job.setTotaltime(totaltime); job.setDt(dt); job.setDx(dx); job.setReportstep(reportstep); job.setInteractionstep(interactionstep); job.setStarttime(XIDateTimeUtils.getNowStr()); job.setState("0");//未执行 return job; } /** * 任务成功 * @param jid * @return */ private void JobSucces(Integer jid) { System.out.println("JobSucces--------start"); DJobMapper dJobDao= UtilTools.getBean(DJobMapper.class); DJob job =dJobDao.selectByPrimaryKey(jid); job.setEndtime(XIDateTimeUtils.getNowStr()); job.setState("1");//完成 dJobDao.updateByPrimaryKey(job); System.out.println("JobSucces--------end"); } /** * 任务失败 * @param jid * @return */ private void JobError(Integer jid) { DJobMapper dJobDao= UtilTools.getBean(DJobMapper.class); DJob job =dJobDao.selectByPrimaryKey(jid); job.setEndtime(XIDateTimeUtils.getNowStr()); job.setState("-1");//任务失败 dJobDao.updateByPrimaryKey(job); } private DJob getNewJob(Integer aid, DJobMapper dJobDao) { DJobSQLBuilder jsb = new DJobSQLBuilder(); DJobSQLBuilder.Criteria jsc =jsb.createCriteria(); jsc.andAidEqualTo(aid); jsb.setOrderByClause("starttime desc"); DJob newJob= dJobDao.selectByExample(jsb).get(0); return newJob; } private void changeAccident(DAccidentMapper accidentDao, DAccident accident, Integer jid) { accident.setJid(jid); accidentDao.updateByPrimaryKey(accident); } private void initDNodeVal(Integer aid) { DNodeValMapper dnvm= UtilTools.getBean(DNodeValMapper.class); DNodeValSQLBuilder sb =new DNodeValSQLBuilder(); DNodeValSQLBuilder.Criteria sc =sb.createCriteria(); sc.andAidEqualTo(aid); dnvm.deleteByExample(sb); DNodeMapper dnm = UtilTools.getBean(DNodeMapper.class); List nlist = dnm.selectAll(); for (DNode dn:nlist) { DNodeVal nv = new DNodeVal(); nv.setAid(aid); nv.setNid(dn.getId()); nv.setNname(dn.getName()); nv.setVal1(0.0f); nv.setVal2(300.0f); nv.setVal3(0.0f); nv.setVal4(0.0f); nv.setNtype(dn.getNtype()); dnvm.insertSelective(nv); } } public void cpGssExe(String gassDir) throws IOException { Runtime runtime = Runtime.getRuntime(); Process p =null; String shell ="cp -f /home/disaster/gas/bin/* "+ gassDir; System.out.println(shell); runtime.exec(shell); } public void cpFile(String file1Path,String file2Path) throws IOException { Runtime runtime = Runtime.getRuntime(); Process p =null; String shell ="cp -f "+file1Path+" "+ file2Path; System.out.println(shell); runtime.exec(shell); } public void saveMonitorValue(Integer aid,Integer jid) throws FileNotFoundException, BusinessException { DJobMapper dJobDao= UtilTools.getBean(DJobMapper.class); DJob job =dJobDao.selectByPrimaryKey(jid); Integer stepNum = Integer.valueOf(job.getTotaltime())/Integer.valueOf(job.getReportstep()); DAccidentMapper accidentDao = UtilTools.getBean(DAccidentMapper.class); DAccident accident= accidentDao.selectByPrimaryKey(aid); List nVals =new ArrayList<>(); List nValCodes =new ArrayList<>(); for (int step = 1; step <=stepNum ; step++) { String outPath=""; String fileName =""; if(accident.getStype().endsWith("Fire")){ outPath= TemplateGenerator.BPATH+"/"+aid+"/"+accident.getJid()+"/fire/out"; fileName ="monitorValue"+step; } if(accident.getStype().endsWith("Water")){ outPath=TemplateGenerator.BPATH+"/"+aid+"/"+accident.getJid()+"/water/out"; fileName ="monitorValue"+step; } try{ FileReader fileReader = new FileReader(outPath+"/"+fileName); BufferedReader reader = new BufferedReader(fileReader); String line; int linNum =1; while ((line = reader.readLine()) != null) { String[] t = line.trim().split(","); if(linNum==1){ if(nValCodes.isEmpty()){ DChecknodeValcode nValCode = new DChecknodeValcode(); nValCode.setJid(jid); nValCode.setAid(aid); nValCode.setValCode1(t.length>1?t[1]:null); nValCode.setValCode2(t.length>2?t[2]:null); nValCode.setValCode3(t.length>3?t[3]:null); nValCode.setValCode4(t.length>4?t[4]:null); nValCode.setValCode5(t.length>5?t[5]:null); nValCode.setValCode6(t.length>6?t[6]:null); nValCodes.add(nValCode); } }else { DChecknodeVal chval=new DChecknodeVal(); chval.setJid(jid); chval.setAid(aid); chval.setStep(step); chval.setChname(t.length>0?t[0]:"0"); chval.setV1(t.length>1?t[1]:"0"); chval.setV2(t.length>2?t[2]:"0"); chval.setV3(t.length>3?t[3]:"0"); chval.setV4(t.length>4?t[4]:"0"); chval.setV5(t.length>5?t[5]:"0"); chval.setV6(t.length>6?t[6]:"0"); nVals.add(chval); } linNum++; } fileReader.close(); reader.close(); }catch (Exception e){ System.out.println(e); throw new BusinessException("EB3100014"); } } DChecknodeValMapper valMapper =UtilTools.getBean(DChecknodeValMapper.class); DChecknodeValcodeMapper valcodeMapper =UtilTools.getBean(DChecknodeValcodeMapper.class); for (DChecknodeVal nVal:nVals) { valMapper.insertSelective(nVal); } for (DChecknodeValcode nValCode:nValCodes) { valcodeMapper.insertSelective(nValCode); } } //异步执行 @SneakyThrows public void exeWater(Integer aid,Integer jid) throws Exception{ CompletableFuture cf = CompletableFuture.supplyAsync(()->{ try { LogService.addLog(aid,jid,"Water","求解——————————————————开始"); Runtime runtime = Runtime.getRuntime(); Process p =null; p= runtime.exec("sh "+TemplateGenerator.BPATH+"/"+aid+"/"+jid+"/water"+"/"+"runWater.sh"); InputStream fis = p.getInputStream(); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { LogService.addLog(aid,jid,"Water",line); System.out.println(line); } saveMonitorValue(aid,jid); JobSucces(jid); LogService.addLog(aid,jid,"Water","求解——————————————————成功"); } catch (IOException | BusinessException e) { LogService.addLog(aid,jid,"Water","求解——————————————————失败"); JobError(jid); e.printStackTrace(); } return null; }); } //异步执行 @SneakyThrows public void exeFire(Integer aid,Integer jid) throws Exception{ CompletableFuture cf = CompletableFuture.supplyAsync(()->{ try { Runtime runtime = Runtime.getRuntime(); Process p =null; LogService.addLog(aid,jid,"Fire","求解——————————————————开始"); p= runtime.exec("sh "+TemplateGenerator.BPATH+"/"+ aid+"/"+jid+"/fire"+"/"+"runFile.sh"); InputStream fis = p.getInputStream(); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { LogService.addLog(aid,jid,"Fire",line); System.out.println(line); } //监测点数据保存 saveMonitorValue(aid,jid); LogService.addLog(aid,jid,"Fire","求解——————————————————成功"); JobSucces(jid); } catch (IOException | BusinessException e) { LogService.addLog(aid,jid,"Fire","求解——————————————————失败"); e.printStackTrace(); JobError(jid); } return null; }); } /** * * * * * 基础系统,“灾情演练”业务前处理 */ public void preTransFlow() throws Exception { this.validater(); } /** * 基础系统,“灾情演练”业务后处理 */ public void afterTransFlow() throws Exception { } /** * 基础系统,“灾情演练”逻辑入口处理方法 */ @SuppressWarnings("rawtypes") @Override public Map execute(Map vars) throws Exception { this.setTransMap(vars); preTransFlow();// 执行业务开始的规则检查和校验 transExecute();// 执行核心业务段 afterTransFlow();// 执行核心逻辑完成后的收尾逻辑 return this.getTransMap(); } }