001package jmri.jmrit.logixng.tools;
002
003import jmri.*;
004import jmri.jmrit.logixng.ConditionalNG_Manager;
005import jmri.jmrit.logixng.LogixNG;
006import jmri.jmrit.logixng.SocketAlreadyConnectedException;
007
008/**
009 * Imports Logixs to LogixNG
010 *
011 * @author Daniel Bergqvist 2019
012 */
013public class ImportLogix {
014
015    private final Logix _logix;
016    private final LogixNG _logixNG;
017    private final boolean _dryRun;
018
019    public ImportLogix(Logix logix) {
020        this(logix, false);
021    }
022
023    public ImportLogix(Logix logix, boolean allowSystemImport) {
024        this(logix, allowSystemImport, false);
025    }
026
027    /**
028     * Create instance of ImportConditional
029     * @param logix             the parent Logix of the conditional to import
030     * @param allowSystemImport true if system logixs is allowed to be imported,
031     *                          false otherwise
032     * @param dryRun            true if import without creating any new beans,
033     *                          false if to create new beans
034     */
035    public ImportLogix(Logix logix, boolean allowSystemImport, boolean dryRun) {
036
037        _dryRun = dryRun;
038        LogixNG logixNG = null;
039
040        if (!_dryRun) {
041            int counter = 0;
042            while ((logixNG == null) && counter < 100) {
043                String name = counter > 0 ? " - " + Integer.toString(counter) : "";
044                logixNG = InstanceManager.getDefault(jmri.jmrit.logixng.LogixNG_Manager.class)
045                        .createLogixNG("Logix: " + logix.getDisplayName() + name);
046                counter++;
047            }
048
049            if (logixNG == null) throw new RuntimeException("Cannot create new LogixNG with name: \"Logix: " + logix.getDisplayName()+"\"");
050
051            logixNG.activate();
052            logixNG.clearStartup();
053
054            log.debug("Import Logix {} to LogixNG {}", logix.getSystemName(), logixNG.getSystemName());
055        }
056
057        _logix = logix;
058        _logixNG = logixNG;
059    }
060
061    public void doImport() throws JmriException {
062        for (int i=0; i < _logix.getNumConditionals(); i++) {
063            Conditional c = _logix.getConditional(_logix.getConditionalByNumberOrder(i));
064
065            if (!_dryRun) {
066                log.warn("Import Conditional '{}' to LogixNG '{}'", c.getSystemName(), _logixNG.getSystemName());
067            }
068
069            ImportConditional ic = new ImportConditional(
070                    _logix, c, _logixNG,
071                    InstanceManager.getDefault(ConditionalNG_Manager.class).getAutoSystemName(),
072                    _dryRun);
073
074            try {
075                ic.doImport();
076            } catch (SocketAlreadyConnectedException ex) {
077                if (!_dryRun) {
078                    log.warn("Exception during import of Conditional {} to ConditionalNG {}",
079                            c.getSystemName(), _logixNG.getSystemName(), ex);
080                }
081            }
082
083            if (!_dryRun) ic.getConditionalNG().setEnabled(true);
084        }
085    }
086
087    public LogixNG getLogixNG() {
088        return _logixNG;
089    }
090
091    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ImportLogix.class);
092
093}