liurunyu
2 天以前 f84d149459b73928c864eb5aebf764cf58105c73
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
package com.dy.pipirrComCreator;
 
import com.dy.common.mw.protocol.p206V1.ProtocolConstantV206V1;
import com.dy.common.util.NumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
 
@Slf4j
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) //禁止启动数据库连接池
@ComponentScan(basePackages = {"com.dy.common", "com.dy.pipIrrGlobal", "com.dy.pipirrComCreator"},
        excludeFilters = {
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
                        com.dy.common.apiDoc.SpringApiConfig.class, //一种排除类的方式
                        com.dy.pipIrrGlobal.config.DingTalk.class
                }),
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = {
                        //二种排除类的方式,写正则表达式,需要对目标类的完全限定名完全匹配,否则不生效
                        "com.dy.common.aop..*",
                        "com.dy.common.apiDoc..*",
                        "com.dy.common.multiDataSource..*",
                        "com.dy.common.singleDataSource..*",
                        "com.dy.common.mybatis..*",
                        "com.dy.common.webFilter..*",
                        "com.dy.common.webListener..*",
                        "com.dy.pipIrrGlobal.config..*",
                        "com.dy.pipIrrGlobal.webCtrls..*"
                })
        }
)
public class PipIrrComCreatorApplication implements CommandLineRunner {
 
 
    /**
     * 参数0:协议名称(当前只支持p206V1)
     * 参数1:协议版本号(当前只支持1)
     * 参数2:rtu地址
     * 参数3:IC卡地址
     * 参数4:IC卡编号
     * @param args
     */
    public static void main(String[] args) {
        int index = 0 ;
        parseArg0(args, index++) ;
        parseArg1(args, index++) ;
        parseArg2(args, index++) ;
        parseArg3(args, index++) ;
        parseArg4(args, index++) ;
 
        new SpringApplicationBuilder(PipIrrComCreatorApplication.class)
                .web(WebApplicationType.NONE)//不启动web服务
                .run(args);
    }
 
 
    private static void parseArg0(String[] args, int index){
        if(args != null
                && args.length > index
                && args[index] != null
                && !args[index].trim().equals("")){
            ServerProperties.protocolName = args[index] ;//协议名称
            log.info("参数" + (index + 1) + ":" + ServerProperties.protocolName);
        }
        if(ServerProperties.protocolName == null || ServerProperties.protocolName.trim().equals("")){
            ServerProperties.protocolName = ProtocolConstantV206V1.protocolName ;
            ServerProperties.protocolVersion = ProtocolConstantV206V1.protocolVer ;
        }
    }
 
    private static void parseArg1(String[] args, int index){
        if(args != null
                && args.length > index
                && args[index] != null
                && !args[index].trim().equals("")){
            if(ServerProperties.protocolVersion == -1){
                if(NumUtil.isPlusIntNumber(args[index].trim())){
                    ServerProperties.protocolVersion = Short.parseShort(args[index].trim()) ;//协议版本号
                }
            }
            log.info("参数" + (index + 1) + ":" + args[index].trim());
        }else{
            if(ServerProperties.protocolVersion == -1){
                ServerProperties.protocolVersion = ProtocolConstantV206V1.protocolVer ;
            }
        }
    }
    private static void parseArg2(String[] args, int index){
        if(args != null
                && args.length > index
                && args[index] != null
                && !args[index].trim().equals("")){
            ServerProperties.rtuAddr = args[index] ;//本模拟器模拟RTU地址
            log.info("参数" + (index + 1) + ":" + ServerProperties.rtuAddr);
        }
        if(ServerProperties.rtuAddr == null){
            ServerProperties.rtuAddr = "37142501020100218" ;
        }
    }
    private static void parseArg3(String[] args, int index){
        if(args != null
                && args.length > index
                && args[index] != null
                && !args[index].trim().equals("")){
            ServerProperties.icCardAddr = args[index] ;//IC卡地址
            log.info("参数" + (index + 1) + ":" + ServerProperties.icCardAddr);
        }
        if(ServerProperties.icCardAddr == null){
            ServerProperties.icCardAddr = "C49A340D" ;
        }
    }
    private static void parseArg4(String[] args, int index){
        if(args != null
                && args.length > index
                && args[index] != null
                && !args[index].trim().equals("")){
            ServerProperties.icCardNo = args[index] ;//IC卡编号
            log.info("参数" + (index + 1) + ":" + ServerProperties.icCardNo);
        }
        if(ServerProperties.icCardNo == null){
            ServerProperties.icCardNo = "37142501020100257" ;
        }
    }
 
    /**
     * Spring容器启动完成后,执行下面方法
     * @param args 参数
     * @throws Exception 异常
     */
    @Override
    public void run(String... args) throws Exception {
        try{
            //等待一下
            Thread.sleep(500L);
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            this.startMwSv() ;
        }
    }
 
    private void startMwSv(){
        if(sv != null){
            try {
                sv.startServer();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
 
 
    private Server sv ;
 
    @Autowired
    public void setSv(Server sv){
        this.sv = sv ;
    }
 
}