package com.lingniu.ingest.protocol.gb32960.codec.parser.vendor.guangdong;

import com.lingniu.ingest.protocol.gb32960.codec.InfoBlockParser;
import com.lingniu.ingest.protocol.gb32960.codec.ValueDecoder;
import com.lingniu.ingest.protocol.gb32960.model.InfoBlock;
import com.lingniu.ingest.protocol.gb32960.model.ProtocolVersion;

import java.nio.ByteBuffer;

/**
 * 广东燃料电池规范 §7.2.3.7 表 18：空调数据。typeCode {@code 0x33}。
 *
 * <p>固定 5 字节：
 * <pre>
 *   status(1)                        0 关闭 / 1 启动
 *   powerKw(2, offset -5, 1 kw, range -5~+30)
 *   compressorInputVoltageV(2, 0.1V, 0~1000V)
 * </pre>
 */
public final class GdFcAirConditionerBlockParser implements InfoBlockParser {

    @Override public ProtocolVersion version() { return ProtocolVersion.V2016; }
    @Override public int typeCode() { return 0x33; }
    @Override public int fixedLength() { return 5; }

    @Override
    public InfoBlock parse(ByteBuffer buf) {
        Integer status = ValueDecoder.u8raw(buf);
        int rawPower = buf.getShort() & 0xFFFF;
        Integer power = (rawPower == 0xFFFE || rawPower == 0xFFFF) ? null : rawPower - 5;
        Double inputV = ValueDecoder.scaledU16(buf, 0.1);
        return new InfoBlock.GuangdongFc.AirConditioner(status, power, inputV);
    }
}
