|  |  |  | 
|---|
|  |  |  | package com.dy.rtuMw.server.upgrade; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson2.annotation.JSONField; | 
|---|
|  |  |  | import com.dy.common.mw.protocol.rtuState.RtuStatus; | 
|---|
|  |  |  | import com.dy.common.softUpgrade.parse.HexFileParse; | 
|---|
|  |  |  | import com.dy.common.softUpgrade.state.UpgradeRtu; | 
|---|
|  |  |  | import com.dy.common.softUpgrade.state.UpgradeState; | 
|---|
|  |  |  | import com.dy.common.softUpgrade.state.UpgradeTaskVo; | 
|---|
|  |  |  | import com.dy.common.util.Callback; | 
|---|
|  |  |  | import com.dy.common.util.DateTime; | 
|---|
|  |  |  | import com.dy.rtuMw.server.forTcp.RtuStatusDealer; | 
|---|
|  |  |  | import lombok.Data; | 
|---|
|  |  |  | import org.apache.logging.log4j.LogManager; | 
|---|
|  |  |  | import org.apache.logging.log4j.Logger; | 
|---|
|  |  |  | 
|---|
|  |  |  | protected Integer failTryTimes ;//升级失败后,重新偿试升级次数,0表示不重新偿试升级 | 
|---|
|  |  |  | @JSONField(serialize = false) | 
|---|
|  |  |  | protected Integer ugMaxRtuSameTime ;//同时升级RTU最大个数 | 
|---|
|  |  |  | @JSONField(serialize = false) | 
|---|
|  |  |  | private Boolean openNoUpgrade ;//阀开(泵开)不执行升级 | 
|---|
|  |  |  | @JSONField(serialize = false) | 
|---|
|  |  |  | private Integer lastOpenMaxGoOn ;//阀开(泵开)状态设置以来持续最长时间(秒钟),超过这个时间认为状态无效(这个时长取决于工作报间隔) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public String setupDt ;//设置时间(yyyy-mm-dd HH:MM:SS) | 
|---|
|  |  |  | @JSONField(serialize = false) | 
|---|
|  |  |  | 
|---|
|  |  |  | /////////////////////////////////////////////////// | 
|---|
|  |  |  | //以下内部控制用 | 
|---|
|  |  |  | @JSONField(serialize = false) | 
|---|
|  |  |  | private int curUgRunningRtuTotal = 0 ;//当前正在升级的RTU个数 | 
|---|
|  |  |  | protected int curUgRunningRtuTotal = 0 ;//当前正在升级的RTU个数 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public UpgradeTask() { | 
|---|
|  |  |  | this.curUgRunningRtuTotal = 0 ; | 
|---|
|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | *  初始化配置信息 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void initOption(Integer failTryTimes, Integer ugMaxRtuSameTime) { | 
|---|
|  |  |  | public void initOption(Boolean openNoUpgrade, | 
|---|
|  |  |  | Integer lastOpenMaxGoOn, | 
|---|
|  |  |  | Integer failTryTimes, | 
|---|
|  |  |  | Integer ugMaxRtuSameTime) { | 
|---|
|  |  |  | this.openNoUpgrade = openNoUpgrade; | 
|---|
|  |  |  | this.lastOpenMaxGoOn = lastOpenMaxGoOn; | 
|---|
|  |  |  | this.failTryTimes = failTryTimes; | 
|---|
|  |  |  | this.ugMaxRtuSameTime = ugMaxRtuSameTime; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param code | 
|---|
|  |  |  | * @param callbackCom | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void trigger(String rtuAddr, String code, String protocolName, Short protocolVersion, Callback callbackCom){ | 
|---|
|  |  |  | public void trigger(String rtuAddr, String code, String protocolName, Short protocolVersion, Callback callbackCom, Object ...objects){ | 
|---|
|  |  |  | if(this.upgradeRtus != null && this.upgradeRtus.size() > 0 | 
|---|
|  |  |  | && this.taskVo.rtuAddrList != null && this.taskVo.rtuAddrList.size() > 0){ | 
|---|
|  |  |  | UpgradeRtu ugRtu = this.upgradeRtus.get(rtuAddr) ; | 
|---|
|  |  |  | 
|---|
|  |  |  | //当前RTU已经升级完成,无需再升级 | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | if(this.openNoUpgrade){ | 
|---|
|  |  |  | //首先判断是否是阀门打开状态 | 
|---|
|  |  |  | RtuStatus rtuStatus = RtuStatusDealer.oneStatus(rtuAddr) ; | 
|---|
|  |  |  | if(rtuStatus != null | 
|---|
|  |  |  | && rtuStatus.valveOpenTrueCloseFalse != null | 
|---|
|  |  |  | && rtuStatus.valveOpenTrueCloseFalse.booleanValue() == true | 
|---|
|  |  |  | && rtuStatus.valveStatusLastTimeStamp != null){ | 
|---|
|  |  |  | //有状态,并且是阀开(泵开) | 
|---|
|  |  |  | Long now = System.currentTimeMillis() ; | 
|---|
|  |  |  | Long gap = now - rtuStatus.valveStatusLastTimeStamp ; | 
|---|
|  |  |  | if(gap < this.lastOpenMaxGoOn){ | 
|---|
|  |  |  | //这时采纳阀门打开状态,进而不能升级 | 
|---|
|  |  |  | ugRtu.isOver = true ; | 
|---|
|  |  |  | ugRtu.state = UpgradeRtu.STATE_FAILOPEN ; | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(UpgradeUnit.confVo.ugMaxRtuAtOnce > 0){ | 
|---|
|  |  |  | //设置了同时升级的RTU最大数量的限制 | 
|---|
|  |  |  | if(ugRtu.state == UpgradeRtuDev.STATE_OFFLINE){ | 
|---|
|  |  |  | //初始态,说明升级任务设置以来,该RTU第一次上行数据 | 
|---|
|  |  |  | if(this.curUgRunningRtuTotal <= UpgradeUnit.confVo.ugMaxRtuAtOnce){ | 
|---|
|  |  |  | //当前正在升级的RTU数量还未受限 | 
|---|
|  |  |  | ugRtu.trigger(code, protocolName, protocolVersion, this.softFileDataGrp, callbackCom) ; | 
|---|
|  |  |  | ugRtu.trigger(code, protocolName, protocolVersion, this.softFileDataGrp, callbackCom, objects) ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | //同时升级的RTU数量受限,等待下次机会 | 
|---|
|  |  |  | //但先表明一下状态 | 
|---|
|  |  |  | 
|---|
|  |  |  | //根据上面逻辑, 说明必然在线了 | 
|---|
|  |  |  | if(this.curUgRunningRtuTotal <= UpgradeUnit.confVo.ugMaxRtuAtOnce){ | 
|---|
|  |  |  | //当前正在升级的RTU数量还未受限 | 
|---|
|  |  |  | ugRtu.trigger(code, protocolName, protocolVersion, this.softFileDataGrp, callbackCom) ; | 
|---|
|  |  |  | ugRtu.trigger(code, protocolName, protocolVersion, this.softFileDataGrp, callbackCom, objects) ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | //同时升级的RTU数量受限,等待下次机会 | 
|---|
|  |  |  | return ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | //RTU已经处于升级过程中 | 
|---|
|  |  |  | ugRtu.trigger(code, protocolName, protocolVersion, this.softFileDataGrp, callbackCom) ; | 
|---|
|  |  |  | ugRtu.trigger(code, protocolName, protocolVersion, this.softFileDataGrp, callbackCom, objects) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | //没有设置同时升级的RTU最大数量的限制 | 
|---|
|  |  |  | ugRtu.trigger(code, protocolName, protocolVersion, this.softFileDataGrp, callbackCom) ; | 
|---|
|  |  |  | ugRtu.trigger(code, protocolName, protocolVersion, this.softFileDataGrp, callbackCom, objects) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 强制结束升级任务 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | 
|---|
|  |  |  | //this.upgradeState.clear(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 当前升级状态 | 
|---|
|  |  |  | 
|---|
|  |  |  | }else if(info.state == UpgradeRtu.STATE_SUCCESS) { | 
|---|
|  |  |  | state.successTotal++; | 
|---|
|  |  |  | }else if(info.state == UpgradeRtu.STATE_FAILONE) { | 
|---|
|  |  |  | state.failOneTotal++; | 
|---|
|  |  |  | state.failTotal++; | 
|---|
|  |  |  | state.dieOneTotal++; | 
|---|
|  |  |  | if(info.isOver){ | 
|---|
|  |  |  | state.failTotal++; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else if(info.state == UpgradeRtu.STATE_FAIL) { | 
|---|
|  |  |  | state.failMultiTotal++; | 
|---|
|  |  |  | state.failTotal++; | 
|---|
|  |  |  | state.dieMultiTotal++; | 
|---|
|  |  |  | if(info.isOver) { | 
|---|
|  |  |  | state.failTotal++; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else if(info.state == UpgradeRtu.STATE_FAILOFFLINE) { | 
|---|
|  |  |  | state.failTotal++; | 
|---|
|  |  |  | state.failOffTotal++; | 
|---|
|  |  |  | }else if(info.state == UpgradeRtu.STATE_FAILOPEN) { | 
|---|
|  |  |  | state.failTotal++; | 
|---|
|  |  |  | state.failOpenTotal++; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(info.isOver){ | 
|---|
|  |  |  | state.overTotal++; | 
|---|
|  |  |  | 
|---|
|  |  |  | /////////////////////////////////////////////////////////// | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 阀开(泵开)不升级处理 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected void openNoUpgrade(){ | 
|---|
|  |  |  | if(this.upgradeRtus != null && this.upgradeRtus.size() > 0){ | 
|---|
|  |  |  | Map<String, RtuStatus> rsAllMap = RtuStatusDealer.allStatus() ; | 
|---|
|  |  |  | Long now = System.currentTimeMillis() ; | 
|---|
|  |  |  | this.upgradeRtus.values().stream().forEach(a -> { | 
|---|
|  |  |  | RtuStatus rs = rsAllMap.get(a.rtuAddr) ; | 
|---|
|  |  |  | if(rs != null | 
|---|
|  |  |  | && rs.valveOpenTrueCloseFalse != null && rs.valveOpenTrueCloseFalse.booleanValue() == true | 
|---|
|  |  |  | && rs.valveStatusLastTimeStamp != null){ | 
|---|
|  |  |  | //有状态,并且是阀开(泵开) | 
|---|
|  |  |  | Long gap = now - rs.valveStatusLastTimeStamp ; | 
|---|
|  |  |  | if(gap < this.lastOpenMaxGoOn){ | 
|---|
|  |  |  | //这时采纳阀门打开状态不能升级 | 
|---|
|  |  |  | a.isOver = true ; | 
|---|
|  |  |  | a.state = UpgradeRtu.STATE_FAILOPEN ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 判断是否没用任何一个RTU进行过升级,而且超过了时限 | 
|---|
|  |  |  | * @return -1:无一RTU升级且超时,0:无RTU升级但未超时等待,1有RTU升级正常执行 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 统计当前正在升级的RTU数量,为同时升级数量限制做准备 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected void countRunningRtuCount(){ | 
|---|
|  |  |  | protected int countRunningRtuCount(){ | 
|---|
|  |  |  | int runningTotal = 0 ; | 
|---|
|  |  |  | Collection<UpgradeRtu> col = this.upgradeRtus.values() ; | 
|---|
|  |  |  | for(UpgradeRtu info : col){ | 
|---|
|  |  |  | 
|---|
|  |  |  | runningTotal ++ ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | this.curUgRunningRtuTotal = runningTotal ; | 
|---|
|  |  |  | return this.curUgRunningRtuTotal = runningTotal ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 统计需要升级但当前离线RTU的情况,超过时限的设备为升级完成 | 
|---|
|  |  |  | * @return -1:没有超时,0超时了且无因离线被强制设置升级完成的RTU,>0离线被强制设置升级完成的RTU数量 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected void countOffRtuAndSetIfOver() { | 
|---|
|  |  |  | protected int countOffRtuAndSetIfOver() { | 
|---|
|  |  |  | Long now = System.currentTimeMillis() ; | 
|---|
|  |  |  | if(now - this.setupDtLong > UpgradeUnit.confVo.rtuOffLineWaitDuration){ | 
|---|
|  |  |  | //rtu离线,等待其升级的时长(毫秒),超过配置的最大时长,设置其升级失败,且设置升级任务完成 | 
|---|
|  |  |  | int count = 0 ; | 
|---|
|  |  |  | if (this.taskVo.rtuAddrList != null && this.taskVo.rtuAddrList.size() > 0) { | 
|---|
|  |  |  | Collection<UpgradeRtu> col = this.upgradeRtus.values() ; | 
|---|
|  |  |  | for(UpgradeRtu info : col){ | 
|---|
|  |  |  | if(info.state == UpgradeRtu.STATE_OFFLINE){ | 
|---|
|  |  |  | info.isOver = true ; | 
|---|
|  |  |  | info.state = UpgradeRtu.STATE_FAILOFFLINE ; | 
|---|
|  |  |  | count ++ ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return count ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | return -1 ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 统计:已经进升级但RTU又进入停止升级发呆状态,超过一定时限,设置设备一包死或多包死,并设置为升级完成 | 
|---|
|  |  |  | * @return -1:没有超时,0超时了且无因离线被强制设置升级完成的RTU,>0离线被强制设置升级完成的RTU数量 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected int countRunningIdleRtuAndSetIfOver() { | 
|---|
|  |  |  | Long now = System.currentTimeMillis() ; | 
|---|
|  |  |  | int count = -1 ; | 
|---|
|  |  |  | if(now - this.setupDtLong > UpgradeUnit.confVo.rtuOffLineWaitDuration){ | 
|---|
|  |  |  | //设置上句,防止频繁进入下面语句进行计算 | 
|---|
|  |  |  | if (this.taskVo.rtuAddrList != null && this.taskVo.rtuAddrList.size() > 0) { | 
|---|
|  |  |  | Collection<UpgradeRtu> col = this.upgradeRtus.values() ; | 
|---|
|  |  |  | for(UpgradeRtu info : col){ | 
|---|
|  |  |  | if(info.state == UpgradeRtu.STATE_RUNNING && info.isOver == false){ | 
|---|
|  |  |  | //升级中,但未升级完成 | 
|---|
|  |  |  | if(now - info.lastDownDtAt > UpgradeUnit.confVo.runningAndIdleDuration){ | 
|---|
|  |  |  | if(info.currentPackage <= 1){ | 
|---|
|  |  |  | //一包死 | 
|---|
|  |  |  | info.state = UpgradeRtu.STATE_FAILONE ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | //多包死 | 
|---|
|  |  |  | info.state = UpgradeRtu.STATE_FAIL ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | info.isOver = true ; | 
|---|
|  |  |  | count ++ ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return count ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 统计是否升级全部结束 | 
|---|
|  |  |  | */ | 
|---|