001package jmri.jmrix.bidib.configurexml;
002
003import jmri.InstanceManager;
004
005import jmri.jmrix.bidib.BiDiBLightManager;
006import jmri.jmrix.bidib.BiDiBSystemConnectionMemo;
007
008import org.jdom2.Element;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Provides load and store functionality for configuring BiDiBLightManagers.
014 * <p>
015 * Uses the store method from the abstract base class, but provides a load
016 * method here.
017 *
018 * @author Dave Duchamp Copyright (c) 2006
019 * @author Eckart Meyer Copyright (C) 2019
020 */
021public class BiDiBLightManagerXml extends jmri.managers.configurexml.AbstractLightManagerConfigXML {
022
023    public BiDiBLightManagerXml() {
024        super();
025    }
026    
027    @Override
028    public void setStoreElementClass(Element lights) {
029        lights.setAttribute("class", "jmri.jmrix.bidib.configurexml.BiDiBLightManagerXml");
030    }
031
032    @Override
033    public void load(Element element, Object o) {
034        log.error("Invalid method called");
035    }
036
037    @Override
038    public boolean load(Element shared, Element perNode) {
039        log.debug("load {} {}", shared, perNode);
040        // We tell the sensor managers that we will be loading sensors from XML and they should
041        // expect additional property set sequences. This is somewhat tricky in the face of
042        // possibly multiple connections registered.
043        for (BiDiBSystemConnectionMemo memo : InstanceManager.getList(BiDiBSystemConnectionMemo.class)) {
044            if (!memo.getDisabled()) {
045                ((BiDiBLightManager)memo.getLightManager()).startLoad();
046            }
047        }
048        // load individual lights
049        boolean result = loadLights(shared);
050        
051        // Notifies sensor managers that the loading of XML is complete.
052        for (BiDiBSystemConnectionMemo memo : InstanceManager.getList(BiDiBSystemConnectionMemo.class)) {
053            if (!memo.getDisabled()) {
054                ((BiDiBLightManager)memo.getLightManager()).finishLoad();
055            }
056        }
057
058        return result;
059    }
060    
061    /* 
062     * Find the system connection memo object when we only have the XML element <LIGHTS>...</LIGHTS>.
063     * This depends on the fact that there is a separate <LIGHTS> element for each connection.
064     * We scan the <LIGHT> elements and check if it is an instance of a BiDiBLight. The BiDiBLight object gives us the
065     * system connection memo object. Use the first found Light object and return.
066     * 
067     * @param lights XML element containing the lights to be configured
068     * @return the BiDiBSystemConnectionMemo object
069     */
070/* UNUSED
071    private BiDiBSystemConnectionMemo findSystemConnectionMemo(Element lights) {
072        for (BiDiBSystemConnectionMemo memo : InstanceManager.getList(BiDiBSystemConnectionMemo.class)) {
073            log.debug("*** found memo: {}", memo.getUserName());
074        }
075
076        List<Element> lightList = lights.getChildren("light");
077        ProxyLightManager mgr = (ProxyLightManager)InstanceManager.getDefault(jmri.LightManager.class);
078        for(Element e : lightList) {
079            String sysName = getSystemName(e);
080            if (sysName != null  &&  !sysName.isEmpty()) {
081                Light lgt = mgr.getBySystemName(sysName);
082                if (lgt != null  &&  lgt instanceof BiDiBLight) {
083                    return ((BiDiBLight)lgt).getMemo();
084                }
085            }
086        }
087        return null;
088    }
089*/
090
091    private final static Logger log = LoggerFactory.getLogger(BiDiBLightManagerXml.class);
092
093}