package com.yeejoin.amos.patrol.mqtt;

import com.alibaba.fastjson.JSON;
import com.yeejoin.amos.patrol.business.service.intfc.IPointService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.typroject.tyboot.component.emq.EmqxListener;
import java.util.Map;

/**
 * @author DELL
 */
@Component
@Slf4j
public class PatrolMqttListener extends EmqxListener {

    @Value("${patrol.point.classify.topic}")
    private String pointClassifyTopic;
    @Autowired
    private IPointService iPointService;

    @Override
    public void processMessage(String topic, MqttMessage message) {
        if(log.isInfoEnabled()){
            log.info("收到消息主题：{},消息内容：{}",topic, message.toString());
        }
        try {
            Map<String, Object> msg = JSON.parseObject(message.toString());
            if (pointClassifyTopic.equals(topic)) {
                iPointService.syncPointClassify(msg);
            }
            log.info("巡检对象同步完成");
        } catch (Exception e) {
            log.info("巡检对象同步失败:{}", e.getMessage());
        }
    }

}
