package com.imdroid.sideslope.message;

import com.imdroid.sideslope.bd.Gga;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Layton
 * @date  2023/2/2 20:47
 */
@Data
@EqualsAndHashCode(callSuper=true)
public class D331RtcmMessage extends BaseMessage {
    Gga gga;
    int packetNum;

    @Override
    public void decodeBody(ByteBuf src) {
        // get操作不会移动指针，这样可以确保整个全转发出去
        this.packetNum = 1;
        this.len = src.readableBytes();//this.len & 0x7FF;//有可能两个d331粘包
        this.header = src.getUnsignedShort(0); // flag
        int onePacketLen = src.getUnsignedShort(2); // length:11 bits
        this.seq = onePacketLen >> 11;
        onePacketLen = onePacketLen&0x7FF;
        this.id = String.valueOf(src.getUnsignedInt(4)); //id
        int pos = onePacketLen + 4;
        while(pos < this.len){
            if(src.getUnsignedShort(pos) == 0xd331){
                onePacketLen = src.getUnsignedShort(pos+2)&0x7FF;
                pos = pos+4+onePacketLen;
                packetNum ++;
            }
            else pos++;
        }
        /*if (src.readableBytes() - 4 < this.len) {
            String msg = String.format("id[%s],长度字段值[%s]效益包的消息体长度[%s]", id, this.len, src.readableBytes() - 4);
            WrongMessageRecorder.INSTANCE.append("receive wrong message," + msg);
        }*/
        gga = Gga.getFrom(src);
        this.srcData = new byte[src.readableBytes()];
        src.readBytes(this.srcData);
    }

    @Override
    public boolean shouldDecodeHeader() {
        return false;
    }
}
