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
package com.dy.simRtu;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
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;
 
 
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) //禁止启动数据库连接池
@ComponentScan(basePackages = {"com.dy.common", "com.dy.pipIrrGlobal", "com.dy.simRtu"},
        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.mybatis..*",
                        "com.dy.common.singleDataSource..*",
                        "com.dy.common.webFilter..*",
                        "com.dy.common.webListener..*",
                        "com.dy.pipIrrGlobal.webCtrls..*"
                })
        }
)
public class PipIrrMwSimulateRtuApplication implements CommandLineRunner {
 
    public static void main(String[] args) {
        if(args != null && args.length > 0){
            if(args.length == 1){
                getArg0(args) ;
            }else if(args.length == 2){
                getArg0(args) ;
                getArg1(args) ;
            }else if(args.length == 3){
                getArg0(args) ;
                getArg1(args) ;
                getArg2(args) ;
            }else if(args.length == 4){
                getArg0(args) ;
                getArg1(args) ;
                getArg2(args) ;
                getArg3(args) ;
            }else if(args.length == 5){
                getArg0(args) ;
                getArg1(args) ;
                getArg2(args) ;
                getArg3(args) ;
                getArg4(args) ;
            }
 
            if(ServerProperties.argOneDie && ServerProperties.argMultiDie){
                ServerProperties.argOneDie = false ;
            }
        }
        new SpringApplicationBuilder(PipIrrMwSimulateRtuApplication.class)
                .web(WebApplicationType.NONE)//不启动web服务
                .run(args);
    }
    private static void getArg0(String[] args){
        if(args[0] instanceof String){
            if(args[0] != null && !args[0].trim().equals("")){
                String rtuAddr = (String)args[0] ;//本模拟器模拟RTU地址
                ServerProperties.argRtuAddr = rtuAddr ;
            }
        }
    }
    private static void getArg1(String[] args){
        if(args[1] != null && !args[1].trim().equals("")){
            Integer oneDie = Integer.parseInt(args[1]) ;//一包死:1是,0否
            ServerProperties.argOneDie = oneDie == 1?true:false ;
        }
    }
    private static void getArg2(String[] args){
        if(args[2] != null && !args[2].trim().equals("")){
            Integer multiDie = Integer.parseInt(args[2]) ;//多包死:1是,0否
            ServerProperties.argMultiDie = multiDie == 1?true:false ;
        }
    }
    private static void getArg3(String[] args){
        if(args[3] instanceof String){
            if(args[3] != null && !args[3].trim().equals("")){
                String serverIp = (String)args[3] ;//服务端IP
                ServerProperties.serverIp = serverIp ;
            }
        }
    }
    private static void getArg4(String[] args){
        if(args[4] != null && !args[4].trim().equals("")){
            Integer serverPort = Integer.parseInt(args[4]) ;//服务端port
            ServerProperties.serverPort = serverPort ;
        }
    }
 
 
    /**
     * 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){
            sv.startServer();
        }
    }
 
 
    private Server sv ;
 
    @Autowired
    public void setSv(Server sv){
        this.sv = sv ;
    }
}