1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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.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 UpgradeTask task ;
 
    private UpgradeRtuDev(){
    }
 
    public UpgradeRtuDev(UpgradeTask task, String rtuAddr, int totalPackage) {
        super();
        this.task = task ;
        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(UpgradeTask task, String rtuAddr, int totalPackage, int state) {
        super();
        this.task = task ;
        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.state == STATE_OFFLINE || this.state == STATE_UNSTART){
            //当前未开始升级过程,下发升级配置指令
            this.setStateAtCom1601Time();
            callbackCom.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() ;
                callbackCom.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() ;
                    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_1603)){
                //下发校验返回
                this.isOver = true ;//升级完成
                this.state = STATE_SUCCESS ;
                this.lastDownDt = DateTime.yyyy_MM_dd_HH_mm_ss() ;
                callbackCom.call(createCommand1600(protocolName, protocolVersion));//下发复位指令,此指令无应答
            }else{
                if(!this.isOver){
                    //升级未完成
                    if(this.state == STATE_FAILONE || this.state == STATE_FAIL){
                        //已经是升级失败态
                        if(this.reTryTimes < task.failTryTimes){
                            //重试次数未达到上限,继续重试
                            this.reTryTimes += 1 ;
                            this.setStateAtCom1601Time();
                            callbackCom.call(createCommand1601(protocolName, protocolVersion));//下发配置指令
                        }else{
                            this.isOver = true ;//升级完成
                        }
                    }else{
                        //非升级功能码,如果在升级过程中收到这个功能码的上行数据,说明升级失败了
                        if(currentPackage == 0 || currentPackage == 1){
                            //1包死,升级失败
                            this.state = STATE_FAILONE ;
                        }else{
                            //非1包死,升级失败
                            this.state = STATE_FAIL ;
                        }
                    }
                }else{
                    //升级完成,无业务逻辑
                }
            }
        }else{
            //在其他状态下,不触发任务工作
        }
    }
 
    /**
     * 设置发送配置命令时刻的状态
     */
    private void setStateAtCom1601Time(){
        this.state = STATE_RUNNING;
        this.currentPackage = 0 ;
        this.currentRamAddr = 0 ;
        this.isOver = false ;
        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.task.taskVo.softStoreAddr ;
        vo.softStartAddr = this.task.taskVo.softStartAddr ;
        vo.totalByte =  this.task.taskVo.softBytesCalculate ;
        vo.softCrc = this.task.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 ;
    }
}