package com.lingniu.ingest.inbound.mqtt.handler;

import com.lingniu.ingest.api.ProtocolId;
import com.lingniu.ingest.api.annotation.EventEmit;
import com.lingniu.ingest.api.annotation.MessageMapping;
import com.lingniu.ingest.api.annotation.ProtocolHandler;
import com.lingniu.ingest.api.annotation.RateLimited;
import com.lingniu.ingest.api.event.VehicleEvent;
import com.lingniu.ingest.inbound.mqtt.model.MqttPayload;
import com.lingniu.ingest.inbound.mqtt.profile.MqttProfileRegistry;

import java.util.List;

/**
 * MQTT 接入 Handler。
 *
 * <p>MQTT 没有"命令字"概念，使用默认的 wildcard 匹配（{@code command} 为空）。
 * 同一 Dispatcher 仍能正确路由，因为协议 ID 做了隔离。
 */
@ProtocolHandler(protocol = ProtocolId.MQTT_YUTONG)
public class MqttRealtimeHandler {

    private final MqttProfileRegistry profileRegistry;

    public MqttRealtimeHandler(MqttProfileRegistry profileRegistry) {
        this.profileRegistry = profileRegistry;
    }

    @MessageMapping(desc = "MQTT profile 实时数据")
    @RateLimited(perVin = 20)
    @EventEmit({VehicleEvent.Realtime.class, VehicleEvent.Location.class, VehicleEvent.Passthrough.class})
    public List<VehicleEvent> onMqtt(MqttPayload payload) {
        return profileRegistry.toEvents(payload);
    }
}
