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 GmtExt {
	private PResultVO pResult;
	private ExtHeaderVO 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_UPS01VO msg_UPS01;
	private ExtMsg_ALR01VO msg_ALR01;
	private ExtMsg_SNDVO msg_SND;
	private ExtMsg_ABKVO msg_ABK;
	private ExtMsg_ETCVO msg_ETC;
	
	public GmtExt() {
		this.pResult   = new PResultVO();	
		this.header    = new ExtHeaderVO();
		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_UPS01 = new ExtMsg_UPS01VO();
		this.msg_ALR01 = new ExtMsg_ALR01VO();
		this.msg_SND   = new ExtMsg_SNDVO();
		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 ExtHeaderVO getHeader() {
		return header;
	}

	public void setHeader(ExtHeaderVO 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_UPS01VO getMsg_UPS01() {
		return msg_UPS01;
	}

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

	public ExtMsg_ALR01VO getMsg_ALR01() {
		return msg_ALR01;
	}

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

	public ExtMsg_SNDVO getMsg_SND() {
		return msg_SND;
	}

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

	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.setPID("");
		this.header.setVTSID("");
		this.header.setBSID("");
		this.header.setPacketTime(GmtUtils.defaultTime());
		this.header.setItemIndex("");
		this.header.setFilteringFlag("");
		this.header.setPResultVO(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=================================================================
	//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) {			
		}
		return result;
	}
	
	//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 01 : {
					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 02 : {
					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) {
		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 < 4) {return result;}
			
			this.msg_SLT01.setChannelA(GmtUtils.strToIntDef(sArr[1], -1));
			this.msg_SLT01.setChannelB(GmtUtils.strToIntDef(sArr[2], -1));
			
			result = true;
			
			if (this.msg_SLT01.getChannelA() == -1 |
				this.msg_SLT01.getChannelB() == -1) {
				result = false;
			}			
		}catch(Exception e) {
			result = false;
		}
		
		return result;
	}
	
	//CON
	public boolean parseCON(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_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] == "1") {this.msg_CON01.setAIS600P(true);}
			if (sArr[2] == "1") {this.msg_CON01.setAIS600S(true);}
			if (sArr[3] == "1") {this.msg_CON01.setVTSP(true);}
			if (sArr[4] == "1") {this.msg_CON01.setVTSS(true);}
			if (sArr[5] == "1") {this.msg_CON01.setASG(true);}
			
			result = true;	
			
		} 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.setMsg(msg);
					this.msg_RPC01.setPacketType(msg.substring(3, 6));
					
					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 < 2) {return result;}

					this.msg_RPC02.setMsg(msg);
					this.msg_RPC02.setPacketType(msg.substring(3, 6));
					this.msg_RPC02.setRequester(9);
					this.msg_RPC02.setCommand(GmtUtils.strToIntDef(sArr[2], -1));
					this.msg_RPC02.setRemark("");
					
					if (GmtUtils.strToIntDef(sArr[2], -1) == -1) {return result;}
					
					result = true;
					break;
				}
				case 3 : {
					if (sArr.length < 17) {return result;}
					
					this.msg_RPC03.setMsg(msg);
					this.msg_RPC03.setPacketType(msg.substring(3, 6));
					this.msg_RPC03.setRequester(9);
					this.msg_RPC03.setRemark("");
					
					this.msg_RPC03.ClearList();
					
					for(int i = 1; i <= 16; i++) {
						this.msg_RPC03.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;
	}
	
	//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;
	}
	
	//ALR
	public boolean parseALR(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_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;
		
		}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;
	}

	//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) {
				result = false;
			} else result = true;
			
		}catch(Exception e) {
			result = false;
		}		
		return result;
	}

	//ETC
	public boolean parseETC(String msg) {
		this.msg_ETC.setMsg(msg);
		
		return true;
	}
	//==============================================================================
	
	
	
	//메시지별 Make====================================================================
	//RPC02
	public String makeRPC02(ExtMsg_RPC02VO vo) {
		String result  = "";
		String makeMsg = "";
		
		try {
			makeMsg = "!--RPC" + ",";
			
			makeMsg = makeMsg + String.valueOf(vo.getCommand());
			
			makeMsg = makeMsg + "*" + GmtUtils.makeCRC(makeMsg);
			
			result  = makeMsg;
			
		}catch(Exception e) {
			result = "";
		}
		return result;
	}
	
	public String makeRPC03(ExtMsg_RPC03VO vo) {
		String result  = "";
		String makeMsg = "";
		int command    = -99;
		
		try {
			makeMsg = "!--RPC" + ",";
			
			if (vo.getRPCCommandList().size() != 16) {return result;}
			
			for(int i = 0; i < 16; i++) {
				command = vo.getRPCCommandItem(i);
				
				if (command == -99) {return result;}
				
				makeMsg = makeMsg + String.valueOf(command);
				
				if (i != 15) {
					makeMsg = makeMsg + ",";
				}				
			}
			
			makeMsg = makeMsg + "*" + GmtUtils.makeCRC(makeMsg);
			
			result = makeMsg;				
			
		}catch(Exception e) {
			result = "";
		}
		return result;
	}
	
	//==============================================================================
	
	

	
	public String makeExtMsg(String msgType, String IECMsg, String VTSID, String BSID, boolean isPrimary, LocalDateTime msgDT) {
		String result     = "";
		String makeMsg    = "";
		String chkVTSID   = "";
		String primary    = "";
		String msgTime    = "";
		String msgTypeTmp = "";
		
		try {
			if (IECMsg.equals("")) {return result;}
			
			chkVTSID = VTSID;
			
			if (!VTSID.substring(0, 3).toUpperCase().equals("VTS")) {
				chkVTSID = "VTS" + VTSID;
			}
			
			if (isPrimary) {
				primary = "P";
			} else primary = "S";
			
			msgTime = msgDT.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); 

			msgTypeTmp = msgType;
			if (msgTypeTmp.equals("$ALR01")) {
				msgTypeTmp = "$AIALR";
			}
			
			makeMsg = msgTypeTmp + "," + 
			          chkVTSID   + "," +
					  BSID       + "," +
			          primary    + "," +
					  "0"        + "," +
			          msgTime    + "|" +
					  IECMsg;
			
			makeMsg = makeMsg + "*" + GmtUtils.makeCRC(makeMsg);
			
			result  = makeMsg;			
			
		}catch(Exception E) {
			result = "";
		}		
		
		return result;
	}	
	
//	public PResultVO parse(String msg) {
//		PResultVO result    = null;
//		String[] sArr       = null;
//		String[] sHeaderArr = null;
//		String headerStr;
//		String dataStr;
//		
//		try {
//			result = new PResultVO();
//			result.setMsgType(ExtType.FAIL);
//			result.setID(-1);
//			result.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(",");
//			
//			if (sHeaderArr.length != 6) {return result;}
//			
//			
//			try {
//				//Header parsing===================================================================
//				result.setMsgType(getExtType(msg));
//				result.setID(getHeaderMsgNo(msg));
//				result.setMsgTypeStr(getHeaderTypeStr(msg));
//				
//				if (result.getMsgType() == ExtType.NONE |
//					result.getID()      == -1           |
//					result.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[5], DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
//				this.header.setItemIndex(sHeaderArr[3]);
//				this.header.setFilteringFlag(sHeaderArr[4]);
//				this.header.setPResultVO(result);
//				this.data = dataStr;
//				//=================================================================================
//				
//				
//				//Data parsing=====================================================================
//				switch(result.getMsgType()) {
//					case NONE : 
//					case FAIL : 
//					case NET : 
//					case STT : 
//					case STA :
//					case CTR : 
//					case SYN : 
//					case CAB : {init();	return result;}
//					case ETC : if (!parseETC(dataStr))                 {init(); return result;}
//					case IEC : if (!parseIEC(dataStr))                 {init(); return result;}
//					case ALR : if (!parseALR(dataStr))                 {init(); return result;}
//					case ABK : if (!parseABK(dataStr))                 {init(); return result;}
//					case SND : if (!parseSND(dataStr, result.getID())) {init(); return result;}
//					case RES : if (!parseRES(dataStr, result.getID())) {init(); return result;}
//					case SLT : if (!parseSLT(dataStr))                 {init(); return result;}
//					case CON : if (!parseCON(dataStr))                 {init(); return result;}
//					case RPC : if (!parseRPC(dataStr, result.getID())) {init(); return result;}
//					case UPS : if (!parseUPS(dataStr, result.getID())) {init(); return result;}			
//				}
//				//=================================================================================
//
//			} catch(Exception e) {
//				init();
//				return result;
//			}
//			
//			this.pResult = result;
//			return result;		
//			
//		} catch(Exception e) {
//			init();
//			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 != 6) {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[5], DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
			this.header.setItemIndex(sHeaderArr[3]);
			this.header.setFilteringFlag(sHeaderArr[4]);
			this.header.setPResultVO(this.pResult);
			this.data = dataStr;
			//=================================================================================
			
			
			//Data parsing=====================================================================
			switch(this.pResult.getMsgType()) {
				case NONE : 
				case FAIL : 
				case NET : 
				case STT : 
				case STA :
				case CTR : 
				case SYN : 
				case CAB : {break;}
				case ETC : if (parseETC(dataStr)) {result = true; break;}
				case IEC : if (parseIEC(dataStr))                       {result = true; break;}
				case ALR : if (parseALR(dataStr))                       {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))                       {result = true; break;}
				case CON : if (parseCON(dataStr))                       {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;}	
			}
			
			if (!result) {init();}
			//=================================================================================
			
		} catch(Exception e) {
			init();
			return result;			
		}
		
		return result;
	}
}
