package com.bz.rmcp.alarm.mq;

import com.alibaba.fastjson.JSON;
import com.bz.rmcp.alarm.service.impl.DeviceAlarmServiceImpl;
import com.rabbitmq.client.Channel;
import com.zcxk.rmcp.api.dto.product.MeasureDataDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author linqingwei 数据接收
 * @date 2021-08-03 17:37
 */
@Slf4j
@Component
public class DeviceDataConsumer {
    public static final String DEVICE_DATA_QUEUE_NAME = "rmcp.dap.device.data.message.queue";

    @Autowired
    private DeviceAlarmServiceImpl deviceAlarmService;

    @RabbitListener(queues = DEVICE_DATA_QUEUE_NAME)
    public void receive(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody(),StandardCharsets.UTF_8);
        log.info("DeviceDataConsumer receive：{}", msg);
        MeasureDataDto data = JSON.parseObject(msg, MeasureDataDto.class);
        deviceAlarmService.handle(data);

        //channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
