| 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.util.Callback; | 
| import com.dy.common.util.DateTime; | 
| import lombok.Data; | 
|   | 
| /** | 
|  * @Author: liurunyu | 
|  * @Date: 2024/11/4 15:00 | 
|  * @Description | 
|  */ | 
| @Data | 
| public class UpgradeRtu { | 
|   | 
|     @JSONField(serialize = false) | 
|     public static final int STATE_UNSTART = 0 ; | 
|     @JSONField(serialize = false) | 
|     public static final int STATE_RUNNING = 1 ; | 
|     @JSONField(serialize = false) | 
|     public static final int STATE_SUCCESS = 2 ; | 
|     @JSONField(serialize = false) | 
|     public static final int STATE_FAILONE = 3 ; | 
|     @JSONField(serialize = false) | 
|     public static final int STATE_FAIL = 4 ; | 
|     @JSONField(serialize = false) | 
|     public static final int RAMADDRADD = 0x20 ;//程序存储内存在址增量 | 
|   | 
|     @JSONField(serialize = false) | 
|     private UpgradeTask task ; | 
|   | 
|     public String rtuAddr ; | 
|     public int state ;// 0-未开始,1-升级中,2-升级成功,3-升级失败(1包死),4-升级失败(非1包死) | 
|     public int totalPackage ;// 总包数 | 
|     public int currentPackage ;// 当前下发升级包数 | 
|     public int currentRamAddr ;// 当前下发升级包RTU存储地址 | 
|     public String lastDownDt ;// 最后下发升级数据包时间(yyyy-mm-dd HH:HH:SS) | 
|     public int reTryTimes ;//升级不成功,重试次数 | 
|   | 
|     private UpgradeRtu(){ | 
|     } | 
|   | 
|     public UpgradeRtu(UpgradeTask task, String rtuAddr, int totalPackage) { | 
|         this.task = task ; | 
|         this.rtuAddr = rtuAddr ; | 
|         this.state = STATE_UNSTART ; | 
|         this.totalPackage = totalPackage ; | 
|         this.currentPackage = 0 ; | 
|         this.currentRamAddr = 0 ; | 
|         this.lastDownDt = "" ; | 
|         this.reTryTimes = 0 ; | 
|     } | 
|   | 
|     /** | 
|      * 触发升级 | 
|      * @param code | 
|      * @param softData | 
|      * @param callback | 
|      */ | 
|     public void trigger(String code, String protocolName, Short protocolVersion, byte[][] softData, Callback callback){ | 
|         if(this.state == STATE_UNSTART){ | 
|             //当前未开始升级过程 | 
|             this.state = STATE_RUNNING; | 
|             this.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
|             callback.call(createCommand1601(protocolName, protocolVersion));//下发配置指令 | 
|         }else if(this.state == STATE_RUNNING){ | 
|             //当前升级过程中 | 
|             if(code.equals(UpgradeCode.cd_1601)){ | 
|                 //下发配置返回 | 
|                 this.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
|                 callback.call(createCommand1602(protocolName, protocolVersion, currentPackage, currentRamAddr, softData));//下发数据包指令 | 
|             }else if(code.equals(UpgradeCode.cd_1602)){ | 
|                 //下发数据包返回 | 
|                 currentPackage++ ; | 
|                 currentRamAddr += RAMADDRADD ; | 
|                 if(currentPackage < totalPackage){ | 
|                     this.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
|                     callback.call(createCommand1602(protocolName, protocolVersion, currentPackage, currentRamAddr, softData)); | 
|                 }else{ | 
|                     this.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
|                     callback.call(createCommand1603(protocolName, protocolVersion));//下发校验指令 | 
|                 } | 
|             }else if(code.equals(UpgradeCode.cd_1603)){ | 
|                 //下发校验返回 | 
|                 this.state = STATE_SUCCESS ; | 
|                 this.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
|                 callback.call(createCommand1600(protocolName, protocolVersion));//下发复位指令,此指令无应答 | 
|             }else{ | 
|                 if(this.state == STATE_FAILONE || this.state == STATE_FAIL){ | 
|                     //已经是升级失败态 | 
|                     if(this.reTryTimes < task.failTryTimes){ | 
|                         //当前未开始升级过程 | 
|                         this.reTryTimes += 1 ; | 
|                         this.state = STATE_RUNNING; | 
|                         this.currentPackage = 0 ; | 
|                         this.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
|                         callback.call(createCommand1601(protocolName, protocolVersion));//下发配置指令 | 
|                     } | 
|                 }else{ | 
|                     //非升级功能码,如果在升级过程中收到这个功能码的上行数据,说明升级失败了 | 
|                     if(currentPackage == 0 || currentPackage == 1){ | 
|                         //1包死,升级失败 | 
|                         this.state = STATE_FAILONE ; | 
|                     }else{ | 
|                         //非1包死,升级失败 | 
|                         this.state = STATE_FAIL ; | 
|                     } | 
|                 } | 
|             } | 
|         }else{ | 
|             //在其他状态下,不触发任务工作 | 
|         } | 
|     } | 
|   | 
|     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.task.softStoreAddr ; | 
|         vo.softStartAddr = this.task.softStartAddr ; | 
|         vo.totalByte =  this.task.softBytesCalculate ; | 
|         vo.softCrc = this.task.softByteSrc16; | 
|         com.param = vo ; | 
|         return com ; | 
|     } | 
|   | 
|     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 ; | 
|     } | 
|   | 
|     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 ; | 
|     } | 
|   | 
|     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 ; | 
|     } | 
| } |