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

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.device.mqtt.enums.DeviceRedisEnum;
import com.ruoyi.device.mqtt.annotation.ConsumerHandler;
import com.ruoyi.device.mqtt.enums.MsgTypeEnum;
import com.ruoyi.device.mqtt.handler.decoder.AbstractDecoder;
import com.ruoyi.device.mqtt.vo.CommonHeader;
import com.ruoyi.device.mqtt.vo.CommonTopic;
import io.netty.buffer.ByteBuf;
import jakarta.annotation.Resource;

/**
 * 解密板设备状态上行
 */
@ConsumerHandler(msgType = MsgTypeEnum.DECRYPT_DEVICE_UP)
public class DecryptStatusDecoder extends AbstractDecoder<Void>
{
    @Resource
    private RedisCache redisCache;

    @Override
    protected Void decode(CommonTopic commonTopic, CommonHeader header, ByteBuf body)
    {
        body.readBytes(48);
        int freeSpace = body.readUnsignedByte();
        redisCache.setCacheObject(
            String.format(DeviceRedisEnum.DEVICE_FREE_SPACE.getKey(), commonTopic.getDeviceSn()), freeSpace);
        return null;
    }
}
