package com.pwt.netty.common.vehicle1.server;

import com.pwt.netty.common.vehicle1.Vehicle1Message;
import io.netty.buffer.ByteBuf;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.nio.charset.StandardCharsets;

/**
 * 换电完成
 *
 * 说明：换电完成后，站控向换电控制器发送换电完成指令，指导换电控制器应答，15S
 * 未收到应答则作为异常结束
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
public class BatterySwapDoneRequest extends Vehicle1Message {

    private String vin;

    public BatterySwapDoneRequest() {
        super((byte)0x13);
    }

    @Override
    public void encode(ByteBuf byteBuf) throws Exception {
        // 命令 1byte
        byteBuf.writeByte(type);

        // vin 17 byte
        if (this.vin == null || this.vin.length() != VIN_LENGTH) {
            // 如果vin不对，发送17个0
            byteBuf.writeCharSequence("00000000000000000", StandardCharsets.US_ASCII);
        } else {
            byteBuf.writeCharSequence(this.getVin(), StandardCharsets.US_ASCII);
        }
    }

    @Override
    public void decode(ByteBuf byteBuf) throws Exception {
        this.vin = byteBuf.readCharSequence(VIN_LENGTH, StandardCharsets.US_ASCII).toString();
    }

    @Override
    public int getProtocolLength() {
        return 0x12; //18
    }
}
