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
package com.dy.pipIrrMwTestRtu;
 
import com.dy.common.mw.UnitInterface;
import com.dy.common.util.ConfigXml4Springboot;
import com.dy.pipIrrMwTestRtu.tcpClient.TcpClUnit;
import com.dy.pipIrrMwTestRtu.tcpClient.TcpClUnitConfigVo;
import org.jdom2.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @Author: liurunyu
 * @Date: 2024/7/12 8:50
 * @Description
 */
 
@Component
public class Server {
    @Autowired
    protected ResourceLoader resourceLoader ;
 
    private ConfigXml4Springboot conf = null ;
    private Document doc = null ;
    private boolean showStartInfo = false ;
 
    private String mwServerUrl ;
 
    private List<UnitInterface> units = new ArrayList<UnitInterface>() ;
 
    /*
     * @param args 参数
    public static void main(String[] args) {
        new Server().startServer();
    }
    */
    /**
     * 启动服务
     */
    public void startServer(){
        long start = System.currentTimeMillis() ;
        try {
            //ConfigProperties.init(this.getClass().getResourceAsStream("/config/config.properties"), false);
 
            this.conf = new ConfigXml4Springboot() ;
            this.doc = this.conf.createDom(resourceLoader, "config.xml") ;
            ////////////////
            //服务 配置
            this.showStartInfo = this.conf.getSetAttrBoolean(this.doc, "config.server", "showStartInfo", null, null) ;
 
            this.startUnits() ;
 
            String svName ;
            try{
                svName = this.conf.getSetAttrTxt(this.doc, "config.server", "name", null, false, null) ;
            }catch(Exception e){
                svName = "" ;
            }
 
            String company ;
            try{
                company = this.conf.getSetAttrTxt(this.doc, "config.server", "company", null, true, null) ;
            }catch(Exception e){
                company = "" ;
            }
            System.out.println("OOOOOOOOOO           OOOOOOOO       OOOOOOOO") ;
            System.out.println("@@@@@@@@@@@@@@@@#O    $@@@@@@@@&    @@@@@@@@#") ;
            System.out.println("@@@@@@@@@@@@@@@@@@@#    @@@@@@@@# $@@@@@@@@&") ;
            System.out.println("@@@@@@@@@@@@@@@@@@@@@#   #@@@@@@@@@@@@@@@@O") ;
            System.out.println("@@@@@@@@@@@@@@@@@@@@@@@   &@@@@@@@@@@@@@@") ;
            System.out.println("@@@@@@$      $@@@@@@@@@&   O@@@@@@@@@@@#") ;
            System.out.println("@@@@@@$        @@@@@@@@@     @@@@@@@@@&      " + svName ) ;
            System.out.println("@@@@@@$       O@@@@@@@@@     &@@@@@@@@") ;
 
            if(this.mwServerUrl != null){
                System.out.println("@@@@@@$       O@@@@@@@@@     &@@@@@@@@       中间件服务端 " + this.mwServerUrl ) ;
            }else{
                System.out.println("@@@@@@$       O@@@@@@@@@     &@@@@@@@@") ;
            }
            System.out.println("@@@@@@$      #@@@@@@@@@$     &@@@@@@@@" ) ;
            System.out.println("@@@@@@@@@@@@@@@@@@@@@@#      &@@@@@@@@       Runing in standalone mode" ) ;
            System.out.println("@@@@@@@@@@@@@@@@@@@@@&       &@@@@@@@@       Startup in " + (System.currentTimeMillis() - start) + " MS" ) ;
            System.out.println("@@@@@@@@@@@@@@@@@@@#         &@@@@@@@@       " + company) ;
            System.out.println("@@@@@@@@@@@@@@@@#O           &@@@@@@@@") ;
 
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    private void startUnits() {
        try {
 
            // ///////////////
            // 基础配置
            ServerProperties.rtuAddr = conf.getSetAttrTxt(doc, "config.base", "rtuAddr", null, false, null);
            ServerProperties.onlyOneHeartBeat = conf.getSetAttrBoolean(doc, "config.base", "onlyOneHeartBeat",  null, null);
            ServerProperties.icCardAddr = conf.getSetAttrTxt(doc, "config.base", "icCardAddr", null, false, null);
            ServerProperties.icCardNo = conf.getSetAttrTxt(doc, "config.base", "icCardNo", null, false, null);
 
            // ///////////////
            // TCP 模块
            TcpClUnitConfigVo tcpVo = new TcpClUnitConfigVo();
            tcpVo.mwServerIp = conf.getSetAttrTxt(doc, "config.tcpCl", "mwServerIp", null, false, null);
            tcpVo.mwServerPort = conf.getSetAttrPlusInt(doc, "config.tcpCl", "mwServerPort", null, 100, 65535, null);
            tcpVo.connectTimeout = conf.getSetAttrPlusInt(doc, "config.tcpCl", "connectTimeout", null, 1000, 10000, null);
            this.mwServerUrl = tcpVo.mwServerIp + ":" + tcpVo.mwServerPort ;
            AdapterImp_TcpClUnit tcpAdap = new AdapterImp_TcpClUnit();
            tcpAdap.setConfig(tcpVo);
            TcpClUnit tcpUnit = TcpClUnit.getInstance();
            tcpUnit.setAdapter(tcpAdap);
            tcpUnit.start(obj -> {
            });
            units.add(tcpUnit) ;
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}