package com.persagy.cameractl.init;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.persagy.cameractl.conf.CameraApiConfig;
import com.persagy.cameractl.conf.ServerConfig;
import com.persagy.cameractl.utils.TimerInterval;
import com.persagy.socket.IotSocketClient;

import cn.hutool.core.util.StrUtil;

import com.persagy.cameractl.conf.AllStaticConfig;

@Component
@Order(1)
public class SystemInit implements ApplicationRunner {
	Logger log = LoggerFactory.getLogger(SystemInit.class);

	@Autowired
	private ServerConfig serverConfig;
	
	@Autowired
	private CameraApiConfig cameraApiConfig;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		AllStaticConfig.groupCode=cameraApiConfig.getGroupCode();
		AllStaticConfig.projectId=cameraApiConfig.getProjectId();
		AllStaticConfig.toupIp=cameraApiConfig.getToupIp();
		AllStaticConfig.toupPort=StrUtil.isBlank(cameraApiConfig.getToupPort()) ? -1 : Integer.valueOf(cameraApiConfig.getToupPort());
		if (StrUtil.isNotBlank(cameraApiConfig.getToupIp())) {
			try {
				IotSocketClient.connect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		AllStaticConfig.dahuaMesHost=cameraApiConfig.getDahuaMesHost();
		AllStaticConfig.dahuaApiIp=cameraApiConfig.getDahuaApiIp();
		AllStaticConfig.dahuaApiPort=cameraApiConfig.getDahuaApiPort();
		AllStaticConfig.bajiuSdkPort=cameraApiConfig.getBajiuSdkPort();
		AllStaticConfig.dahuaApiKey=cameraApiConfig.getDahuaApiKey();
		AllStaticConfig.dahuaApiSecret=cameraApiConfig.getDahuaApiSecret();
		AllStaticConfig.dahuaTvWallId=cameraApiConfig.getDahuaTvWallId();
		AllStaticConfig.dahuaTvScreenId=cameraApiConfig.getDahuaTvScreenId();
		
		log.info("SystemInit start");
		System.out.println("端口号："+serverConfig.getUrl());
		log.info("SystemInit ...");
		log.info("SystemInit end");

		//启动定时清除文件的定时器
		TimerInterval.startClearFileTimer();
	}

}
