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