zhubaomin
2024-08-22 81636c3be211bdd6c32b60df5b89d2026911f24e
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
package com.dy.testServer.console;
 
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 
import com.dy.common.mw.channel.rmi.RmiConfigVo;
import com.dy.common.mw.channel.rmi.RmiUnit;
import com.dy.testServer.ServerProperties;
import com.dy.testServer.forRmi.MwTestClientStatus;
import com.dy.testServer.forRmi.Manager;
 
public class Command {
    
    private static final String[] commands ;
    static{
        commands = new String[]{
                "config 查看配置信息",    
                "show 显示mwTest情况",    
                "start 启动mwTest建立TCP连接数据",
                "report 启动mwTest RTU上报数据",
                "exit 退出",
        };
    }
    
    
    public static boolean dealCommand(String command, PrintWriter prtWrt){
        boolean exit = false ;
        if (command.equals("help")) {
            outCommand(prtWrt);
        } else if (command.equals("config")) {
            config(prtWrt);
        } else if (command.equals("show")) {
            show(prtWrt);
        } else if (command.equals("start")) {
            startNetConnect(prtWrt);
        } else if (command.equals("report")) {
            startRtuReport(prtWrt);
        } else if(command.equals("exit")){
            exit = true ;
        } else {
            outError(prtWrt);
        }
        return exit ;
    }
    private static void outCommand(PrintWriter prtWrt){
        prtWrt.println("");
        prtWrt.println("命令");
        for(String s : commands){
            prtWrt.println("  " + s);
        }
        prtWrt.println("");
    }
    private static void outError(PrintWriter prtWrt){
        prtWrt.println("");
        prtWrt.println("命令不可识别!");
        prtWrt.println("");
    }
    
    private static void config(PrintWriter prtWrt){
        prtWrt.println("");
        prtWrt.println("相关mwTest的配置");
        prtWrt.println("  模拟客户的RtuAddr起始号:" + ServerProperties.rtuAddrStart);
        prtWrt.println("  模拟客户的RtuAddr截止号:" + ServerProperties.rtuAddrEnd);
        prtWrt.println("  每个mwTest分配的RtuAddr数:" + ServerProperties.rtuAddrPerMwTest);
        prtWrt.println("  通信中间件IP:" + ServerProperties.tcpServerIp);
        prtWrt.println("  通信中间件端口:" + ServerProperties.tcpServerPort);
        prtWrt.println("  mwTest发送数据间隔(秒):" + ServerProperties.sendInterval);
        prtWrt.println("  mwTest每RtuAddr号发送数据次数:" + ServerProperties.sendTimes);
        prtWrt.println("  mwTest每一轮次发送数据,其中包括心跳发送次数:" + ServerProperties.heartbeatTimes);
        prtWrt.println("本服务RMI Server配置");
        String ip = null ;
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        RmiConfigVo confVo = RmiUnit.getInstance().getAdapter().getConfig() ;
        if(ip != null){
            prtWrt.println("  RMI Server:" + ip + ":" + confVo.port + "/" + confVo.context);
        }else{
            prtWrt.println("  RMI Server:[ip]:" + confVo.port + "/" + confVo.context);
        }
        prtWrt.println("");
    }
    
    private static void show(PrintWriter prtWrt){
        prtWrt.println("");
        prtWrt.println("当前已注册mwTest客户端数量:" + Manager.id2TokenMap.size());
        if(Manager.id2TokenMap.size() > 0){
            prtWrt.println("mwTest客户端及其工作信息");
            Set<Map.Entry<String, MwTestClientStatus>> set = Manager.token2ClientMap.entrySet() ;
            Iterator<Map.Entry<String, MwTestClientStatus>> it = set.iterator() ;
            Map.Entry<String, MwTestClientStatus> ent ;
            while(it.hasNext()){
                ent = it.next() ;
                prtWrt.println("  mwTest客户端(token=" + ent.getKey() + ")");
                MwTestClientStatus sta = ent.getValue() ;
                if(sta != null){
                    if(sta.confVo != null){
                        prtWrt.println("    rtuAddr范围:" + sta.confVo.rtuAddrStart + "--" + sta.confVo.rtuAddrEnd);
                    }
                    if(sta.startVo != null){
                        prtWrt.println("    TCP连接:" + (sta.startVo.start?"已经开始":"未开始"));
                    }else{
                        prtWrt.println("    TCP连接:未开始") ;
                    }
                    if(sta.reportVo != null){
                        prtWrt.println("    RTU上报数据:" + (sta.reportVo.report?"已经开始":"未开始"));
                    }else{
                        prtWrt.println("    RTU上报数据:未开始") ;
                    }
                    if(sta.count != null){
                        prtWrt.println("    已经上报数据:" + sta.count + "条");
                    }
                    if(sta.overCount != null){
                        prtWrt.println("    已经完成上报数据:" + sta.overCount + "台RTU");
                    }
                    if(sta.over != null && sta.over){
                        prtWrt.println("    上报数据已经完成,共用时:" + sta.seconds + "秒");
                    }
                }
            }
        }
        prtWrt.println("");
    }
    
    private static void startNetConnect(PrintWriter prtWrt){
        prtWrt.println("");
        prtWrt.println("  已经允许mwTest建立TCP连接");
        prtWrt.println("");
        Manager.enablemwTestStartTcpConnect = true ;
    }
 
    private static void startRtuReport(PrintWriter prtWrt){
        prtWrt.println("");
        prtWrt.println("  已经允许mwTest RTU上报数据了");
        prtWrt.println("");
        Manager.enablemwTestStartRtuReport = true ;
    }
 
 
}