Search in sources :

Example 1 with HolidaysDownloader

use of de.avanux.smartapplianceenabler.HolidaysDownloader 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)

Aggregations

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