001package jmri.jmrix.ieee802154.serialdriver.configurexml;
002
003import java.util.List;
004import jmri.jmrix.configurexml.AbstractSerialConnectionConfigXml;
005import jmri.jmrix.ieee802154.serialdriver.ConnectionConfig;
006import jmri.jmrix.ieee802154.serialdriver.SerialDriverAdapter;
007import jmri.jmrix.ieee802154.serialdriver.SerialNode;
008import jmri.jmrix.ieee802154.serialdriver.SerialSystemConnectionMemo;
009import jmri.jmrix.ieee802154.serialdriver.SerialTrafficController;
010import org.jdom2.Element;
011
012/**
013 * Handle XML persistance of layout connections by persisting the
014 * SerialDriverAdapter (and connections). Note this is named as the XML version
015 * of a ConnectionConfig object, but it's actually persisting the
016 * SerialDriverAdapter.
017 * <p>
018 * This class is invoked from jmrix.JmrixConfigPaneXml on write, as that class
019 * is the one actually registered. Reads are brought here directly via the class
020 * attribute in the XML.
021 *
022 * @author Bob Jacobsen Copyright: Copyright (c) 2003, 2006, 2007, 2008
023 */
024public class ConnectionConfigXml extends AbstractSerialConnectionConfigXml {
025
026    public ConnectionConfigXml() {
027        super();
028    }
029
030    /**
031     * Write out the SerialNode objects too
032     *
033     * @param e Element being extended
034     */
035    @Override
036    protected void extendElement(Element e) {
037        SerialSystemConnectionMemo scm;
038        try {
039            scm = (SerialSystemConnectionMemo) adapter.getSystemConnectionMemo();
040        } catch (NullPointerException npe) {
041            // The adapter doesn't have a memo, so no nodes can be defined.
042            return;
043        }
044        SerialTrafficController stc = (SerialTrafficController) scm.getTrafficController();
045        SerialNode node = (SerialNode) stc.getNode(0);
046        int index = 1;
047        while (node != null) {
048            // add node as an element
049            Element n = new Element("node");
050            n.setAttribute("name", "" + node.getNodeAddress());
051            e.addContent(n);
052            // add parameters to the node as needed
053            n.addContent(makeParameter("PAN", ""
054                    + jmri.util.StringUtil.hexStringFromBytes(node.getPANAddress())));
055            n.addContent(makeParameter("address", ""
056                    + jmri.util.StringUtil.hexStringFromBytes(node.getUserAddress())));
057            n.addContent(makeParameter("GUID", ""
058                    + jmri.util.StringUtil.hexStringFromBytes(node.getGlobalAddress())));
059
060            // look for the next node
061            node = (SerialNode) stc.getNode(index);
062            index++;
063        }
064    }
065
066    protected Element makeParameter(String name, String value) {
067        Element p = new Element("parameter");
068        p.setAttribute("name", name);
069        p.addContent(value);
070        return p;
071    }
072
073    @Override
074    protected void getInstance() {
075        adapter = new SerialDriverAdapter();
076    }
077
078    @Override
079    protected void getInstance(Object object) {
080        adapter = ((ConnectionConfig) object).getAdapter();
081    }
082
083    @Override
084    protected void unpackElement(Element shared, Element perNode) {
085        List<Element> l = shared.getChildren("node");
086        for (int i = 0; i < l.size(); i++) {
087            Element n = l.get(i);
088            //int addr = Integer.parseInt(n.getAttributeValue("name"));
089            byte PAN[] = jmri.util.StringUtil.bytesFromHexString(findParmValue(n, "PAN"));
090            byte address[] = jmri.util.StringUtil.bytesFromHexString(findParmValue(n, "address"));
091            byte GUID[] = jmri.util.StringUtil.bytesFromHexString(findParmValue(n, "GUID"));
092
093            // create node (they register themselves)
094            SerialNode node = new SerialNode(PAN, address, GUID);
095
096            // Trigger initialization of this Node to reflect these parameters
097            SerialSystemConnectionMemo scm = (SerialSystemConnectionMemo) adapter.getSystemConnectionMemo();
098            SerialTrafficController stc = (SerialTrafficController) scm.getTrafficController();
099            stc.registerNode(node);
100        }
101    }
102
103    @Override
104    protected void register() {
105        this.register(new ConnectionConfig(adapter));
106    }
107
108}