All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.wizard.localhost.config.LocalHostConfig Maven / Gradle / Ivy

package org.bidib.wizard.localhost.config;

import java.util.function.Supplier;

import org.bidib.jbidibc.core.BidibDistributedMessageListener;
import org.bidib.jbidibc.messages.message.BidibRequestFactory;
import org.bidib.wizard.api.model.NodeProvider;
import org.bidib.wizard.api.model.connection.BidibConnection;
import org.bidib.wizard.api.service.node.CommandStationService;
import org.bidib.wizard.api.service.node.NodeService;
import org.bidib.wizard.api.service.node.SwitchingNodeService;
import org.bidib.wizard.localhost.DefaultLocalHostSubscriptionEventProvider;
import org.bidib.wizard.localhost.LocalHostBidibDistributedMessageHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

@ConditionalOnProperty(prefix = "wizardmodule_localHostConfiguration", name = "localHostServiceEnabled")
@Configuration
@EnableConfigurationProperties(LocalHostSettings.class)
public class LocalHostConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(LocalHostConfig.class);

    @Bean
    LocalHostHandlerFactory localHostHandlerFactory(
        final BidibRequestFactory requestFactory, final NodeService nodeService,
        final Supplier commandStationService, final SwitchingNodeService switchingNodeService) {

        final LocalHostHandlerFactory localHostHandlerFactory = new LocalHostHandlerFactory() {

            @Override
            public BidibDistributedMessageListener createLocalHostHandler(final BidibConnection connection) {

                final NodeProvider nodeProvider = connection.getNodeProvider();
                LOGGER
                    .info("Create the LocalHostBidibDistributedMessageHandler instance for connection: {}", connection);
                return localHostHandler(connection, nodeProvider, requestFactory, nodeService,
                    commandStationService.get(), switchingNodeService);
            }
        };
        return localHostHandlerFactory;
    }

    @Bean
    @Lazy
    @Primary
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    BidibDistributedMessageListener localHostHandler(
        final BidibConnection connection, final NodeProvider nodeProvider, final BidibRequestFactory requestFactory,
        final NodeService nodeService, final CommandStationService commandStationService,
        final SwitchingNodeService switchingNodeService) {
        LocalHostBidibDistributedMessageHandler localHostHandler =
            new LocalHostBidibDistributedMessageHandler(connection, nodeProvider, requestFactory, nodeService,
                commandStationService, switchingNodeService);

        final DefaultLocalHostSubscriptionEventProvider eventProvider = localHostSubscriptionEventProvider();
        eventProvider.setLocalHostHandler(localHostHandler);

        return localHostHandler;
    }

    @Bean
    DefaultLocalHostSubscriptionEventProvider localHostSubscriptionEventProvider() {
        LOGGER.info("Create new LocalHostSubscriptionEventProvider instance.");
        return new DefaultLocalHostSubscriptionEventProvider();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy