package com.xy.event.listener;

import com.xy.entity.DeviceInfo;
import com.xy.entity.DeviceTempSpi;
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 DeviceTempEventListener {

    private DeviceInfoServiceImpl deviceInfoService;

    /**
     * spi推送
     *
     * @param tempEvent
     */
    @Async
    @EventListener
    public void spi(DeviceEvent.TempEvent tempEvent) {
        DeviceInfo deviceInfo = deviceInfoService.getById(tempEvent.getValue());
        SpiUtils.sendSpi(deviceInfo.getMercId(), DeviceTempSpi.spiKey, () -> new DeviceTempSpi()
                .setDeviceId(deviceInfo.getDeviceId())
                .setTempValue(tempEvent.getTempValue())
        );
    }

}
