zhubaomin
2 天以前 10a0b0ca34824307aa7d23b0ad6679b36bd57842
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package com.dy.rtuMw.server.local;
 
import com.dy.common.mw.UnitCallbackInterface;
import com.dy.common.mw.channel.tcp.TcpUnit;
import com.dy.common.mw.protocol.Command;
import com.dy.common.mw.protocol.rtuState.RtuStatus;
import com.dy.rtuMw.server.local.localProtocol.*;
import com.dy.rtuMw.server.mqtt.DevStatus;
import com.dy.rtuMw.server.mqtt.DevStatusDealer;
import com.dy.rtuMw.server.mqtt.MqttUnit;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * @Author liurunyu
 * @Date 2023/12/21 15:56
 * @LastEditTime 2023/12/21 15:56
 * @Description
 */
public class CommandInnerDeaLer {
    /**
     * 处理内部命令
     * @param com
     * @return
     */
    public Command deal(Command com) throws Exception{
        Command rCom ;
        String code = com.getCode() ;
        switch (code) {
            case CodeLocal.clock -> {
                rCom = this.clock(com);
                break;
            }
            case CodeLocal.mwState -> {
                rCom = this.mwInfo(com);
                break;
            }
 
            ////////////////////////////////////////////
            //
            // 以下是相关基于TCP连接的RTU设备的内部命令
            //
            ////////////////////////////////////////////
            case CodeLocal.onAllLine -> {
                rCom = this.onAllLine(com);
                break;
            }
            case CodeLocal.onPartLine -> {
                rCom = this.onPartLine(com);
                break;
            }
            case CodeLocal.onLineStatistics -> {
                rCom = this.onLineStateStatistics(com);
                break;
            }
            case CodeLocal.allRtuStates -> {
                rCom = this.allRtuStates(com);
                break;
            }
            case CodeLocal.partRtuStates -> {
                rCom = this.someRtuStates(com);
                break;
            }
            case CodeLocal.oneRtuStates -> {
                rCom = this.oneRtuStates(com);
                break;
            }
            case CodeLocal.allProtocols -> {
                rCom = this.allProtocols(com);
                break;
            }
            case CodeLocal.stopTcpSv -> {
                rCom = this.stopTcpSv(com);
                break;
            }
            case CodeLocal.recoverTcpSv -> {
                rCom = this.recoverTcpSv(com);
                break;
            }
 
 
            ////////////////////////////////////////////
            //
            // 以下是相关基于MQTT连接的设备的内部命令
            //
            ////////////////////////////////////////////
            case CodeLocal.onAllLineMqtt -> {
                rCom = this.onAllLineMqtt(com);
                break;
            }
            case CodeLocal.onPartLineMqtt -> {
                rCom = this.onPartLineMqtt(com);
                break;
            }
            case CodeLocal.onLineStatisticsMqtt -> {
                rCom = this.onLineStateStatisticsMqtt(com);
                break;
            }
            case CodeLocal.allRtuStatesMqtt -> {
                rCom = this.allRtuStatesMqtt(com);
                break;
            }
            case CodeLocal.partRtuStatesMqtt -> {
                rCom = this.someRtuStatesMqtt(com);
                break;
            }
            case CodeLocal.oneRtuStatesMqtt -> {
                rCom = this.oneRtuStatesMqtt(com);
                break;
            }
            case CodeLocal.stopMqttSv -> {
                rCom = this.stopMqttSv(com);
                break;
            }
            default -> {
                rCom = ReturnCommand.errored("出错,收到内部命令的功能码不能识别!", com.getId(), com.getCode());
                break;
            }
        }
        return rCom ;
    }
 
    /**
     * 查询通信中间件时钟
     * @param  command
     * @throws Exception
     */
    private Command clock(Command command) throws Exception{
        ClockVo c = new ClockDeal().deal() ;
        return ReturnCommand.successed("查询通信中间件时钟", command.getId(), command.getCode(), c) ;
    }
 
    /**
     * 查询所有RTU在线情况
     * @throws Exception
     */
    private Command onAllLine(Command command) throws Exception{
        HashMap<String, Boolean> map = new RtuOnLineDeal().dealAll() ;
        return ReturnCommand.successed("查询所有RTU在线情况结果", command.getId(), command.getCode(), map) ;
    }
 
    /**
     * 查询部分RTU在线情况
     * @throws Exception
     */
    private Command onPartLine(Command command) throws Exception{
        if(command.param != null && command.param instanceof String && !command.param.equals("")){
            String[] rtuAddrGrp = ((String)command.param).split(",");
            HashMap<String, Boolean> map = new RtuOnLineDeal().dealPart(rtuAddrGrp) ;
            return ReturnCommand.successed("查询部分RTU在线情况结果", command.getId(), command.getCode(), map) ;
        }else{
            return ReturnCommand.errored("出错,命令参数应该是所查询RTU的地址串",  command.getId(), command.getCode()) ;
        }
    }
 
    /**
     * 统计在线与不在线情况
     * @throws Exception
     */
    private Command onLineStateStatistics(Command command) throws Exception{
        RtuOnLineStateStatisticsVo vo = new RtuOnLineStateStatisticsDeal().deal() ;
        return ReturnCommand.successed("查询所有RTU在线情况结果", command.getId(), command.getCode(), vo) ;
    }
 
    /**
     * 查询所有RTU状态
     * @throws Exception
     */
    private Command allRtuStates(Command command) throws Exception{
        Map<String, RtuStatus> map =  new RtuStatusDeal().dealAll() ;
        return ReturnCommand.successed("查询所有RTU状态结果", command.getId(), command.getCode(), map) ;
    }
 
    /**
     * 查询部分RTU状态
     * @throws Exception
     */
    private Command someRtuStates(Command command) throws Exception{
        if(command.param != null && command.param instanceof String && !command.param.equals("")){
            String[] rtuAddrGrp = ((String)command.param).split(",");
            Map<String, RtuStatus> map = new RtuStatusDeal().dealSome(rtuAddrGrp) ;
            return ReturnCommand.successed("查询部分RTU状态结果", command.getId(), command.getCode(), map) ;
        }else{
            return ReturnCommand.errored("出错,命令参数应该有所查询RTU的地址串",  command.getId(), command.getCode()) ;
        }
    }
 
    /**
     * 查询部分RTU状态
     * @throws Exception
     */
    private Command oneRtuStates(Command command) throws Exception{
        if(command.param != null && command.param instanceof String && !command.param.equals("")){
            String rtuAddr = (String)command.param;
            RtuStatus rtuStatus = new RtuStatusDeal().dealOne(rtuAddr) ;
            return ReturnCommand.successed("查询一个RTU状态结果", command.getId(), command.getCode(), rtuStatus) ;
        }else{
            return ReturnCommand.errored("出错,命令参数应该有所查询RTU的地址",  command.getId(), command.getCode()) ;
        }
    }
 
    /**
     * 查询所有RTU协议配置
     * @throws Exception
     */
    private Command allProtocols(Command command) throws Exception{
        RtuProtocolVo mc = new RtuProtocolsDeal().deal() ;
        return ReturnCommand.successed("查询所有通信协议配置", command.getId(), command.getCode(), mc) ;
    }
 
    /**
     * 停止TCP服务,不再接入新的TCP连接,已经TCP连接的全部断连接
     * @throws Exception
     */
    private Command stopTcpSv(Command command) throws Exception{
        TcpUnit.getInstance().stop(new UnitCallbackInterface(){
            public void call(Object obj) throws Exception {
            }
        });
        return ReturnCommand.successed("已经启动停止TCP服务", command.getId(), command.getCode(), null) ;
    }
 
    /**
     * 恢复TCP服务,接入新的TCP连接
     * @throws Exception
     */
    private Command recoverTcpSv(Command command) throws Exception{
        TcpUnit.getInstance().recover();
        return ReturnCommand.successed("已经启动恢复TCP服务", command.getId(), command.getCode(), null) ;
    }
 
 
    /**
     * 查询所有MQTT设备在线情况
     * @throws Exception
     */
    private Command onAllLineMqtt(Command command) throws Exception{
        HashMap<String, Boolean> map = DevStatusDealer.allOnLine() ;
        return ReturnCommand.successed("查询所有Mqtt设备在线情况结果", command.getId(), command.getCode(), map) ;
    }
 
    /**
     * 查询部分MQTT设备在线情况
     * @throws Exception
     */
    private Command onPartLineMqtt(Command command) throws Exception{
        if(command.param != null && command.param instanceof String && !command.param.equals("")){
            String[] devIds = ((String)command.param).split(",");
            HashMap<String, Boolean> map = DevStatusDealer.partOnLine(devIds) ;
            return ReturnCommand.successed("查询部分Mqtt设备在线情况结果", command.getId(), command.getCode(), map) ;
        }else{
            return ReturnCommand.errored("出错,命令参数应该有所查询Mqtt设备的地址串",  command.getId(), command.getCode()) ;
        }
    }
 
    /**
     * 统计MQTT设备在线与不在线情况
     * @throws Exception
     */
    private Command onLineStateStatisticsMqtt(Command command) throws Exception{
        RtuOnLineStateStatisticsVo vo = DevStatusDealer.statisticsOnLine() ;
        return ReturnCommand.successed("查询所有Mqtt设备在线情况结果", command.getId(), command.getCode(), vo) ;
    }
 
    /**
     * 查询所有MQTT设备状态
     * @throws Exception
     */
    private Command allRtuStatesMqtt(Command command) throws Exception{
        Map<String, DevStatus> map =  DevStatusDealer.allStatus() ;
        return ReturnCommand.successed("查询所有Mqtt设备状态结果", command.getId(), command.getCode(), map) ;
    }
 
    /**
     * 查询部分MQTT设备状态
     * @throws Exception
     */
    private Command someRtuStatesMqtt(Command command) throws Exception{
        if(command.param != null && command.param instanceof String && !command.param.equals("")){
            String[] devIds = ((String)command.param).split(",");
            Map<String, DevStatus> map = DevStatusDealer.someStatus(devIds) ;
            return ReturnCommand.successed("查询部分Mqtt设备状态结果", command.getId(), command.getCode(), map) ;
        }else{
            return ReturnCommand.errored("出错,命令参数应该有所查询Mqtt设备的地址串",  command.getId(), command.getCode()) ;
        }
    }
 
    /**
     * 查询部分MQTT设备状态
     * @throws Exception
     */
    private Command oneRtuStatesMqtt(Command command) throws Exception{
        if(command.param != null && command.param instanceof String && !command.param.equals("")){
            String devId = (String)command.param;
            DevStatus devStatus = DevStatusDealer.oneStatus(devId) ;
            return ReturnCommand.successed("查询一个Mqtt设备状态结果", command.getId(), command.getCode(), devStatus) ;
        }else{
            return ReturnCommand.errored("出错,命令参数应该有所查询Mqtt设备的地址",  command.getId(), command.getCode()) ;
        }
    }
    /**
     * 停止MQTT服务
     * @throws Exception
     */
    private Command stopMqttSv(Command command) throws Exception{
        MqttUnit.getInstance().stop(new UnitCallbackInterface(){
            public void call(Object obj) throws Exception {
            }
        });
        return ReturnCommand.successed("已经启动停止Mqtt服务", command.getId(), command.getCode(), null) ;
    }
 
 
    /**
     * 恢复MQTT服务
     * @throws Exception
     */
    private Command recoverMqttSv(Command command) throws Exception{
        MqttUnit.getInstance().recover(new UnitCallbackInterface(){
            public void call(Object obj) throws Exception {
            }
        });
        return ReturnCommand.successed("已经启动恢复Mqtt服务", command.getId(), command.getCode(), null) ;
    }
 
 
    /**
     * 查询通信中间件运行情况
     * @throws Exception
     */
    private Command mwInfo(Command command) throws Exception{
        MwInfoVo mwInfo = new MwInfoDeal().deal() ;
        return ReturnCommand.successed("查询通信中间件运行情况", command.getId(), command.getCode(), mwInfo) ;
    }
 
 
}