Newer
Older
pgdsc / src / com / szpg / plc / protocol / fins / FINSDTProtocolImp.java
ty-pc\admin on 22 May 2019 26 KB 20190522 环境监测功能梳理
package com.szpg.plc.protocol.fins;

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

import com.szpg.db.dao.PgAcuCmdDao;
import com.szpg.db.dao.impl.PgAcuCmdDaoImpl;
import com.szpg.db.data.PgAcuCmd;
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.command.WriteMemoryCommand;
import com.szpg.plc.message.response.LinkCommandResponse;
import com.szpg.plc.message.response.WriteMemoryCommandResponse;
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中的命令类型值保持一致
			
			// 1首先解析出消息的PLC端地址(即响应消息的源地址)和返回的内存内容的字数
			String dest = FINSByteFrameTool.getControlSour(byteMessage); //命令的目标地址即响应消息的源地址
			
			// 2查询数据中最近的有效的读内存命令,获取其读取的参数类型
			PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl();
			PgAcuCmd cmd = cmdDao.findLatestCmdByDestAndType(dest, commandType);
			
			if (commandCode.equalsIgnoreCase("0101")) {
				// 读内存命令响应的解析
				if (null != cmd) {
					// 3根据参数类型调用相应的方法进行解析
					switch(commandType) {
						case AppMessageConstants.CMD_TYPE_READCH4VALUE:
							received = bytesToReadCH4ValueCommandResponse(finsFrame, cmd);
							break;
						case AppMessageConstants.CMD_TYPE_READCH4STATUS:
							received = bytesToReadCH4StatusCommandResponse(finsFrame, cmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READWSVALUE:
							received = bytesToReadWSValueCommandResponse(finsFrame, cmd);
							break;
						case AppMessageConstants.CMD_TYPE_READWSSTATUS:
							received = bytesToReadWSStatusCommandResponse(finsFrame, cmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READCOVALUE:
							received = bytesToReadCOValueCommandResponse(finsFrame, cmd);
							break;
						case AppMessageConstants.CMD_TYPE_READCOSTATUS:
							received = bytesToReadCOStatusCommandResponse(finsFrame, cmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READO2VALUE:
							received = bytesToReadO2ValueCommandResponse(finsFrame, cmd);
							break;
						case AppMessageConstants.CMD_TYPE_READO2STATUS:
							received = bytesToReadO2StatusCommandResponse(finsFrame, cmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READHSVALUE:
							received = bytesToReadHSValueCommandResponse(finsFrame, cmd);
							break;
						case AppMessageConstants.CMD_TYPE_READHSSTATUS:
							received = bytesToReadHSStatusCommandResponse(finsFrame, cmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READYWSTATUS:
							received = bytesToReadYWStatusCommandResponse(finsFrame, cmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READDSSTATUS:
							received = bytesToReadDSStatusCommandResponse(finsFrame, cmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READFJSTAT:
							received = bytesToReadFjStatCommandResponse(finsFrame, cmd);
							break;
						case AppMessageConstants.CMD_TYPE_READFJRUNTIME:
							received = bytesToReadFjRtCommandResponse(finsFrame, cmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READSBSTAT:
							received = bytesToReadSbStatCommandResponse(finsFrame, cmd);
							break;
						case AppMessageConstants.CMD_TYPE_READSBRUNTIME:
							received = bytesToReadSbRtCommandResponse(finsFrame, cmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READZMSTAT:
							received = bytesToReadZmStatCommandResponse(finsFrame, cmd);
							break;
						case AppMessageConstants.CMD_TYPE_READZMRUNTIME:
							received = bytesToReadZmRtCommandResponse(finsFrame, cmd);
							break;
							
						case AppMessageConstants.CMD_TYPE_READJGSTATUS:
							received = bytesToReadJgStatusCommandResponse(finsFrame, cmd);
							break;
					}
					
					// 4将已响应的命令删除
					cmdDao.deleteCmdRecord(cmd.getId());
					
				}
			} else if (commandCode.equalsIgnoreCase("0102")) {
				if (null != cmd) {
					WriteMemoryCommandResponse wmcr = new WriteMemoryCommandResponse();
					
					// 设置ACU代码
					wmcr.setAcucode(cmd.getDest_acu_code());
					
					// 写内存命令响应
					byte[] body = finsFrame.TEXT_DATA_BODY;
					wmcr.setMessageProducerId(FINSByteFrameTool.getControlDest(finsFrame));
					
					if (body.length == 4) {
						if (body[2] == 0x00 && body[3] == 0x00) {
							// 写入成功
							wmcr.setSuccess(true);
						} else {
							wmcr.setSuccess(false);
						}
						wmcr.setValid(true);
					} else {
						wmcr.setValid(false);
					}
					
					wmcr.setCmdId(cmd.getId());
					wmcr.setCommandType(commandType);
					
					// 4将已响应的命令删除
					cmdDao.deleteCmdRecord(cmd.getId());
					
					received = wmcr;
				}
			}
		} else {
			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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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, PgAcuCmd 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 WriteMemoryCommand) {
			frame = writeMemoryCommandToBytes((WriteMemoryCommand) 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();
	}
	
	/**
	 * 将写PLC内存命令转换为字节数组
	 * @param message
	 * @return
	 */
	private byte[] writeMemoryCommandToBytes(WriteMemoryCommand message) {
		if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_BIT) {
			// 按位操作
			byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 2);
			
			FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), 
														message.getMessageProducerId(), 
														message.getCommandType(), 
														message.getMemoryArea(), 
														start, 
														message.getBit(),
														message.getCount(), 
														message.getValue());
			
			return finsFrame.toBytes();
		} else if (message.getMemoryArea() == FINSConstants.MEMORY_WORK_AREA_WORD) {
			// 按字操作
			byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3);
			
			FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), 
														message.getMessageProducerId(), 
														message.getCommandType(), 
														message.getMemoryArea(), 
														start, 
														message.getCount(), 
														message.getValue());
			
			return finsFrame.toBytes();
		} else if (message.getMemoryArea() == FINSConstants.MEMORY_DM_AREA) {
			// 写D区,按字操作
			byte[] start = ByteUtil.hexStringToBytes(message.getStartAddress(), 3);
			
			FINSByteFrame finsFrame = new FINSByteFrame(message.getDestinationId(), 
														message.getMessageProducerId(), 
														message.getCommandType(), 
														message.getMemoryArea(), 
														start, 
														message.getCount(), 
														message.getValue());

			return finsFrame.toBytes();
		} else {
			return null;
		}
	}
}