001package jmri.jmrit.logixng.actions.configurexml; 002 003import java.util.ArrayList; 004import java.util.List; 005import java.util.Map; 006 007import jmri.InstanceManager; 008import jmri.jmrit.logixng.DigitalActionManager; 009import jmri.jmrit.logixng.MaleSocket; 010import jmri.jmrit.logixng.actions.ActionTimer; 011import jmri.jmrit.logixng.util.TimerUnit; 012 013import org.jdom2.Element; 014 015/** 016 * Handle XML configuration for ActionLightXml objects. 017 * 018 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010 019 * @author Daniel Bergqvist Copyright (C) 2019 020 */ 021public class ActionTimerXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML { 022 023 public ActionTimerXml() { 024 } 025 026 /** 027 * Default implementation for storing the contents of a SE8cSignalHead 028 * 029 * @param o Object to store, of type TripleLightSignalHead 030 * @return Element containing the complete info 031 */ 032 @Override 033 public Element store(Object o) { 034 ActionTimer p = (ActionTimer) o; 035 036 Element element = new Element("ActionTimer"); 037 element.setAttribute("class", this.getClass().getName()); 038 element.addContent(new Element("systemName").addContent(p.getSystemName())); 039 040 storeCommon(p, element); 041 042 Element e2 = new Element("StartSocket"); 043 e2.addContent(new Element("socketName").addContent(p.getChild(0).getName())); 044 MaleSocket socket = p.getStartExpressionSocket().getConnectedSocket(); 045 String socketSystemName; 046 if (socket != null) { 047 socketSystemName = socket.getSystemName(); 048 } else { 049 socketSystemName = p.getStartExpressionSocketSystemName(); 050 } 051 if (socketSystemName != null) { 052 e2.addContent(new Element("systemName").addContent(socketSystemName)); 053 } 054 element.addContent(e2); 055 056 e2 = new Element("StopSocket"); 057 e2.addContent(new Element("socketName").addContent(p.getChild(1).getName())); 058 socket = p.getStopExpressionSocket().getConnectedSocket(); 059 if (socket != null) { 060 socketSystemName = socket.getSystemName(); 061 } else { 062 socketSystemName = p.getStopExpressionSocketSystemName(); 063 } 064 if (socketSystemName != null) { 065 e2.addContent(new Element("systemName").addContent(socketSystemName)); 066 } 067 element.addContent(e2); 068 069 Element e = new Element("Actions"); 070 for (int i=0; i < p.getNumActions(); i++) { 071 e2 = new Element("Socket"); 072 e2.addContent(new Element("delay").addContent(Integer.toString(p.getDelay(i)))); 073 e2.addContent(new Element("socketName").addContent(p.getActionSocket(i).getName())); 074 socket = p.getActionSocket(i).getConnectedSocket(); 075 if (socket != null) { 076 socketSystemName = socket.getSystemName(); 077 } else { 078 socketSystemName = p.getActionSocketSystemName(i); 079 } 080 if (socketSystemName != null) { 081 e2.addContent(new Element("systemName").addContent(socketSystemName)); 082 } 083 e.addContent(e2); 084 } 085 element.addContent(e); 086 087 element.addContent(new Element("startImmediately").addContent(p.isStartImmediately() ? "yes" : "no")); 088 element.addContent(new Element("runContinuously").addContent(p.isRunContinuously() ? "yes" : "no")); 089 element.addContent(new Element("startAndStopByStartExpression") 090 .addContent(p.isStartAndStopByStartExpression()? "yes" : "no")); 091 element.addContent(new Element("unit").addContent(p.getUnit().name())); 092 element.addContent(new Element("delayByLocalVariables").addContent(p.isDelayByLocalVariables() ? "yes" : "no")); 093 element.addContent(new Element("delayLocalVariablePrefix").addContent(p.getDelayLocalVariablePrefix())); 094 095 return element; 096 } 097 098 @Override 099 public boolean load(Element shared, Element perNode) { 100 List<Map.Entry<String, String>> expressionSystemNames = new ArrayList<>(); 101 102 Element socketNameElement = shared.getChild("StartSocket").getChild("socketName"); 103 String startSocketName = socketNameElement.getTextTrim(); 104 Element socketSystemNameElement = shared.getChild("StartSocket").getChild("systemName"); 105 String startSocketSystemName = null; 106 if (socketSystemNameElement != null) { 107 startSocketSystemName = socketSystemNameElement.getTextTrim(); 108 } 109 110 socketNameElement = shared.getChild("StopSocket").getChild("socketName"); 111 String stopSocketName = socketNameElement.getTextTrim(); 112 socketSystemNameElement = shared.getChild("StopSocket").getChild("systemName"); 113 String stopSocketSystemName = null; 114 if (socketSystemNameElement != null) { 115 stopSocketSystemName = socketSystemNameElement.getTextTrim(); 116 } 117 118 List<ActionTimer.ActionData> actionDataList = new ArrayList<>(); 119 120 Element actionElement = shared.getChild("Actions"); 121 for (Element socketElement : actionElement.getChildren()) { 122 String socketName = socketElement.getChild("socketName").getTextTrim(); 123 Element systemNameElement = socketElement.getChild("systemName"); 124 String systemName = null; 125 if (systemNameElement != null) { 126 systemName = systemNameElement.getTextTrim(); 127 } 128 Element delayElement = socketElement.getChild("delay"); 129 int delay = 0; 130 if (delayElement != null) { 131 delay = Integer.parseInt(delayElement.getText()); 132 } 133 actionDataList.add(new ActionTimer.ActionData(delay, socketName, systemName)); 134 } 135 136 String sys = getSystemName(shared); 137 String uname = getUserName(shared); 138 ActionTimer h = new ActionTimer(sys, uname, expressionSystemNames, actionDataList); 139 140 loadCommon(h, shared); 141 142 h.getChild(0).setName(startSocketName); 143 h.setStartExpressionSocketSystemName(startSocketSystemName); 144 145 h.getChild(1).setName(stopSocketName); 146 h.setStopExpressionSocketSystemName(stopSocketSystemName); 147 148 Element startImmediately = shared.getChild("startImmediately"); 149 if (startImmediately != null) { 150 h.setStartImmediately("yes".equals(startImmediately.getTextTrim())); 151 } else { 152 h.setStartImmediately(false); 153 } 154 155 Element runContinuously = shared.getChild("runContinuously"); 156 if (runContinuously != null) { 157 h.setRunContinuously("yes".equals(runContinuously.getTextTrim())); 158 } else { 159 h.setRunContinuously(false); 160 } 161 162 Element startAndStopByStartExpression = shared.getChild("startAndStopByStartExpression"); 163 if (startAndStopByStartExpression != null) { 164 h.setStartAndStopByStartExpression("yes".equals(startAndStopByStartExpression.getTextTrim())); 165 } else { 166 h.setStartAndStopByStartExpression(false); 167 } 168 169 Element unit = shared.getChild("unit"); 170 if (unit != null) { 171 h.setUnit(TimerUnit.valueOf(unit.getTextTrim())); 172 } 173 174 Element delayByLocalVariables = shared.getChild("delayByLocalVariables"); 175 if (delayByLocalVariables != null) { 176 h.setDelayByLocalVariables("yes".equals(delayByLocalVariables.getTextTrim())); 177 } else { 178 h.setDelayByLocalVariables(false); 179 } 180 181 Element delayLocalVariablePrefix = shared.getChild("delayLocalVariablePrefix"); 182 if (delayLocalVariablePrefix != null) { 183 h.setDelayLocalVariablePrefix(delayLocalVariablePrefix.getTextTrim()); 184 } else { 185 h.setDelayLocalVariablePrefix(""); 186 } 187 188 InstanceManager.getDefault(DigitalActionManager.class).registerAction(h); 189 return true; 190 } 191 192// private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionTimerXml.class); 193}