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.OpwPassthroughDataDown;
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:passthrough:down
 * <p>
 * 收到 WebSocket页面数据并推送到 设备 MQTT 下行应答
 *
 * @author lwm
 */
@JsonCmdDownHandler(cmdType = CmdTypeEnum.COMMON_OPW_PASSTHROUGH)
public class OpwPassthroughDataDownService implements IJsonCmdDownHandler
{
    private static final Logger log = LoggerFactory.getLogger(OpwPassthroughDataDownService.class);

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