package com.xy.event.listener;

import com.xy.entity.DeviceBusyStateSpi;
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 DeviceBusyStateEventListener {

    private DeviceInfoServiceImpl deviceInfoService;

    /**
     * spi推送
     *
     * @param busyStateEvent
     */
    @Async
    @EventListener
    public void spi(DeviceEvent.BusyStateEvent busyStateEvent) {
        DeviceInfo deviceInfo = deviceInfoService.getById(busyStateEvent.getValue());
        SpiUtils.sendSpi(deviceInfo.getMercId(), DeviceBusyStateSpi.spiKey, () -> new DeviceBusyStateSpi()
                .setDeviceId(deviceInfo.getDeviceId())
                .setBusyState(busyStateEvent.getBusyState())
        );
    }

}
