package com.szpg.plc.protocol.fins;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.szpg.db.dao.PgAcuRdcmdDao;
import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl;
import com.szpg.db.data.PgAcuRdcmd;
import com.szpg.plc.message.AppMessage;
import com.szpg.plc.message.AppMessageConstants;
import com.szpg.plc.message.UnKnownMessage;
import com.szpg.plc.message.command.LinkCommand;
import com.szpg.plc.message.command.ReadMemoryCommand;
import com.szpg.plc.message.response.LinkCommandResponse;
import com.szpg.plc.message.response.read.ReadCH4StatusCommandResponse;
import com.szpg.plc.message.response.read.ReadCH4ValueCommandResponse;
import com.szpg.plc.message.response.read.ReadCOStatusCommandResponse;
import com.szpg.plc.message.response.read.ReadCOValueCommandResponse;
import com.szpg.plc.message.response.read.ReadDSStatusCommandResponse;
import com.szpg.plc.message.response.read.ReadFjRtCommandResponse;
import com.szpg.plc.message.response.read.ReadFjStatCommandResponse;
import com.szpg.plc.message.response.read.ReadHSStatusCommandResponse;
import com.szpg.plc.message.response.read.ReadHSValueCommandResponse;
import com.szpg.plc.message.response.read.ReadJgStatusCommandResponse;
import com.szpg.plc.message.response.read.ReadO2StatusCommandResponse;
import com.szpg.plc.message.response.read.ReadO2ValueCommandResponse;
import com.szpg.plc.message.response.read.ReadSbRtCommandResponse;
import com.szpg.plc.message.response.read.ReadSbStatCommandResponse;
import com.szpg.plc.message.response.read.ReadWSStatusCommandResponse;
import com.szpg.plc.message.response.read.ReadWSValueCommandResponse;
import com.szpg.plc.message.response.read.ReadYWStatusCommandResponse;
import com.szpg.plc.message.response.read.ReadZmRtCommandResponse;
import com.szpg.plc.message.response.read.ReadZmStatCommandResponse;
import com.szpg.plc.protocol.DTProtocolInterface;
import com.szpg.plc.protocol.fins.frame.FINSByteFrame;
import com.szpg.plc.protocol.fins.frame.FINSByteFrameTool;
import com.szpg.plc.util.ByteUtil;
import com.szpg.plc.util.Bytes;

public class FINSDTProtocolImp implements DTProtocolInterface {

//	private final Logger logger = Logger.getLogger(this.getClass().getName()); // 输出到message
	
	/**
	 * 从重叠消息中提取规范消息
	 */
	@Override
	public List<byte[]> extractByteMessage(byte[] byteMessage) {
		List<byte[]> bytesList = new ArrayList<byte[]>();
		int count = 0;
		
		try {
			int i = 0;
			while (i < byteMessage.length) {
				if (byteMessage[i] == FINSByteFrame.HEADER[0] && 
					byteMessage[i + 1] == FINSByteFrame.HEADER[1] &&
					byteMessage[i + 2] == FINSByteFrame.HEADER[2] &&
					byteMessage[i + 3] == FINSByteFrame.HEADER[3]) {
					
					// 匹配上FINS帧头
					int length = ByteUtil.binToInt(new byte[]{byteMessage[i + 4], byteMessage[i + 5], byteMessage[i + 6], byteMessage[i + 7]});
					if (i + length + 8 <= byteMessage.length) {
						Bytes bytes = new Bytes();
						bytes.append(byteMessage[i]).append(byteMessage[i + 1]).append(byteMessage[i + 2]).append(byteMessage[i + 3]); //FINS帧头
						bytes.append(byteMessage[i + 4]).append(byteMessage[i + 5]).append(byteMessage[i + 6]).append(byteMessage[i + 7]); //FINS长度
						
						// FINS的数据区
						for (int j = 0; j < length; j++) {
							bytes.append(byteMessage[i + 8 + j]);
						}
						
						i = i + length + 8;
						bytesList.add(bytes.toBytes());
						count++;
					} else {
						i++;
					}
				} else {
					i++;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			bytesList.clear();
			bytesList.add(byteMessage);
			return bytesList;
		}
		
		if (count == 0) {
			bytesList.add(byteMessage);
		}
		
		return bytesList;
	}

	/**
	 * 将字节数组解析为收到的消息对象
	 * 
	 * @param byte[] byteMessage
	 * @return RecievedMessage
	 */
	public AppMessage bytesToMessage(byte[] byteMessage) {

		AppMessage received = null;

		FINSByteFrame finsFrame = new FINSByteFrame(byteMessage);
		if (!finsFrame.valid) {
			received = new UnKnownMessage(byteMessage);
			received.setTime(Calendar.getInstance());
			return received;
		}

		// 根据不同字节内容，解析为各类型应用消息
		String commandStr = FINSByteFrameTool.getCommandStr(byteMessage);
		
		// 握手消息的响应
		if (commandStr.equalsIgnoreCase("00000001")) {
			received = bytesToLinkCommandResponse(finsFrame);
		}
		
		// 读写命令的响应
		if (commandStr.equalsIgnoreCase("00000002")) {
			String commandCode = FINSByteFrameTool.getFinsCommandCode(byteMessage);
			String commandType = FINSByteFrameTool.getControlSID(byteMessage); //用SID字节来代表命令类型，与APPMessageConstants中的命令类型值保持一致
			if (commandCode.equalsIgnoreCase("0101")) {
				// 读内存命令响应的解析
				
				// 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数
				String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址
				
				// 2查询数据中最近的有效的读内存命令，获取其读取的参数类型
				PgAcuRdcmdDao readCmdDao = new PgAcuRdcmdDaoImpl();
				PgAcuRdcmd readCmd = readCmdDao.findLatestCmdByDestAndType(dest, commandType);
				if (null != readCmd) {
					// 3根据参数类型调用相应的方法进行解析
					switch(commandType) {
						case AppMessageConstants.CMD_TYPE_READCH4VALUE:
							received = bytesToReadCH4ValueCommandResponse(finsFrame, readCmd);
							break;
						case AppMessageConstants.CMD_TYPE_READCH4STATUS:
							received = bytesToReadCH4StatusCommandResponse(finsFrame, readCmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READWSVALUE:
							received = bytesToReadWSValueCommandResponse(finsFrame, readCmd);
							break;
						case AppMessageConstants.CMD_TYPE_READWSSTATUS:
							received = bytesToReadWSStatusCommandResponse(finsFrame, readCmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READCOVALUE:
							received = bytesToReadCOValueCommandResponse(finsFrame, readCmd);
							break;
						case AppMessageConstants.CMD_TYPE_READCOSTATUS:
							received = bytesToReadCOStatusCommandResponse(finsFrame, readCmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READO2VALUE:
							received = bytesToReadO2ValueCommandResponse(finsFrame, readCmd);
							break;
						case AppMessageConstants.CMD_TYPE_READO2STATUS:
							received = bytesToReadO2StatusCommandResponse(finsFrame, readCmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READHSVALUE:
							received = bytesToReadHSValueCommandResponse(finsFrame, readCmd);
							break;
						case AppMessageConstants.CMD_TYPE_READHSSTATUS:
							received = bytesToReadHSStatusCommandResponse(finsFrame, readCmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READYWSTATUS:
							received = bytesToReadYWStatusCommandResponse(finsFrame, readCmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READDSSTATUS:
							received = bytesToReadDSStatusCommandResponse(finsFrame, readCmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READFJSTAT:
							received = bytesToReadFjStatCommandResponse(finsFrame, readCmd);
							break;
						case AppMessageConstants.CMD_TYPE_READFJRUNTIME:
							received = bytesToReadFjRtCommandResponse(finsFrame, readCmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READSBSTAT:
							received = bytesToReadSbStatCommandResponse(finsFrame, readCmd);
							break;
						case AppMessageConstants.CMD_TYPE_READSBRUNTIME:
							received = bytesToReadSbRtCommandResponse(finsFrame, readCmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READZMSTAT:
							received = bytesToReadZmStatCommandResponse(finsFrame, readCmd);
							break;
						case AppMessageConstants.CMD_TYPE_READZMRUNTIME:
							received = bytesToReadZmRtCommandResponse(finsFrame, readCmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READJGSTATUS:
							received = bytesToReadJgStatusCommandResponse(finsFrame, readCmd);
							break;
					}
					
					// 4将已响应的命令删除
					readCmdDao.deleteCmdRecord(readCmd.getId());
					
				}
			} else if (commandCode.equalsIgnoreCase("0102")) {
				// 写内存命令响应
			}
		}
		
//		
//		
//
//		default:
//			received = new UnKnownMessage(byteMessage);
//			received.setTime(Calendar.getInstance());
//		}

		return received;
	}

	

	/**
	 * 将握手响应字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToLinkCommandResponse(FINSByteFrame finsFrame) {
		LinkCommandResponse lcr = new LinkCommandResponse();

		byte[] data = finsFrame.TEXT_DATA_BODY;
		lcr.parseData(data);
		
		return lcr;
	}

	/**
	 * 将查询甲烷参数响应消息字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToReadCH4ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadCH4ValueCommandResponse rcvcr = new ReadCH4ValueCommandResponse();
		
		// 设置ACU代码
		rcvcr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rcvcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rcvcr.parseData(data);
			
			// 设置响应对应的命令ID
			rcvcr.setCmdId(cmd.getId());
		} else {
			rcvcr.setValid(false);
		}

		return rcvcr;
	}
	
	
	/**
	 * 将查询甲烷报警状态响应消息字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToReadCH4StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadCH4StatusCommandResponse rcscr = new ReadCH4StatusCommandResponse();
		
		// 设置ACU代码
		rcscr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rcscr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rcscr.parseData(data);
			
			// 设置响应对应的命令ID
			rcscr.setCmdId(cmd.getId());
		} else {
			rcscr.setValid(false);
		}
	
		return rcscr;
	}

	/**
	 * 将读取温湿度监测值响应字节数组转换为消息对象
	 * 
	 * @param finsFrame
	 * @return
	 */
	private AppMessage bytesToReadWSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadWSValueCommandResponse rwvcr = new ReadWSValueCommandResponse();
		
		// 设置ACU代码
		rwvcr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rwvcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rwvcr.parseData(data);
			
			// 设置响应对应的命令ID
			rwvcr.setCmdId(cmd.getId());
		}

		return rwvcr;
	}
	
	
	private AppMessage bytesToReadWSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadWSStatusCommandResponse rwsscr = new ReadWSStatusCommandResponse();
		
		// 设置ACU代码
		rwsscr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rwsscr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rwsscr.parseData(data);
			
			// 设置响应对应的命令ID
			rwsscr.setCmdId(cmd.getId());
		}
	
		return rwsscr;
	}
	
	
	/**
	 * 将查询一氧化碳参数响应消息字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToReadCOValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadCOValueCommandResponse rcvcr = new ReadCOValueCommandResponse();
		
		// 设置ACU代码
		rcvcr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rcvcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rcvcr.parseData(data);
			
			// 设置响应对应的命令ID
			rcvcr.setCmdId(cmd.getId());
		}

		return rcvcr;
	}
	
	
	/**
	 * 将查询一氧化碳报警状态响应消息字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToReadCOStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadCOStatusCommandResponse rcscr = new ReadCOStatusCommandResponse();
		
		// 设置ACU代码
		rcscr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rcscr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rcscr.parseData(data);
			
			// 设置响应对应的命令ID
			rcscr.setCmdId(cmd.getId());
		}
	
		return rcscr;
	}
	
	
	/**
	 * 将查询氧气参数响应消息字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToReadO2ValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadO2ValueCommandResponse rovcr = new ReadO2ValueCommandResponse();
		
		// 设置ACU代码
		rovcr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rovcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rovcr.parseData(data);
			
			// 设置响应对应的命令ID
			rovcr.setCmdId(cmd.getId());
		}

		return rovcr;
	}
	
	
	/**
	 * 将查询氧气报警状态响应消息字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToReadO2StatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadO2StatusCommandResponse roscr = new ReadO2StatusCommandResponse();
		
		// 设置ACU代码
		roscr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		roscr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			roscr.parseData(data);
			
			// 设置响应对应的命令ID
			roscr.setCmdId(cmd.getId());
		}
	
		return roscr;
	}

	
	/**
	 * 将查询硫化氢参数响应消息字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToReadHSValueCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadHSValueCommandResponse rhvcr = new ReadHSValueCommandResponse();
		
		// 设置ACU代码
		rhvcr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rhvcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rhvcr.parseData(data);
			
			// 设置响应对应的命令ID
			rhvcr.setCmdId(cmd.getId());
		}

		return rhvcr;
	}
	
	
	/**
	 * 将查询硫化氢报警状态响应消息字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToReadHSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadHSStatusCommandResponse rhscr = new ReadHSStatusCommandResponse();
		
		// 设置ACU代码
		rhscr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rhscr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rhscr.parseData(data);
			
			// 设置响应对应的命令ID
			rhscr.setCmdId(cmd.getId());
		}
	
		return rhscr;
	}
	
	/**
	 * 将查询爆管液位报警状态响应消息字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToReadYWStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadYWStatusCommandResponse ryscr = new ReadYWStatusCommandResponse();
		
		// 设置ACU代码
		ryscr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		ryscr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			ryscr.parseData(data);
			
			// 设置响应对应的命令ID
			ryscr.setCmdId(cmd.getId());
		}
	
		return ryscr;
	}
	
	/**
	 * 将查询对射报警状态响应消息字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToReadDSStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadDSStatusCommandResponse rdscr = new ReadDSStatusCommandResponse();
		
		// 设置ACU代码
		rdscr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rdscr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rdscr.parseData(data);
			
			// 设置响应对应的命令ID
			rdscr.setCmdId(cmd.getId());
		}
	
		return rdscr;
	}
	
	
	/**
	 * 将查询风机运行状态响应消息字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToReadFjStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadFjStatCommandResponse rfscr = new ReadFjStatCommandResponse();
		
		// 设置ACU代码
		rfscr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rfscr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rfscr.parseData(data);
			
			// 设置响应对应的命令ID
			rfscr.setCmdId(cmd.getId());
		}
	
		return rfscr;
	}
	
	
	/**
	 * 将读取风机运行时长响应字节数组转换为消息对象
	 * 
	 * @param finsFrame
	 * @return
	 */
	private AppMessage bytesToReadFjRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadFjRtCommandResponse rfrcr = new ReadFjRtCommandResponse();
		
		// 设置ACU代码
		rfrcr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rfrcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rfrcr.parseData(data);
			
			// 设置响应对应的命令ID
			rfrcr.setCmdId(cmd.getId());
		}

		return rfrcr;
	}
	
	
	/**
	 * 将查询水泵运行状态响应消息字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToReadSbStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadSbStatCommandResponse rsscr = new ReadSbStatCommandResponse();
		
		// 设置ACU代码
		rsscr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rsscr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rsscr.parseData(data);
			
			// 设置响应对应的命令ID
			rsscr.setCmdId(cmd.getId());
		}
	
		return rsscr;
	}
	
	
	/**
	 * 将读取水泵运行时长响应字节数组转换为消息对象
	 * 
	 * @param finsFrame
	 * @return
	 */
	private AppMessage bytesToReadSbRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadSbRtCommandResponse rsrcr = new ReadSbRtCommandResponse();
		
		// 设置ACU代码
		rsrcr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rsrcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rsrcr.parseData(data);
			
			// 设置响应对应的命令ID
			rsrcr.setCmdId(cmd.getId());
		}

		return rsrcr;
	}
	
	
	/**
	 * 将查询照明运行状态响应消息字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToReadZmStatCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadZmStatCommandResponse rsscr = new ReadZmStatCommandResponse();
		
		// 设置ACU代码
		rsscr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rsscr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rsscr.parseData(data);
			
			// 设置响应对应的命令ID
			rsscr.setCmdId(cmd.getId());
		}
	
		return rsscr;
	}
	
	
	/**
	 * 将读取照明运行时长响应字节数组转换为消息对象
	 * 
	 * @param finsFrame
	 * @return
	 */
	private AppMessage bytesToReadZmRtCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadZmRtCommandResponse rsrcr = new ReadZmRtCommandResponse();
		
		// 设置ACU代码
		rsrcr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rsrcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rsrcr.parseData(data);
			
			// 设置响应对应的命令ID
			rsrcr.setCmdId(cmd.getId());
		}

		return rsrcr;
	}
	
	
	/**
	 * 将查询井盖状态响应消息字节数组转换为消息对象
	 * 
	 * @param byteMessage
	 * @return
	 */
	private AppMessage bytesToReadJgStatusCommandResponse(FINSByteFrame finsFrame, PgAcuRdcmd cmd) {
		ReadJgStatusCommandResponse rjscr = new ReadJgStatusCommandResponse();
		
		// 设置ACU代码
		rjscr.setAcucode(cmd.getDest_acu_code());
		
		byte[] body = finsFrame.TEXT_DATA_BODY;
		rjscr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
		if (body[2] == 0x00 && body[3] == 0x00) {
			// 正常返回
			byte[] data = FINSByteFrameTool.getDataWithoutEndCode(finsFrame); //获取返回的内存
			rjscr.parseData(data);
			
			// 设置响应对应的命令ID
			rjscr.setCmdId(cmd.getId());
		}
	
		return rjscr;
	}
	

	/**
	 * 将消息对象解析为字节数组
	 * 
	 * @param SentMessage message
	 * @return byte[]
	 */
	public byte[] messageToBytes(AppMessage message) {
		byte[] frame = null;

		// 握手命令
		if (message instanceof LinkCommand) {
			frame = LinkCommandToBytes((LinkCommand) message);
		}
		
		// 读内存命令
		if (message instanceof ReadMemoryCommand) {
			frame = readMemoryCommandToBytes((ReadMemoryCommand) message);
		}
//		
//		// 写内存命令
//		if (message instanceof QueryRealTimeValueCommand) {
//			frame = queryRealTimeValueCommandToBytes((QueryRealTimeValueCommand) message);
//		}
		
		return frame;
	}



	/**
	 * 将握手命令转换为字节数组
	 * 
	 * @param hmr
	 * @return
	 */
	private byte[] LinkCommandToBytes(LinkCommand link) {
		FINSByteFrame finsFrame = new FINSByteFrame(link.getMessageProducerId());

		return finsFrame.toBytes();
	}
	
	/**
	 * 将读取PLC内存命令转换为字节数组
	 * 
	 * @param message
	 * @return
	 */
	private byte[] readMemoryCommandToBytes(ReadMemoryCommand message) {
		byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3);
		
		FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), 
													message.getMessageProducerId(), 
													message.getCommandType(),
													message.getMemoryArea(), 
													start, 
													message.getCountWord());
		
		return finsFrame.toBytes();
	}
	
	/**
	 * 将读取甲烷监测值命令转换为字节数组
	 * @param message
	 * @return
	 */
//	private byte[] ReadCH4ValueCommandToBytes(ReadCH4ValueCommand message) {
//		byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3);
//		
//		FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), 
//													message.getMessageProducerId(), 
//													message.getMemoryArea(), 
//													start, 
//													message.getCountWord());
//		
//		return finsFrame.toBytes();
//	}

	/**
	 * 将读取甲烷报警状态命令转换为字节数组
	 * @param message
	 * @return
	 */
//	private byte[] ReadCH4StatusCommandToBytes(ReadCH4StatusCommand message) {
//		byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3);
//		
//		FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), 
//													message.getMessageProducerId(), 
//													message.getMemoryArea(), 
//													start, 
//													message.getCountWord());
//
//		return finsFrame.toBytes();
//	}
	
	/**
	 * 将读取温湿度监测值命令转换为字节数组
	 * 
	 * @param message
	 * @return
	 */
//	private byte[] ReadWSValueCommandToBytes(ReadWSValueCommand message) {
//		byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3);
//		
//		FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), 
//													message.getMessageProducerId(), 
//													message.getMemoryArea(), 
//													start, 
//													message.getCountWord());
//		
//		return finsFrame.toBytes();
//	}
	
	/**
	 * 将读取温湿度报警状态命令转换为字节数组
	 * 
	 * @param message
	 * @return
	 */
//	private byte[] ReadWSStatusCommandToBytes(ReadWSStatusCommand message) {
//		byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3);
//		
//		FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), 
//													message.getMessageProducerId(), 
//													message.getMemoryArea(), 
//													start, 
//													message.getCountWord());
//
//		return finsFrame.toBytes();
//	}

}
