package vip.xiaonuo.coldchain.core.event;

/**
 * @author jackzhou
 * @version 1.0
 * @project jfcloud-coldchain
 * @description
 * @date 2024/11/12 15:31:43
 */

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import vip.xiaonuo.coldchain.core.alarm.service.messagepush.LocalSensorAlarmMessagePushService;
import vip.xiaonuo.coldchain.core.alarm.service.messagepush.RedisSensorAlarmMessagePushService;
import vip.xiaonuo.coldchain.core.renke.config.JfcloudColdChainServerProperties;

import static vip.xiaonuo.coldchain.core.config.JfcloudColdChainConstants.DEFAULT_CACHE_TYPE;

@Slf4j
@RequiredArgsConstructor
@Component
public class SensorAlarmEventListener {
    private final JfcloudColdChainServerProperties jfcloudColdChainServerProperties;
    private final LocalSensorAlarmMessagePushService localSensorAlarmMessagePushService;
    private final RedisSensorAlarmMessagePushService redisSensorAlarmMessagePushService;

    @Async("coldChainAsyncTask")
    @EventListener
    public void handleSensorAlarmEvent(SensorAlarmEvent event) {
        if (DEFAULT_CACHE_TYPE.equalsIgnoreCase(jfcloudColdChainServerProperties.getCacheType())) {
            redisSensorAlarmMessagePushService.pushAlarmMessage(event.getSensorAlarm());
        } else {
            localSensorAlarmMessagePushService.pushAlarmMessage(event.getSensorAlarm());
        }
    }
}
