package cn.iododer.yudao.module.mqtt.api.common;

import cn.iocoder.yudao.module.mqtt.api.common.CommonApi;
import cn.iododer.yudao.module.mqtt.config.MqttFactory;
import cn.iododer.yudao.module.mqtt.util.MqttUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
@Slf4j
public class CommonApiImpl implements CommonApi {

    @Autowired
    private MqttUtils mqttUtils;

    /*@Override
    public void commonMethod(Object obj, String topic) {
        try {
            String str = JSON.toJSONString(obj);
            mqttUtils.pub(topic, JSON.toJSONString(obj));
            if(topic.equals("AGV_POSE")) {
                return;
            }
            if (str.length() > 510) {
                log.info("MQTT消息发送成功topic :{}, 内容 :{}",topic, str.substring(0, 500));
            }else {
                log.info("MQTT消息发送成功topic :{}, 内容 :{}",topic, str);
            }
        } catch (MqttException e) {
            log.info("MQTT消息发送异常 :{}",e);
        }
    }

    @Override
    public void commonMethodStr(String str, String topic) {
        try {
            mqttUtils.pub(topic, str);
            if(topic.equals("AGV_POSE")) {
                return;
            }
            if (str.length() > 510) {
                log.info("MQTT消息发送成功topic :{}, 内容 :{}",topic, str.substring(0, 500));
            }else {
                log.info("MQTT消息发送成功topic :{}, 内容 :{}",topic, str);
            }
        } catch (MqttException e) {
            log.info("MQTT消息发送异常 :{}",e);
        }
    }*/
}
