package com.rehome.mqttclienttemperature;


import com.rehome.mqttclienttemperature.service.TemperatureEspService;
import com.rehome.mqttclienttemperature.service.TemperatureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
//@EnableScheduling
//@EnableAsync
@SpringBootApplication
public class AdminClientTemperatureApplication implements CommandLineRunner, ApplicationContextAware {

    /**
     * 获取Spring框架的上下文
     */
    private ApplicationContext applicationContext;
    /**
     后台接口自动导入
     */
    @Resource
    private TemperatureService temperatureService;

    /**
     后台接口自动导入
     */
    @Resource
    private TemperatureEspService temperatureEspService;


    public static void main(String[] args) {
        SpringApplication.run(AdminClientTemperatureApplication.class, args);
    }

    /**
     * 调用 applicationContext(不能在main中使用,main是static的,不能调用)
     * @param args
     */
    @Override
    public void run(String... args) throws Exception {
        //在这里可以调用applicationContext了
        Map<String, Object> controllers = applicationContext.getBeansWithAnnotation(RequestMapping.class);
        for (Map.Entry<String, Object> entry : controllers.entrySet()) {
            log.info("------------------------");
            log.info(entry.getKey());//demo1Controller
        }
        try {
            if (temperatureService != null) {
                log.info("------------------------");
                log.info("TemperatureController is not empty");
                MqttRSAClient client = new MqttRSAClient();
                //client.start(temperatureService);
            }else {
                log.info("temperatureService is empty");
            }

//            if (temperatureService != null) {
//                log.info("------------------------");
//                log.info("TemperatureController is not empty");
//                MqttHuaWeiYunClient client = new MqttHuaWeiYunClient();
//                client.start(temperatureService);
//            }else {
//                log.info("temperatureService is empty");
//            }

            if (temperatureEspService != null) {
                log.info("------------------------");
                log.info("temperatureEspService is not empty");
                MqttDianDengTechClient client = new MqttDianDengTechClient();
                //client.start(temperatureEspService);
            }else {
                log.info("temperatureEspService is empty");
            }

            String DeviceAlermInfoPwd = "";
            String strPrivateEncode = RSAAndroid.encryptByPrivateKeyForSpiltStr("AnFang@2025",RSAAndroid.privateRsaKeyLocal);
            System.out.println("---------strPrivateEncode----------");
            System.out.println(strPrivateEncode);
            String strPublicDecode = RSAAndroid.decryptByPublicKeyForSpiltStr(
                    strPrivateEncode,
                    RSAAndroid.publicRsaKeyLocal
            );

            System.out.println("---------strPublicDecode----------");
            System.out.println(strPublicDecode);



        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}