001package jmri.jmrix.bidib.configurexml;
002
003import jmri.InstanceManager;
004import jmri.configurexml.JmriConfigureXmlException;
005import org.jdom2.Element;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009import jmri.jmrix.bidib.BiDiBSensorManager;
010import jmri.jmrix.bidib.BiDiBSystemConnectionMemo;
011
012
013/**
014 * Provides load and store functionality for configuring BiDiBSensorManagers.
015 * <p>
016 * Uses the store method from the abstract base class, but provides a load
017 * method here.
018 *
019 * @author Bob Jacobsen Copyright: Copyright (c) 2008
020 * @author Eckart Meyer Copyright (C) 2019
021 * @since 2.3.1
022 */
023public class BiDiBSensorManagerXml extends jmri.managers.configurexml.AbstractSensorManagerConfigXML {
024
025    public BiDiBSensorManagerXml() {
026        super();
027    }
028
029    @Override
030    public void setStoreElementClass(Element sensors) {
031        sensors.setAttribute("class", this.getClass().getName());
032    }
033
034    @Override
035    public void load(Element element, Object o) {
036        log.error("Invalid method called");
037    }
038
039    @Override
040    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
041        log.debug("load {} {}", shared, perNode);
042        // We tell the sensor managers that we will be loading sensors from XML and they should
043        // expect additional property set sequences. This is somewhat tricky in the face of
044        // possibly multiple connections registered.
045        for (BiDiBSystemConnectionMemo memo : InstanceManager.getList(BiDiBSystemConnectionMemo.class)) {
046            if (!memo.getDisabled()) {
047                ((BiDiBSensorManager)memo.getSensorManager()).startLoad();
048            }
049        }
050        // load individual sensors
051        boolean result = loadSensors(shared);
052
053        // Notifies sensor managers that the loading of XML is complete.
054        for (BiDiBSystemConnectionMemo memo : InstanceManager.getList(BiDiBSystemConnectionMemo.class)) {
055            if (!memo.getDisabled()) {
056                ((BiDiBSensorManager)memo.getSensorManager()).finishLoad();
057            }
058        }
059
060//        if (result) {
061//            ProxySensorManager pm = (ProxySensorManager)InstanceManager.getDefault(jmri.SensorManager.class);
062//            //log.debug("Sensor Manager List: {}", pm.getManagerList());
063//            // the following is probably wrong since the we cannot guarantee that the first
064//            // entry in the list is the correct one.
065//            BiDiBSensorManager mgr = (BiDiBSensorManager)pm.getManagerList().get(0);
066//            if (mgr != null  && mgr instanceof BiDiBSensorManager) {
067//                mgr.updateAll();
068//            }
069//        }
070        
071        return result;
072    }
073
074    private final static Logger log = LoggerFactory.getLogger(BiDiBSensorManagerXml.class);
075}