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