Newer
Older
pgdsc / src / com / szpg / plc / message / UnKnownMessage.java
xiaowei on 23 Nov 2018 1 KB 增加巡检运维数据同步
package com.szpg.plc.message;

import org.apache.log4j.Logger;

import com.szpg.plc.util.ByteUtil;
import com.szpg.util.TimeFormat;

public class UnKnownMessage extends AppMessage {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2630503903072334174L;
	
	private final Logger logger = Logger.getLogger(this.getClass().getName());
	
	private byte[] content;

	public UnKnownMessage(byte[] content) {
		this.content = content;
	}

	public String getHexContent() {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < content.length; i++) {
			sb.append(ByteUtil.binToHexString(new byte[] { content[i] }) + " ");
		}

		return sb.toString().trim();
	}
	
	public String getHexContentShort() {
		StringBuilder sb = new StringBuilder();
		int len = 30;
		if (content.length < 30) {
			for (int i = 0; i < content.length; i++) {
				sb.append(ByteUtil.binToHexString(new byte[] { content[i] }) + " ");
			}
			return sb.toString().trim();
		} else {
			for (int i = 0; i < len; i++) {
				sb.append(ByteUtil.binToHexString(new byte[] { content[i] }) + " ");
			}
			return sb.toString().trim() + "...";
		}
	}

	public void afterAction() {
		// 记录日志
		logger.info("未知消息:" + getHexContentShort());
	}

	public String toString() {
		String _time = TimeFormat.format(getTime().getTime());
		String from = getMessageProducerId();
		String str = _time + " 收到 " + from + " 发送未知消息: ";
		for (int i = 0; i < content.length; i++) {
			str = str + ByteUtil.binToHexString(new byte[] { content[i] }) + " ";
		}
		return str;
	}

}