package com.ruoyi.device.mqtt.handler.encoder.json.service;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.device.domain.model.TsbUserDeviceBind;
import com.ruoyi.device.mqtt.annotation.JsonCmdDownHandler;
import com.ruoyi.device.mqtt.domain.BaseJsonBody;
import com.ruoyi.device.mqtt.domain.encoder.OpwMainDataDown;
import com.ruoyi.device.mqtt.enums.CmdTypeEnum;
import com.ruoyi.device.mqtt.handler.encoder.json.IJsonCmdDownHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 液位仪主页下行：common:opw:down
 * <p>
 * 收到 WebSocket页面数据并推送到 设备 MQTT 下行应答
 *
 * @author lwm
 */
@JsonCmdDownHandler(cmdType = CmdTypeEnum.COMMON_OPW)
public class OpwMainDataDownService implements IJsonCmdDownHandler
{
    private static final Logger log = LoggerFactory.getLogger(OpwMainDataDownService.class);

    @Override
    public BaseJsonBody handle(TsbUserDeviceBind bind, JSONObject data, CmdTypeEnum cmd)
    {
        OpwMainDataDown opwMainDataDown = data.to(OpwMainDataDown.class);
        if (opwMainDataDown == null)
        {
            log.info("液位仪主页报文体解析失败, deviceSn={}", bind.getDeviceSn());
            return null;
        }
        opwMainDataDown.setDeviceType(bind.getDeviceType());
        opwMainDataDown.setDeviceSn(bind.getDeviceSn());
        opwMainDataDown.setCmdType(cmd.getCmdDownType());
        return opwMainDataDown;
    }
}
