| package com.dy.rtuMw.server.upgrade; | 
|   | 
| import com.alibaba.fastjson2.annotation.JSONField; | 
| import com.dy.common.mw.protocol.Command; | 
| import com.dy.common.mw.protocol.CommandType; | 
| import com.dy.common.softUpgrade.Com1601Vo; | 
| import com.dy.common.softUpgrade.state.UpgradeRtu; | 
| import com.dy.common.softUpgrade.state.UpgradeTaskVo; | 
| import com.dy.common.util.Callback; | 
| import com.dy.common.util.DateTime; | 
| import lombok.Data; | 
| import lombok.EqualsAndHashCode; | 
|   | 
| /** | 
|  * @Author: liurunyu | 
|  * @Date: 2024/11/4 15:00 | 
|  * @Description | 
|  */ | 
| @Data | 
| @EqualsAndHashCode(callSuper=false) | 
| public class UpgradeRtuDev extends UpgradeRtu { | 
|   | 
|     @JSONField(serialize = false) | 
|     private Integer failTryTimes ;//升级失败后,重新偿试升级次数,0表示不重新偿试升级 | 
|   | 
|     @JSONField(serialize = false) | 
|     public UpgradeTaskVo taskVo ;//升级任务值对象 | 
|   | 
|     private UpgradeRtuDev(){ | 
|     } | 
|   | 
|     public UpgradeRtuDev(UpgradeTaskVo taskVo, Integer failTryTimes , String rtuAddr, int totalPackage) { | 
|         super(); | 
|         this.taskVo = taskVo ; | 
|         this.failTryTimes = failTryTimes ; | 
|         this.rtuAddr = rtuAddr ; | 
|         this.state = STATE_UNSTART ; | 
|         this.totalPackage = totalPackage ; | 
|         this.currentPackage = 0 ; | 
|         this.currentRamAddr = 0 ; | 
|         this.lastDownDt = "" ; | 
|         this.reTryTimes = 0 ; | 
|         this.isOver = false ; | 
|     } | 
|   | 
|     public UpgradeRtuDev(UpgradeTaskVo taskVo, Integer failTryTimes, String rtuAddr, int totalPackage, int state) { | 
|         super(); | 
|         this.taskVo = taskVo ; | 
|         this.failTryTimes = failTryTimes ; | 
|         this.rtuAddr = rtuAddr ; | 
|         this.state = state ; | 
|         this.totalPackage = totalPackage ; | 
|         this.currentPackage = 0 ; | 
|         this.currentRamAddr = 0 ; | 
|         this.lastDownDt = "" ; | 
|         this.reTryTimes = 0 ; | 
|         this.isOver = false ; | 
|     } | 
|   | 
|     /** | 
|      * 触发升级 | 
|      * @param code | 
|      * @param softData | 
|      * @param callbackCom | 
|      */ | 
|     @Override | 
|     public void trigger(String code, String protocolName, Short protocolVersion, byte[][] softData, Callback callbackCom){ | 
|         if(!this.isOver){ | 
|             //升级未完成 | 
|             if(this.state == STATE_OFFLINE || this.state == STATE_UNSTART){ | 
|                 //当前未开始升级过程,下发升级配置指令 | 
|                 this.setStateAtSendCom1601Time(); | 
|                 callbackCom.call(createCommand1601(protocolName, protocolVersion)); | 
|             }else if(this.state == STATE_RUNNING){ | 
|                 //当前升级过程中 | 
|                 if(code.equals(UpgradeCode.cd_9601)){ | 
|                     //下发配置返回 | 
|                     this.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
|                     callbackCom.call(createCommand1602(protocolName, protocolVersion, currentPackage, currentRamAddr, softData));//下发数据包指令 | 
|                 }else if(code.equals(UpgradeCode.cd_9602)){ | 
|                     //下发数据包返回 | 
|                     currentPackage++ ; | 
|                     currentRamAddr += RAMADDRADD ; | 
|                     if(currentPackage < totalPackage){ | 
|                         this.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
|                         callbackCom.call(createCommand1602(protocolName, protocolVersion, currentPackage, currentRamAddr, softData)); | 
|                     }else{ | 
|                         this.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
|                         callbackCom.call(createCommand1603(protocolName, protocolVersion));//下发校验指令 | 
|                     } | 
|                 }else if(code.equals(UpgradeCode.cd_9603)){ | 
|                     //下发校验返回 | 
|                     this.setStateAtReceiveRes9603Time() ; | 
|                     callbackCom.call(createCommand1600(protocolName, protocolVersion));//下发复位指令,此指令无应答 | 
|                 }else{ | 
|                     //当前是升级中状态,并且升级未完成,收到了非960X系列功能码,则升级失败 | 
|                     if(currentPackage == 0 || currentPackage == 1){ | 
|                         //1包死,升级失败 | 
|                         this.state = STATE_FAILONE ; | 
|                     }else{ | 
|                         //非1包死,升级失败 | 
|                         this.state = STATE_FAIL ; | 
|                     } | 
|                 } | 
|             }else if(this.state == STATE_FAILONE || this.state == STATE_FAIL){ | 
|                 //上次设置了失败状态,并且升级未完成,收到了非960X系列功能码,则升级失败 | 
|                 if(this.reTryTimes < this.failTryTimes){ | 
|                     //重试次数未达到上限,继续重试 | 
|                     this.reTryTimes += 1 ; | 
|                     this.setStateAtSendCom1601Time(); | 
|                     callbackCom.call(createCommand1601(protocolName, protocolVersion));//下发配置指令 | 
|                 }else{ | 
|                     //升级失败,并且重试次数达到上限,强制设置升级完成 | 
|                     this.isOver = true ; | 
|                 } | 
|             }else if(this.state == STATE_SUCCESS){ | 
|                 //升级成功态,不会出现此情况,因为设置为成功态时,升级结束属性isOver设置为true了 | 
|                 //为安全见,这里再设置为结束态 | 
|                 this.isOver = true ; | 
|             }else if(this.state == STATE_FAILOFFLINE){ | 
|                 //已经被其他逻辑模块强制设置成离线失败态了,不会出现此情况,因为设置为离线失败态时,升级结束属性isOver设置为true了 | 
|                 //这里不再有作为 | 
|                 //为安全见,这里再设置为结束态 | 
|                 this.isOver = true ; | 
|             }else{ | 
|                 //其他状态(没有其他态了,除非再设计开发时增加了状态) | 
|                 //为安全见,这里再设置为结束态 | 
|                 this.isOver = true ; | 
|             } | 
|         }else{ | 
|             //升级完成,无业务逻辑 | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 设置发送配置命令时刻的状态 | 
|      */ | 
|     private void setStateAtSendCom1601Time(){ | 
|         this.state = STATE_RUNNING;//升级进行中 | 
|         this.currentPackage = 0 ; | 
|         this.currentRamAddr = 0 ; | 
|         this.isOver = false ; | 
|         this.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
|     } | 
|   | 
|     /** | 
|      * 设置1603指令返回时刻的状态 | 
|      */ | 
|     private void setStateAtReceiveRes9603Time(){ | 
|         this.isOver = true ;//升级完成 | 
|         this.state = STATE_SUCCESS ; | 
|         this.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
|     } | 
|   | 
|     @Override | 
|     public Command createCommand1601(String protocolName, Short protocolVersion){ | 
|         Command com = new Command() ; | 
|         com.id = Command.defaultId ; | 
|         com.type = CommandType.outerCommand ; | 
|         com.rtuAddr = this.rtuAddr ; | 
|         com.code = UpgradeCode.cd_1601 ; | 
|         com.protocol = protocolName ; | 
|         com.protocolVersion = protocolVersion ; | 
|         com.rtuResultSendWebUrl = Command.ignoreRtuResultSendWebUrl ; | 
|   | 
|         Com1601Vo vo = new Com1601Vo() ; | 
|         vo.softStoreAddr = this.taskVo.softStoreAddr ; | 
|         vo.softStartAddr = this.taskVo.softStartAddr ; | 
|         vo.totalByte =  this.taskVo.softBytesCalculate ; | 
|         vo.softCrc = this.taskVo.softByteSrc16; | 
|         com.param = vo ; | 
|         return com ; | 
|     } | 
|   | 
|     @Override | 
|     public Command createCommand1602(String protocolName, Short protocolVersion, int currentPackage, Integer ramAddr, byte[][] softData){ | 
|         Command com = new Command() ; | 
|         com.id = Command.defaultId ; | 
|         com.type = CommandType.outerCommand ; | 
|         com.rtuAddr = this.rtuAddr ; | 
|         com.code = UpgradeCode.cd_1602 ; | 
|         com.protocol = protocolName ; | 
|         com.protocolVersion = protocolVersion ; | 
|   | 
|         com.param = ramAddr ; | 
|   | 
|         if(currentPackage < softData.length){ | 
|             com.attachment = softData[currentPackage] ; | 
|         } | 
|         com.rtuResultSendWebUrl = Command.ignoreRtuResultSendWebUrl ; | 
|         return com ; | 
|     } | 
|   | 
|     @Override | 
|     public Command createCommand1603(String protocolName, Short protocolVersion){ | 
|         Command com = new Command() ; | 
|         com.id = Command.defaultId ; | 
|         com.type = CommandType.outerCommand ; | 
|         com.rtuAddr = this.rtuAddr ; | 
|         com.code = UpgradeCode.cd_1603 ; | 
|         com.protocol = protocolName ; | 
|         com.protocolVersion = protocolVersion ; | 
|         com.rtuResultSendWebUrl = Command.ignoreRtuResultSendWebUrl ; | 
|         return com ; | 
|     } | 
|   | 
|     @Override | 
|     public Command createCommand1600(String protocolName, Short protocolVersion){ | 
|         Command com = new Command() ; | 
|         com.id = Command.defaultId ; | 
|         com.type = CommandType.outerCommand ; | 
|         com.rtuAddr = this.rtuAddr ; | 
|         com.code = UpgradeCode.cd_1600 ; | 
|         com.protocol = protocolName ; | 
|         com.protocolVersion = protocolVersion ; | 
|         com.rtuResultSendWebUrl = Command.ignoreRtuResultSendWebUrl ; | 
|         return com ; | 
|     } | 
| } |