001package jmri.implementation.configurexml; 002 003import java.util.List; 004import jmri.InstanceManager; 005import jmri.NamedBeanHandle; 006import jmri.SignalHead; 007import jmri.Turnout; 008import jmri.implementation.LsDecSignalHead; 009import org.jdom2.Element; 010import org.slf4j.Logger; 011import org.slf4j.LoggerFactory; 012 013/** 014 * Handle XML configuration for LsDecSignalHead objects. 015 * 016 * This file is part of JMRI. 017 * 018 * JMRI is free software; you can redistribute it and/or modify it under the 019 * terms of version 2 of the GNU General Public License as published by the Free 020 * Software Foundation. See the "COPYING" file for a copy of this license. 021 * 022 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY 023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 024 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 025 * 026 * @author Bob Jacobsen Copyright: Copyright (c) 2003, 2008 027 * @author Petr Koud'a Copyright: Copyright (c) 2007 028 */ 029public class LsDecSignalHeadXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML { 030 031 public LsDecSignalHeadXml() { 032 } 033 034 /** 035 * Default implementation for storing the contents of a LsDecSignalHead. 036 * 037 * @param o Object to store, of type LsDecSignalHead 038 * @return Element containing the complete info 039 */ 040 @Override 041 public Element store(Object o) { 042 LsDecSignalHead p = (LsDecSignalHead) o; 043 044 Element element = new Element("signalhead"); 045 element.setAttribute("class", this.getClass().getName()); 046 047 element.addContent(new Element("systemName").addContent(p.getSystemName())); 048 049 storeCommon(p, element); 050 051 element.addContent(addTurnoutElement(p.getGreen().getName(), p.getGreenState())); 052 element.addContent(addTurnoutElement(p.getYellow().getName(), p.getYellowState())); 053 element.addContent(addTurnoutElement(p.getRed().getName(), p.getRedState())); 054 element.addContent(addTurnoutElement(p.getFlashGreen().getName(), p.getFlashGreenState())); 055 element.addContent(addTurnoutElement(p.getFlashYellow().getName(), p.getFlashYellowState())); 056 element.addContent(addTurnoutElement(p.getFlashRed().getName(), p.getFlashRedState())); 057 element.addContent(addTurnoutElement(p.getDark().getName(), p.getDarkState())); 058 059 return element; 060 } 061 062 Element addTurnoutElement(String name, int s) { 063 int state = s; 064 065 Element el = new Element("turnout"); 066 el.setAttribute("systemName", name); 067 if (state == Turnout.THROWN) { 068 el.setAttribute("state", "THROWN"); 069 } else { 070 el.setAttribute("state", "CLOSED"); 071 } 072 073 return el; 074 } 075 076 @Override 077 public boolean load(Element shared, Element perNode) { 078 List<Element> l = shared.getChildren("turnout"); 079 NamedBeanHandle<Turnout> green = loadTurnout(l.get(0)); 080 NamedBeanHandle<Turnout> yellow = loadTurnout(l.get(1)); 081 NamedBeanHandle<Turnout> red = loadTurnout(l.get(2)); 082 NamedBeanHandle<Turnout> flashgreen = loadTurnout(l.get(3)); 083 NamedBeanHandle<Turnout> flashyellow = loadTurnout(l.get(4)); 084 NamedBeanHandle<Turnout> flashred = loadTurnout(l.get(5)); 085 NamedBeanHandle<Turnout> dark = loadTurnout(l.get(6)); 086 int greenstatus = loadTurnoutStatus(l.get(0)); 087 int yellowstatus = loadTurnoutStatus(l.get(1)); 088 int redstatus = loadTurnoutStatus(l.get(2)); 089 int flashgreenstatus = loadTurnoutStatus(l.get(3)); 090 int flashyellowstatus = loadTurnoutStatus(l.get(4)); 091 int flashredstatus = loadTurnoutStatus(l.get(5)); 092 int darkstatus = loadTurnoutStatus(l.get(6)); 093 094 // put it together 095 String sys = getSystemName(shared); 096 String uname = getUserName(shared); 097 SignalHead h; 098 if (uname == null) { 099 h = new LsDecSignalHead(sys, green, greenstatus, yellow, yellowstatus, red, redstatus, flashgreen, flashgreenstatus, flashyellow, flashyellowstatus, flashred, flashredstatus, dark, darkstatus); 100 } else { 101 h = new LsDecSignalHead(sys, uname, green, greenstatus, yellow, yellowstatus, red, redstatus, flashgreen, flashgreenstatus, flashyellow, flashyellowstatus, flashred, flashredstatus, dark, darkstatus); 102 } 103 104 loadCommon(h, shared); 105 106 SignalHead existingBean = 107 InstanceManager.getDefault(jmri.SignalHeadManager.class) 108 .getBySystemName(sys); 109 110 if ((existingBean != null) && (existingBean != h)) { 111 log.error("systemName is already registered: {}", sys); 112 } else { 113 InstanceManager.getDefault(jmri.SignalHeadManager.class).register(h); 114 } 115 116 return true; 117 } 118 119 NamedBeanHandle<Turnout> loadTurnout(Object o) { 120 Element e = (Element) o; 121 String name = e.getAttribute("systemName").getValue(); 122 Turnout t = InstanceManager.turnoutManagerInstance().getTurnout(name); 123 if (t != null) { 124 return jmri.InstanceManager.getDefault(jmri.NamedBeanHandleManager.class).getNamedBeanHandle(name, t); 125 } else { 126 log.warn("Failed to find turnout {}. Check connection and configuration", name); 127 return null; 128 } 129 } 130 131 int loadTurnoutStatus(Object o) { 132 Element e = (Element) o; 133 String rState = e.getAttribute("state").getValue(); 134 int tSetState = Turnout.CLOSED; 135 if (rState.equals("THROWN")) { 136 tSetState = Turnout.THROWN; 137 } 138 return tSetState; 139 } 140 141 @Override 142 public void load(Element element, Object o) { 143 log.error("Invalid method called"); 144 } 145 146 private final static Logger log = LoggerFactory.getLogger(LsDecSignalHeadXml.class); 147 148}