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;
    String auxInfo;

    @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) auxInfo = " type:" + verType;

        if(src.readableBytes()>=4) {
            statusMsg.setTemperature((float) (src.readShort() / 10));
            statusMsg.setHumidity((float) (src.readShort() / 10));
        }
        // imei
        if(src.readableBytes()>=15){
            imei = src.readCharSequence(15, StandardCharsets.UTF_8).toString();
        }
        if(src.readableBytes()>=3){
            auxInfo = auxInfo +
                    " blVer:"+src.readUnsignedByte() +
                    " reboot:"+src.readUnsignedShort();
        }
        if(src.readableBytes()>=8){
            long lastActiveDate = src.readUnsignedInt();
            long lastActiveTime = src.readUnsignedInt();
            short year = (short) ((lastActiveDate>>16)&0xFF);
            short month = (short) ((lastActiveDate>>8)&0xFF);
            short day = (short) (lastActiveDate&0xFF);
            byte hour = (byte) (lastActiveTime/3600);
            byte munite = (byte) (lastActiveTime/60 - hour*60);
            auxInfo = auxInfo + " last active time:"+
                    year+"-"+month+"-"+day+" "+hour+":"+munite;
        }
        if(src.readableBytes()>=14){
            int uart1Exception = src.readUnsignedShort();
            int uart2Exception = src.readUnsignedShort();
            int dtuException = src.readUnsignedShort();
            long lastDtuErrDate = src.readUnsignedInt();
            long lastDtuErrTime = src.readUnsignedInt();
            if(uart1Exception>0) {
                auxInfo = auxInfo + " uart1 err: "+uart1Exception;
            }
            if(uart2Exception>0) {
                auxInfo = auxInfo + " uart2 err: "+uart2Exception;
            }
            if(lastDtuErrDate!=0){
                auxInfo = auxInfo + " last dtu err state: "+dtuException;
                short year = (short) ((lastDtuErrDate>>16)&0xFF);
                short month = (short) ((lastDtuErrDate>>8)&0xFF);
                short day = (short) (lastDtuErrDate&0xFF);
                byte hour = (byte) (lastDtuErrTime/3600);
                byte munite = (byte) (lastDtuErrTime/60 - hour*60);
                auxInfo = auxInfo + " time: "+
                        year+"-"+month+"-"+day+" "+hour+":"+munite;
            }
        }
        if(src.readableBytes()>=4){
            statusMsg.setSolarvoltage(src.readUnsignedShort());
            statusMsg.setChargecurrency(src.readUnsignedShort()*10/8); // 1A 0.8V
        }
        if(src.readableBytes()>=2){
            statusMsg.setChargewatt((float) (src.readUnsignedShort()*0.03));// 1V 30wh
        }

        // read 会移动 bytebuf 的指针，所以保存原始码流需要将此指针挑拨回开始处
        src.readerIndex(0);
        this.srcData = new byte[src.readableBytes()];
        src.readBytes(this.srcData);

    }

}
