package org.dromara.app.handler;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.app.mqtt.MqttTopicHandler;
import org.dromara.common.json.utils.JsonUtils;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.regex.Pattern;

/**
 * 设备异常告警处理器 — 匹配 /{deviceNo}/publish/error
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ErromesHandler implements MqttTopicHandler {

    private static final Pattern PATTERN = Pattern.compile("^/([^/]+)/publish/error$");

    @Override
    public Pattern topicPattern() {
        return PATTERN;
    }

    @Override
    public void handle(String deviceNo, String payload) {
        try {
            Map<String, Object> dto = JsonUtils.parseObject(payload, Map.class);
            log.info("[MQTT] 设备异常告警 deviceNo={} payload={}", deviceNo, dto);
        } catch (Exception e) {
            log.error("[MQTT] 设备异常处理失败 deviceNo={}", deviceNo, e);
        }
    }
}
