package com.imdroid.ntripproxy.message;

import io.netty.buffer.ByteBuf;
import lombok.Data;

/**
 * @author Layton
 * @date 2023/2/2 20:32
 */
@Data
public abstract class NtripMessage {
    // D3XX(2B)	Seq/len(2B)	ID	PPS(2B)	PITCH	ROLL	YAW	 Shock
    final int DEVICE_ID_OFFSET = 4;
    final int GGA_OFFSET = 26;
    final int NON_NTRIP_HEAD_LEN = 18;

    protected int header;
    protected String id;
    protected int len;
    protected int seq;

    boolean fromDevice = false;

    ByteBuf srcBuf;
    ByteBuf transBuf;

    public void decode(ByteBuf src) {
        if (shouldDecodeHeader()) {
            // read操作会移动ByteBuf内部指针，除D331外，其他都用read来读
            //int packetLen = src.readableBytes();
            int pos = 0;
            this.header = src.getUnsignedShort(pos);pos+=2;
            this.len = src.getUnsignedShort(pos);pos+=2;
            this.seq = this.len >> 11;
            this.len = this.len & 0x7FF;
            this.id = String.valueOf(src.getUnsignedInt(pos));
            srcBuf = src;
        }
        decodeBody(src);
    }

    public abstract void decodeBody(ByteBuf src);

    public boolean shouldDecodeHeader() {
        return true;
    }
}
