Message81Cmd.java 3.15 KB
package com.bsth.socket.protocol;

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

import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Hill
 * 参数设置
 */
public class Message81Cmd implements IMessageBody {

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

	/**
	 * 参数总数
	 * n
	 */
	private byte paramNum;

    /**
     * 参数具体数据
	 */
	private Map<Byte, Object> params = new LinkedHashMap<>();

	private static Map<Byte, Byte> sizes;

	static {
		AtomicInteger atomicInteger = new AtomicInteger(1);
		List<Byte> byteList = Arrays.asList((byte) 2, (byte) 2, (byte) 2, (byte) 1, (byte) 0, (byte) 2, (byte) 5, (byte) 5, (byte) 1, (byte) 2, (byte) 2, (byte) 1, (byte) 1, (byte) 0, (byte) 2, (byte) 1);
		sizes = byteList.stream().collect(Collectors.toMap(val -> (byte) atomicInteger.getAndIncrement(), val -> val));
	}

	public static void main(String[] args) {
		System.out.println("aaa");
	}
	
	@Override
	public void read(byte[] bytes) {
		// TODO Auto-generated method stub

	}
	
	@Override
	public byte[] write() {
		// TODO Auto-generated method stub
		int len = 0;
		for (Map.Entry<Byte, Object> entry : params.entrySet()) {
			Byte key = entry.getKey();
			Byte size = sizes.get(entry.getKey());
			if (size == 0)  {
				size = (Byte) params.get((byte) (key - 1));
				if (size == null) {
					throw new RuntimeException("参数设置中参数不符合协议规范");
				}
			}
			len += size;
		}

		ByteBuffer buf = ByteBuffer.allocate(7 + paramNum + len);
		buf.put(ConvertUtil.timestamp2bytes(timestamp));
		buf.put(paramNum);

		for (Map.Entry<Byte, Object> entry : params.entrySet()) {
			generateParam(buf, entry.getKey(), entry.getValue());
		}

		return buf.array();
	}

	private void generateParam(ByteBuffer buf, Byte type, Object value) {
		buf.put(type);
		switch (type) {
			case 0x01:
			case 0x02:
			case 0x03:
			case 0x06:
			case 0x0A:
			case 0x0B:
			case 0x0F:
				buf.putShort((Short) value);
				break;
			case 0x04:
			case 0x09:
			case 0x0C:
			case 0x0D:
			case 0x10:
				buf.put((Byte) value);
				break;
			case 0x05:
			case 0x0E:
				String val = (String) value;
				buf.put(ConvertUtil.string2ascii(val, val.length()));
				break;
			case 0x07:
			case 0x08:
				buf.put(ConvertUtil.string2ascii((String) value, 5));
				break;
			default:
				break;
		}
	}

	public long getTimestamp() {
		return timestamp;
	}

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

	public byte getParamNum() {
		return paramNum;
	}

	public void setParamNum(byte paramNum) {
		this.paramNum = paramNum;
	}

	public Map<Byte, Object> getParams() {
		return params;
	}

	public void setParams(Map<Byte, Object> params) {
		this.params = params;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(" 数据采集时间:").append(new DateTime(timestamp).toString("yyyy-MM-dd HH:mm:ss"))
		.append(" 参数数量:").append(paramNum)
		.append(" 参数命令IDS:").append(params.size());

		return sb.toString();
	}
}