001package jmri.jmrit.logixng.expressions.configurexml;
002
003import jmri.InstanceManager;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.*;
006import jmri.jmrit.logixng.expressions.Timer;
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 Timer objects.
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
016 * @author Daniel Bergqvist Copyright (C) 2023
017 */
018public class TimerXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
019
020    public TimerXml() {
021    }
022
023    /**
024     * Default implementation for storing the contents of a ActionMany
025     *
026     * @param o Object to store, of type ActionMany
027     * @return Element containing the complete info
028     */
029    @Override
030    public Element store(Object o) {
031        Timer p = (Timer) o;
032
033        Element element = new Element("Timer");
034        element.setAttribute("class", this.getClass().getName());
035        element.addContent(new Element("systemName").addContent(p.getSystemName()));
036
037        storeCommon(p, element);
038
039        element.addContent(new Element("delayAddressing").addContent(p.getDelayAddressing().name()));
040        element.addContent(new Element("delay").addContent(Integer.toString(p.getDelay())));
041        element.addContent(new Element("delayReference").addContent(p.getDelayReference()));
042        element.addContent(new Element("delayLocalVariable").addContent(p.getDelayLocalVariable()));
043        element.addContent(new Element("delayFormula").addContent(p.getDelayFormula()));
044
045        element.addContent(new Element("unit").addContent(p.getUnit().name()));
046
047        return element;
048    }
049
050    @Override
051    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
052        String sys = getSystemName(shared);
053        String uname = getUserName(shared);
054        Timer h = new Timer(sys, uname);
055
056        loadCommon(h, shared);
057
058        Element delayElement = shared.getChild("delay");
059        int delay = 0;
060        if (delayElement != null) {
061            delay = Integer.parseInt(delayElement.getText());
062        }
063        h.setDelay(delay);
064
065        Element unit = shared.getChild("unit");
066        if (unit != null) {
067            h.setUnit(TimerUnit.valueOf(unit.getTextTrim()));
068        }
069
070        try {
071            Element elem = shared.getChild("delayAddressing");
072            if (elem != null) {
073                h.setDelayAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
074            }
075
076            elem = shared.getChild("delayReference");
077            if (elem != null) h.setDelayReference(elem.getTextTrim());
078
079            elem = shared.getChild("delayLocalVariable");
080            if (elem != null) h.setDelayLocalVariable(elem.getTextTrim());
081
082            elem = shared.getChild("delayFormula");
083            if (elem != null) h.setDelayFormula(elem.getTextTrim());
084
085        } catch (ParserException e) {
086            throw new JmriConfigureXmlException(e);
087        }
088
089        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
090        return true;
091    }
092
093//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TimerXml.class);
094}