| | |
| | | package com.dy.pipIrrRemote.rtuUpgrage; |
| | | |
| | | import com.dy.common.contant.Constant; |
| | | import com.dy.common.multiDataSource.DataSourceContext; |
| | | import com.dy.common.softUpgrade.state.UpgradeInfo; |
| | | import com.dy.common.softUpgrade.state.UpgradeRtu; |
| | | import com.dy.common.softUpgrade.state.UpgradeState; |
| | |
| | | import com.dy.common.util.DateTime; |
| | | import com.dy.common.util.ThreadJob; |
| | | import com.dy.common.webUtil.BaseResponse; |
| | | import com.dy.pipIrrGlobal.pojoRm.UgRtuProgram; |
| | | import com.dy.pipIrrGlobal.pojoRm.UgRtuTask; |
| | | import com.dy.pipIrrGlobal.pojoUg.UgRtuProgram; |
| | | import com.dy.pipIrrGlobal.pojoUg.UgRtuTask; |
| | | import io.swagger.v3.oas.annotations.Hidden; |
| | | import io.swagger.v3.oas.annotations.tags.Tag; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | |
| | | */ |
| | | @Hidden //不公开接口,其只有通信中间件调用 |
| | | @PostMapping(path = "/receive") |
| | | public BaseResponse<Boolean> receive(@RequestBody UpgradeInfo info){ |
| | | log.info("接收到的RTU远程升级状态数据为:{}", info.toString()); |
| | | public BaseResponse<Boolean> receive(@RequestBody UpgradeInfo info, HttpServletRequest req, HttpServletResponse rep){ |
| | | log.info("接收到的RTU远程升级状态数据为:{}", info.toString("532328000214")); |
| | | //进行排序 |
| | | //Comparator<UpgradeRtu> comparator = Comparator.comparing(UpgradeRtu::getRtuAddr, Comparator.naturalOrder()); |
| | | //info.ugRtuStateList = info.ugRtuStateList.stream().sorted(comparator).collect(Collectors.toList()); |
| | | |
| | | //通信中间件传过来的机构tag,以用于查找数据源 |
| | | String token = req.getHeader(Constant.UserTokenKeyInHeader); |
| | | DataSourceContext.set(token); |
| | | |
| | | if(info.ugRtuStateList != null && info.ugRtuStateList.size() > 0){ |
| | | if(info.ugRtuStateList.size() < 10){ |
| | |
| | | |
| | | private static ThreadJob threadJob ; |
| | | |
| | | protected void resetDemo(){ |
| | | if(threadJob != null){ |
| | | threadJob.stop() ; |
| | | threadJob = null ; |
| | | } |
| | | cache = null ; |
| | | } |
| | | |
| | | protected void demo(){ |
| | | if(cache == null){ |
| | | UgRtuTask tpo = this.sv.selectLastTask() ; |
| | | if(tpo != null) { |
| | | UgRtuProgram ppo = this.sv.selectProgramById(tpo.programId); |
| | | if (ppo == null) { |
| | | if (ppo != null) { |
| | | List<String> taskRtuAddrs = this.sv.selectAllRtuAddrByTask("" + tpo.id); |
| | | if (taskRtuAddrs != null && taskRtuAddrs.size() > 0) { |
| | | cache = new UpgradeInfo(); |
| | |
| | | threadJob = new ThreadJob() { |
| | | @Override |
| | | public Object execute() throws Exception { |
| | | runDemo() ; |
| | | while(!this.stop){ |
| | | if(!runDemo()){ |
| | | this.stop = true ; |
| | | }else{ |
| | | try { |
| | | Thread.sleep(500); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | }; |
| | |
| | | } |
| | | } |
| | | } |
| | | private void runDemo(){ |
| | | boolean hasRunning = true ; |
| | | while (true){ |
| | | for(UpgradeRtu rtu : cache.ugRtuStateList){ |
| | | this.rtuUpgrade(rtu) ; |
| | | hasRunning = this.statisticsNowUpgradeState() ; |
| | | if(!hasRunning){ |
| | | break ; |
| | | } |
| | | } |
| | | if(!hasRunning){ |
| | | break ; |
| | | } |
| | | try { |
| | | Thread.sleep(500); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | private boolean runDemo(){ |
| | | for(UpgradeRtu rtu : cache.ugRtuStateList){ |
| | | this.rtuUpgrade(rtu) ; |
| | | } |
| | | return this.statisticsNowUpgradeState() ; |
| | | } |
| | | private void rtuUpgrade(UpgradeRtu rtu){ |
| | | if(rtu.lastDownDt == null){ |
| | |
| | | //离线的,不处理 |
| | | return ; |
| | | } |
| | | int n = Integer.parseInt(new CreateRandom().create(2)) ; |
| | | if(n == 44 || n == 45 || n == 54){ |
| | | if(rtu.currentPackage == 1){ |
| | | //1包死 |
| | | rtu.state = UpgradeRtu.STATE_FAILONE ; |
| | | return ; |
| | | } |
| | | } |
| | | |
| | | if(rtu.currentPackage == rtu.totalPackage){ |
| | | //升级结束 |
| | | rtu.state = UpgradeRtu.STATE_SUCCESS ; |
| | | rtu.isOver = true ; |
| | | return ; |
| | | } |
| | | if(rtu.reTryTimes >= 2){ |
| | | //升级结束 |
| | | rtu.isOver = true ; |
| | | return ; |
| | | } |
| | | |
| | | rtu.state = UpgradeRtu.STATE_RUNNING ; |
| | | rtu.currentPackage += 1 ; |
| | | rtu.currentRamAddr = 0x00 + UpgradeRtu.RAMADDRADD ; |
| | | rtu.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; |
| | | rtu.reTryTimes = 0 ; |
| | | rtu.isOver = false ; |
| | | int n = Integer.parseInt(new CreateRandom().create(3)) ; |
| | | if(n == 540 || n == 541 || n == 542 || n == 543 || n == 544 || n == 545){ |
| | | if(rtu.currentPackage == 1){ |
| | | //1包死 |
| | | rtu.state = UpgradeRtu.STATE_FAILONE ; |
| | | return ; |
| | | } |
| | | } |
| | | |
| | | if(n == 450 || n == 451 || n == 452 || n == 453 || n == 454 || n == 455){ |
| | | if(rtu.currentPackage != 1){ |
| | | //升死 |
| | | rtu.state = UpgradeRtu.STATE_FAIL ; |
| | | return ; |
| | | } |
| | | } |
| | | |
| | | if(rtu.state != UpgradeRtu.STATE_FAILONE && |
| | | rtu.state != UpgradeRtu.STATE_FAIL){ |
| | | if(rtu.reTryTimes < 2){ |
| | | rtu.state = UpgradeRtu.STATE_RUNNING ; |
| | | rtu.currentPackage = 1 ; |
| | | rtu.currentRamAddr = 0x00 ; |
| | | rtu.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; |
| | | rtu.reTryTimes++ ; |
| | | rtu.isOver = false ; |
| | | return ; |
| | | } |
| | | } |
| | | |
| | | if(rtu.state != UpgradeRtu.STATE_FAILONE && |
| | | rtu.state != UpgradeRtu.STATE_FAIL){ |
| | | rtu.state = UpgradeRtu.STATE_RUNNING ; |
| | | rtu.currentPackage += 1 ; |
| | | rtu.currentRamAddr = 0x00 + UpgradeRtu.RAMADDRADD ; |
| | | rtu.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; |
| | | rtu.reTryTimes = 0 ; |
| | | rtu.isOver = false ; |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | public boolean statisticsNowUpgradeState() { |
| | | boolean hasRunning = false ; |
| | | if(cache.ugRtuStateList != null && cache.ugRtuStateList.size() > 0){ |
| | | cache.ugOverallState.init(); |
| | | cache.ugOverallState.rtuTotal = cache.ugRtuStateList.size() ; |
| | | for(UpgradeRtu rtu : cache.ugRtuStateList){ |
| | | if(rtu.state == UpgradeRtu.STATE_OFFLINE){ |
| | | cache.ugOverallState.offLineTotal ++ ; |
| | |
| | | } |
| | | } |
| | | } |
| | | if(!hasRunning){ |
| | | cache.ugOverallState.allOver = true ; |
| | | } |
| | | return hasRunning ; |
| | | } |
| | | } |