|  |  |  | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Comparator; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | private RtuUpgradeSv sv ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 强制结束升级任务通信中间件成功执行后 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static void afterMwForceOverCurUgTask(){ | 
|---|
|  |  |  | cache = null ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * rtu远程升级任务通信中间件执行情况统计回收 | 
|---|
|  |  |  | * @param info 数据 | 
|---|
|  |  |  | * @return 操作结果 | 
|---|
|  |  |  | 
|---|
|  |  |  | @Hidden //不公开接口,其只有通信中间件调用 | 
|---|
|  |  |  | @PostMapping(path = "/receive") | 
|---|
|  |  |  | 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){ | 
|---|
|  |  |  | for (UpgradeRtu rtuVo : info.ugRtuStateList) { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | */ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //进行排序 | 
|---|
|  |  |  | 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(cache == null){ | 
|---|
|  |  |  | cache = info; | 
|---|
|  |  |  | //此时不做任务操作,只保障cache不为空, 等待下次发来数据 | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | cache = info; | 
|---|
|  |  |  | //当cache中有值时,进行比对存储,对比目的是防止重复操作数据库 | 
|---|
|  |  |  | if(info.ugRtuStateList != null && info.ugRtuStateList.size() > 0){ | 
|---|
|  |  |  | //此时保证两个集合都不为null | 
|---|
|  |  |  | this.save2Db(info.ugTaskId, info.ugRtuStateList, cache.ugRtuStateList); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //cache赋值必须放在上面处理的后面,否则上面的比较不成功 | 
|---|
|  |  |  | cache = info; | 
|---|
|  |  |  | if(info.ugOverallState != null && info.ugOverallState.allOver){ | 
|---|
|  |  |  | this.saveTaskOver(info.ugTaskId) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void save2Db(String taskId, List<UpgradeRtu> newList, List<UpgradeRtu> oldList){ | 
|---|
|  |  |  | List<UpgradeRtu> newOverList = newList.stream().filter(vo -> vo.isOver).collect(Collectors.toList()) ; | 
|---|
|  |  |  | List<UpgradeRtu> oldNoOverList = newList.stream().filter(vo -> !vo.isOver).collect(Collectors.toList()) ; | 
|---|
|  |  |  | boolean oldExist = false ; | 
|---|
|  |  |  | for(UpgradeRtu nvo : newOverList){ | 
|---|
|  |  |  | if(nvo != null) { | 
|---|
|  |  |  | if(oldList.stream().anyMatch(vo -> vo.rtuAddr.equals(nvo.rtuAddr) && vo.isOver == false)){ | 
|---|
|  |  |  | //上次没有升级结束,而当前升级结束了 | 
|---|
|  |  |  | this.sv.saveRtuUpgradeState(Long.parseLong(taskId), nvo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | oldExist = false ; | 
|---|
|  |  |  | if(oldNoOverList.stream().anyMatch(vo -> vo.rtuAddr.equals(nvo.rtuAddr))){ | 
|---|
|  |  |  | oldExist = true ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(!oldExist){ | 
|---|
|  |  |  | //上次没有升级结束,而当前升级结束了 | 
|---|
|  |  |  | this.sv.saveRtuUpgradeState(Long.parseLong(taskId), nvo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 保存升级任务已经执行完成 | 
|---|
|  |  |  | * @param taskId | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void saveTaskOver(String taskId){ | 
|---|
|  |  |  | this.sv.updateTaskOver(taskId) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ///////////////////////////////////////////////////// | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // 以下模拟数据 | 
|---|
|  |  |  | 
|---|
|  |  |  | rtu.currentPackage = 0 ; | 
|---|
|  |  |  | rtu.currentRamAddr = 0x00 ; | 
|---|
|  |  |  | rtu.lastDownDt = "" ; | 
|---|
|  |  |  | rtu.lastDownDtAt = 0L ; | 
|---|
|  |  |  | rtu.reTryTimes = 0 ; | 
|---|
|  |  |  | rtu.isOver = false ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | 
|---|
|  |  |  | rtu.currentPackage = 1 ; | 
|---|
|  |  |  | rtu.currentRamAddr = 0x00 ; | 
|---|
|  |  |  | rtu.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
|---|
|  |  |  | rtu.lastDownDtAt = System.currentTimeMillis() ; | 
|---|
|  |  |  | rtu.reTryTimes = 0 ; | 
|---|
|  |  |  | rtu.isOver = false ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | rtu.currentPackage = 1 ; | 
|---|
|  |  |  | rtu.currentRamAddr = 0x00 ; | 
|---|
|  |  |  | rtu.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
|---|
|  |  |  | rtu.lastDownDtAt = System.currentTimeMillis() ; | 
|---|
|  |  |  | rtu.reTryTimes++ ; | 
|---|
|  |  |  | rtu.isOver = false ; | 
|---|
|  |  |  | return ; | 
|---|
|  |  |  | 
|---|
|  |  |  | rtu.currentPackage += 1 ; | 
|---|
|  |  |  | rtu.currentRamAddr = 0x00 + UpgradeRtu.RAMADDRADD ; | 
|---|
|  |  |  | rtu.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
|---|
|  |  |  | rtu.lastDownDtAt = System.currentTimeMillis() ; | 
|---|
|  |  |  | rtu.isOver = false ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | }else if(rtu.state == UpgradeRtu.STATE_SUCCESS) { | 
|---|
|  |  |  | cache.ugOverallState.successTotal++; | 
|---|
|  |  |  | }else if(rtu.state == UpgradeRtu.STATE_FAILONE) { | 
|---|
|  |  |  | cache.ugOverallState.failOneTotal++; | 
|---|
|  |  |  | cache.ugOverallState.dieOneTotal++; | 
|---|
|  |  |  | cache.ugOverallState.failTotal++; | 
|---|
|  |  |  | }else if(rtu.state == UpgradeRtu.STATE_FAIL) { | 
|---|
|  |  |  | cache.ugOverallState.failMultiTotal++; | 
|---|
|  |  |  | cache.ugOverallState.dieMultiTotal++; | 
|---|
|  |  |  | cache.ugOverallState.failTotal++; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(rtu.isOver){ | 
|---|