package com.zd.forward.listener;

import com.zd.common.core.exception.ServiceException;
import com.zd.forward.properties.FireProperties;
import com.zd.forward.serivce.FireImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 监听Spring容器启动完成，完成后启动Netty服务器
 *
 * @author dgs
 **/
@Component
@Slf4j
public class StartListener implements CommandLineRunner {
    @Resource
    private FireImageService fireImageService;
    @Resource
    private FireProperties fireProperties;

    @Resource
    private ScheduledExecutorService scheduledExecutorService;

    @Override
    public void run(String... args) {
        scheduledExecutorService.scheduleWithFixedDelay(() -> {
            try {
                String streamUrl = fireProperties.getStreamUrl();
                if (streamUrl == null) {
                    log.error("=========调用产生异常：未配置流媒体地址============");
                    return;
                }
                fireImageService.catchImage();
            } catch (ServiceException | IOException e) {
                //异常回调,防止系统因异常问题被杀死
                log.error("=========调用产生异常：{}============", e.getMessage());
                run();
            }
        },0,fireProperties.getWaitTime(), TimeUnit.SECONDS);
    }

}
