@Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        super.channelUnregistered(ctx);
        logger.debug("{} channelUnregistered: {}", LocalTime.now(), ctx.name());
        String strCanal = (tcpChannel == primaryChannel) ? "PRIMARY" : "SECONDARY";
        logger.info("Unregistered of channel: {}", strCanal);
        //TODO: If embedded channel is closed, we need close all channels
        if (ctx == embedCtx) return;

        if (tcpChannel == ctx.channel()) {
            embededChannel.attr(IS_CONNECTED).set(false);
            embededChannel.attr(WAS_CONNECTED).set(true);
            embededChannel.pipeline().fireUserEventTriggered(new DisconnectedEvent());
        }

        if (embedCtx != null)
            logger.info(embedCtx.executor().toString());

        if ((tcpChannel == primaryChannel) &&
            (primaryChannel == ctx.channel()))
            if ((!primaryChannel.isActive()) &&
                (secondaryChannel != null))
                if (secondaryChannel.isActive()) {
                    synchronized (tcpChannel) {
                        logger.info("Using secondary TCP channel.");
                        tcpChannel = secondaryChannel;
                        embededChannel.attr(IS_PRIMARY).set(false);
                        embededChannel.pipeline().fireUserEventTriggered(new ConnectEvent());
                    }
                }


        if ((tcpChannel == secondaryChannel) &&
            (secondaryChannel == ctx.channel()))
            if ((!secondaryChannel.isActive()) &&
                (primaryChannel != null))
                if (primaryChannel.isActive()) {
                    synchronized (tcpChannel) {
                        logger.info("Using primary TCP channel.");
                        tcpChannel = primaryChannel;
                        embededChannel.attr(IS_PRIMARY).set(true);
                        embededChannel.pipeline().fireUserEventTriggered(new ConnectEvent());
                    }
                }
    }
Implementation of a multiplexing channel, from an embedded channel to two
possible TCP connections, primary and secondary.
The objective is to allow connections to individual systems
with a two CP (PN CPUs, CP343-1, CP443-1 or similar), or H-type systems
(S7-400H or S7-1500H).
The user App must be in charge of restoring the requests or
subscriptions that it is requesting.
plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HMuxImpl.java