Search in sources :

Example 1 with Connectivity

use of de.avanux.smartapplianceenabler.configuration.Connectivity in project SmartApplianceEnabler by camueller.

the class SaeController method setSettings.

@RequestMapping(value = SETTINGS_URL, method = RequestMethod.PUT, consumes = "application/json")
@CrossOrigin(origins = CROSS_ORIGIN_URL)
public void setSettings(@RequestBody Settings settings) {
    logger.debug("Received request to set " + settings);
    List<PulseReceiver> pulseReceivers = null;
    if (settings.isPulseReceiverEnabled()) {
        PulseReceiver pulseReceiver = new PulseReceiver();
        pulseReceiver.setId(PulseReceiver.DEFAULT_ID);
        pulseReceiver.setPort(settings.getPulseReceiverPort());
        pulseReceivers = Collections.singletonList(pulseReceiver);
    }
    List<ModbusTcp> modbusTCPs = null;
    if (settings.isModbusEnabled()) {
        ModbusTcp modbusTcp = new ModbusTcp();
        modbusTcp.setId(ModbusTcp.DEFAULT_ID);
        modbusTcp.setHost(settings.getModbusTcpHost());
        modbusTcp.setPort(settings.getModbusTcpPort());
        modbusTCPs = Collections.singletonList(modbusTcp);
    }
    Connectivity connectivity = null;
    if (pulseReceivers != null || modbusTCPs != null) {
        connectivity = new Connectivity();
        connectivity.setPulseReceivers(pulseReceivers);
        connectivity.setModbusTCPs(modbusTCPs);
    }
    ApplianceManager.getInstance().setConnectivity(connectivity);
    List<Configuration> configurations = null;
    if (settings.isHolidaysEnabled()) {
        Configuration configuration = new Configuration();
        configuration.setParam(HolidaysDownloader.urlConfigurationParamName);
        configuration.setValue(settings.getHolidaysUrl());
        configurations = Collections.singletonList(configuration);
    }
    ApplianceManager.getInstance().setConfiguration(configurations);
}
Also used : Configuration(de.avanux.smartapplianceenabler.configuration.Configuration) Connectivity(de.avanux.smartapplianceenabler.configuration.Connectivity) ModbusTcp(de.avanux.smartapplianceenabler.modbus.ModbusTcp)

Example 2 with Connectivity

use of de.avanux.smartapplianceenabler.configuration.Connectivity in project SmartApplianceEnabler by camueller.

the class ApplianceManager method init.

public void init() {
    Map<String, PulseReceiver> pulseReceiverIdWithPulseReceiver = new HashMap<String, PulseReceiver>();
    Map<String, ModbusTcp> modbusIdWithModbusTcp = new HashMap<String, ModbusTcp>();
    Connectivity connectivity = appliances.getConnectivity();
    if (connectivity != null) {
        // make PulseReceiver accessible by id
        if (connectivity.getPulseReceivers() != null) {
            for (PulseReceiver pulseReceiver : connectivity.getPulseReceivers()) {
                logger.info("PulseReceiver (" + pulseReceiver.getId() + ") configured for port " + pulseReceiver.getPort());
                pulseReceiverIdWithPulseReceiver.put(pulseReceiver.getId(), pulseReceiver);
                pulseReceiver.start();
            }
        }
        // make ModbusTcp accessible by id
        if (connectivity.getModbusTCPs() != null) {
            for (ModbusTcp modbusTCP : connectivity.getModbusTCPs()) {
                logger.info("ModBus (" + modbusTCP.getId() + ") configured for " + modbusTCP.toString());
                modbusIdWithModbusTcp.put(modbusTCP.getId(), modbusTCP);
            }
        }
    }
    Integer additionRunningTime = null;
    String additionRunningTimeString = appliances.getConfigurationValue("TimeframeIntervalAdditionalRunningTime");
    if (additionRunningTimeString != null) {
        additionRunningTime = Integer.valueOf(additionRunningTimeString);
    }
    boolean holidaysUsed = false;
    for (Appliance appliance : getAppliances()) {
        if (appliance.hasTimeframeForHolidays()) {
            holidaysUsed = true;
        }
        appliance.init(additionRunningTime);
        appliance.start(timer, gpioController, pulseReceiverIdWithPulseReceiver, modbusIdWithModbusTcp);
    }
    if (holidaysUsed) {
        logger.debug("Holidays are used.");
        /**
         * Once a day check availability of holidays file - the year might have changed!
         * Download it if it is not available. If it is available (either downloaded or just placed there)
         * load holidays from the file pass them on to all appliances.
         */
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                FileHandler fileHandler = new FileHandler();
                if (!fileHandler.isHolidayFileAvailable()) {
                    HolidaysDownloader downloader = new HolidaysDownloader();
                    String downloadUrl = appliances.getConfigurationValue(HolidaysDownloader.urlConfigurationParamName);
                    if (downloadUrl != null) {
                        downloader.setUrl(downloadUrl);
                    }
                    Map<LocalDate, String> holidayWithName = downloader.downloadHolidays();
                    fileHandler.saveHolidays(holidayWithName);
                }
                List<LocalDate> holidays = fileHandler.loadHolidays();
                if (holidays != null) {
                    for (Appliance appliance : getAppliances()) {
                        appliance.setHolidays(holidays);
                    }
                }
            }
        }, 0, 24 * 60 * 60 * 1000);
    } else {
        logger.debug("Holidays are NOT used.");
    }
}
Also used : PulseReceiver(de.avanux.smartapplianceenabler.meter.PulseReceiver) ModbusTcp(de.avanux.smartapplianceenabler.modbus.ModbusTcp) FileHandler(de.avanux.smartapplianceenabler.util.FileHandler) Connectivity(de.avanux.smartapplianceenabler.configuration.Connectivity) HolidaysDownloader(de.avanux.smartapplianceenabler.HolidaysDownloader)

Example 3 with Connectivity

use of de.avanux.smartapplianceenabler.configuration.Connectivity in project SmartApplianceEnabler by camueller.

the class ApplianceManager method restartAppliances.

private void restartAppliances() {
    if (this.appliances != null) {
        Connectivity connectivity = this.appliances.getConnectivity();
        if (connectivity != null) {
            // make PulseReceiver accessible by id
            if (connectivity.getPulseReceivers() != null) {
                for (PulseReceiver pulseReceiver : connectivity.getPulseReceivers()) {
                    logger.info("Stopping PulseReceiver (" + pulseReceiver.getId() + ") configured for port " + pulseReceiver.getPort());
                    pulseReceiver.stop();
                }
            }
        }
        for (Appliance appliance : appliances.getAppliances()) {
            appliance.stop();
        }
    }
    logger.info("Restarting appliances ...");
    this.appliances = null;
    this.device2EM = null;
    startAppliances();
}
Also used : Connectivity(de.avanux.smartapplianceenabler.configuration.Connectivity) PulseReceiver(de.avanux.smartapplianceenabler.meter.PulseReceiver)

Example 4 with Connectivity

use of de.avanux.smartapplianceenabler.configuration.Connectivity in project SmartApplianceEnabler by camueller.

the class SaeController method getSettings.

@RequestMapping(value = SETTINGS_URL, method = RequestMethod.GET, produces = "application/json")
@CrossOrigin(origins = CROSS_ORIGIN_URL)
public Settings getSettings() {
    logger.debug("Received request for Settings");
    Settings settings = new Settings();
    Appliances appliances = ApplianceManager.getInstance().getAppliancesRoot();
    Connectivity connectivity = appliances.getConnectivity();
    if (connectivity != null) {
        List<PulseReceiver> pulseReceivers = connectivity.getPulseReceivers();
        if (pulseReceivers != null && pulseReceivers.size() > 0) {
            settings.setPulseReceiverEnabled(true);
            PulseReceiver pulseReceiver = pulseReceivers.get(0);
            settings.setPulseReceiverPort(pulseReceiver.getPort());
        }
        List<ModbusTcp> modbusTCPs = connectivity.getModbusTCPs();
        if (modbusTCPs != null && modbusTCPs.size() > 0) {
            settings.setModbusEnabled(true);
            ModbusTcp modbusTcp = modbusTCPs.get(0);
            settings.setModbusTcpHost(modbusTcp.getHost());
            settings.setModbusTcpPort(modbusTcp.getPort());
        }
    }
    String holidaysUrl = appliances.getConfigurationValue(HolidaysDownloader.urlConfigurationParamName);
    settings.setHolidaysEnabled(holidaysUrl != null);
    settings.setHolidaysUrl(holidaysUrl);
    logger.debug("Returning Settings " + settings);
    return settings;
}
Also used : Connectivity(de.avanux.smartapplianceenabler.configuration.Connectivity) ModbusTcp(de.avanux.smartapplianceenabler.modbus.ModbusTcp)

Aggregations

Connectivity (de.avanux.smartapplianceenabler.configuration.Connectivity)4 ModbusTcp (de.avanux.smartapplianceenabler.modbus.ModbusTcp)3 PulseReceiver (de.avanux.smartapplianceenabler.meter.PulseReceiver)2 HolidaysDownloader (de.avanux.smartapplianceenabler.HolidaysDownloader)1 Configuration (de.avanux.smartapplianceenabler.configuration.Configuration)1 FileHandler (de.avanux.smartapplianceenabler.util.FileHandler)1