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

org.bidib.wizard.gateway.config.GatewayServiceSettings Maven / Gradle / Ivy

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

import java.net.InetAddress;
import java.net.NetworkInterface;

import javax.validation.constraints.NotNull;

import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.wizard.common.model.settings.AbstractSettings;
import org.bidib.wizard.common.model.settings.GatewayServiceSettingsInterface;
import org.bidib.wizard.common.model.settings.annotation.SettingsIdentifier;
import org.bidib.wizard.common.model.settings.configuration.YamlPropertySourceFactory;
import org.bidib.wizard.core.model.settings.converter.LongToUniqueIdConverter;
import org.bidib.wizard.core.model.settings.converter.UniqueIdToLongConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.validation.annotation.Validated;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@ConfigurationProperties(prefix = "wizardmodule-gateway-configuration")
@SettingsIdentifier(storageIdentifier = "wizardmodule_gatewayConfiguration")
@PropertySource(value = "file:${wizard.configuration.file-location}/wizardmodule_gatewayConfiguration.yml", factory = YamlPropertySourceFactory.class, ignoreResourceNotFound = true)
@Validated
public class GatewayServiceSettings extends AbstractSettings implements GatewayServiceSettingsInterface {

    private static final long serialVersionUID = 1L;

    private static final Logger LOGGER = LoggerFactory.getLogger(GatewayServiceSettings.class);

    @NotNull
    private String bindAddress = "0.0.0.0";

    // private int listenPort = 15471;

    private int listenPortNetBidib = 62888;

    @JsonSerialize(converter = LongToUniqueIdConverter.class)
    @JsonDeserialize(converter = UniqueIdToLongConverter.class)
    private Long netBidibGatewayUniqueId;

    private boolean autoConnectBackend;

    private boolean bidibDistributedEnabled = false;

    /**
     * @return the bindAddress
     */
    @Override
    public String getBindAddress() {
        return bindAddress;
    }

    /**
     * @param bindAddress
     *            the bindAddress to set
     */
    @Override
    public void setBindAddress(String bindAddress) {
        String oldValue = this.bindAddress;
        this.bindAddress = bindAddress;

        firePropertyChange(PROPERTY_BIND_ADDRESS, oldValue, this.bindAddress);
    }

    // /**
    // * @return the listenPort
    // */
    // @Override
    // public int getListenPort() {
    // return listenPort;
    // }
    //
    // /**
    // * @param listenPort
    // * the listenPort to set
    // */
    // @Override
    // public void setListenPort(int listenPort) {
    // int oldValue = this.listenPort;
    // this.listenPort = listenPort;
    //
    // firePropertyChange(PROPERTY_LISTEN_PORT, oldValue, this.listenPort);
    // }

    /**
     * @return the listenPortNetBidib
     */
    @Override
    public int getListenPortNetBidib() {
        return listenPortNetBidib;
    }

    /**
     * @param listenPortNetBidib
     *            the listenPortNetBidib to set
     */
    @Override
    public void setListenPortNetBidib(int listenPortNetBidib) {
        int oldValue = this.listenPortNetBidib;
        this.listenPortNetBidib = listenPortNetBidib;

        firePropertyChange(PROPERTY_LISTEN_PORT_NETBIDIB, oldValue, this.listenPortNetBidib);
    }

    /**
     * @return the autoConnectBackend
     */
    @Override
    public boolean isAutoConnectBackend() {
        return autoConnectBackend;
    }

    /**
     * @param autoConnectBackend
     *            the autoConnectBackend to set
     */
    @Override
    public void setAutoConnectBackend(boolean autoConnectBackend) {
        boolean oldValue = this.autoConnectBackend;
        this.autoConnectBackend = autoConnectBackend;

        firePropertyChange(PROPERTY_AUTO_CONNECT_BACKEND, oldValue, this.autoConnectBackend);
    }

    /**
     * @return the bidibDistributedEnabled flag
     */
    @Override
    public boolean isBidibDistributedEnabled() {
        return bidibDistributedEnabled;
    }

    /**
     * @param bidibDistributedEnabled
     *            the bidibDistributedEnabled flag to set
     */
    @Override
    public void setBidibDistributedEnabled(boolean bidibDistributedEnabled) {
        boolean oldValue = this.bidibDistributedEnabled;
        this.bidibDistributedEnabled = bidibDistributedEnabled;

        firePropertyChange(PROPERTY_BIDIB_DISTRIBUTED_ENABLED, oldValue, this.bidibDistributedEnabled);
    }

    /**
     * @param netBidibGatewayUniqueId
     *            the netBidibUniqueId to set
     */
    @Override
    public void setNetBidibGatewayUniqueId(Long netBidibGatewayUniqueId) {
        Long oldValue = this.netBidibGatewayUniqueId;

        if (netBidibGatewayUniqueId != null && ((netBidibGatewayUniqueId.longValue() & 0xFFFFFFL) == 0)) {
            LOGGER
                .warn("Invalid netBiDiB Gateway uniqueId delivered that is discarded: {}",
                    ByteUtils.formatHexUniqueId(netBidibGatewayUniqueId));

            netBidibGatewayUniqueId = null;
        }
        this.netBidibGatewayUniqueId = netBidibGatewayUniqueId;

        firePropertyChange(PROPERTY_NETBIDIB_GATEWAY_UNIQUEID, oldValue, this.netBidibGatewayUniqueId);
    }

    /**
     * @return the netBidibGatewayUniqueId
     */
    @Override
    public Long getNetBidibGatewayUniqueId() {
        if (netBidibGatewayUniqueId == null) {
            byte[] uniqueId =
                new byte[] { 0x00, 0x00, 0x0D, ByteUtils.getLowByte(WIZARD_GATEWAY_PID), 0x00, 0x03, (byte) 0xF1 };

            // generate the uniqueId from the mac address
            try {
                InetAddress localHost = InetAddress.getLocalHost();
                NetworkInterface ni = NetworkInterface.getByInetAddress(localHost);
                if (ni != null) {
                    byte[] hardwareAddress = ni.getHardwareAddress();

                    if (hardwareAddress != null) {
                        String[] hexadecimal = new String[hardwareAddress.length];
                        for (int i = 0; i < hardwareAddress.length; i++) {
                            hexadecimal[i] = String.format("%02X", hardwareAddress[i]);
                        }
                        String macAddress = String.join("", hexadecimal);
                        int hashCode = macAddress.hashCode();

                        uniqueId =
                            new byte[] { 0x00, 0x00, 0x0D, ByteUtils.getLowByte(WIZARD_GATEWAY_PID), 0x00,
                                ByteUtils.getHighByte(hashCode), ByteUtils.getLowByte(hashCode) };

                        LOGGER
                            .info("Generated netBiDiB Gateway uniqueId from the MAC address: {}",
                                ByteUtils.convertUniqueIdToString(uniqueId));
                    }
                    else {
                        LOGGER
                            .warn(
                                "No hardware address for localhost available. Use default netBiDiB Gateway uniqueId.");
                    }
                }
                else {
                    LOGGER.warn("No network interface for localhost available. Use default netBiDiB Gateway uniqueId.");
                }
            }
            catch (Exception ex) {
                LOGGER.warn("Generate the netBiDiB Gateway uniqueId from the MAC address failed.", ex);
            }
            netBidibGatewayUniqueId = ByteUtils.convertUniqueIdToLong(uniqueId);
        }
        return netBidibGatewayUniqueId;
    }

    @Override
    public void migrate() {

        if (getSettingsVersion() == 0) {
            LOGGER.info("Migrate settings to version 1.");

            if (this.listenPortNetBidib == 62876) {
                this.listenPortNetBidib = 62888;
                LOGGER.info("Adjust the listenPortNetBidib to: {}", this.listenPortNetBidib);
            }

            setSettingsVersion(1);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy