package gmt.protocol.ext;

import gmt.common.util.GmtUtils;
import gmt.protocol.ext.VO.*;
import gmt.protocol.ext.type.ExtType;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class GmtNewExt {
	private PResultVO pResult;
	private NewExtHeaderVO header;
	private String data;
	private ExtMsg_IECVO msg_IEC;
	private ExtMsg_RES01VO msg_RES01;
	private ExtMsg_RES02VO msg_RES02;
	private ExtMsg_SLT01VO msg_SLT01;
	private ExtMsg_CON01VO msg_CON01;
	private ExtMsg_RPC01VO msg_RPC01;
	private ExtMsg_RPC02VO msg_RPC02;
	private ExtMsg_RPC03VO msg_RPC03;
	private ExtMsg_RPC04VO msg_RPC04;
	private ExtMsg_RPC11VO msg_RPC11;
	private ExtMsg_RPC21VO msg_RPC21;
	private ExtMsg_RPC22VO msg_RPC22;
	private ExtMsg_RPC31VO msg_RPC31;
	private ExtMsg_RPC32VO msg_RPC32;
	private ExtMsg_UPS01VO msg_UPS01;
	private ExtMsg_SYN01VO msg_SYN01;
	private ExtMsg_SYN02VO msg_SYN02;
	private ExtMsg_SYN11VO msg_SYN11;
	private ExtMsg_SYN12VO msg_SYN12;
	private ExtMsg_SYN13VO msg_SYN13;
	private ExtMsg_STA11VO msg_STA11;
	private ExtMsg_STA12VO msg_STA12;
	private ExtMsg_CTR02VO msg_CTR02;
	private ExtMsg_STT01VO msg_STT01;
	private ExtMsg_ALR01VO msg_ALR01;
	private ExtMsg_ALR11VO msg_ALR11;
	private ExtMsg_ALR21VO msg_ALR21;
	private ExtMsg_NET01VO msg_NET01;
	private ExtMsg_SNDVO msg_SND;
	private ExtMsg_CABVO msg_CAB;
	private ExtMsg_ABKVO msg_ABK;
	private ExtMsg_ETCVO msg_ETC;
	
	public GmtNewExt() {
		this.pResult   = new PResultVO();
		this.header    = new NewExtHeaderVO();
		this.data      = "";
		this.msg_IEC   = new ExtMsg_IECVO();  
		this.msg_RES01 = new ExtMsg_RES01VO();
		this.msg_RES02 = new ExtMsg_RES02VO();
		this.msg_SLT01 = new ExtMsg_SLT01VO();
		this.msg_CON01 = new ExtMsg_CON01VO();
		this.msg_RPC01 = new ExtMsg_RPC01VO();
		this.msg_RPC02 = new ExtMsg_RPC02VO();
		this.msg_RPC03 = new ExtMsg_RPC03VO();
		this.msg_RPC04 = new ExtMsg_RPC04VO();
		this.msg_RPC11 = new ExtMsg_RPC11VO();
		this.msg_RPC21 = new ExtMsg_RPC21VO();
		this.msg_RPC22 = new ExtMsg_RPC22VO();
		this.msg_RPC31 = new ExtMsg_RPC31VO();
		this.msg_RPC32 = new ExtMsg_RPC32VO();
		this.msg_UPS01 = new ExtMsg_UPS01VO();
		this.msg_SYN01 = new ExtMsg_SYN01VO();
		this.msg_SYN02 = new ExtMsg_SYN02VO();
		this.msg_SYN11 = new ExtMsg_SYN11VO();
		this.msg_SYN12 = new ExtMsg_SYN12VO();
		this.msg_SYN13 = new ExtMsg_SYN13VO();
		this.msg_STA11 = new ExtMsg_STA11VO();
		this.msg_STA12 = new ExtMsg_STA12VO();
		this.msg_CTR02 = new ExtMsg_CTR02VO();
		this.msg_STT01 = new ExtMsg_STT01VO();
		this.msg_ALR01 = new ExtMsg_ALR01VO();
		this.msg_ALR11 = new ExtMsg_ALR11VO();
		this.msg_ALR21 = new ExtMsg_ALR21VO();
		this.msg_NET01 = new ExtMsg_NET01VO();
		this.msg_SND   = new ExtMsg_SNDVO();  
		this.msg_CAB   = new ExtMsg_CABVO();  
		this.msg_ABK   = new ExtMsg_ABKVO();  
		this.msg_ETC   = new ExtMsg_ETCVO();  
	}

	public PResultVO getpResult() {
		return pResult;
	}

	public void setpResult(PResultVO pResult) {
		this.pResult = pResult;
	}

	public NewExtHeaderVO getHeader() {
		return header;
	}

	public void setHeader(NewExtHeaderVO header) {
		this.header = header;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public ExtMsg_IECVO getMsg_IEC() {
		return msg_IEC;
	}

	public void setMsg_IEC(ExtMsg_IECVO msg_IEC) {
		this.msg_IEC = msg_IEC;
	}

	public ExtMsg_RES01VO getMsg_RES01() {
		return msg_RES01;
	}

	public void setMsg_RES01(ExtMsg_RES01VO msg_RES01) {
		this.msg_RES01 = msg_RES01;
	}

	public ExtMsg_RES02VO getMsg_RES02() {
		return msg_RES02;
	}

	public void setMsg_RES02(ExtMsg_RES02VO msg_RES02) {
		this.msg_RES02 = msg_RES02;
	}

	public ExtMsg_SLT01VO getMsg_SLT01() {
		return msg_SLT01;
	}

	public void setMsg_SLT01(ExtMsg_SLT01VO msg_SLT01) {
		this.msg_SLT01 = msg_SLT01;
	}

	public ExtMsg_CON01VO getMsg_CON01() {
		return msg_CON01;
	}

	public void setMsg_CON01(ExtMsg_CON01VO msg_CON01) {
		this.msg_CON01 = msg_CON01;
	}

	public ExtMsg_RPC01VO getMsg_RPC01() {
		return msg_RPC01;
	}

	public void setMsg_RPC01(ExtMsg_RPC01VO msg_RPC01) {
		this.msg_RPC01 = msg_RPC01;
	}

	public ExtMsg_RPC02VO getMsg_RPC02() {
		return msg_RPC02;
	}

	public void setMsg_RPC02(ExtMsg_RPC02VO msg_RPC02) {
		this.msg_RPC02 = msg_RPC02;
	}

	public ExtMsg_RPC03VO getMsg_RPC03() {
		return msg_RPC03;
	}

	public void setMsg_RPC03(ExtMsg_RPC03VO msg_RPC03) {
		this.msg_RPC03 = msg_RPC03;
	}

	public ExtMsg_RPC04VO getMsg_RPC04() {
		return msg_RPC04;
	}

	public void setMsg_RPC04(ExtMsg_RPC04VO msg_RPC04) {
		this.msg_RPC04 = msg_RPC04;
	}

	public ExtMsg_RPC11VO getMsg_RPC11() {
		return msg_RPC11;
	}

	public void setMsg_RPC11(ExtMsg_RPC11VO msg_RPC11) {
		this.msg_RPC11 = msg_RPC11;
	}

	public ExtMsg_RPC21VO getMsg_RPC21() {
		return msg_RPC21;
	}

	public void setMsg_RPC21(ExtMsg_RPC21VO msg_RPC21) {
		this.msg_RPC21 = msg_RPC21;
	}

	public ExtMsg_RPC22VO getMsg_RPC22() {
		return msg_RPC22;
	}

	public void setMsg_RPC22(ExtMsg_RPC22VO msg_RPC22) {
		this.msg_RPC22 = msg_RPC22;
	}

	public ExtMsg_RPC31VO getMsg_RPC31() {
		return msg_RPC31;
	}

	public void setMsg_RPC31(ExtMsg_RPC31VO msg_RPC31) {
		this.msg_RPC31 = msg_RPC31;
	}

	public ExtMsg_RPC32VO getMsg_RPC32() {
		return msg_RPC32;
	}

	public void setMsg_RPC32(ExtMsg_RPC32VO msg_RPC32) {
		this.msg_RPC32 = msg_RPC32;
	}

	public ExtMsg_UPS01VO getMsg_UPS01() {
		return msg_UPS01;
	}

	public void setMsg_UPS01(ExtMsg_UPS01VO msg_UPS01) {
		this.msg_UPS01 = msg_UPS01;
	}

	public ExtMsg_SYN01VO getMsg_SYN01() {
		return msg_SYN01;
	}

	public void setMsg_SYN01(ExtMsg_SYN01VO msg_SYN01) {
		this.msg_SYN01 = msg_SYN01;
	}

	public ExtMsg_SYN02VO getMsg_SYN02() {
		return msg_SYN02;
	}

	public void setMsg_SYN02(ExtMsg_SYN02VO msg_SYN02) {
		this.msg_SYN02 = msg_SYN02;
	}

	public ExtMsg_SYN11VO getMsg_SYN11() {
		return msg_SYN11;
	}

	public void setMsg_SYN11(ExtMsg_SYN11VO msg_SYN11) {
		this.msg_SYN11 = msg_SYN11;
	}

	public ExtMsg_SYN12VO getMsg_SYN12() {
		return msg_SYN12;
	}

	public void setMsg_SYN12(ExtMsg_SYN12VO msg_SYN12) {
		this.msg_SYN12 = msg_SYN12;
	}

	public ExtMsg_SYN13VO getMsg_SYN13() {
		return msg_SYN13;
	}

	public void setMsg_SYN13(ExtMsg_SYN13VO msg_SYN13) {
		this.msg_SYN13 = msg_SYN13;
	}

	public ExtMsg_STA11VO getMsg_STA11() {
		return msg_STA11;
	}

	public void setMsg_STA11(ExtMsg_STA11VO msg_STA11) {
		this.msg_STA11 = msg_STA11;
	}

	public ExtMsg_STA12VO getMsg_STA12() {
		return msg_STA12;
	}

	public void setMsg_STA12(ExtMsg_STA12VO msg_STA12) {
		this.msg_STA12 = msg_STA12;
	}

	public ExtMsg_CTR02VO getMsg_CTR02() {
		return msg_CTR02;
	}

	public void setMsg_CTR02(ExtMsg_CTR02VO msg_CTR02) {
		this.msg_CTR02 = msg_CTR02;
	}

	public ExtMsg_STT01VO getMsg_STT01() {
		return msg_STT01;
	}

	public void setMsg_STT01(ExtMsg_STT01VO msg_STT01) {
		this.msg_STT01 = msg_STT01;
	}

	public ExtMsg_ALR01VO getMsg_ALR01() {
		return msg_ALR01;
	}

	public void setMsg_ALR01(ExtMsg_ALR01VO msg_ALR01) {
		this.msg_ALR01 = msg_ALR01;
	}

	public ExtMsg_ALR11VO getMsg_ALR11() {
		return msg_ALR11;
	}

	public void setMsg_ALR11(ExtMsg_ALR11VO msg_ALR11) {
		this.msg_ALR11 = msg_ALR11;
	}

	public ExtMsg_ALR21VO getMsg_ALR21() {
		return msg_ALR21;
	}

	public void setMsg_ALR21(ExtMsg_ALR21VO msg_ALR21) {
		this.msg_ALR21 = msg_ALR21;
	}

	public ExtMsg_NET01VO getMsg_NET01() {
		return msg_NET01;
	}

	public void setMsg_NET01(ExtMsg_NET01VO msg_NET01) {
		this.msg_NET01 = msg_NET01;
	}

	public ExtMsg_SNDVO getMsg_SND() {
		return msg_SND;
	}

	public void setMsg_SND(ExtMsg_SNDVO msg_SND) {
		this.msg_SND = msg_SND;
	}

	public ExtMsg_CABVO getMsg_CAB() {
		return msg_CAB;
	}

	public void setMsg_CAB(ExtMsg_CABVO msg_CAB) {
		this.msg_CAB = msg_CAB;
	}

	public ExtMsg_ABKVO getMsg_ABK() {
		return msg_ABK;
	}

	public void setMsg_ABK(ExtMsg_ABKVO msg_ABK) {
		this.msg_ABK = msg_ABK;
	}

	public ExtMsg_ETCVO getMsg_ETC() {
		return msg_ETC;
	}

	public void setMsg_ETC(ExtMsg_ETCVO msg_ETC) {
		this.msg_ETC = msg_ETC;
	}
	

	public void init() {
		this.pResult.setMsgType(ExtType.NONE);
		this.pResult.setID(-1);
		this.pResult.setMsgTypeStr("");
		this.header.setStartFlag("");
		this.header.setpID("");
		this.header.setVTSID("");
		this.header.setBSID("");
		this.header.setPacketTime(GmtUtils.defaultTime());
		this.header.setTimeStamp(99);
		this.header.setDualCode(9);
		this.header.setSeq1(0);
		this.header.setSeq2(0);
		this.header.setTimeSyncCode(9);
		this.header.setpResultVO(this.pResult);
		this.data = "";		
	}
	
	public ExtType getExtType(String msg) {
		ExtType result = ExtType.NONE;
		String typeStr = "";
		
		try {
			if (msg.length() < 6) {return result;}
			
			typeStr = msg.substring(1, 4);
			
			if      (typeStr == "ETC") {result = ExtType.ETC;}
			else if (typeStr == "IEC") {result = ExtType.IEC;}
			else if (typeStr == "ALR") {result = ExtType.ALR;}
			else if (typeStr == "ABK") {result = ExtType.ABK;}
			else if (typeStr == "SND") {result = ExtType.SND;}
			else if (typeStr == "RES") {result = ExtType.RES;}
			else if (typeStr == "SLT") {result = ExtType.SLT;}
			else if (typeStr == "CON") {result = ExtType.CON;}
			else if (typeStr == "NET") {result = ExtType.NET;}
			else if (typeStr == "STT") {result = ExtType.STT;}
			else if (typeStr == "STA") {result = ExtType.STA;}
			else if (typeStr == "CTR") {result = ExtType.CTR;}
			else if (typeStr == "RPC") {result = ExtType.RPC;}
			else if (typeStr == "UPS") {result = ExtType.UPS;}
			else if (typeStr == "SYN") {result = ExtType.SYN;}
			else if (typeStr == "CAB") {result = ExtType.CAB;}
			else                       {result = ExtType.NONE;}
			
			typeStr = msg.substring(3, 6);
			if      (typeStr == "ABK") {result = ExtType.ABK;}
			else if (typeStr == "ALR") {result = ExtType.ALR;}
			else if (typeStr == "ACK") {result = ExtType.ETC;}
			
			typeStr = msg.substring(1, 6);
			if    (typeStr == "OTHER") {result = ExtType.ETC;}			
						
		} catch(Exception e){
			result = ExtType.NONE;
		}
		
		return result;
	}
	
	public int getHeaderMsgNo(String msg) {
		int result = -1;
		String msgNoStr;
		
		try {
			if (msg.length() < 6) {return result;}
			
			msgNoStr = msg.substring(4, 6);
			
			if (msgNoStr == "--") {result = -1;}
			else result = GmtUtils.strToIntDef(msgNoStr, -1);			
			
		} catch (Exception e) {
			result = -1;
		}
		return result;		
	}
	
	public String getHeaderTypeStr(String msg) {
		String result = "";
		
		if (msg.length() < 6) {return result;}
		
		result = msg.substring(0, 6);
		
		return result;	
	}	
	
	//메시지별 parsing=================================================================
    //RES
	public boolean parseRES(String msg, int msgNo) {
		boolean result = false;
		String rcvMsg  = "";
		String[] sArr  = null;
		
		try {
			if (msg.length() < 6) {return result;}
			
			rcvMsg = msg.substring(0, msg.length() - 3);

			sArr = rcvMsg.split(",");
			
			if (sArr.length < 3) {return result;}
						
			switch(msgNo) {
				case 1 : {
					this.msg_RES01.setPacketType(msg.substring(3, 6));
					this.msg_RES01.setMsg(msg);
					this.msg_RES01.setCPU(GmtUtils.strToIntDef(sArr[1], 0));
					this.msg_RES01.setRAM(GmtUtils.strToIntDef(sArr[2], 0));
					result = true;
					break;
				}
				case 2 : {
					this.msg_RES02.setPacketType(msg.substring(3, 6));
					this.msg_RES02.setMsg(msg);
					this.msg_RES02.setCPU(GmtUtils.strToIntDef(sArr[1], 0));
					this.msg_RES02.setRAM(GmtUtils.strToIntDef(sArr[2], 0));
					result = true;
					break;
				}
			}				
		} catch(Exception e) {
			result = false;
		}
		
		return result;
	}
	
	//SLT
	public boolean parseSLT(String msg, int msgNo) {
		boolean result = false;
		String rcvMsg  = "";
		String[] sArr  = null;
		
		try {
			if (msg.length() < 6) {return result;}
			
			rcvMsg = msg.substring(0, msg.length() - 3);

			sArr = rcvMsg.split(",");
			
			if (sArr.length < 3) {return result;}
			
			switch(msgNo) {
				case 1 : {
					this.msg_SLT01.setPacketType(msg.substring(3, 6));
					this.msg_SLT01.setMsg(msg);
					this.msg_SLT01.setChannelA(GmtUtils.strToIntDef(sArr[1], -1));
					this.msg_SLT01.setChannelB(GmtUtils.strToIntDef(sArr[2], -1));

					if (this.msg_SLT01.getChannelA() == -1 | 
						this.msg_SLT01.getChannelB() == -1) {return result;}
			
					result = true;
					break;				
				}
			}		 
		} catch(Exception e) {
			result = false;
		}
		
		return result;
	}
	
	//CON
	public boolean parseCON(String msg, int msgNo) {
		boolean result = false;
		String rcvMsg  = "";
		String[] sArr  = null;
		
		try {
			if (msg.length() < 6) {return result;}
			
			rcvMsg = msg.substring(0, msg.length() - 3);

			sArr = rcvMsg.split(",");
			
			if (sArr.length < 6) {return result;}
			
			switch(msgNo) {
				case 1 : {
					this.msg_CON01.setAIS600P(false);
					this.msg_CON01.setAIS600S(false);
					this.msg_CON01.setVTSP(false);
					this.msg_CON01.setVTSS(false);
					this.msg_CON01.setASG(false);
					
					this.msg_CON01.setPacketType(msg.substring(3, 6));
					this.msg_CON01.setMsg(msg);
					
					if (sArr[1].equals("1")) {this.msg_CON01.setAIS600P(true);}				
					if (sArr[2].equals("1")) {this.msg_CON01.setAIS600S(true);}
					if (sArr[3].equals("1")) {this.msg_CON01.setVTSP(true);}
					if (sArr[4].equals("1")) {this.msg_CON01.setVTSS(true);}
					if (sArr[5].equals("1")) {this.msg_CON01.setASG(true);}
					
					result = true;
					break;
				}
			}			
		} catch(Exception e) {
			result = false;
		}
		
		return result;
	}
	
	//RPC
	public boolean parseRPC(String msg, int msgNo) {
		boolean result = false;
		String rcvMsg  = "";
		String[] sArr  = null;
		
		try {
			if (msg.length() < 6) {return result;}
			
			rcvMsg = msg.substring(0, msg.length() - 3);

			sArr = rcvMsg.split(",");
			
			switch(msgNo) {
				case  1 : {
					if (sArr.length < 17) {return result;}
					
					this.msg_RPC01.setPacketType(msg.substring(3, 6));
					this.msg_RPC01.setMsg(msg);
					
					this.msg_RPC01.clearList();
					
					for(int i = 1; i <= 16; i++) {
						
						this.msg_RPC01.addList(GmtUtils.strToIntDef(sArr[i], -1));
						
						if (GmtUtils.strToIntDef(sArr[i], -1) == -1) {return result;} 
					}
					result = true;	
					break;					
				}
				case  2 : {
					if (sArr.length < 4) {return result;}
					
					this.msg_RPC02.setPacketType(msg.substring(3, 6));
					this.msg_RPC02.setMsg(msg);
					this.msg_RPC02.setRequester(GmtUtils.strToIntDef(sArr[1], -1));
					this.msg_RPC02.setCommand(GmtUtils.strToIntDef(sArr[2], -1));
					this.msg_RPC02.setRemark(sArr[3]);
					
					if (this.msg_RPC02.getRequester() == -1) {return result;}
					if (this.msg_RPC02.getCommand()   == -1) {return result;}
					
					result = true;
					break;				
				}
				case  3 : {
					if (sArr.length < 19) {return result;}
					this.msg_RPC03.setPacketType(msg.substring(3, 6));
					this.msg_RPC03.setMsg(msg);
					this.msg_RPC03.setRequester(GmtUtils.strToIntDef(sArr[1], -1));					
					this.msg_RPC03.setRemark(sArr[18]);					
					
					this.msg_RPC03.ClearList();
					
					for(int i = 1; i <= 16; i++) {
						this.msg_RPC03.addList(GmtUtils.strToIntDef(sArr[i + 1], -1));
						
						if (GmtUtils.strToIntDef(sArr[i + 1], -1) == -1) {return result;}					
					}		
					
					if (this.msg_RPC03.getRequester() == -1) {return result;}
					
					result = true;
					break;					
				}
				case 4  : {
					if (sArr.length < 4) {return result;}
					
					this.msg_RPC04.setPacketType(msg.substring(3, 6));
					this.msg_RPC04.setMsg(msg);
					this.msg_RPC04.setRequester(GmtUtils.strToIntDef(sArr[1], -1));
					this.msg_RPC04.setCommand(GmtUtils.strToIntDef(sArr[2], -1));
					this.msg_RPC04.setRemark(sArr[3]);
					
					if (this.msg_RPC04.getRequester() == -1) {return result;}
					if (this.msg_RPC04.getCommand()   == -1) {return result;}
					
					result = true;
					break;	
				}
				case 11 : {
					if (sArr.length < 17) {return result;}
					
					this.msg_RPC11.setPacketType(msg.substring(3, 6));
					this.msg_RPC11.setMsg(msg);
					
					this.msg_RPC11.ClearList();
					
					for(int i = 1; i <= 16; i++) {
						this.msg_RPC11.addList(GmtUtils.strToIntDef(sArr[i], -1));
						
						if (GmtUtils.strToIntDef(sArr[i], -1) == -1) {return result;}					
					}		
					result = true;
					break;		
				}
				case 21 : {
					if (sArr.length < 15) {return result;}

					this.msg_RPC21.setPacketType(msg.substring(3, 6));
					this.msg_RPC21.setMsg(msg);
					this.msg_RPC21.setChkTxMal(false);
					this.msg_RPC21.setChkVSWR(false);
					this.msg_RPC21.setChkRxChA(false);
					this.msg_RPC21.setChkRxChB(false);
					this.msg_RPC21.setChkGeneral(false);
					this.msg_RPC21.setChkUTC(false);
					this.msg_RPC21.setChkNoSensor(false);
					this.msg_RPC21.setChkFrameSync(false);
					this.msg_RPC21.setChkDGPS(false);
					this.msg_RPC21.setChkCommunication(false);
					this.msg_RPC21.setChkTemperature(false);
					this.msg_RPC21.setChkBootMode(false);
					this.msg_RPC21.setChkConfig(false);
					this.msg_RPC21.setChkRcvMsg(false);

					if (sArr[1].equals("1"))  {this.msg_RPC21.setChkTxMal(true);}
					if (sArr[2].equals("1"))  {this.msg_RPC21.setChkVSWR(true);}
					if (sArr[3].equals("1"))  {this.msg_RPC21.setChkRxChA(true);}
					if (sArr[4].equals("1"))  {this.msg_RPC21.setChkRxChB(true);}
					if (sArr[5].equals("1"))  {this.msg_RPC21.setChkGeneral(true);}
					if (sArr[6].equals("1"))  {this.msg_RPC21.setChkUTC(true);}
					if (sArr[7].equals("1"))  {this.msg_RPC21.setChkNoSensor(true);}
					if (sArr[8].equals("1"))  {this.msg_RPC21.setChkFrameSync(true);}
					if (sArr[9].equals("1"))  {this.msg_RPC21.setChkDGPS(true);}
					if (sArr[10].equals("1")) {this.msg_RPC21.setChkCommunication(true);}
					if (sArr[11].equals("1")) {this.msg_RPC21.setChkTemperature(true);}
					if (sArr[12].equals("1")) {this.msg_RPC21.setChkBootMode(true);}
					if (sArr[13].equals("1")) {this.msg_RPC21.setChkConfig(true);}
					if (sArr[14].equals("1")) {this.msg_RPC21.setChkRcvMsg(true);}
					
					result = true;
					break;			
				}
				case 22 : {
					if (sArr.length < 10) {return result;}

					this.msg_RPC22.setPacketType(msg.substring(3, 6));
					this.msg_RPC22.setMsg(msg);				
					this.msg_RPC22.setHUB(GmtUtils.strToIntDef(sArr[1], -1));
					this.msg_RPC22.setROUTER(GmtUtils.strToIntDef(sArr[2], -1));
					this.msg_RPC22.setMODEM(GmtUtils.strToIntDef(sArr[3], -1));
					this.msg_RPC22.setBSPrimary(GmtUtils.strToIntDef(sArr[4], -1));
					this.msg_RPC22.setBSCPrimary1(GmtUtils.strToIntDef(sArr[5], -1));
					this.msg_RPC22.setBSCPrimary2(GmtUtils.strToIntDef(sArr[6], -1));
					this.msg_RPC22.setBSSecondary(GmtUtils.strToIntDef(sArr[7], -1));
					this.msg_RPC22.setBSCSecondary1(GmtUtils.strToIntDef(sArr[8], -1));
					this.msg_RPC22.setBSCSecondary1(GmtUtils.strToIntDef(sArr[9], -1));
					
					if (this.msg_RPC22.getHUB()           == -1 |
						this.msg_RPC22.getROUTER()        == -1 |
						this.msg_RPC22.getMODEM()         == -1 |
						this.msg_RPC22.getBSPrimary()     == -1 |
						this.msg_RPC22.getBSCPrimary1()   == -1 |
						this.msg_RPC22.getBSCPrimary2()   == -1 |
						this.msg_RPC22.getBSSecondary()   == -1 |
						this.msg_RPC22.getBSCSecondary1() == -1 |
						this.msg_RPC22.getBSCSecondary2() == -1) {
						return result;
					}
					result = true;
					break;						
				}
				case 31 : {
					if (sArr.length < 23) {return result;}

					this.msg_RPC31.setPacketType(msg.substring(3, 6));
					this.msg_RPC31.setMsg(msg);
					this.msg_RPC31.setID(sArr[1]);
					this.msg_RPC31.setCtrlType(GmtUtils.strToIntDef(sArr[2], 9));
					this.msg_RPC31.setRequester(GmtUtils.strToIntDef(sArr[3], 9));
					this.msg_RPC31.setPerformer(GmtUtils.strToIntDef(sArr[4], 9));
					this.msg_RPC31.setCommand(GmtUtils.strToIntDef(sArr[5], 9));
					this.msg_RPC31.setRemark(sArr[22]);
					
					this.msg_RPC31.ClearList();
					
					for(int i = 1; i <= 16; i++) {
						this.msg_RPC11.addList(GmtUtils.strToIntDef(sArr[i + 5], -1));
						
						if (GmtUtils.strToIntDef(sArr[i + 5], -1) == -1) {return result;}					
					}		
					result = true;
					break;							
				}
				case 32 : {
					if (sArr.length < 5) {return result;}

					this.msg_RPC32.setPacketType(msg.substring(3, 6));
					this.msg_RPC32.setMsg(msg);
					
					this.msg_RPC32.setID(sArr[1]);
					this.msg_RPC32.setRequester(GmtUtils.strToIntDef(sArr[2], -1));
					this.msg_RPC32.setPerformer(GmtUtils.strToIntDef(sArr[3], -1));
					this.msg_RPC32.setSuccess(GmtUtils.strToIntDef(sArr[4], -1));

					if (this.msg_RPC32.getRequester() == -1 |
						this.msg_RPC32.getPerformer() == -1 |
						this.msg_RPC32.getSuccess()   == -1) {
							return result;
					}
					
					result = true;
					break;						
				}
			}			
		} catch(Exception e) {
			result = false;			
		}
		
		return result;
	}
	
	//UPS
	public boolean parseUPS(String msg, int msgNo) {
		boolean result = false;
		String rcvMsg  = "";
		String[] sArr  = null;
		
		try {
			if (msg.length() < 6) {return result;}
			
			rcvMsg = msg.substring(0, msg.length() - 3);

			sArr = rcvMsg.split(",");	
			
			if (sArr.length < 5) {return result;}
			
			this.msg_UPS01.setPacketType(msg.substring(3, 6));
			this.msg_UPS01.setMsg(msg);
			this.msg_UPS01.setPowerSource(GmtUtils.strToIntDef(sArr[1], 0));
			this.msg_UPS01.setBatteryLevel(GmtUtils.strToIntDef(sArr[2], 0));
			this.msg_UPS01.setPowerLevel(GmtUtils.strToIntDef(sArr[3], 0));
			this.msg_UPS01.setBackupTime(GmtUtils.strToIntDef(sArr[4], 0));
			
			result = true;
			
		} catch(Exception e) {
			result = false;
		}
		
		return result;
	}
	
	//SYN
	public boolean parseSYN(String msg, int msgNo) {
		boolean result = false;
		String rcvMsg  = "";
		String[] sArr  = null;
		
		try {
			if (msg.length() < 6) {return result;}
			
			rcvMsg = msg.substring(0, msg.length() - 3);

			sArr = rcvMsg.split(",");	

			switch(msgNo) {
				case 1 : {
					if (sArr.length < 4) {return result;}
					
					this.msg_SYN01.setPacketType(msg.substring(3, 6));
					this.msg_SYN01.setMsg(msg);
					this.msg_SYN01.setSyncCode(GmtUtils.strToIntDef(sArr[1], -1));
					this.msg_SYN01.setSyncBSID(sArr[2]);
					this.msg_SYN01.setSyncDT(LocalDateTime.parse(sArr[3], DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
					
					if (this.msg_SYN01.getSyncCode() == -1) {return result;}
					
					result = true;
					break;					
				}
				case 2 : {
					if (sArr.length < 3) {return result;}

					this.msg_SYN02.setPacketType(msg.substring(3, 6));
					this.msg_SYN02.setMsg(msg);
					this.msg_SYN02.setSyncCode(GmtUtils.strToIntDef(sArr[1], -1));
					this.msg_SYN02.setSyncBSID(sArr[2]);

					if (this.msg_SYN02.getSyncCode() == -1) {return result;}
					
					result = true;
					break;					
				}
				case 11 : {
					if (sArr.length < 3) {return result;}

					this.msg_SYN11.setPacketType(msg.substring(3, 6));
					this.msg_SYN11.setMsg(msg);
					this.msg_SYN11.setRemark(sArr[2]);
			        				
					//{TODO : SYSTEMTIME??}
					
					result = true;
					break;					
				}
				case 12 : {
					if (sArr.length < 4) {return result;}
					
					this.msg_SYN12.setPacketType(msg.substring(3, 6));
					this.msg_SYN12.setMsg(msg);
					this.msg_SYN12.setRemark(sArr[3]);
					
					//{TODO : SYSTEMTIME??}

					result = true;
					break;					
				}
				case 13 : {
					if (sArr.length < 3) {return result;}

					this.msg_SYN13.setPacketType(msg.substring(3, 6));
					this.msg_SYN13.setMsg(msg);
					this.msg_SYN13.setRemark(sArr[2]);
					
					//{TODO : SYSTEMTIME??}
					
					result = true;
					break;					
				}				
			}
			
		} catch(Exception e) {
			result = false;			
		}
		
		return result;
	}
	
	//STA
	public boolean parseSTA(String msg, int msgNo) {
		boolean result = false;
		String rcvMsg  = "";
		String[] sArr  = null;
		
		try {
			if (msg.length() < 6) {return result;}
			
			rcvMsg = msg.substring(0, msg.length() - 3);

			sArr = rcvMsg.split(",");	
			
			switch(msgNo) {
				case 11 : {
					if (sArr.length < 14) {return result;}

					this.msg_STA11.setPacketType(msg.substring(3, 6));
					this.msg_STA11.setMsg(msg);
					this.msg_STA11.setProcessCode(GmtUtils.strToIntDef(sArr[1], -1));
					this.msg_STA11.setStatus(GmtUtils.strToIntDef(sArr[2], -1));
					this.msg_STA11.setThread(GmtUtils.strToIntDef(sArr[3], -1));
					this.msg_STA11.setDB(GmtUtils.strToIntDef(sArr[4], -1));
					this.msg_STA11.setTxRx(GmtUtils.strToIntDef(sArr[5], -1));
					this.msg_STA11.setUserDef(GmtUtils.strToIntDef(sArr[6], -1));
					this.msg_STA11.setCPU(GmtUtils.strToIntDef(sArr[7], -1));
					this.msg_STA11.setMemory(GmtUtils.strToIntDef(sArr[8], -1));
					this.msg_STA11.setFreezing(GmtUtils.strToIntDef(sArr[9], -1));
					this.msg_STA11.setChkPort(GmtUtils.strToIntDef(sArr[10], -1));
					this.msg_STA11.setchkMW(GmtUtils.strToIntDef(sArr[11], -1));
					this.msg_STA11.setRemark(sArr[12]);
					this.msg_STA11.setVer(sArr[13]);
					
					if (this.msg_STA11.getProcessCode() == -1 |
						this.msg_STA11.getStatus()      == -1 |
						this.msg_STA11.getThread()      == -1 |
						this.msg_STA11.getDB()     	    == -1 |
						this.msg_STA11.getTxRx()   		== -1 |
						this.msg_STA11.getUserDef()   	== -1 |
						this.msg_STA11.getCPU()   		== -1 |
						this.msg_STA11.getMemory() 		== -1 |
						this.msg_STA11.getFreezing() 	== -1 |
						this.msg_STA11.getChkPort() 	== -1 |
						this.msg_STA11.getchkMW() 		== -1) {
						return result;
					}							
					result = true;
					break;					
				}
				case 12 : {
					if (sArr.length < 5) {return result;}
					
					this.msg_STA12.setPacketType(msg.substring(3, 6));
					this.msg_STA12.setMsg(msg);
					this.msg_STA12.setProcessCode(GmtUtils.strToIntDef(sArr[1], -1));
					this.msg_STA12.setStatusCode(GmtUtils.strToIntDef(sArr[2], -1));
					this.msg_STA12.setStatus(GmtUtils.strToIntDef(sArr[3], -1));
					this.msg_STA12.setRemark(sArr[4]);
										
					if (this.msg_STA12.getProcessCode() == -1 |
						this.msg_STA12.getStatusCode() 	== -1 |
						this.msg_STA12.getStatus() 		== -1) {
							return result;
						}							
					
					result = true;
					break;					
				}
			}
		} catch(Exception e) {
			result = false;			
		}
		
		return result;
	}
	
	//CTR
	public boolean parseCTR(String msg, int msgNo) {
		boolean result = false;
		String rcvMsg  = "";
		String[] sArr  = null;
		
		try {
			if (msg.length() < 6) {return result;}
			
			rcvMsg = msg.substring(0, msg.length() - 3);

			sArr = rcvMsg.split(",");	
			
			switch(msgNo) {
				case 02 : {
					if (sArr.length < 4) {return result;}
	
					this.msg_CTR02.setPacketType(msg.substring(3, 6));
					this.msg_CTR02.setMsg(msg);
					this.msg_CTR02.setProcessCode(GmtUtils.strToIntDef(sArr[1], -1));
					this.msg_CTR02.setCommand(GmtUtils.strToIntDef(sArr[2], -1));
					this.msg_CTR02.setRemark(sArr[3]);
					
					if (this.msg_CTR02.getProcessCode() == -1 |
						this.msg_CTR02.getCommand()  	== -1) {
						return result;
					}							
					result = true;
					break;					
				}
			}					
			
		} catch(Exception e) {
			result = false;
		}
		
		return result;
	}
	
	//STT
	public boolean parseSTT(String msg, int msgNo) {
		boolean result = false;
		String rcvMsg  = "";
		String[] sArr  = null;
		
		try {
			if (msg.length() < 6) {return result;}
			
			rcvMsg = msg.substring(0, msg.length() - 3);

			sArr = rcvMsg.split(",");	

			switch(msgNo) {
				case 01 : {
					if (sArr.length < 28) {return result;}
	
					this.msg_STT01.setPacketType(msg.substring(3, 6));
					this.msg_STT01.setMsg(msg);
	
					this.msg_STT01.ClearList();
					
					for(int i = 1; i <= 27; i++) {
						this.msg_STT01.addList(GmtUtils.strToIntDef(sArr[i], -1));
						
						if (GmtUtils.strToIntDef(sArr[i], -1) == -1) {return result;}					
					}		
					result = true;
					break;					
				}
			}					
						
		} catch(Exception e) {
			result = false;
		}
		
		return result;
	}
	
	//ALR
	public boolean parseALR(String msg, int msgNo) {
		boolean result = false;
		String rcvMsg  = "";
		String[] sArr  = null;
		
		try {
			if (msg.length() < 6) {return result;}
			
			rcvMsg = msg.substring(0, msg.length() - 3);

			sArr = rcvMsg.split(",");	
			
			switch(msgNo) {
				case 1 : {
					if (sArr.length < 6) {return result;}
	
					this.msg_ALR01.setPacketType(msg.substring(3, 6));
					this.msg_ALR01.setMsg(msg);
					this.msg_ALR01.setUTC(sArr[1]);
					this.msg_ALR01.setAlarmID(sArr[2]);
					this.msg_ALR01.setCondition(sArr[3]);
					this.msg_ALR01.setAckState(sArr[4]);
					this.msg_ALR01.setRemark(sArr[5]);
	
					result = true;
					break;					
				}
				case 11 : {
					if (sArr.length < 15) {return result;}
	
					this.msg_ALR11.setPacketType(msg.substring(3, 6));
					this.msg_ALR11.setMsg(msg);
					
					this.msg_ALR11.setTxMal(GmtUtils.strToIntDef(sArr[1], -1));
					this.msg_ALR11.setVSWR(GmtUtils.strToIntDef(sArr[2], -1));
					this.msg_ALR11.setRxChA(GmtUtils.strToIntDef(sArr[3], -1));
					this.msg_ALR11.setRxChB(GmtUtils.strToIntDef(sArr[4], -1));
					this.msg_ALR11.setgeneral(GmtUtils.strToIntDef(sArr[5], -1));
					this.msg_ALR11.setUTC(GmtUtils.strToIntDef(sArr[6], -1));
					this.msg_ALR11.setNoSensor(GmtUtils.strToIntDef(sArr[7], -1));
					this.msg_ALR11.setFrameSync(GmtUtils.strToIntDef(sArr[8], -1));
					this.msg_ALR11.setDGPS(GmtUtils.strToIntDef(sArr[9], -1));
					this.msg_ALR11.setCommunication(GmtUtils.strToIntDef(sArr[10], -1));
					this.msg_ALR11.setTemperature(GmtUtils.strToIntDef(sArr[11], -1));
					this.msg_ALR11.setBootMode(GmtUtils.strToIntDef(sArr[12], -1));
					this.msg_ALR11.setConfig(GmtUtils.strToIntDef(sArr[13], -1));
					this.msg_ALR11.setMsgCount(GmtUtils.strToIntDef(sArr[14], -1));
					
					if (this.msg_ALR11.getTxMal() 		  == -1 |
						this.msg_ALR11.getVSWR()          == -1 |
						this.msg_ALR11.getRxChA()      	  == -1 |
						this.msg_ALR11.getRxChB()     	  == -1 |
						this.msg_ALR11.getgeneral()   	  == -1 |
						this.msg_ALR11.getUTC()   		  == -1 |
						this.msg_ALR11.getNoSensor()   	  == -1 |
						this.msg_ALR11.getFrameSync()     == -1 |
						this.msg_ALR11.getDGPS()   		  == -1 |
						this.msg_ALR11.getCommunication() == -1 |
						this.msg_ALR11.getTemperature()   == -1 |
						this.msg_ALR11.getBootMode() 	  == -1 |
						this.msg_ALR11.getConfig()	      == -1 |
						this.msg_ALR11.getMsgCount() 	  == -1) {
						return result;
					}							
					result = true;
					break;					
				}
				case 21 : {
					this.msg_ALR21.setPacketType(msg.substring(3, 6));
					this.msg_ALR21.setMsg(msg);
					this.msg_ALR21.setBSCount(GmtUtils.strToIntDef(sArr[1], -1));
					
					if (this.msg_ALR21.getBSCount() == -1) {return result;}
					
					if (sArr.length != (this.msg_ALR21.getBSCount() * 3 + 2)) {return result;}
					
					int j = 2;
					
					this.msg_ALR21.ClearBSIDList();
					this.msg_ALR21.ClearBSNameList();;
					this.msg_ALR21.ClearStatusList();;
					
					for (int i = 0; i < this.msg_ALR21.getBSCount(); i++) {
						this.msg_ALR21.addBSIDList(sArr[j]);
						j++;
						this.msg_ALR21.addBSNameList(sArr[j]);
						j++;
						this.msg_ALR21.addStatusList(GmtUtils.strToIntDef(sArr[j], -1));
						if (GmtUtils.strToIntDef(sArr[j], -1) == -1) {return result;}		
						j++;
					}
					result = true;
					break;					
				}
			}					
			
			
		} catch(Exception e) {
			result = false;
		}
		
		return result;
	}
	
	//NET
	public boolean parseNET(String msg, int msgNo) {
		boolean result = false;
		String rcvMsg  = "";
		String[] sArr  = null;
		
		try {
			if (msg.length() < 6) {return result;}
			
			rcvMsg = msg.substring(0, msg.length() - 3);

			sArr = rcvMsg.split(",");	
			
			switch(msgNo) {
				case 01 : {
					if (sArr.length < 3) {return result;}
	
					this.msg_NET01.setPacketType(msg.substring(3, 6));
					this.msg_NET01.setMsg(msg);
					this.msg_NET01.setStatus(GmtUtils.strToIntDef(sArr[1], -1));
					this.msg_NET01.setRemark(sArr[2]);
					
					if (this.msg_NET01.getStatus() == -1) {
						return result;
					}							
					result = true;
					break;					
				}
			}				
			
		} catch(Exception e) {
			result = false;
		}
		
		return result;
	}
	
	//SND
	public boolean parseSND(String msg, int msgNo) {
		boolean result = false;
		
		try {			
			if (msg.length() < 6) {return result;}

			this.msg_SND.setPacketType(msg.substring(3, 6));
			this.msg_SND.setMsg(msg);
			
			result = true;
			
		} catch(Exception e) {
			result = false;
		}
		
		return result;
	}
	
	//CAB
	public boolean parseCAB(String msg) {
		boolean result = false;
		String rcvMsg  = "";
		String[] sArr  = null;
		
		try {
			if (msg.length() < 6) {return result;}
			
			rcvMsg = msg.substring(0, msg.length() - 3);

			sArr = rcvMsg.split(",");	
			
			if (sArr.length < 5) {return result;}
			
			this.msg_CAB.setReStart(false);
			this.msg_CAB.setReSet(false);
			
			this.msg_CAB.setPacketType(msg.substring(3, 6));
			this.msg_CAB.setMsg(msg);
			this.msg_CAB.setTRChA(GmtUtils.strToIntDef(sArr[1], 9));
			this.msg_CAB.setTRChB(GmtUtils.strToIntDef(sArr[2], 9));
			
			if (sArr[3].equals("1")) {this.msg_CAB.setReStart(true);}
			if (sArr[4].equals("1")) {this.msg_CAB.setReSet(true);}
			
			result = true;
		} catch(Exception e) {
			result = false;
		}
		
		return result;
	}
	
	//ABK
	public boolean parseABK(String msg) {
		boolean result = false;
		String rcvMsg  = "";
		String[] sArr  = null;
		
		try {
			if (msg.length() < 6) {return result;}
			
			rcvMsg = msg.substring(0, msg.length() - 3);

			sArr = rcvMsg.split(",");	
			
			if (sArr.length < 6) {return result;}
			
			this.msg_ABK.setPacketType(msg.substring(3, 6));
			this.msg_ABK.setMsg(msg);
			this.msg_ABK.setMMSI(sArr[1]);
			this.msg_ABK.setChannel(sArr[2]);
			
			this.msg_ABK.setMsgID(GmtUtils.strToIntDef(sArr[3], -1));
			this.msg_ABK.setSeq(GmtUtils.strToIntDef(sArr[4], -1));
			this.msg_ABK.setAckType(GmtUtils.strToIntDef(sArr[5], -1));

			if (this.msg_ABK.getMsgID()   == -1 |
				this.msg_ABK.getSeq()     == -1 |
				this.msg_ABK.getAckType() == -1) {
				return result;
			}							
			result = true;
		} catch(Exception e) {
			result = false;
		}
		
		return result;
	}	
	
	//ETC
	public boolean parseETC(String msg) {
		boolean result = false;
		
		try {
			this.msg_ETC.setMsg(msg);
			result = true;
			
		} catch(Exception e) {
			result = false;
		}
		
		return result;
	}	
	
	//IEC
	public boolean parseIEC(String msg) {
		boolean result = false;
		
		try {			
			if (msg.length() < 6) {return result;}

			this.msg_IEC.setPacketType(msg.substring(3, 6));
			this.msg_IEC.setMsg(msg);
			
			result = true;
			
		} catch(Exception e) {
			result = false;
		}
		
		return result;
	}	
	
    //==============================================================================
	
	
	public boolean parse(String msg) {
		boolean result      = false;
		String[] sArr       = null;
		String[] sHeaderArr = null;		
		String headerStr;		
		String dataStr;
		
		try {
			this.pResult.setMsgType(ExtType.FAIL);
			this.pResult.setID(-1);
			this.pResult.setMsgTypeStr("");
			init();
			
			if (msg.equals("")) {return result;}
			
			sArr = msg.split("\\|");
			
			if (sArr.length != 2) {return result;}
			
			if (GmtUtils.checkCRC(msg, "*")) {return result;}
			
			headerStr = sArr[0];
			dataStr   = sArr[1].substring(0, sArr[1].length() - 3);
			
			sHeaderArr = headerStr.split(",");
			
			//6 : ExtIEC, 9 : NewExtIEC 
			if (sHeaderArr.length != 9) {return result;}
			
			//Header parsing===================================================================
			this.pResult.setMsgType(getExtType(msg));
			this.pResult.setID(getHeaderMsgNo(msg));
			this.pResult.setMsgTypeStr(getHeaderTypeStr(msg));
			
			if (this.pResult.getMsgType() == ExtType.NONE |
					this.pResult.getID()      == -1           |
					this.pResult.getMsgTypeStr().equals("")) {
					init();
					return result;					
				}		
			
			this.header.setStartFlag(sHeaderArr[0].substring(0, 1));
			this.header.setpID(sHeaderArr[0].substring(1, sHeaderArr[0].length()));
			this.header.setVTSID(sHeaderArr[1]);
			this.header.setBSID(sHeaderArr[2]);
			this.header.setPacketTime(LocalDateTime.parse(sHeaderArr[3], DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
			this.header.setTimeStamp(GmtUtils.strToIntDef(sHeaderArr[4], 99));
			this.header.setDualCode(GmtUtils.strToIntDef(sHeaderArr[5], 9));
			this.header.setSeq1(GmtUtils.strToIntDef(sHeaderArr[6], 0));
			this.header.setSeq2(GmtUtils.strToIntDef(sHeaderArr[7], 0));
			this.header.setTimeSyncCode(GmtUtils.strToIntDef(sHeaderArr[8], 9));
			this.header.setpResultVO(this.pResult);
			this.data = dataStr;
			//=================================================================================

			//Data parsing=====================================================================
			switch(this.pResult.getMsgType()) {
				case NONE :
				case FAIL : {break;} 
				case ETC  : if (parseETC(dataStr))                       {result = true; break;}
				case IEC  : if (parseIEC(dataStr))                       {result = true; break;}
				case ALR  : if (parseALR(dataStr, this.pResult.getID())) {result = true; break;}
				case ABK  : if (parseABK(dataStr))                       {result = true; break;}
				case SND  : if (parseSND(dataStr, this.pResult.getID())) {result = true; break;}
				case RES  : if (parseRES(dataStr, this.pResult.getID())) {result = true; break;}
				case SLT  : if (parseSLT(dataStr, this.pResult.getID())) {result = true; break;}
				case CON  : if (parseCON(dataStr, this.pResult.getID())) {result = true; break;}
				case NET  : if (parseNET(dataStr, this.pResult.getID())) {result = true; break;}
				case STT  : if (parseSTT(dataStr, this.pResult.getID())) {result = true; break;}
				case STA  : if (parseSTA(dataStr, this.pResult.getID())) {result = true; break;}
				case CTR  : if (parseCTR(dataStr, this.pResult.getID())) {result = true; break;}
				case RPC  : if (parseRPC(dataStr, this.pResult.getID())) {result = true; break;}
				case UPS  : if (parseUPS(dataStr, this.pResult.getID())) {result = true; break;}
				case SYN  : if (parseSYN(dataStr, this.pResult.getID())) {result = true; break;}
				case CAB  : if (parseCAB(dataStr))                       {result = true; break;}
			}
			
			if (!result) {init();}
			//=================================================================================					
		} catch(Exception e) {
			init();
			return result;			
		}
			
		return result;
	}

}
