package com.imdroid.sideslope.message;

import com.imdroid.secapi.dto.GnssStatusMsg;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.nio.charset.StandardCharsets;
import java.time.LocalTime;

/**
 * 自检消息
 *
 * @author Layton
 * @date 2023/2/2 20:38
 */
@EqualsAndHashCode(callSuper=true)
@Data
public class D3F0SelfCheckMessage extends BaseMessage {

    GnssStatusMsg statusMsg = new GnssStatusMsg();
    String firmwareVersion;
    String imei;

    @Override
    public void decodeBody(ByteBuf src) {
        statusMsg.setDeviceid(getId());
        statusMsg.setCreatetime(createTime);
        statusMsg.setDevicetime(LocalTime.ofSecondOfDay(getPps()));
        statusMsg.setPitch(src.readFloat());
        statusMsg.setRoll(src.readFloat());
        statusMsg.setYaw(src.readFloat());
        // skip shock
        if(src.readableBytes()>=4) {
            src.readFloat();
            statusMsg.setDtustate(src.readUnsignedByte());
            statusMsg.setRssi(src.readUnsignedByte());
            statusMsg.setVoltage(src.readUnsignedShort());
        }
        // skip version
        int ver = src.readUnsignedShort();
        short verType = (short) ((ver >> 12) & 0xF);
        firmwareVersion = ((ver >> 8) & 0xF) + "." + ((ver >> 4) & 0xF) + "." + (ver & 0xF);
        if (verType > 0) firmwareVersion = firmwareVersion + " type:" + verType;

        if(src.readableBytes()>=4) {
            statusMsg.setTemperature((float) (src.readUnsignedShort() / 10));
            statusMsg.setHumidity((float) (src.readUnsignedShort() / 10));
        }
        // imei
        if(src.readableBytes()>=15){
            imei = src.readCharSequence(15, StandardCharsets.UTF_8).toString();
        }
        if(src.readableBytes()>=3){
            firmwareVersion = firmwareVersion +
                    " bVer:"+src.readUnsignedByte() +
                    " reboot:"+src.readUnsignedShort();
        }

    }

}
