Message02.java 1.63 KB
package com.bsth.socket.protocol;

import com.bsth.util.ConvertUtil;
import org.joda.time.DateTime;

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

/**
 * @author Hill
 * 实时信息上报
 */
public class Message02 implements IMessageBody {

	/**
	 * byte[6]
	 * 数据采集时间
	 */
	private long timestamp;

	/**
	 * 数据体
	 */
	private List<IMessageBody02> body02List = new ArrayList<>();

	/**
	 * 缓存标志
	 */
	private boolean cache;
	
	@Override
	public void read(byte[] bytes) {
		// TODO Auto-generated method stub
		int idx = 0;
		timestamp = ConvertUtil.bytes2timestamp(bytes, idx); idx += 6;
		while (idx < bytes.length - 1) {
			IMessageBody02 body02 = Message02Factory.create(bytes[idx] & 0xff, bytes, idx);
			body02List.add(body02);
			idx += body02.getByteLen();
		}
	}
	
	@Override
	public byte[] write() {
		// TODO Auto-generated method stub
		return ConvertUtil.timestamp2bytes(timestamp);
	}

	public long getTimestamp() {
		return timestamp;
	}

	@Override
	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}

	public List<IMessageBody02> getBody02List() {
		return body02List;
	}

	public void setBody02List(List<IMessageBody02> body02List) {
		this.body02List = body02List;
	}

	public boolean isCache() {
		return cache;
	}

	public void setCache(boolean cache) {
		this.cache = cache;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(" 数据采集时间:").append(new DateTime(timestamp).toString("yyyy-MM-dd HH:mm:ss"));
		for (IMessageBody02 body02 : body02List) {
			sb.append(body02.toString()).append(" \n");
		}

		return sb.toString();
	}
}