001package jmri.jmrit.logixng.expressions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.*;
006import jmri.jmrit.logixng.expressions.ExpressionSensor;
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 ExpressionSensorXml objects.
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
016 * @author Daniel Bergqvist Copyright (C) 2019
017 */
018public class ExpressionSensorXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
019
020    public ExpressionSensorXml() {
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        ExpressionSensor p = (ExpressionSensor) o;
032
033        Element element = new Element("ExpressionSensor");
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 selectEnumXml = new LogixNG_SelectEnumXml<ExpressionSensor.SensorState>();
041        element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean"));
042        element.addContent(new Element("is_isNot").addContent(p.get_Is_IsNot().name()));
043        element.addContent(selectEnumXml.store(p.getSelectEnum(), "state"));
044
045        return element;
046    }
047
048    @Override
049    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
050        String sys = getSystemName(shared);
051        String uname = getUserName(shared);
052        ExpressionSensor h = new ExpressionSensor(sys, uname);
053
054        loadCommon(h, shared);
055
056        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Sensor>();
057        var selectEnumXml = new LogixNG_SelectEnumXml<ExpressionSensor.SensorState>();
058        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
059        selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "sensor");
060
061        Element is_IsNot = shared.getChild("is_isNot");
062        if (is_IsNot != null) {
063            h.set_Is_IsNot(Is_IsNot_Enum.valueOf(is_IsNot.getTextTrim()));
064        }
065
066        selectEnumXml.load(shared.getChild("state"), h.getSelectEnum());
067        selectEnumXml.loadLegacy(
068                shared, h.getSelectEnum(),
069                "stateAddressing",
070                "sensorState",
071                "stateReference",
072                "stateLocalVariable",
073                "stateFormula");
074
075        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
076        return true;
077    }
078
079//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionSensorXml.class);
080}