001package jmri.jmrit.logixng.actions.configurexml;
002
003import jmri.InstanceManager;
004import jmri.jmrit.logixng.DigitalActionManager;
005import jmri.jmrit.logixng.actions.ActionThrottle;
006
007import org.jdom2.Attribute;
008import org.jdom2.Element;
009
010import jmri.jmrit.logixng.MaleSocket;
011
012/**
013 * Handle XML configuration for ActionLightXml objects.
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
016 * @author Daniel Bergqvist Copyright (C) 2019
017 */
018public class ActionThrottleXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
019
020    public ActionThrottleXml() {
021    }
022
023    /**
024     * Default implementation for storing the contents of a ActionThrottle
025     *
026     * @param o Object to store, of type TripleTurnoutSignalHead
027     * @return Element containing the complete info
028     */
029    @Override
030    public Element store(Object o) {
031        ActionThrottle p = (ActionThrottle) o;
032
033        Element element = new Element("Throttle");
034        element.setAttribute("class", this.getClass().getName());
035        element.addContent(new Element("systemName").addContent(p.getSystemName()));
036        
037        storeCommon(p, element);
038
039        Element e2 = new Element("LocoAddressSocket");
040        e2.addContent(new Element("socketName").addContent(p.getLocoAddressSocket().getName()));
041        MaleSocket socket = p.getLocoAddressSocket().getConnectedSocket();
042        String socketSystemName;
043        if (socket != null) {
044            socketSystemName = socket.getSystemName();
045        } else {
046            socketSystemName = p.getLocoAddressSocketSystemName();
047        }
048        if (socketSystemName != null) {
049            e2.addContent(new Element("systemName").addContent(socketSystemName));
050        }
051        element.addContent(e2);
052
053        e2 = new Element("LocoSpeedSocket");
054        e2.addContent(new Element("socketName").addContent(p.getLocoSpeedSocket().getName()));
055        socket = p.getLocoSpeedSocket().getConnectedSocket();
056        if (socket != null) {
057            socketSystemName = socket.getSystemName();
058        } else {
059            socketSystemName = p.getLocoSpeedSocketSystemName();
060        }
061        if (socketSystemName != null) {
062            e2.addContent(new Element("systemName").addContent(socketSystemName));
063        }
064        element.addContent(e2);
065
066        e2 = new Element("LocoDirectionSocket");
067        e2.addContent(new Element("socketName").addContent(p.getLocoDirectionSocket().getName()));
068        socket = p.getLocoDirectionSocket().getConnectedSocket();
069        if (socket != null) {
070            socketSystemName = socket.getSystemName();
071        } else {
072            socketSystemName = p.getLocoDirectionSocketSystemName();
073        }
074        if (socketSystemName != null) {
075            e2.addContent(new Element("systemName").addContent(socketSystemName));
076        }
077        element.addContent(e2);
078
079        e2 = new Element("LocoFunctionSocket");
080        e2.addContent(new Element("socketName").addContent(p.getLocoFunctionSocket().getName()));
081        socket = p.getLocoFunctionSocket().getConnectedSocket();
082        if (socket != null) {
083            socketSystemName = socket.getSystemName();
084        } else {
085            socketSystemName = p.getLocoFunctionSocketSystemName();
086        }
087        if (socketSystemName != null) {
088            e2.addContent(new Element("systemName").addContent(socketSystemName));
089        }
090        element.addContent(e2);
091
092        e2 = new Element("LocoFunctionOnOffSocket");
093        e2.addContent(new Element("socketName").addContent(p.getLocoFunctionOnOffSocket().getName()));
094        socket = p.getLocoFunctionOnOffSocket().getConnectedSocket();
095        if (socket != null) {
096            socketSystemName = socket.getSystemName();
097        } else {
098            socketSystemName = p.getLocoFunctionOnOffSocketSystemName();
099        }
100        if (socketSystemName != null) {
101            e2.addContent(new Element("systemName").addContent(socketSystemName));
102        }
103        element.addContent(e2);
104
105        return element;
106    }
107    
108    @Override
109    public boolean load(Element shared, Element perNode) {
110        
111        String sys = getSystemName(shared);
112        String uname = getUserName(shared);
113        ActionThrottle h = new ActionThrottle(sys, uname);
114
115        loadCommon(h, shared);
116        
117        Element socketName = shared.getChild("LocoAddressSocket").getChild("socketName");
118        h.getLocoAddressSocket().setName(socketName.getTextTrim());
119        Element socketSystemName = shared.getChild("LocoAddressSocket").getChild("systemName");
120        if (socketSystemName != null) {
121            h.setLocoAddressSocketSystemName(socketSystemName.getTextTrim());
122        }
123        
124        socketName = shared.getChild("LocoSpeedSocket").getChild("socketName");
125        h.getLocoSpeedSocket().setName(socketName.getTextTrim());
126        socketSystemName = shared.getChild("LocoSpeedSocket").getChild("systemName");
127        if (socketSystemName != null) {
128            h.setLocoSpeedSocketSystemName(socketSystemName.getTextTrim());
129        }
130        
131        socketName = shared.getChild("LocoDirectionSocket").getChild("socketName");
132        h.getLocoDirectionSocket().setName(socketName.getTextTrim());
133        socketSystemName = shared.getChild("LocoDirectionSocket").getChild("systemName");
134        if (socketSystemName != null) {
135            h.setLocoDirectionSocketSystemName(socketSystemName.getTextTrim());
136        }
137        
138        Element locoFunctionSocket = shared.getChild("LocoFunctionSocket");
139        if (locoFunctionSocket != null) {
140            socketName = locoFunctionSocket.getChild("socketName");
141            h.getLocoFunctionSocket().setName(socketName.getTextTrim());
142            socketSystemName = locoFunctionSocket.getChild("systemName");
143            if (socketSystemName != null) {
144                h.setLocoFunctionSocketSystemName(socketSystemName.getTextTrim());
145            }
146        }
147        
148        Element locoFunctionOnOffSocket = shared.getChild("LocoFunctionOnOffSocket");
149        if (locoFunctionOnOffSocket != null) {
150            socketName = locoFunctionOnOffSocket.getChild("socketName");
151            h.getLocoFunctionOnOffSocket().setName(socketName.getTextTrim());
152            socketSystemName = locoFunctionOnOffSocket.getChild("systemName");
153            if (socketSystemName != null) {
154                h.setLocoFunctionOnOffSocketSystemName(socketSystemName.getTextTrim());
155            }
156        }
157        
158        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
159        return true;
160    }
161    
162//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionThrottleXml.class);
163}