liurunyu
2024-11-20 f695fa17fce26c0266ff682622d761767a8dcbfa
pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/upgrade/UpgradeManager.java
@@ -1,6 +1,16 @@
package com.dy.rtuMw.server.upgrade;
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.softUpgrade.state.UpgradeTaskVo;
import com.dy.common.springUtil.SpringContextUtil;
import com.dy.common.util.Callback;
import com.dy.common.util.DateTime;
import com.dy.common.util.ThreadJob;
import com.dy.rtuMw.web.webRequest.WebRequestDeal;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
@@ -9,7 +19,9 @@
 * @Date: 2024/11/4 16:03
 * @Description
 */
public class UpgradeManager {
public class UpgradeManager extends ThreadJob implements Callback {
    private static final Logger log = LogManager.getLogger(UpgradeManager.class.getName());
    private static final UpgradeManager INSTANCE = new UpgradeManager();
@@ -17,9 +29,6 @@
    private Integer ugMaxRtuSameTime ;//同时升级RTU最大个数
    private UpgradeTask task ;//升级任务
    private boolean taskIsOver = false ;//升级任务是否结束
    private int triggerTimes = 0 ;//连接触发次数
    private UpgradeManager(){}
@@ -32,46 +41,49 @@
     */
    public void initOption(UpgradeUnitConfigVo configVo) {
        this.failTryTimes = configVo.failTryTimes;
        this.ugMaxRtuSameTime = configVo.ugMaxRtuSameTime;
        this.ugMaxRtuSameTime = configVo.ugMaxRtuAtOnce;
    }
    /**
     * 设置升级任务
     * @param softFileName 升级程序文件名
     * @param softStoreAddr 升级程序存放地址
     * @param softStartAddr 程序覆盖起始地址
     * @param softFileData 升级程序字节数组
     * @param softBytesCalculate 升级程序字节数(按公式计算)
     * @param rtuAddrList 升级RTU
     * @param vo UpgradeTaskVo 升级任务对象
     * @throws Exception
     */
    public void setUpgradeTask(String softFileName,
                               String softStoreAddr,
                               String softStartAddr,
                               byte[] softFileData,
                               Integer softBytesCalculate,
                               List<String> rtuAddrList) throws Exception {
        if(this.task != null && !this.task.isOver()){
            throw new Exception("当前存在升级任务,请等待当前任务执行完或强制停止当前任务");
        }else{
            this.task.forceOver();
            this.task = new UpgradeTask();
            this.task.initOption(this.failTryTimes, this.ugMaxRtuSameTime);
            this.task.setTask(softFileName,
                    softStoreAddr,
                    softStartAddr,
                    softFileData,
                    softBytesCalculate,
                    rtuAddrList);
    public void setUpgradeTask(UpgradeTaskVo vo) throws Exception {
        if(this.task != null && !this.task.taskIsOver){
            throw new Exception("当前存在升级任务,请等待当前任务执行完或强制结束当前任务");
        }else {
            Exception ex = null ;
            try{
                if(this.task != null){
                    this.task.forceOver();
                }
                this.task = new UpgradeTask();
                this.task.initOption(this.failTryTimes, this.ugMaxRtuSameTime);
                this.task.setTask(vo);
            }catch (Exception e){
                ex = e ;
            }finally {
                if(ex != null){
                    this.task = null ;
                    throw ex ;
                }else{
                    this.start(this);
                }
            }
        }
    }
    /**
     * 停止当前升级任务
     * 强制结束当前升级任务,
     * 此功能可能不会开放出去,
     * 因为强制结束升级任务,对一个未升级完成的RTU就会卡死,
     * 所以当强制结束升级任务,代码逻辑并没有强制结果RTU升级过程,如果升级过程也强制停止,那么RTU真会卡死
     * @throws Exception
     */
    public void stopUpgradeTask() throws Exception {
    public void forceOverUpgradeTask() throws Exception {
        if(this.task != null){
            this.stop();
            this.task.forceOver();
        }
        this.task = null ;
@@ -83,20 +95,11 @@
     * @param code
     * @param protocolName
     * @param protocolVersion
     * @param callback
     * @param callbackCom
     */
    public void trigger(String rtuAddr, String code, String protocolName, Short protocolVersion, Callback callback){
        if(task != null && !taskIsOver){
            triggerTimes ++ ;
            if(triggerTimes == 100){
                triggerTimes = 0 ;
                if(this.task.isOver()){
                    taskIsOver = true ;
                }
            }
            if(!taskIsOver){
                this.task.trigger(rtuAddr, code, protocolName, protocolVersion, callback);
            }
    public void trigger(String rtuAddr, String code, String protocolName, Short protocolVersion, Callback callbackCom){
        if(task != null && !task.taskIsOver){
            this.task.trigger(rtuAddr, code, protocolName, protocolVersion, callbackCom);
        }
    }
@@ -122,7 +125,7 @@
     * @param rtuAddr
     * @return
     */
    public UpgradeRtu upgradeInfos(String rtuAddr){
    public UpgradeRtu upgradeRtuInfo(String rtuAddr){
        if(task != null){
            return task.upgradeInfos(rtuAddr) ;
        }else{
@@ -135,7 +138,7 @@
     * @param rtuAddrList
     * @return
     */
    public List<UpgradeRtu> upgradeInfos(List<String> rtuAddrList){
    public List<UpgradeRtu> upgradeRtuInfos(List<String> rtuAddrList){
        if(task != null){
            return task.upgradeInfos(rtuAddrList) ;
        }else{
@@ -144,4 +147,138 @@
    }
    /**
     * Rtu升级信息
     * @return
     */
    public List<UpgradeRtu> upgradeRtuInfoAll(){
        if(task != null){
            return task.upgradeInfoAll() ;
        }else{
            return null ;
        }
    }
    ////////////////////////////////////////////////////
    //
    // 升级服务工作线程执行的方法
    // 统计状态 + 状态通知
    //
    ////////////////////////////////////////////////////
    @Override
    public Object execute() throws Exception {
        boolean first = true ;
        while (true){
            if(this.isStop()){
                break ;
            }
            if(first){
                try{
                    //首次启动,停1秒
                    Thread.sleep(1000L);
                }catch (Exception e){
                }
            }else{
                try{
                    //停X毫秒
                    Thread.sleep(UpgradeUnit.confVo.notifyStateInterval);
                }catch (Exception e){
                }
            }
            if(this.task == null
                    || this.task.taskVo == null
                    || this.task.taskVo.rtuAddrList == null
                    || this.task.taskVo.rtuAddrList.size() == 0){
                //任务为空
                break ;
            }else{
                if(!this.task.taskIsOver){
                    //升级任务未完成
                    //工作1:判断是否无任何一个RTU进行过升级,并且达到时限,则认为当前升级任务完成
                    //-1:无一RTU升级且超时,0:无RTU升级但未超时等待,1有RTU升级正常执行
                    int temp = this.task.countNoOneRtuUpgradeInDuration() ;
                    if(temp == -1){
                        this.task.taskIsOver = true ;
                        //任务已经完成
                        this.stop();
                    }else if(temp == 1){
                        //工作2:统计当前正在升级的RTU数量,为同时升级数量限制做准备
                        this.task.countRunningRtuCount() ;
                        //工作3:统计需要升级但当前离线RTU的情况,超过时限的设备为升级完成
                        this.task.countOffRtuAndSetIfOver() ;
                        //工作4:统计是否全部升级完成
                        this.task.taskIsOver = this.task.countIsAllOver() ;
                    }else if(temp == 0){
                        //不作为
                    }
                    if(this.task.taskIsOver){
                        if(!this.task.taskOverType.equals(UpgradeTask.TaskOverType_Force)){
                            //任务不是强制结束的
                            this.task.taskOverType = UpgradeTask.TaskOverType_Natural ;//任务完成方式(自然,强制)
                            this.task.taskOverDt = DateTime.yyyy_MM_dd_HH_mm_ss() ;//任务完成时间(yyyy-mm-dd HH:MM:SS)
                        }
                        //任务完成,执行最后一次升级状态通知
                        //工作5:升级状态通知
                        //if(!first){
                        //    this.notifyUpgradeStatus() ;
                        //}
                    }else{
                        //任务未完成,继续执行升级状态通知
                        //工作5: 升级状态通知
                        //if(!first){
                        //    this.notifyUpgradeStatus() ;
                        //}
                    }
                    //工作5:升级状态通知
                    if(!first){
                        this.notifyUpgradeStatus() ;
                    }
                }else{
                    //任务已经完成
                    this.stop();
                }
            }
            if(first){
                first = false ;
            }
        }
        return true ;
    }
    /**
     * 升级状态通知
     */
    private void notifyUpgradeStatus(){
        if(this.task.taskVo.callbackWebUrl != null && this.task.taskVo.callbackWebUrl.length() > 0){
            UpgradeInfo info = new UpgradeInfo() ;
            info.ugTaskId = this.task.taskVo.id ;//任务ID
            info.ugOverallState = this.currentUpgradeState() ;
            info.ugRtuStateList = this.upgradeRtuInfoAll() ;
            WebRequestDeal deal = SpringContextUtil.getBean(WebRequestDeal.class) ;
            deal.deal(this.task.taskVo.callbackWebUrl, info);
        }
    }
    ////////////////////////////////////////////////////
    //
    // 升级状态通知工作线程执行完成后回调的方法,
    // 也就是上面execute方法执行完成返回或抛出异常后,执行下面三个方法
    //
    ////////////////////////////////////////////////////
    @Override
    public void call(Object obj) {
        //线程工作执行完了,obj = Boolean(true)
        this.thread = null ;//赋值为null,使线程对象被垃圾回收器回收
    }
    @Override
    public void call(Object... objs) {
    }
    @Override
    public void exception(Exception e) {
        log.error("远程升级伺服线程发生异常", e);
    }
}