001package jmri.jmrix.maple.serialdriver.configurexml;
002
003import java.util.List;
004import jmri.jmrix.configurexml.AbstractSerialConnectionConfigXml;
005import jmri.jmrix.maple.InputBits;
006import jmri.jmrix.maple.OutputBits;
007import jmri.jmrix.maple.SerialNode;
008import jmri.jmrix.maple.MapleSystemConnectionMemo;
009import jmri.jmrix.maple.serialdriver.ConnectionConfig;
010import jmri.jmrix.maple.serialdriver.SerialDriverAdapter;
011import org.jdom2.Element;
012
013/**
014 * Handle XML persistance of layout connections by persisting the
015 * SerialDriverAdapter (and connections). Note this is named as the XML version
016 * of a ConnectionConfig object, but it's actually persisting the
017 * SerialDriverAdapter.
018 * <p>
019 * This class is invoked from jmrix.JmrixConfigPaneXml on write, as that class
020 * is the one actually registered. Reads are brought here directly via the class
021 * attribute in the XML.
022 * <p>
023 * NOTE: Code related to pulsed turnout control has been commented out.
024 *
025 * @author Bob Jacobsen Copyright: Copyright (c) 2003, 2008
026 * @author Bob Jacobsen, Dave Duchamp Copyright (c) 2009 - Maple modifications
027 */
028public class ConnectionConfigXml extends AbstractSerialConnectionConfigXml {
029
030    public ConnectionConfigXml() {
031        super();
032    }
033
034    /**
035     * Write out the SerialNode objects too.
036     *
037     * @param e Element being extended
038     */
039    @Override
040    protected void extendElement(Element e) {
041        SerialNode node = (SerialNode) ((MapleSystemConnectionMemo)adapter.getSystemConnectionMemo()).getTrafficController().getNode(0);
042        int index = 1;
043        while (node != null) {
044            // add node as an element
045            Element n = new Element("node");
046            n.setAttribute("name", "" + node.getNodeAddress());
047            e.addContent(n);
048            // add parameters to the node as needed
049            n.addContent(makeParameter("transmissiondelay", "" + InputBits.getTimeoutTime()));
050            n.addContent(makeParameter("inputbits", "" + InputBits.getNumInputBits()));
051            n.addContent(makeParameter("senddelay", "" + OutputBits.getSendDelay()));
052            n.addContent(makeParameter("outputbits", "" + OutputBits.getNumOutputBits()));
053//            n.addContent(makeParameter("pulsewidth", ""+node.getPulseWidth()));
054
055            // look for the next node
056            node = (SerialNode) ((MapleSystemConnectionMemo)adapter.getSystemConnectionMemo()).getTrafficController().getNode(index);
057            index++;
058        }
059    }
060
061    protected Element makeParameter(String name, String value) {
062        Element p = new Element("parameter");
063        p.setAttribute("name", name);
064        p.addContent(value);
065        return p;
066    }
067
068    @Override
069    protected void getInstance() {
070        if (adapter == null ) {
071           adapter = new SerialDriverAdapter();
072        }
073    }
074
075    @Override
076    protected void unpackElement(Element shared, Element perNode) {
077        List<Element> l = shared.getChildren("node");
078        for (int i = 0; i < l.size(); i++) {
079            Element n = l.get(i);
080            int addr = Integer.parseInt(n.getAttributeValue("name"));
081            int delay = Integer.parseInt(findParmValue(n, "transmissiondelay"));
082            int senddelay = Integer.parseInt(findParmValue(n, "senddelay"));
083            int numinput = Integer.parseInt(findParmValue(n, "inputbits"));
084            int numoutput = Integer.parseInt(findParmValue(n, "outputbits"));
085//   int pulseWidth = 500;
086//   if ((findParmValue(n,"pulsewidth")) != null) {
087//    pulseWidth = Integer.parseInt(findParmValue(n,"pulsewidth"));
088//   }
089
090            // create node (they register themselves)
091            SerialNode node = new SerialNode(addr, 0, ((MapleSystemConnectionMemo)adapter.getSystemConnectionMemo()).getTrafficController());
092            InputBits.setTimeoutTime(delay);
093            InputBits.setNumInputBits(numinput);
094            OutputBits.setSendDelay(senddelay);
095            OutputBits.setNumOutputBits(numoutput);
096//   node.setPulseWidth(pulseWidth);
097
098            // Trigger initialization of this Node to reflect these parameters
099            ((MapleSystemConnectionMemo)adapter.getSystemConnectionMemo()).getTrafficController().initializeSerialNode(node);
100        }
101    }
102
103    @Override
104    protected void register() {
105        this.register(new ConnectionConfig(adapter));
106    }
107
108    @Override
109    protected void getInstance(Object object) {
110        adapter = ((ConnectionConfig) object).getAdapter();
111    }
112
113}