/*
 * 文 件 名:  HeartbeatRespFrame
 * 版    权:  华设设计集团股份有限公司
 * 描    述:  充电桩心跳包应答帧
 * 修 改 人:  lvwenbin
 * 修改时间:  2025/12/22
 */
package com.ruoyi.ems.charging.model.resp;

import com.ruoyi.ems.charging.model.BaseFrame;
import com.ruoyi.ems.charging.protocol.ProtocolConstants;
import com.ruoyi.ems.charging.utils.ByteUtils;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 充电桩心跳包应答帧 (0x04)
 * 用于链路状态判断
 *
 * @author lvwenbin
 * @version [版本号, 2025/12/22]
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class HeartbeatRespFrame extends BaseFrame {
    
    /**
     * 桩编码 (BCD码 7字节)
     */
    private String pileCode;
    
    /**
     * 枪号 (BCD码 1字节)
     */
    private String gunNo;
    
    /**
     * 心跳应答 (BIN码 1字节)
     * 置0
     */
    private byte heartbeatResp = 0x00;
    
    public HeartbeatRespFrame() {
    }
    
    public HeartbeatRespFrame(String pileCode, String gunNo) {
        this.pileCode = pileCode;
        this.gunNo = gunNo;
    }
    
    @Override
    public byte getFrameType() {
        return ProtocolConstants.FRAME_TYPE_HEARTBEAT_RESP;
    }
    
    @Override
    protected void encodeBody(ByteBuf buf) {
        ByteUtils.writeBcd(buf, pileCode, 7);
        ByteUtils.writeBcd(buf, gunNo, 1);
        buf.writeByte(heartbeatResp);
    }
    
    @Override
    protected void decodeBody(ByteBuf buf) {
        this.pileCode = ByteUtils.readBcd(buf, 7);
        this.gunNo = ByteUtils.readBcd(buf, 1);
        this.heartbeatResp = buf.readByte();
    }
    
    /**
     * 创建心跳应答
     *
     * @param pileCode   桩编码
     * @param gunNo      枪号
     * @param sequenceNo 序列号
     * @return 应答帧
     */
    public static HeartbeatRespFrame create(String pileCode, String gunNo, int sequenceNo) {
        HeartbeatRespFrame frame = new HeartbeatRespFrame(pileCode, gunNo);
        frame.setSequenceNo(sequenceNo);
        return frame;
    }
    
    @Override
    public String toString() {
        return String.format("HeartbeatRespFrame[pileCode=%s, gunNo=%s]", pileCode, gunNo);
    }
}
