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        if (!p.isStopLocoWhenSwitchingLoco()) {
113            element.addContent(new Element("stopLocoWhenSwitchingLoco")
114                    .addContent("no"));
115        }
116
117        if (p.isWaitForThrottle()) {
118            element.addContent(new Element("waitForThrottle")
119                    .addContent("yes"));
120        }
121
122        return element;
123    }
124
125    @Override
126    public boolean load(Element shared, Element perNode) {
127
128        String sys = getSystemName(shared);
129        String uname = getUserName(shared);
130        ActionThrottle h = new ActionThrottle(sys, uname);
131
132        loadCommon(h, shared);
133
134        Element socketName = shared.getChild("LocoAddressSocket").getChild("socketName");
135        h.getLocoAddressSocket().setName(socketName.getTextTrim());
136        Element socketSystemName = shared.getChild("LocoAddressSocket").getChild("systemName");
137        if (socketSystemName != null) {
138            h.setLocoAddressSocketSystemName(socketSystemName.getTextTrim());
139        }
140
141        socketName = shared.getChild("LocoSpeedSocket").getChild("socketName");
142        h.getLocoSpeedSocket().setName(socketName.getTextTrim());
143        socketSystemName = shared.getChild("LocoSpeedSocket").getChild("systemName");
144        if (socketSystemName != null) {
145            h.setLocoSpeedSocketSystemName(socketSystemName.getTextTrim());
146        }
147
148        socketName = shared.getChild("LocoDirectionSocket").getChild("socketName");
149        h.getLocoDirectionSocket().setName(socketName.getTextTrim());
150        socketSystemName = shared.getChild("LocoDirectionSocket").getChild("systemName");
151        if (socketSystemName != null) {
152            h.setLocoDirectionSocketSystemName(socketSystemName.getTextTrim());
153        }
154
155        Element locoFunctionSocket = shared.getChild("LocoFunctionSocket");
156        if (locoFunctionSocket != null) {
157            socketName = locoFunctionSocket.getChild("socketName");
158            h.getLocoFunctionSocket().setName(socketName.getTextTrim());
159            socketSystemName = locoFunctionSocket.getChild("systemName");
160            if (socketSystemName != null) {
161                h.setLocoFunctionSocketSystemName(socketSystemName.getTextTrim());
162            }
163        }
164
165        Element locoFunctionOnOffSocket = shared.getChild("LocoFunctionOnOffSocket");
166        if (locoFunctionOnOffSocket != null) {
167            socketName = locoFunctionOnOffSocket.getChild("socketName");
168            h.getLocoFunctionOnOffSocket().setName(socketName.getTextTrim());
169            socketSystemName = locoFunctionOnOffSocket.getChild("systemName");
170            if (socketSystemName != null) {
171                h.setLocoFunctionOnOffSocketSystemName(socketSystemName.getTextTrim());
172            }
173        }
174
175        Element systemConnection = shared.getChild("systemConnection");
176        if (systemConnection != null) {
177            String systemConnectionName = systemConnection.getTextTrim();
178            List<SystemConnectionMemo> systemConnections =
179                    jmri.InstanceManager.getList(SystemConnectionMemo.class);
180
181            for (SystemConnectionMemo memo : systemConnections) {
182                if (memo.getSystemPrefix().equals(systemConnectionName)) {
183                    h.setMemo(memo);
184                    break;
185                }
186            }
187        }
188
189        Element stopLocoWhenSwitchingLoco = shared.getChild("stopLocoWhenSwitchingLoco");
190        if (stopLocoWhenSwitchingLoco != null) {
191            h.setStopLocoWhenSwitchingLoco("yes".equals(stopLocoWhenSwitchingLoco.getTextTrim()));
192        }
193
194        Element waitForThrottle = shared.getChild("waitForThrottle");
195        if (waitForThrottle != null) {
196            h.setWaitForThrottle("yes".equals(waitForThrottle.getTextTrim()));
197        }
198
199        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
200        return true;
201    }
202
203//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionThrottleXml.class);
204}