package com.wsm.admin.device.init;

import com.wsm.admin.device.mqtt.ClientMQTT;
import com.wsm.admin.device.tcp.CoverTcpServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

public class StartupUdpEvent implements ApplicationListener<ContextRefreshedEvent> {

    private static final Logger log = LoggerFactory.getLogger(StartupUdpEvent.class);

    private static ApplicationContext context;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        try {
            context = contextRefreshedEvent.getApplicationContext();

//            log.warn("启动UDP线程，接收UDP消息，端口：2000");
//            //接收UDP消息并保存至redis中
//            UdpServer udpServer = (UdpServer)StartupUdpEvent.getBean(UdpServer.class);
//            udpServer.run(2000);
//
//            log.warn("启动TCP线程，接收TCP消息，端口：2020");
//            TcpServer tcpServer = (TcpServer)StartupUdpEvent.getBean(TcpServer.class);
//            tcpServer.startSocketServer(2020);

            CoverTcpServer tcpServer = (CoverTcpServer)StartupUdpEvent.getBean(CoverTcpServer.class);
            tcpServer.run(2000);
            log.warn("启动井盖TCP线程，接收TCP消息，端口：2000");


//            ClientMQTT clientMqtt = (ClientMQTT)StartupUdpEvent.getBean(ClientMQTT.class);
//            clientMqtt.start();
            ClientMQTT clientMQTT = new ClientMQTT();
            clientMQTT.start();
            log.warn("启动烟雾传感器MQTT");


        } catch (Exception e) {
            log.error("Exception", e);
        }
    }

    public static Object getBean(Class beanName) {
        return context != null ? context.getBean(beanName) : null;
    }
}
