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