package com.ruoyi.device.mqtt.handler.decoder.upgrade;

import com.ruoyi.device.mqtt.annotation.ConsumerHandler;
import com.ruoyi.device.mqtt.domain.decoder.upgrade.QueryApkListMessage;
import com.ruoyi.device.mqtt.enums.MsgTypeEnum;
import com.ruoyi.device.mqtt.handler.decoder.AbstractDecoder;
import com.ruoyi.device.mqtt.util.TargetTypeConvertUtil;
import com.ruoyi.device.mqtt.vo.CommonHeader;
import com.ruoyi.device.mqtt.vo.CommonTopic;
import com.ruoyi.device.service.IDeviceUpgradePackageService;
import io.netty.buffer.ByteBuf;
import jakarta.annotation.Resource;

/**
 * 固件版本列表查询上行解码 GET_APK_UP
 */
@ConsumerHandler(msgType = MsgTypeEnum.GET_APK_UP)
public class QueryApkListDecoder extends AbstractDecoder<Void>
{
    @Resource
    private IDeviceUpgradePackageService deviceUpgradePackageService;

    @Override
    protected Void decode(CommonTopic topic, CommonHeader header, ByteBuf body)
    {
        String deviceType = TargetTypeConvertUtil.convert(body.readUnsignedShortLE());
        Long deviceSn = body.readUnsignedIntLE();
        String targetDeviceType = TargetTypeConvertUtil.convert(body.readUnsignedShortLE());
        int firmwareType = body.readByte();

        QueryApkListMessage queryApkListMessage = new QueryApkListMessage();
        queryApkListMessage.setDeviceSn(deviceSn);
        queryApkListMessage.setDeviceType(deviceType);
        queryApkListMessage.setTargetDeviceType(targetDeviceType);
        queryApkListMessage.setFirmwareType(firmwareType);

        deviceUpgradePackageService.handleQueryApkList(queryApkListMessage);
        return null;
    }
}
