package com.zd.laboratory.event.handle;

import com.zd.common.core.constant.CacheConstants;
import com.zd.common.core.constant.CacheDevice;
import com.zd.common.core.enums.HardwareOperate;
import com.zd.common.core.enums.SenseStateEnum;
import com.zd.common.core.utils.SpringUtils;
import com.zd.laboratory.event.OnecStatusEvent;
import com.zd.laboratory.event.RelayHardwareStatusEvent;
import com.zd.laboratory.event.SensorStatusEvent;
import com.zd.laboratory.event.SubFunctionEvent;
import com.zd.laboratory.mqtt.entiy.EquipmentStatus;
import com.zd.laboratory.mqtt.entiy.SensorStatus;
import com.zd.laboratory.service.ILabAbnormalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.function.Consumer;

/**
 * 事件处理配置
 * @Author: zhoupan
 * @Date: 2021/11/07/13:19
 * @Description:
 */
@Component
public class EventHandlerConfig {
    @Autowired
    private ILabAbnormalService labAbnormalService;
    /**
     * 传感器事件处理
     * @return
     */
    @Bean
    public EventHandler  SensorEventHandler()
    {

        return new EventHandler() {
            @Override
            public Consumer<String> eventHandler() {
                return a->{
                    SensorStatus sensorStatus = new SensorStatus();
                    sensorStatus.setNum(a);
                    sensorStatus.setSenseState(SenseStateEnum.OFFLINE);
                    SensorStatusEvent sensorStatusEvent = new SensorStatusEvent(sensorStatus);
                    //发送传感器上下线数据
                    SpringUtils.getApplicationContext().publishEvent(sensorStatusEvent);
                    //发送更新测点数据事件
                    SubFunctionEvent subFunctionEvent = new SubFunctionEvent(sensorStatus.getNum());
                    SpringUtils.getApplicationContext().publishEvent(subFunctionEvent);
                };
            }
            @Override
            public String handleKey() {
                return CacheDevice.SENSOR_KEY.getRedisKey();
            }
        };

    }

    /**
     * 继电器状态
     * @return
     */
    @Bean
    public  EventHandler RelayStatusEventHandler()
    {
        return new EventHandler(){

            @Override
            public Consumer<String> eventHandler() {
                return a->{
                    //创建离线对象
                    EquipmentStatus equipmentStatus = new EquipmentStatus();
                    equipmentStatus.setNum(a);
                    equipmentStatus.setHardwareOperate(HardwareOperate.OFFLINE);
                    //抛出继电器上下线切换事件
                    RelayHardwareStatusEvent relayHardwareStatusEvent = new RelayHardwareStatusEvent(equipmentStatus);
                    SpringUtils.getApplicationContext().publishEvent(relayHardwareStatusEvent);
                };

            }

            @Override
            public String handleKey() {
                return CacheDevice.RELAY_KEY.getRedisKey();
            }
        };

    }

    /**
     * 一体机上线下
     * @return
     */
    @Bean
    public  EventHandler OnecStatusEventHandler()
    {
        return new EventHandler(){
            @Override
            public Consumer<String> eventHandler() {
                return a->{
                    //创建离线对象
                    EquipmentStatus equipmentStatus = new EquipmentStatus();
                    equipmentStatus.setNum(a);
                    equipmentStatus.setHardwareOperate(HardwareOperate.OFFLINE);
                    //抛出一体机上线下事件
                    OnecStatusEvent onecStatusEvent = new OnecStatusEvent(equipmentStatus);
                    SpringUtils.getApplicationContext().publishEvent(onecStatusEvent);
                };
            }
            @Override
            public String handleKey() {
                return CacheConstants.HEARTBEAT;
            }
        };
    }

    /**
     * 设备异常
     * @return
     */
    @Bean
    public  EventHandler relayLifeEventHandler()
    {
        return new EventHandler(){
            @Override
            public Consumer<String> eventHandler() {
                return a->{
                    labAbnormalService.insertRelayAbnormal(a);
                };
            }
            @Override
            public String handleKey() {
                return CacheDevice.RELAY_LIFE.getRedisKey();
            }
        };
    }
    /**
     * 传感器异常
     * @return
     */
    @Bean
    public  EventHandler sensorLifeEventHandler()
    {
        return new EventHandler(){
            @Override
            public Consumer<String> eventHandler() {
                return a->{
                    labAbnormalService.insertSensorAbnormal(a);
                };
            }
            @Override
            public String handleKey() {
                return CacheDevice.SENSOR_LIFE.getRedisKey();
            }
        };
    }
}
