package com.skr.mdm.config;

import io.netty.channel.ChannelHandlerContext;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author nfq
 * @date 2020/7/24 18:24
 */
public class MdmWebsocketChannelFactory {

    private static final ConcurrentMap<String, ChannelHandlerContext> CHANNEL_MAP = new ConcurrentHashMap<>(256);

    /**
     * {@code @key:} chanelId
     * {@code @value:} equipmentId
     */
    private static final ConcurrentMap<String, Integer> CHANNEL_DEVICE_MAP = new ConcurrentHashMap<>(256);

    public static void addChannel(ChannelHandlerContext ctx, Integer equipmentId) {
        String channelId = ctx.channel().id().asShortText();
        CHANNEL_MAP.put(channelId, ctx);
        CHANNEL_DEVICE_MAP.put(channelId, equipmentId);
    }

    public static void removeChannel(ChannelHandlerContext ctx) {
        String channelId = ctx.channel().id().asShortText();
        CHANNEL_MAP.remove(channelId);
        CHANNEL_DEVICE_MAP.remove(channelId);
    }

    public static ChannelHandlerContext getChannel(String channelId) {
        return CHANNEL_MAP.get(channelId);
    }

    public static Integer getEquipmentId(String channelId) {
        return CHANNEL_DEVICE_MAP.get(channelId);
    }
}
