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

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