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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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 ;
    }
}