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 org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.coldchain.modular.monitordevice.service.MonitorDeviceService;
import vip.xiaonuo.coldchain.modular.monitortarget.service.MonitorTargetService;

import java.util.Date;

@Slf4j
@RequiredArgsConstructor
@Component
public class MonitorDeviceHeartBeatEventListener {
    private final MonitorDeviceService monitorDeviceService;
    private final MonitorTargetService monitorTargetService;

    @Async("coldChainAsyncTask")
    @EventListener
    public void handleMonitorDeviceHeartBeatEvent(MonitorDeviceHeartBeatEvent event) {
        Integer deviceId = event.getDeviceId();
        String ip = event.getIp();
        Date timestamp = event.getTimestamps();
        try {
            updateHeartBeatTime(deviceId, timestamp);
        } catch (Exception e) {
        }
    }

    public void updateHeartBeatTime(Integer deviceId, Date timestamp) {
        monitorDeviceService.updateLastHeartbeatTimeByDeviceId(deviceId, timestamp);
        monitorTargetService.updateLastHeartbeatTimeByDeviceId(deviceId, timestamp);
    }

}
