package com.imdroid.sideslope.message;

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

/**
 * @author Layton
 * @date 2023/2/2 20:50
 */
@Data
@EqualsAndHashCode(callSuper=true)
public class D341LocationMessage extends BaseMessage {
    Tilt tilt;
    double[] b562_loc;
    Gga gga;
    @Override
    public void decodeBody(ByteBuf src) {
        // 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));pos+=4; //id
        /*if (packetLen - 4 != this.len) {
            String msg = (String.format("id[%s],长度字段值[%s]与包的消息体长度[%s]不匹配", id, this.len, packetLen - 4));
            WrongMessageRecorder.INSTANCE.append("receive wrong message," + msg);
        }*/
        this.pps = src.getUnsignedShort(pos);pos+=2;

        tilt = new Tilt(src.getFloat(pos), src.getFloat(pos+4),src.getFloat(pos+8),src.getFloat(pos+12));

        // b562
        b562_loc = UBXUtil.getLocation(src);
        gga = Gga.getFrom(src);
    }

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