package com.usky.rule.mq;

import com.alibaba.fastjson.JSON;
import com.usky.rule.mq.vo.DeviceReportPayload;
import com.usky.rule.subscribe.TriggerDeviceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 订阅 RocketMQ 设备上报 topic，触发数据规则（对标 MQTTUtil 订阅后解析并投递规则引擎）。
 * 配置：rocketmq.consumer.group / topic 与 bootstrap.yml 或 Nacos 中一致。
 */
@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "${rocketmq.consumer.group}",
        topic = "${rocketmq.consumer.topic}"
)
public class RuleReportConsumer implements RocketMQListener<String> {

    private final TriggerDeviceUtil triggerDeviceUtil;

    @Value("${spring.application.name:service-rule}")
    private String appName;

    public RuleReportConsumer(TriggerDeviceUtil triggerDeviceUtil) {
        this.triggerDeviceUtil = triggerDeviceUtil;
    }

    @Override
    public void onMessage(String message) {
        log.debug("[{}] RuleReportConsumer received: {}", appName, message);
        triggerDeviceUtil.processMessage(message);
    }
}
