package com.fuyuanshen.global.mqtt.base;

import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * MQTT消息引擎
 */
@Component
public class MqttRuleEngine {
    

    private final LinkedHashMap<String, MqttMessageRule> rulesMap = new LinkedHashMap<>();
    public MqttRuleEngine(List<MqttMessageRule> rules) {
        // 按优先级排序
        rules.sort(Comparator.comparing(MqttMessageRule::getPriority));
        rules.forEach(rule -> rulesMap.put(rule.getCommandType(), rule)
        );
    }
    
    /**
     * 执行匹配
     * @param context 处理上下文
     * @return
     */
    public boolean executeRule(MqttRuleContext context) {
        int commandType = context.getCommandType();
        MqttMessageRule mqttMessageRule = rulesMap.get("Light_"+commandType);
        if (mqttMessageRule != null) {
            mqttMessageRule.execute(context);
            return true;
        }
        
        return false;
    }
}
