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
053            log.debug("Import Logix {} to LogixNG {}", logix.getSystemName(), logixNG.getSystemName());
054        }
055
056        _logix = logix;
057        _logixNG = logixNG;
058    }
059
060    public void doImport() throws JmriException {
061        for (int i=0; i < _logix.getNumConditionals(); i++) {
062            Conditional c = _logix.getConditional(_logix.getConditionalByNumberOrder(i));
063
064            if (!_dryRun) {
065                log.warn("Import Conditional '{}' to LogixNG '{}'", c.getSystemName(), _logixNG.getSystemName());
066            }
067
068            ImportConditional ic = new ImportConditional(
069                    _logix, c, _logixNG,
070                    InstanceManager.getDefault(ConditionalNG_Manager.class).getAutoSystemName(),
071                    _dryRun);
072
073            try {
074                ic.doImport();
075            } catch (SocketAlreadyConnectedException ex) {
076                if (!_dryRun) {
077                    log.warn("Exception during import of Conditional {} to ConditionalNG {}",
078                            c.getSystemName(), _logixNG.getSystemName(), ex);
079                }
080            }
081
082            if (!_dryRun) ic.getConditionalNG().setEnabled(true);
083        }
084    }
085
086    public LogixNG getLogixNG() {
087        return _logixNG;
088    }
089
090    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ImportLogix.class);
091
092}