zhubaomin
3 天以前 ff3d35c172c5cf5f3ebf8ed710643f8038c3675b
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
package com.dy.pipirrComCreator.console;
 
import com.dy.common.mw.protocol.p206V1.CodeV1;
import com.dy.common.mw.protocol.p206V1.ProtocolConstantV206V1;
import com.dy.pipirrComCreator.ServerProperties;
import com.dy.pipirrComCreator.p206V1.Cd02;
import com.dy.pipirrComCreator.p206V1.Cd10;
import com.dy.pipirrComCreator.p206V1.Cd92;
import com.dy.pipirrComCreator.p206V1.Cd93;
 
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
 
 
public class Command {
    
    private static String[] commands ;
    static{
        commands = new String[]{
                "config 查看配置信息",    
                "XY ... 协议命令",
                "exit 退出",
        };
    }
    
    
    public static boolean dealCommand(String command, PrintWriter prtWrt) throws Exception{
        boolean exit = false ;
        command = command.trim() ;
        if (command.equals("help")) {
            outHelp(prtWrt);
        }else if (command.equals("config")) {
            outConfig(prtWrt);
        }else if(command.equals("exit")){
            exit = true ;
        }else if(isProtocolCom(command)){
            dealProtocolCom(command, prtWrt);
        }else {
            outNoIdentify(prtWrt);
        }
        return exit ;
    }
    private static boolean isProtocolCom(String command){
        if(ServerProperties.protocolName.equals(ProtocolConstantV206V1.protocolName)){
            String[] coms = command.split(" ");
            for (String com : coms){
                if(!com.equals("")){
                    if(CodeV1.isValid(com)){
                        return true ;
                    }else{
                        return false ;
                    }
                }
            }
        }
        return false ;
    }
    private static void dealProtocolCom(String command, PrintWriter prtWrt) throws Exception{
        String[] coms = command.split(" ");
        List<String> comList = new ArrayList<>();
        for (String com : coms){
            if(!com.equals("")){
                comList.add(com) ;
            }
        }
        String com = comList.get(0) ;
        switch (com) {
            case CodeV1.cd_02: cd02(comList, prtWrt); break;
            case CodeV1.cd_10: cd10(comList, prtWrt); break;
            case CodeV1.cd_92: cd92(comList, prtWrt); break;
            case CodeV1.cd_93: cd93(comList, prtWrt); break;
            default: outNoIdentify(prtWrt); break;
        }
    }
 
    private static void cd02(List<String> comList, PrintWriter prtWrt)throws Exception{
        if(comList.size() > 1){
            String p = comList.get(1);
            if(p.equals("-h")){
                prtWrt.println("02[Enter](心跳命令应答(链路维持报应答))");
            }else{
                outNoIdentify(prtWrt) ;
            }
        }else{
            out(new Cd02().hex(ServerProperties.rtuAddr), prtWrt);
        }
    }
    private static void cd10(List<String> comList, PrintWriter prtWrt)throws Exception{
        if(comList.size() > 1){
            String p1 = comList.get(1);
            if(p1.equals("-h")){
                prtWrt.println("10 *...*[Enter](设置控制器地址)");
            }else{
                String[] ps = params2Grp(comList) ;
                Cd10 cd = new Cd10() ;
                String msg = cd.checkParams(ps) ;
                if(msg == null){
                    out(new Cd10().hex(ServerProperties.rtuAddr, ps), prtWrt);
                }else{
                    out(msg, prtWrt);
                }
            }
        }else{
            outNoParams(prtWrt); ;
        }
    }
    private static void cd92(List<String> comList, PrintWriter prtWrt)throws Exception{
        if(comList.size() > 1){
            String p = comList.get(1);
            if(p.equals("-h")){
                prtWrt.println("92[Enter](平台远程开启阀门)");
            }else{
                outNoIdentify(prtWrt) ;
            }
        }else{
            out(new Cd92().hex(ServerProperties.rtuAddr), prtWrt);
        }
    }
    private static void cd93(List<String> comList, PrintWriter prtWrt)throws Exception{
        if(comList.size() > 1){
            String p = comList.get(1);
            if(p.equals("-h")){
                prtWrt.println("93[Enter](平台远程关闭阀门)");
            }else{
                outNoIdentify(prtWrt) ;
            }
        }else{
            out(new Cd93().hex(ServerProperties.rtuAddr), prtWrt);
        }
    }
 
    private static String[] params2Grp(List<String> comList){
        String[] ps = new String[comList.size()-1] ;
        for(int i = 1; i < comList.size(); i++){
            ps[i-1] = comList.get(i) ;
        }
        return ps ;
    }
 
    private static void out(String str, PrintWriter prtWrt){
        prtWrt.println(str==null?"":str);
    }
    private static void outHelp(PrintWriter prtWrt){
        prtWrt.println("");
        prtWrt.println("命令");
        for(String s : commands){
            prtWrt.println("  " + s);
        }
        prtWrt.println("");
    }
    private static void outConfig(PrintWriter prtWrt){
        prtWrt.println("");
        prtWrt.println("预先设置");
        prtWrt.println("  协议:" + ServerProperties.protocolName);
        prtWrt.println("  协议版本号:" + ServerProperties.protocolVersion);
        prtWrt.println("  Rtu地址:" + ServerProperties.rtuAddr);
        prtWrt.println("  IC卡地址:" + ServerProperties.icCardAddr);
        prtWrt.println("  IC卡编号:" + ServerProperties.icCardNo);
        prtWrt.println("");
    }
    private static void outNoIdentify(PrintWriter prtWrt){
        prtWrt.println("");
        prtWrt.println("命令不可识别!");
        prtWrt.println("");
    }
    private static void outNoParams(PrintWriter prtWrt){
        prtWrt.println("");
        prtWrt.println("请输入命令参数!");
        prtWrt.println("");
    }
 
 
 
 
    public static void main(String[] args) {
        String com = "   02 123       345          789";
        com = com.trim() ;
        String[] coms = com.split(" ");
        for(String s : coms){
            System.out.println(s);
        }
    }
 
}