001package jmri.jmrit.logixng.expressions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.*;
006import jmri.jmrit.logixng.expressions.ExpressionSensorEdge;
007import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectEnumXml;
008import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
009
010import org.jdom2.Element;
011
012/**
013 * Handle XML configuration for ExpressionSensorEdgeXml objects.
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
016 * @author Daniel Bergqvist Copyright (C) 2022
017 */
018public class ExpressionSensorEdgeXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
019
020    public ExpressionSensorEdgeXml() {
021    }
022
023    /**
024     * Default implementation for storing the contents of a SE8cSignalHead
025     *
026     * @param o Object to store, of type TripleSensorSignalHead
027     * @return Element containing the complete info
028     */
029    @Override
030    public Element store(Object o) {
031        ExpressionSensorEdge p = (ExpressionSensorEdge) o;
032
033        Element element = new Element("ExpressionSensorEdge");
034        element.setAttribute("class", this.getClass().getName());
035        element.addContent(new Element("systemName").addContent(p.getSystemName()));
036
037        storeCommon(p, element);
038
039        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Sensor>();
040        var selectEnumFromStateXml = new LogixNG_SelectEnumXml<ExpressionSensorEdge.SensorState>();
041        var selectEnumToStateXml = new LogixNG_SelectEnumXml<ExpressionSensorEdge.SensorState>();
042        element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean"));
043        element.addContent(selectEnumFromStateXml.store(p.getSelectEnumFromState(), "fromState"));
044        element.addContent(selectEnumToStateXml.store(p.getSelectEnumToState(), "toState"));
045        element.addContent(new Element("onlyTrueOnce").addContent(p.getOnlyTrueOnce() ? "yes" : "no"));
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        ExpressionSensorEdge h = new ExpressionSensorEdge(sys, uname);
055
056        loadCommon(h, shared);
057
058        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Sensor>();
059        var selectEnumFromStateXml = new LogixNG_SelectEnumXml<ExpressionSensorEdge.SensorState>();
060        var selectEnumToStateXml = new LogixNG_SelectEnumXml<ExpressionSensorEdge.SensorState>();
061
062        try {
063            selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
064        } catch (IllegalArgumentException e) {
065            log.error("Error during loading Sensor Edge expression {} due to: {}", h.getDisplayName(), e.getMessage());
066        }
067
068        selectEnumFromStateXml.load(shared.getChild("fromState"), h.getSelectEnumFromState());
069        selectEnumToStateXml.load(shared.getChild("toState"), h.getSelectEnumToState());
070
071        Element onlyTrueOnceElem = shared.getChild("onlyTrueOnce");
072        if (onlyTrueOnceElem != null) {
073            h.setOnlyTrueOnce("yes".equals(onlyTrueOnceElem.getTextTrim()));
074        }
075
076        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
077        return true;
078    }
079
080    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionSensorEdgeXml.class);
081}