package com.szpg.plc.message.response;

import java.util.Calendar;

import org.apache.log4j.Logger;

import com.szpg.plc.message.CommandResponse;
import com.szpg.plc.util.ByteUtil;

public class LinkCommandResponse extends CommandResponse {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3918316368349985732L;
	
	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	public LinkCommandResponse() {
		super();
	}

	public LinkCommandResponse(Calendar time) {
		super(time);
	}

	public LinkCommandResponse(Calendar time, String messageProducerId) {
		super(time, messageProducerId);
	}

	@Override
	public void afterAction() {
		if (isValid() == true) {
			logger.debug("终端[" + getMessageProducerId() + "]握手成功");
		}
		else {
			logger.warn("终端[" + getMessageProducerId() + "]握手失败");
		}
	}

	@Override
	public void parseData(byte[] messageData) {
		if (messageData.length == 8) {
			String sour = ByteUtil.binToHexString(messageData, 8, 16); //握手响应消息的源地址，实际上是PLC地址
			String dest = ByteUtil.binToHexString(messageData, 0, 8); //握手响应消息的目标地址，实际上是本机地址
		
			setMessageProducerId(sour);
			setDestinationId(dest);
			setTime(Calendar.getInstance());
			setValid(true);
		} else {
			setValid(false);
			logger.debug("握手命令响应消息数据域长度不符");
		}
	}
	
	@Override
	public String toString() {
		return "终端[" + getMessageProducerId() + "]握手命令响应";
	}
	
}
