001package jmri.jmrit.logixng.actions.configurexml;
002
003import jmri.InstanceManager;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.*;
006import jmri.jmrit.logixng.actions.ExecuteDelayed;
007import jmri.jmrit.logixng.util.TimerUnit;
008import jmri.jmrit.logixng.util.parser.ParserException;
009
010import org.jdom2.Element;
011
012/**
013 * Handle XML configuration for ExecuteDelayed objects.
014 *
015 * @author Bob Jacobsen      Copyright: Copyright (c) 2004, 2008, 2010
016 * @author Daniel Bergqvist  Copyright (C) 2021
017 */
018public class ExecuteDelayedXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
019
020    public ExecuteDelayedXml() {
021    }
022    
023    /**
024     * Default implementation for storing the contents of a SE8cSignalHead
025     *
026     * @param o Object to store, of type TripleLightSignalHead
027     * @return Element containing the complete info
028     */
029    @Override
030    public Element store(Object o) {
031        ExecuteDelayed p = (ExecuteDelayed) o;
032
033        Element element = new Element("ExecuteDelayed");
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("Socket");
040        e2.addContent(new Element("socketName").addContent(p.getChild(0).getName()));
041        MaleSocket socket = p.getSocket().getConnectedSocket();
042        String socketSystemName;
043        if (socket != null) {
044            socketSystemName = socket.getSystemName();
045        } else {
046            socketSystemName = p.getSocketSystemName();
047        }
048        if (socketSystemName != null) {
049            e2.addContent(new Element("systemName").addContent(socketSystemName));
050        }
051        element.addContent(e2);
052        
053        element.addContent(new Element("delayAddressing").addContent(p.getDelayAddressing().name()));
054        element.addContent(new Element("delay").addContent(Integer.toString(p.getDelay())));
055        element.addContent(new Element("delayReference").addContent(p.getDelayReference()));
056        element.addContent(new Element("delayLocalVariable").addContent(p.getDelayLocalVariable()));
057        element.addContent(new Element("delayFormula").addContent(p.getDelayFormula()));
058
059        element.addContent(new Element("unit").addContent(p.getUnit().name()));
060        element.addContent(new Element("resetIfAlreadyStarted").addContent(p.getResetIfAlreadyStarted() ? "yes" : "no"));  // NOI18N
061        
062        if (p.getUseIndividualTimers()) {   // Only set this element if true, to keep backward compability if not used
063            element.addContent(new Element("useIndividualTimers").addContent(p.getUseIndividualTimers() ? "yes" : "no"));  // NOI18N
064        }
065        
066        return element;
067    }
068    
069    @Override
070    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
071        
072        Element socketNameElement = shared.getChild("Socket").getChild("socketName");
073        String socketName = socketNameElement.getTextTrim();
074        Element socketSystemNameElement = shared.getChild("Socket").getChild("systemName");
075        String socketSystemName = null;
076        if (socketSystemNameElement != null) {
077            socketSystemName = socketSystemNameElement.getTextTrim();
078        }
079        
080        Element delayElement = shared.getChild("delay");
081        int delay = 0;
082        if (delayElement != null) {
083            delay = Integer.parseInt(delayElement.getText());
084        }
085        
086        String sys = getSystemName(shared);
087        String uname = getUserName(shared);
088        ExecuteDelayed h = new ExecuteDelayed(sys, uname);
089        
090        loadCommon(h, shared);
091        
092        h.getChild(0).setName(socketName);
093        h.setSocketSystemName(socketSystemName);
094        h.setDelay(delay);
095        
096        Element unit = shared.getChild("unit");
097        if (unit != null) {
098            h.setUnit(TimerUnit.valueOf(unit.getTextTrim()));
099        }
100        
101        String resetIfAlreadyStarted = "no";
102        if (shared.getChild("resetIfAlreadyStarted") != null) {  // NOI18N
103            resetIfAlreadyStarted = shared.getChild("resetIfAlreadyStarted").getTextTrim();  // NOI18N
104        }
105        h.setResetIfAlreadyStarted("yes".equals(resetIfAlreadyStarted));
106        
107        String useIndividualTimers = "no";
108        if (shared.getChild("useIndividualTimers") != null) {  // NOI18N
109            useIndividualTimers = shared.getChild("useIndividualTimers").getTextTrim();  // NOI18N
110        }
111        h.setUseIndividualTimers("yes".equals(useIndividualTimers));
112        
113        try {
114            Element elem = shared.getChild("delayAddressing");
115            if (elem != null) {
116                h.setDelayAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
117            }
118            
119            elem = shared.getChild("delayReference");
120            if (elem != null) h.setDelayReference(elem.getTextTrim());
121            
122            elem = shared.getChild("delayLocalVariable");
123            if (elem != null) h.setDelayLocalVariable(elem.getTextTrim());
124            
125            elem = shared.getChild("delayFormula");
126            if (elem != null) h.setDelayFormula(elem.getTextTrim());
127            
128        } catch (ParserException e) {
129            throw new JmriConfigureXmlException(e);
130        }
131
132        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
133        return true;
134    }
135    
136//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExecuteDelayedXml.class);
137}