package com.pwt.netty.common.chargerold.client;

import com.pwt.netty.common.chargerold.RequestData;
import io.netty.buffer.ByteBuf;
import lombok.Data;

import java.nio.charset.StandardCharsets;

/**
 * @author linyun
 * @version V1.0
 * @Title: ChargerChargeStatusData
 * @Description: 充电机充电状态数据包
 * @date 2022/11/13
 **/
@Data
public class ChargerChargeStatusData extends RequestData {

    /**电池编码长度*/
    private final int BATTERY_CODE_LENGTH = 24;

    /** 充电状态 0：待机；1：充电；2：停止；3：故障 **/
    private byte chargingStatus;

    /** 输出电压 **/
    private int outputVoltage;

    /** 输出电流 **/
    private int outputCurrent;

    /** 需求电压 **/
    private int demandVoltage;

    /** 需求电流 **/
    private int demandCurrent;

    /** 已充电时间(分钟) **/
    private short chargedTime;

    /** 当前soc **/
    private byte SOC;

    /** 整车动力蓄电池当前电池电压 **/
    private float batteryVoltage;

    /** 电池温度 **/
    private float batteryTemperature;

    /** 剩余充电时间(分钟) **/
    private short remainingChargingTime;

    /** 接插件状态 1：接入；2：未接入 **/
    private byte connectorStatus;

    /** 电池组编码 **/
    private String batteryNumber;

    /** 充电次数 **/
    private short chargingQuantity;

    /** 电池包累计行驶总里程 （若无该值填 0） **/
    private int cumulativeMileage;

    /** 电池包累计电量 （10kw/h,若无该值填 0）**/
    private short accumulatedElectricity;

    /** 充电模式 1：室内充电 2：应急充电*/
    private byte chargeType;

    /***
     * 子类编码
     * @param byteBuf
     */
    @Override
    protected void encodeInternal(ByteBuf byteBuf)
    {
        byteBuf.writeByte(getChargingStatus());
        byteBuf.writeInt(getOutputVoltage());
        byteBuf.writeInt(getOutputCurrent());
        byteBuf.writeInt(getDemandVoltage());
        byteBuf.writeInt(getDemandCurrent());
        byteBuf.writeShort(getChargedTime());
        byteBuf.writeByte(getSOC());
        byteBuf.writeFloat(getBatteryVoltage());
        byteBuf.writeFloat(getBatteryTemperature());
        byteBuf.writeShort(getRemainingChargingTime());
        byteBuf.writeByte(getConnectorStatus());
        byteBuf.writeBytes(getBatteryNumber().getBytes(StandardCharsets.US_ASCII));
        byteBuf.writeShort(getChargingQuantity());
        byteBuf.writeInt(getCumulativeMileage());
        byteBuf.writeShort(getAccumulatedElectricity());
        byteBuf.writeByte(getChargeType());
    }

    /**
     * 子类解码
     *
     * @param byteBuf
     */
    @Override
    protected void decodeInternal(ByteBuf byteBuf)
    {
        setChargingStatus(byteBuf.readByte());
        setOutputVoltage(byteBuf.readInt());
        setOutputCurrent(byteBuf.readInt());
        setDemandVoltage(byteBuf.readInt());
        setDemandCurrent(byteBuf.readInt());
        setChargedTime(byteBuf.readShort());
        setSOC(byteBuf.readByte());
        setBatteryVoltage(byteBuf.readFloat());
        setBatteryTemperature(byteBuf.readFloat());
        setRemainingChargingTime(byteBuf.readShort());
        setConnectorStatus(byteBuf.readByte());
        setBatteryNumber(byteBuf.readCharSequence(BATTERY_CODE_LENGTH, StandardCharsets.US_ASCII).toString());
        setChargingQuantity(byteBuf.readShort());
        setCumulativeMileage(byteBuf.readInt());
        setAccumulatedElectricity(byteBuf.readShort());
        setChargeType(byteBuf.readByte());
    }
}
