package com.yfyy.yingtan.subdata;

import com.alibaba.fastjson.JSONObject;
import com.yfyy.yingtan.bean.response.DevSubBean;
import com.yfyy.yingtan.util.DataUtil;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// 消息回调
public class MqPushCallback implements MqttCallback {
    private static final Logger logger = LoggerFactory.getLogger(MqPushCallback.class);
    @Override
    public void connectionLost(Throwable throwable) {
        logger.info("MqPushCallback-连接丢失.............");
    }

    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
        logger.info("MqPushCallback-接收消息主题 : " + topic);
        logger.info("MqPushCallback-接收消息Qos : " + mqttMessage.getQos());
        String msgData = new String(mqttMessage.getPayload());
        logger.info("MqPushCallback-接收消息内容 : " + msgData);

        try {
            DevSubBean devSubBean = JSONObject.parseObject(msgData,DevSubBean.class);
            DataUtil.handleAndReport(devSubBean);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        logger.info("MqPushCallback-deliveryComplete.............");
    }


}
