use of jmri.configurexml.TurnoutOperationManagerXml in project JMRI by JMRI.
the class AbstractTurnoutManagerConfigXML method loadTurnouts.
/**
* Utility method to load the individual Turnout objects. If there's no
* additional info needed for a specific turnout type, invoke this with the
* parent of the set of Turnout elements.
*
* @param shared Element containing the Turnout elements to load.
* @param perNode Element containing per-node Turnout data.
* @return true if succeeded
*/
@SuppressWarnings("unchecked")
public boolean loadTurnouts(Element shared, Element perNode) {
boolean result = true;
List<Element> operationList = shared.getChildren("operations");
if (operationList.size() > 1) {
log.warn("unexpected extra elements found in turnout operations list");
result = false;
}
if (operationList.size() > 0) {
TurnoutOperationManagerXml tomx = new TurnoutOperationManagerXml();
tomx.load(operationList.get(0), null);
}
List<Element> turnoutList = shared.getChildren("turnout");
if (log.isDebugEnabled()) {
log.debug("Found " + turnoutList.size() + " turnouts");
}
TurnoutManager tm = InstanceManager.turnoutManagerInstance();
try {
if (shared.getChild("defaultclosedspeed") != null) {
String closedSpeed = shared.getChild("defaultclosedspeed").getText();
if (closedSpeed != null && !closedSpeed.equals("")) {
tm.setDefaultClosedSpeed(closedSpeed);
}
}
} catch (jmri.JmriException ex) {
log.error(ex.toString());
}
try {
if (shared.getChild("defaultthrownspeed") != null) {
String thrownSpeed = shared.getChild("defaultthrownspeed").getText();
if (thrownSpeed != null && !thrownSpeed.equals("")) {
tm.setDefaultThrownSpeed(thrownSpeed);
}
}
} catch (jmri.JmriException ex) {
log.error(ex.toString());
}
for (int i = 0; i < turnoutList.size(); i++) {
Element elem = turnoutList.get(i);
String sysName = getSystemName(elem);
if (sysName == null) {
log.error("unexpected null in systemName " + elem);
result = false;
break;
}
String userName = getUserName(elem);
checkNameNormalization(sysName, userName, tm);
if (log.isDebugEnabled()) {
log.debug("create turnout: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
}
Turnout t = tm.getBySystemName(sysName);
if (t == null) {
t = tm.newTurnout(sysName, userName);
//Nothing is logged in the console window as the newTurnoutFunction already does this.
} else if (userName != null) {
t.setUserName(userName);
}
// Load common parts
loadCommon(t, elem);
// now add feedback if needed
Attribute a;
a = elem.getAttribute("feedback");
if (a != null) {
try {
t.setFeedbackMode(a.getValue());
} catch (IllegalArgumentException e) {
log.error("Can not set feedback mode: '" + a.getValue() + "' for turnout: '" + sysName + "' user name: '" + (userName == null ? "" : userName) + "'");
result = false;
}
}
a = elem.getAttribute("sensor1");
if (a != null) {
try {
t.provideFirstFeedbackSensor(a.getValue());
} catch (jmri.JmriException e) {
result = false;
}
}
a = elem.getAttribute("sensor2");
if (a != null) {
try {
t.provideSecondFeedbackSensor(a.getValue());
} catch (jmri.JmriException e) {
result = false;
}
}
// check for turnout inverted
t.setInverted(getAttributeBool(elem, "inverted", false));
// check for turnout decoder
a = turnoutList.get(i).getAttribute("decoder");
if (a != null) {
t.setDecoderName(a.getValue());
}
// check for turnout lock mode
a = turnoutList.get(i).getAttribute("lockMode");
if (a != null) {
if (a.getValue().equals("both")) {
t.enableLockOperation(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, true);
}
if (a.getValue().equals("cab")) {
t.enableLockOperation(Turnout.CABLOCKOUT, true);
t.enableLockOperation(Turnout.PUSHBUTTONLOCKOUT, false);
}
if (a.getValue().equals("pushbutton")) {
t.enableLockOperation(Turnout.PUSHBUTTONLOCKOUT, true);
t.enableLockOperation(Turnout.CABLOCKOUT, false);
}
}
// check for turnout locked
a = turnoutList.get(i).getAttribute("locked");
if (a != null) {
t.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, a.getValue().equals("true"));
}
// number of bits, if present - if not, defaults to 1
a = turnoutList.get(i).getAttribute("numBits");
if (a == null) {
t.setNumberOutputBits(1);
} else {
int iNum = Integer.parseInt(a.getValue());
if ((iNum == 1) || (iNum == 2)) {
t.setNumberOutputBits(iNum);
} else {
log.warn("illegal number of output bits for control of turnout " + sysName);
t.setNumberOutputBits(1);
result = false;
}
}
// control type, if present - if not, defaults to 0
a = turnoutList.get(i).getAttribute("controlType");
if (a == null) {
t.setControlType(0);
} else {
int iType = Integer.parseInt(a.getValue());
if (iType >= 0) {
t.setControlType(iType);
} else {
log.warn("illegal control type for control of turnout " + sysName);
t.setControlType(0);
result = false;
}
}
// operation stuff
List<Element> myOpList = turnoutList.get(i).getChildren("operation");
if (myOpList.size() > 0) {
if (myOpList.size() > 1) {
log.warn("unexpected extra elements found in turnout-specific operations");
result = false;
}
TurnoutOperation toper = TurnoutOperationXml.loadOperation(myOpList.get(0));
t.setTurnoutOperation(toper);
} else {
a = turnoutList.get(i).getAttribute("automate");
if (a != null) {
String str = a.getValue();
if (str.equals("Off")) {
t.setInhibitOperation(true);
} else if (!str.equals("Default")) {
t.setInhibitOperation(false);
TurnoutOperation toper = TurnoutOperationManager.getInstance().getOperation(str);
t.setTurnoutOperation(toper);
} else {
t.setInhibitOperation(false);
}
}
}
// set initial state from sensor feedback if appropriate
t.setInitialKnownStateFromFeedback();
try {
t.setDivergingSpeed("Global");
if (elem.getChild("divergingSpeed") != null) {
String speed = elem.getChild("divergingSpeed").getText();
if (speed != null && !speed.equals("") && !speed.contains("Global")) {
t.setDivergingSpeed(speed);
}
}
} catch (jmri.JmriException ex) {
log.error(ex.toString());
}
try {
t.setStraightSpeed("Global");
if (elem.getChild("straightSpeed") != null) {
String speed = elem.getChild("straightSpeed").getText();
if (speed != null && !speed.equals("") && !speed.contains("Global")) {
t.setStraightSpeed(speed);
}
}
} catch (jmri.JmriException ex) {
log.error(ex.toString());
}
}
return result;
}
use of jmri.configurexml.TurnoutOperationManagerXml in project JMRI by JMRI.
the class AbstractTurnoutManagerConfigXML method store.
/**
* Default implementation for storing the contents of a TurnoutManager and
* associated TurnoutOperation's
*
* @param o Object to store, of type TurnoutManager
* @return Element containing the complete info
*/
@Override
public Element store(Object o) {
Element turnouts = new Element("turnouts");
setStoreElementClass(turnouts);
TurnoutManager tm = (TurnoutManager) o;
if (tm != null) {
TurnoutOperationManagerXml tomx = new TurnoutOperationManagerXml();
Element opElem = tomx.store(TurnoutOperationManager.getInstance());
turnouts.addContent(opElem);
java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
// don't return an element if there are not turnouts to include
if (!iter.hasNext()) {
return null;
}
String defaultclosed = tm.getDefaultClosedSpeed();
String defaultthrown = tm.getDefaultThrownSpeed();
turnouts.addContent(new Element("defaultclosedspeed").addContent(defaultclosed));
turnouts.addContent(new Element("defaultthrownspeed").addContent(defaultthrown));
// store the turnouts
while (iter.hasNext()) {
String sname = iter.next();
log.debug("system name is " + sname);
Turnout t = tm.getBySystemName(sname);
Element elem = new Element("turnout");
elem.addContent(new Element("systemName").addContent(sname));
log.debug("store turnout " + sname);
storeCommon(t, elem);
// include feedback info
elem.setAttribute("feedback", t.getFeedbackModeName());
NamedBeanHandle<Sensor> s;
s = t.getFirstNamedSensor();
if (s != null) {
elem.setAttribute("sensor1", s.getName());
}
s = t.getSecondNamedSensor();
if (s != null) {
elem.setAttribute("sensor2", s.getName());
}
// include turnout inverted
elem.setAttribute("inverted", t.getInverted() ? "true" : "false");
if (t.canLock(Turnout.CABLOCKOUT | Turnout.PUSHBUTTONLOCKOUT)) {
// include turnout locked
elem.setAttribute("locked", t.getLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT) ? "true" : "false");
// include turnout lock mode
String lockOpr;
if (t.canLock(Turnout.CABLOCKOUT) && t.canLock(Turnout.PUSHBUTTONLOCKOUT)) {
lockOpr = "both";
} else if (t.canLock(Turnout.CABLOCKOUT)) {
lockOpr = "cab";
} else if (t.canLock(Turnout.PUSHBUTTONLOCKOUT)) {
lockOpr = "pushbutton";
} else {
lockOpr = "none";
}
elem.setAttribute("lockMode", lockOpr);
// include turnout decoder
elem.setAttribute("decoder", t.getDecoderName());
}
// include number of control bits, if different from one
int iNum = t.getNumberOutputBits();
if (iNum != 1) {
elem.setAttribute("numBits", "" + iNum);
}
// include turnout control type, if different from 0
int iType = t.getControlType();
if (iType != 0) {
elem.setAttribute("controlType", "" + iType);
}
// add operation stuff
String opstr = null;
TurnoutOperation op = t.getTurnoutOperation();
if (t.getInhibitOperation()) {
opstr = "Off";
} else if (op == null) {
opstr = "Default";
} else if (op.isNonce()) {
// nonce operation appears as subelement
TurnoutOperationXml adapter = TurnoutOperationXml.getAdapter(op);
if (adapter != null) {
Element nonceOpElem = adapter.store(op);
elem.addContent(nonceOpElem);
}
} else {
opstr = op.getName();
}
if (opstr != null) {
elem.setAttribute("automate", opstr);
}
if ((t.getDivergingSpeed() != null) && (!t.getDivergingSpeed().equals("")) && !t.getDivergingSpeed().contains("Global")) {
elem.addContent(new Element("divergingSpeed").addContent(t.getDivergingSpeed()));
}
if ((t.getStraightSpeed() != null) && (!t.getStraightSpeed().equals("")) && !t.getStraightSpeed().contains("Global")) {
elem.addContent(new Element("straightSpeed").addContent(t.getStraightSpeed()));
}
// add element
turnouts.addContent(elem);
}
}
return turnouts;
}
Aggregations