package com.fuyuanshen.system.mqtt.receiver;

import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author: HarryLin
 * @Date: 2025/3/20  15:24
 * @Company: 北京红山信息科技研究院有限公司
 * @Email: linyun@***.com.cn
 **/
@Service
@Slf4j
public class ReceiverMessageHandler implements MessageHandler {
    @Override
    public void handleMessage(Message<?> message) throws MessagingException{
        Object payload = message.getPayload();
        MessageHeaders headers = message.getHeaders();
        String receivedTopic = Objects.requireNonNull(headers.get("mqtt_receivedTopic")).toString();
        String receivedQos = Objects.requireNonNull(headers.get("mqtt_receivedQos")).toString();
        String timestamp = Objects.requireNonNull(headers.get("timestamp")).toString();
        log.info("MQTT payload= {} \n receivedTopic = {} \n receivedQos = {} \n timestamp = {}"
                ,payload,receivedTopic,receivedQos,timestamp);
    }
}