package com.dy.simRtu; 
 | 
  
 | 
import com.dy.common.mw.UnitInterface; 
 | 
import com.dy.common.util.ConfigXml4Springboot; 
 | 
import com.dy.simRtu.tcpClient.TcpClUnit; 
 | 
import com.dy.simRtu.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/11/18 9:54 
 | 
 * @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>() ; 
 | 
  
 | 
    /** 
 | 
     * 启动服务 
 | 
     */ 
 | 
    public void startServer() { 
 | 
        long start = System.currentTimeMillis(); 
 | 
        try { 
 | 
            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() ; 
 | 
  
 | 
            if(ServerProperties.argRtuAddr != null && !ServerProperties.argRtuAddr.trim().equals("")){ 
 | 
                System.out.println("参数RTU地址:" + ServerProperties.argRtuAddr) ; 
 | 
                ServerProperties.rtuAddr = ServerProperties.argRtuAddr ; 
 | 
            }else{ 
 | 
                 System.out.println("配置RTU地址:" + ServerProperties.rtuAddr) ; 
 | 
            } 
 | 
  
 | 
            System.out.println("启动一包死:" + (ServerProperties.argOneDie==null?"否":(ServerProperties.argOneDie?"是":"否"))) ; 
 | 
            System.out.println("启动多包死:" + (ServerProperties.argMultiDie==null?"否":(ServerProperties.argMultiDie?"是":"否"))) ; 
 | 
  
 | 
  
 | 
  
 | 
            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("@@@@@@$      #@@@@@@@@@$     &@@@@@@@@       本RTU模拟地址 " + ServerProperties.rtuAddr ) ; 
 | 
            System.out.println("@@@@@@@@@@@@@@@@@@@@@@#      &@@@@@@@@       Running 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); 
 | 
  
 | 
            // /////////////// 
 | 
            // TCP 模块 
 | 
            TcpClUnitConfigVo tcpVo = new TcpClUnitConfigVo(); 
 | 
            if(ServerProperties.serverIp != null && !ServerProperties.serverIp.trim().equals("")){ 
 | 
                tcpVo.mwServerIp = ServerProperties.serverIp ; 
 | 
            }else{ 
 | 
                tcpVo.mwServerIp = conf.getSetAttrTxt(doc, "config.tcpCl", "mwServerIp", null, false, null); 
 | 
            } 
 | 
            if(ServerProperties.serverPort != null){ 
 | 
                tcpVo.mwServerPort = ServerProperties.serverPort ; 
 | 
            }else{ 
 | 
                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(); 
 | 
        } 
 | 
    } 
 | 
} 
 |