package com.yeejoin.amos;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.yeejoin.amos.boot.biz.common.utils.oConvertUtils;

import com.yeejoin.amos.boot.module.jxiop.biz.listener.SyncDasSuccessMqttListener;
import com.yeejoin.amos.boot.module.jxiop.biz.listener.SyncESDataToTdengineMqttListener;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.typroject.tyboot.component.emq.EmqKeeper;
import org.typroject.tyboot.core.restful.exception.GlobalExceptionHandler;

import java.net.InetAddress;

/**
 * <pre>
 * 智信能源科技服务启动类
 * </pre>
 *
 * @author DELL
 */
@EnableTransactionManagement
@EnableConfigurationProperties
@ServletComponentScan
@EnableDiscoveryClient
@EnableFeignClients
@EnableAsync
@EnableEurekaClient
@EnableScheduling
@MapperScan({ "org.typroject.tyboot.demo.face.orm.dao*", "org.typroject.tyboot.face.*.orm.dao*",
		"org.typroject.tyboot.core.auth.face.orm.dao*", "org.typroject.tyboot.component.*.face.orm.dao*",
		"com.yeejoin.amos.boot.module.**.api.mapper", "com.yeejoin.amos.boot.biz.common.dao.mapper",
		"com.yeejoin.amos.boot.module.common.biz.*", "com.yeejoin.amos.boot.module.jxiop.api.mapper",
		"com.yeejoin.amos.boot.module.jxiop.biz.tdmapper", "com.yeejoin.amos.boot.module.jxiop.biz.mapper5" })
@ComponentScan(basePackages = { "org.typroject", "com.yeejoin.amos" },excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
		GlobalExceptionHandler.class }))
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class })
//@SpringBootApplication
public class AmosJxiopAnalyseApplication {
	@Autowired
	private EmqKeeper emqKeeper;
	// 本地是否执行健康指数算法开关
	@Value("${openHealth:false}")
	Boolean openHealth;
	@Autowired
	private SyncESDataToTdengineMqttListener syncESDataToTdengineMqttListener;
	@Autowired
	private SyncDasSuccessMqttListener syncDasSuccessMqttListener;

	private static final Logger logger = LoggerFactory.getLogger(AmosJxiopAnalyseApplication.class);

	public static void main(String[] args) throws Exception {
		ConfigurableApplicationContext context = SpringApplication.run(AmosJxiopAnalyseApplication.class, args);
		Environment env = context.getEnvironment();
		String ip = InetAddress.getLocalHost().getHostAddress();
		String port = env.getProperty("server.port");
		String path = oConvertUtils.getString(env.getProperty("server.servlet.context-path"));

		logger.info("\n----------------------------------------------------------\n\t"
				+ "Application Amos-Biz-Boot-Jxiop-Montior is running! Access URLs:\n\t" + "Swagger文档: \thttp://" + ip
				+ ":" + port + path + "/doc.html\n" + "----------------------------------------------------------");
	}

	@Bean
	public void initMqtt() throws Exception {
		if (!openHealth) {
			return;
		}
		// 订阅固化周期性数据成功的消息
		emqKeeper.subscript("sync_esdata_to_tdengine_notice1", 1, syncESDataToTdengineMqttListener);
		// 订阅业务固化同步数据成功消息
		//emqKeeper.subscript("sync_iotdata_to_tdengine_notice", 1, syncDasSuccessMqttListener);
	}
}
