package com.its.pis.xnettcp.vds.protocol;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;

@Slf4j
@Getter
public class VdsReqFrameHead {

    public static int SIZE = 7;
    public static int OPCODE_POS = 6;

    private final byte dle = VdsProtocol.vds_DLE;
    private final byte stx = VdsProtocol.vds_STX;

    private short groupNo;
    private short controllerNo;
    private byte  opCode;

    public VdsReqFrameHead() {
        this.groupNo = 0;
        this.controllerNo = 0;
        this.opCode = 0;
    }

    public VdsReqFrameHead(short groupNo, short controllerNo, byte opCode) {
        this.groupNo = groupNo;
        this.controllerNo = controllerNo;
        this.opCode = opCode;
    }

    public VdsReqFrameHead(byte[] headBytes) {
        if (headBytes != null && headBytes.length >= SIZE) {
            ByteBuffer byteBuffer = ByteBuffer.wrap(headBytes);
            byteBuffer.order(VdsProtocol.byteOrder);
            byte dle = byteBuffer.get();
            byte stx = byteBuffer.get();
            this.groupNo = byteBuffer.getShort();
            this.controllerNo = byteBuffer.getShort();
            this.opCode = byteBuffer.get();
        }
        else {
            log.error("VdsReqFrameHead: create failed, headBytes error");
        }
    }

}
