package com.steerinfo.ems.websocket.controller;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * @author Shadow
 * @create 2021-10-12 14:00
 * @project xt-ems-api-new
 * @description: 直连交换机队列配置文件
 */
@Configuration
@EnableRabbit
public class DirectRabbitConfig {
    @Value("${spring.rabbitmq.wsExchangeName}")
    private String WSExchangName;
    /*@Autowired
    StringRedisTemplate stringRedisTemplate;*/
    //动态获取节点的IP
    String ipaddr;
   /* {
        try {
            ipaddr = InetAddress.getLocalHost().getHostAddress().replace(".","");
            //redisTemplate.opsForValue().set(ipaddr,ipaddr);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }*/

    /**
     * @Description: 根据IP地址动态的创建队列
     * @Param:
     * @return:
     * @Author:
     * @Date: 2020/5/18
     */
    @Bean
    public Queue createWsDirectQueue(){
        ipaddr = getInternetIp().replace(".","")+"8088";
        return new Queue(ipaddr);
    }
    /**
     * @Description: 创建直连交换机
     * @Param:
     * @return:
     * @Author:
     * @Date: 2020/5/18
     */
    @Bean
    public DirectExchange createWsDirectExchange(){
        return new DirectExchange(WSExchangName);
    }

    //@Bean
    //public ExchangeBuilder createWsDirectExchange(){
    //    return new ExchangeBuilder(WSExchangName,"fanout");
    //}
    /**
     * @Description: 将队列与直连交换机绑定，并指定ip地址为路由
     * @Param:
     * @return:
     * @Author:
     * @Date: 2020/5/18
     */
    @Bean
    public Binding bindingExchange(){
        return BindingBuilder.bind(createWsDirectQueue()).to(createWsDirectExchange()).with("ems");
    }

    /**
     * @Description:  获取外网IP
     * @Param:
     * @return:
     * @Author:
     * @Date: 2020/5/22
     */
    private  String getInternetIp(){
        try{
            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            Enumeration<InetAddress> addrs;
            while (networks.hasMoreElements())
            {
                addrs = networks.nextElement().getInetAddresses();
                while (addrs.hasMoreElements())
                {
                    ip = addrs.nextElement();
                    if (ip != null
                            && ip instanceof Inet4Address
                            && ip.isSiteLocalAddress()
                    )
                    {
                        return ip.getHostAddress();
                    }
                }
            }

            // 如果没有外网IP，就返回内网IP
            return "";
        } catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * @Description: 根据类型获取IP地址
     * @Param:
     * @return:
     * @Author:
     * @Date: 2020/5/22
     */
    private String getInternetIp(String type) {
        try {
            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
            while (networks.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) networks.nextElement();
                if (!ni.getName().equals(type)) {
                    continue;
                } else {
                    Enumeration<?> e2 = ni.getInetAddresses();
                    while (e2.hasMoreElements()) {
                        InetAddress ia = (InetAddress) e2.nextElement();
                        if (ia instanceof Inet6Address) {
                            continue;
                        }
                        return ia.getHostAddress();
                    }
                }
            }
            // 如果没有外网IP，就返回内网IP
            return "";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
