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

org.bidib.wizard.server.controllers.SettingsController Maven / Gradle / Ivy

There is a newer version: 2.0.29
Show newest version
package org.bidib.wizard.server.controllers;

import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.collections4.CollectionUtils;
import org.bidib.api.json.types.ConnectionSetting;
import org.bidib.api.json.types.ConnectionSetting.ConnectionType;
import org.bidib.api.json.types.SettingsData;
import org.bidib.wizard.common.model.settings.ConnectionConfiguration;
import org.bidib.wizard.common.service.SettingsService;
import org.bidib.wizard.core.model.connection.ConnectionRegistry;
import org.bidib.wizard.server.aspect.LogExecutionTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/settings")
public class SettingsController {

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

    @Autowired
    private SettingsService settingsService;

    public SettingsController() {

    }

    @PostConstruct
    public void initialize() throws Exception {
        LOGGER.info("Fetch connections from globalSettings ...");

    }

    protected SettingsData getSettingsDataInstance() {

        SettingsData settingsData = new SettingsData();

        // TODO enhance to not always use the first connection configuration
        List connections = settingsService.getGlobalSettings().getConnectionConfigurations();
        if (CollectionUtils.isNotEmpty(connections)) {
            for (ConnectionConfiguration connection : connections) {
                LOGGER.info("Create the connection entry for connection: {}", connection);

                try {
                    prepareSettings(settingsData, connection);
                }
                catch (Exception ex) {
                    LOGGER.warn("Prepare settings from global settings failed.", ex);
                }
            }
        }

        if (CollectionUtils.isEmpty(settingsData.getConnectionSettings())) {
            LOGGER.warn("No connections available in global settings. Prepare and return default settings.");
            prepareDefaultSettings(settingsData);
        }
        return settingsData;
    }

    private void prepareSettings(final SettingsData settingsData, ConnectionConfiguration connection) {

        ConnectionSetting connectionSetting = new ConnectionSetting();

        connectionSetting.setId(connection.getId());
        connectionSetting.setName(connection.getName());
        connectionSetting.setConnectionType(ConnectionType.valueOf(connection.getConnectionProvider()));
        connectionSetting.setComport(connection.getConnectionUri());

        LOGGER.info("Prepared the connectionSetting: {}", connectionSetting);

        settingsData.getConnectionSettings().add(connectionSetting);
    }

    private void prepareDefaultSettings(final SettingsData settingsData) {

        ConnectionSetting connectionSetting = new ConnectionSetting();

        connectionSetting.setId(ConnectionRegistry.CONNECTION_ID_MAIN);
        connectionSetting.setName("default");
        connectionSetting.setConnectionType(ConnectionType.SerialSimulation);

        settingsData.getConnectionSettings().add(connectionSetting);
    }

    private void updateGlobalSettings(final SettingsData settingsData) {
        LOGGER.info("Update the global settings from settingsData: {}", settingsData);
        // TODO enhance to not always use the first connection configuration
        List connections = settingsService.getGlobalSettings().getConnectionConfigurations();
        connections.clear();

        for (ConnectionSetting connectionSetting : settingsData.getConnectionSettings()) {
            LOGGER.info("Update connectionConfiguration from connectionSetting: {}", connectionSetting);

            ConnectionConfiguration configuration = new ConnectionConfiguration();
            configuration.setId(connectionSetting.getId());
            configuration.setName(connectionSetting.getName());

            configuration.setConnectionProvider(connectionSetting.getConnectionType().name());
            switch (connectionSetting.getConnectionType()) {
                case SerialSimulation:
                    configuration.setConnectionUri(connectionSetting.getComport());
                    break;
                case SerialPort:
                    configuration.setConnectionUri(connectionSetting.getComport());
                    break;
                case SerialOverTcp:
                    configuration.setConnectionUri(connectionSetting.getNetworkHostAddress());
                    break;
                case NetworkSimulation:
                    configuration.setConnectionUri(connectionSetting.getNetworkHostAddress());
                    break;
                case Speedometer:
                case UsbStickBasis:
                    configuration.setConnectionUri(connectionSetting.getComport());
                    break;
                case NetBidibClient:
                    configuration.setConnectionUri(connectionSetting.getNetworkHostAddress());
                    break;
                default:
                    LOGGER
                        .warn("The provided connection type is not handled: {}", connectionSetting.getConnectionType());
                    break;
            }

            LOGGER.info("Add connection configuration: {}", configuration);
            connections.add(configuration);
        }
    }

    /**
     * @return the settings data
     */
    @GetMapping
    public ResponseEntity getSettingsData() {
        SettingsData result = getSettingsDataInstance();
        LOGGER.info("Get the current settings data: {}", result);

        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @MessageMapping("/settings")
    @SendToUser(destinations = "/queue/settings", broadcast = false)
    @LogExecutionTime
    public SettingsData msgGetSettingsData() {

        SettingsData result = getSettingsDataInstance();

        LOGGER.info("Get the current settings data: {}", result);

        return result;
    }

    @MessageMapping("/settings/update")
    @SendToUser(destinations = "/queue/settings", broadcast = false)
    @LogExecutionTime
    public SettingsData msgSetSettingsData(SettingsData settingsData) {
        LOGGER.info("Save the new settings data: {}", settingsData);

        // TODO this is not clever because it prevents listeners on the original object to be notified
        // this.settingsData = settingsData;

        // update the global configuration
        updateGlobalSettings(settingsData);

        try {
            settingsService.storeSettings();
        }
        catch (Exception ex) {
            LOGGER.warn("Store global settings failed.", ex);
        }

        return settingsData;
    }

    @PutMapping
    public ResponseEntity setSettingsData(@RequestBody SettingsData settingsData) {
        LOGGER.info("Save the new settings data: {}", settingsData);

        // update the global configuration
        updateGlobalSettings(settingsData);

        try {
            settingsService.storeSettings();
        }
        catch (Exception ex) {
            LOGGER.warn("Store global settings failed.", ex);
        }

        // return this.settingsData;
        return new ResponseEntity<>(settingsData, HttpStatus.OK);
    }

    @GetMapping(path = "/connectionConfigurations")
    public ResponseEntity> getConnectionConfigurations() {
        LOGGER.info("Get connection configurations.");

        return new ResponseEntity<>(settingsService.getGlobalSettings().getConnectionConfigurations(), HttpStatus.OK);
    }

    @PutMapping(path = "/connectionConfigurations")
    public void addConnectionConfiguration(ConnectionConfiguration configuration) {
        LOGGER.info("Add connection configuration: {}", configuration);

        settingsService.getGlobalSettings().addConnectionConfiguration(configuration);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy