package com.xy.event.listener;

import com.xy.entity.DeviceFaultSpi;
import com.xy.entity.DeviceInfo;
import com.xy.event.DeviceEvent;
import com.xy.service.DeviceInfoServiceImpl;
import com.xy.utils.SpiUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 设备故障事件监听
 */
@Slf4j
@Service
@AllArgsConstructor
public class DeviceFaultEventListener {

    private DeviceInfoServiceImpl deviceInfoService;

    /**
     * spi推送
     *
     * @param faultEvent
     */
    @Async
    @EventListener
    public void spi(DeviceEvent.FaultEvent faultEvent) {
        DeviceInfo deviceInfo = deviceInfoService.getById(faultEvent.getValue());
        SpiUtils.sendSpi(deviceInfo.getMercId(), DeviceFaultSpi.spiKey, () -> new DeviceFaultSpi()
                .setDeviceId(deviceInfo.getDeviceId())
                .setFaultId(faultEvent.getFaultId())
                .setDescribes(faultEvent.getDescribes())
                .setEventNum(faultEvent.getEventNum())
                .setEventCode(faultEvent.getEventCode())
                .setState(faultEvent.getState())
        );
    }

}
