zhubaomin
2 天以前 c2976b80e8850be2d28dc2132c6659856b871f3c
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
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.P206V1Deal;
 
import java.io.PrintWriter;
 
 
public class Command {
    
    private static String[] commands ;
    static{
        commands = new String[]{
                "config 查看配置信息",    
                "XY ... 协议命令",
                "XY -h 协议命令帮助",
                "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)){
            if(ServerProperties.protocolName.equals(ProtocolConstantV206V1.protocolName)){
                P206V1Deal.dealProtocolCom(command, prtWrt);
            }else{
                out("当前不支持协议" + ServerProperties.protocolName, prtWrt)  ;
            }
        }else {
            outNoIdentify(prtWrt);
        }
        return exit ;
    }
    public 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 ;
    }
 
    public 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("");
    }
    public 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("");
    }
    public static void outNoIdentify(PrintWriter prtWrt){
        prtWrt.println("");
        prtWrt.println("命令不可识别!");
        prtWrt.println("");
    }
    public 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);
        }
    }
 
}