liurunyu
2024-11-08 87a49ccc47abbb3505403d174001ceb3a2d2341d
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
package com.dy.rtuMw.server.upgrade;
 
import com.dy.common.mw.UnitAdapterInterface;
import com.dy.common.mw.UnitCallbackInterface;
import com.dy.common.mw.UnitInterface;
import com.dy.common.util.Callback;
 
import java.util.List;
 
/**
 * @Author: liurunyu
 * @Date: 2024/11/4 14:16
 * @Description
 */
public class UpgradeUnit implements UnitInterface {
 
    private static UpgradeUnit instance = new UpgradeUnit() ;
 
    public static UpgradeUnitAdapter adapter ;
    public static UpgradeUnitConfigVo confVo ;
 
    private static UpgradeManager manager ;
 
    private UpgradeUnit(){} ;
 
    public static UpgradeUnit getInstance(){
        return instance ;
    }
 
    @Override
    public void setAdapter(UnitAdapterInterface adapter) throws Exception {
        if(adapter == null){
            throw new Exception("RTU远程升级模块适配器对象不能为空!") ;
        }
        UpgradeUnit.adapter = (UpgradeUnitAdapter)adapter ;
        UpgradeUnit.confVo = UpgradeUnit.adapter.getConfig() ;
        if(UpgradeUnit.confVo == null){
            throw new Exception("RTU远程升级模块配置对象不能为空!") ;
        }
    }
 
    /**
     * 初始化上行数据处理任务池
     */
    @Override
    public void start(UnitCallbackInterface callback) throws Exception {
        if(confVo.enable){
            manager = UpgradeManager.getInstance() ;
            manager.initOption(confVo);
            callback.call(null) ;
            System.out.println("RTU远程升级模块成功启动");
        }else{
            System.out.println("RTU远程升级模块配置不启动");
        }
 
    }
 
    @Override
    public void stop(UnitCallbackInterface callback) throws Exception {
        stopUpgradeTask() ;
    }
 
 
 
    /**
     * 设置升级任务
     * @param softFileName 升级程序文件名
     * @param softStoreAddr 升级程序存放地址
     * @param softStartAddr 程序覆盖起始地址
     * @param softFileData 升级程序字节数组
     * @param softBytesCalculate 升级程序字节数(按公式计算)
    * @param rtuAddrList 升级RTU
     * @throws Exception
     */
    public void setUpgradeTask(String softFileName,
                               String softStoreAddr,
                               String softStartAddr,
                               byte[] softFileData,
                               Integer softBytesCalculate,
                               List<String> rtuAddrList) throws Exception {
        if(manager != null ){
            manager.setUpgradeTask(softFileName,
                    softStoreAddr,
                    softStartAddr,
                    softFileData,
                    softBytesCalculate,
                    rtuAddrList) ;
        }
    }
 
    /**
     * 停止当前升级任务
     * @throws Exception
     */
    public void stopUpgradeTask() throws Exception {
        if(manager != null ){
            manager.stopUpgradeTask() ;
        }
    }
 
    /**
     * RTU有上行数据了,触发下发升级数据
     * @param rtuAddr
     * @param code
     * @param callback
     */
    public void trigger(String rtuAddr, String code, String protocolName, Short protocolVersion, Callback callback){
        if(manager != null ){
            manager.trigger(rtuAddr, code, protocolName, protocolVersion, callback);
        }
    }
 
    ////////////////////////////////////////////////////
    //
    // 查询升级状态信息
    //
    ////////////////////////////////////////////////////
    /**
     * 当前升级状态
     * @return
     */
    public UpgradeState currentUpgradeState() {
        if(manager != null ){
            return manager.currentUpgradeState();
        }
        return null ;
    }
 
    /**
     * Rtu升级信息
     * @param rtuAddr
     * @return
     */
    public UpgradeRtu upgradeInfos(String rtuAddr){
        if(manager != null ){
            return manager.upgradeInfos(rtuAddr);
        }
        return null ;
    }
 
    /**
     * Rtu升级信息
     * @param rtuAddrList
     * @return
     */
    public List<UpgradeRtu> upgradeInfos(List<String> rtuAddrList){
        if(manager != null ){
            return manager.upgradeInfos(rtuAddrList);
        }
        return null ;
    }
 
}