001package jmri.jmrix.pi.simulator.configurexml;
002
003import jmri.jmrix.configurexml.AbstractConnectionConfigXml;
004import jmri.jmrix.pi.RaspberryPiAdapter;
005import jmri.jmrix.pi.simulator.RaspberryPiSimulatorConnectionConfig;
006
007import org.jdom2.Element;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Handle XML persistence of layout connections by persisting the
013 * RaspberryPiAdapter. Note this is named as the XML version of a
014 * RaspberryPiSimulatorConnectionConfig object, but it's actually persisting the
015 * RaspberryPiAdapter.
016 * <p>
017 * This class is invoked from jmrix.JmrixConfigPaneXml on write, as that class
018 * is the one actually registered. Reads are brought here directly via the class
019 * attribute in the XML.
020 *
021 * @author Paul Bender Copyright: Copyright (c) 2015
022 */
023public class RaspberryPiSimulatorConnectionConfigXml extends AbstractConnectionConfigXml {
024
025    private RaspberryPiAdapter adapter = null;
026
027    public RaspberryPiSimulatorConnectionConfigXml() {
028        super();
029    }
030
031    @Override
032    protected void getInstance() {
033        log.debug("getInstance without Parameter called");
034        if (adapter == null) {
035            adapter = new RaspberryPiAdapter(true);
036            if (adapter.getGPIOController() == null) {
037                handleException("Not running on Raspberry PI.", null, adapter.getSystemPrefix(), adapter.getUserName(), null);
038            }
039        }
040    }
041
042    protected void getInstance(Object object) {
043        log.debug("getInstance with Parameter called");
044        adapter = ((RaspberryPiSimulatorConnectionConfig) object).getAdapter();
045    }
046
047    @Override
048    protected void register() {
049        this.register(new RaspberryPiSimulatorConnectionConfig(adapter));
050    }
051
052    /**
053     * Default implementation for storing the static contents of the serial port
054     * implementation
055     *
056     * @param o Object to store, of type PositionableLabel
057     * @return Element containing the complete info
058     */
059    @Override
060    public Element store(Object o) {
061        getInstance(o);
062        Element e = new Element("connection");
063        storeCommon(e, adapter);
064        e.setAttribute("class", this.getClass().getName());
065        return e;
066    }
067
068    @Override
069    public boolean load(Element shared, Element perNode) {
070        getInstance();
071        loadCommon(shared, perNode, adapter);
072
073        // register, so can be picked up next time
074        register();
075
076        adapter.configure();
077        return true;
078    }
079
080    @Override
081    protected void dispose() {
082        adapter.dispose();
083    }
084
085    private final static Logger log = LoggerFactory.getLogger(RaspberryPiSimulatorConnectionConfigXml.class);
086
087}