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

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

There is a newer version: 2.0.29
Show newest version
package org.bidib.wizard.localhost.config;

import java.util.function.Supplier;

import org.bidib.jbidibc.core.BidibDistributedMessageListener;
import org.bidib.jbidibc.messages.message.BidibResponseFactory;
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.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
    public LocalHostHandlerFactory localHostHandlerFactory(
            final BidibResponseFactory responseFactory, 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, responseFactory, commandStationService.get(),
                        switchingNodeService);
            }
        };
        return localHostHandlerFactory;
    }

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

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

        return localHostHandler;
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy