use of jmri.Turnout in project JMRI by JMRI.
the class SignalHeadTableAction method updatePressed.
@SuppressWarnings("fallthrough")
@SuppressFBWarnings(value = "SF_SWITCH_FALLTHROUGH")
void updatePressed(ActionEvent e) {
String nam = eUserName.getText();
// check if user name changed
String uname = curS.getUserName();
// TODO: not sure this if statement is right. I think (uname != null && !uname.equals(nam))
if (!((uname != null) && (uname.equals(nam)))) {
if (checkUserName(nam)) {
curS.setUserName(nam);
} else {
return;
}
}
// update according to class of signal head
if (className.equals("jmri.implementation.QuadOutputSignalHead")) {
Turnout t1 = updateTurnoutFromPanel(eto1, "SignalHead:" + eSysNameLabel.getText() + ":Green", ((QuadOutputSignalHead) curS).getGreen().getBean(), ev1Border.getTitle());
if (t1 == null) {
return;
} else {
((QuadOutputSignalHead) curS).setGreen(nbhm.getNamedBeanHandle(eto1.getDisplayName(), t1));
}
Turnout t2 = updateTurnoutFromPanel(eto2, "SignalHead:" + eSysNameLabel.getText() + ":Yellow", ((QuadOutputSignalHead) curS).getYellow().getBean(), ev2Border.getTitle());
if (t2 == null) {
return;
} else {
((QuadOutputSignalHead) curS).setYellow(nbhm.getNamedBeanHandle(eto2.getDisplayName(), t2));
}
Turnout t3 = updateTurnoutFromPanel(eto3, "SignalHead:" + eSysNameLabel.getText() + ":Red", ((QuadOutputSignalHead) curS).getRed().getBean(), ev3Border.getTitle());
if (t3 == null) {
return;
} else {
((QuadOutputSignalHead) curS).setRed(nbhm.getNamedBeanHandle(eto3.getDisplayName(), t3));
}
Turnout t4 = updateTurnoutFromPanel(eto4, "SignalHead:" + eSysNameLabel.getText() + ":Lunar", ((QuadOutputSignalHead) curS).getLunar().getBean(), ev4Border.getTitle());
if (t4 == null) {
return;
} else {
((QuadOutputSignalHead) curS).setLunar(nbhm.getNamedBeanHandle(eto4.getDisplayName(), t4));
}
} else if (className.equals("jmri.implementation.TripleTurnoutSignalHead")) {
Turnout t1 = updateTurnoutFromPanel(eto1, "SignalHead:" + eSysNameLabel.getText() + ":Green", ((TripleTurnoutSignalHead) curS).getGreen().getBean(), ev1Border.getTitle());
if (t1 == null) {
return;
} else {
((TripleTurnoutSignalHead) curS).setGreen(nbhm.getNamedBeanHandle(eto1.getDisplayName(), t1));
}
Turnout t2 = updateTurnoutFromPanel(eto2, "SignalHead:" + eSysNameLabel.getText() + ":Yellow", ((TripleTurnoutSignalHead) curS).getYellow().getBean(), ev2Border.getTitle());
if (t2 == null) {
return;
} else {
((TripleTurnoutSignalHead) curS).setYellow(nbhm.getNamedBeanHandle(eto2.getDisplayName(), t2));
}
Turnout t3 = updateTurnoutFromPanel(eto3, "SignalHead:" + eSysNameLabel.getText() + ":Red", ((TripleTurnoutSignalHead) curS).getRed().getBean(), ev3Border.getTitle());
if (t3 == null) {
return;
} else {
((TripleTurnoutSignalHead) curS).setRed(nbhm.getNamedBeanHandle(eto3.getDisplayName(), t3));
}
} else if (className.equals("jmri.implementation.TripleOutputSignalHead")) {
Turnout t1 = updateTurnoutFromPanel(eto1, "SignalHead:" + eSysNameLabel.getText() + ":Green", ((TripleOutputSignalHead) curS).getGreen().getBean(), ev1Border.getTitle());
if (t1 == null) {
return;
} else {
((TripleOutputSignalHead) curS).setGreen(nbhm.getNamedBeanHandle(eto1.getDisplayName(), t1));
}
Turnout t2 = updateTurnoutFromPanel(eto2, "SignalHead:" + eSysNameLabel.getText() + ":Blue", ((TripleOutputSignalHead) curS).getBlue().getBean(), ev2Border.getTitle());
if (t2 == null) {
return;
} else {
((TripleOutputSignalHead) curS).setBlue(nbhm.getNamedBeanHandle(eto2.getDisplayName(), t2));
}
Turnout t3 = updateTurnoutFromPanel(eto3, "SignalHead:" + eSysNameLabel.getText() + ":Red", ((TripleOutputSignalHead) curS).getRed().getBean(), ev3Border.getTitle());
if (t3 == null) {
return;
} else {
((TripleOutputSignalHead) curS).setRed(nbhm.getNamedBeanHandle(eto3.getDisplayName(), t3));
}
} else if (className.equals("jmri.implementation.DoubleTurnoutSignalHead")) {
Turnout t1 = updateTurnoutFromPanel(eto1, "SignalHead:" + eSysNameLabel.getText() + ":Green", ((DoubleTurnoutSignalHead) curS).getGreen().getBean(), ev1Border.getTitle());
Turnout t2 = updateTurnoutFromPanel(eto2, "SignalHead:" + eSysNameLabel.getText() + ":Red", ((DoubleTurnoutSignalHead) curS).getRed().getBean(), ev2Border.getTitle());
if (t1 == null) {
return;
} else {
((DoubleTurnoutSignalHead) curS).setGreen(nbhm.getNamedBeanHandle(eto1.getDisplayName(), t1));
}
if (t2 == null) {
return;
} else {
((DoubleTurnoutSignalHead) curS).setRed(nbhm.getNamedBeanHandle(eto2.getDisplayName(), t2));
}
} else if (className.equals("jmri.implementation.SingleTurnoutSignalHead")) {
Turnout t1 = updateTurnoutFromPanel(eto1, "SignalHead:" + eSysNameLabel.getText() + ":" + (String) es2aBox.getSelectedItem() + ":" + (String) es3aBox.getSelectedItem(), ((SingleTurnoutSignalHead) curS).getOutput().getBean(), ev1Border.getTitle());
if (t1 == null) {
noTurnoutMessage(ev1Border.getTitle(), eto1.getDisplayName());
return;
}
((SingleTurnoutSignalHead) curS).setOutput(nbhm.getNamedBeanHandle(eto1.getDisplayName(), t1));
((SingleTurnoutSignalHead) curS).setOnAppearance(signalStateFromBox(es2aBox));
((SingleTurnoutSignalHead) curS).setOffAppearance(signalStateFromBox(es3aBox));
} else if (className.equals("jmri.implementation.LsDecSignalHead")) {
Turnout t1 = updateTurnoutFromPanel(eto1, "SignalHead:" + eSysNameLabel.getText() + ":Green", ((jmri.implementation.LsDecSignalHead) curS).getGreen().getBean(), ev1Border.getTitle());
if (t1 == null) {
return;
} else {
((jmri.implementation.LsDecSignalHead) curS).setGreen(nbhm.getNamedBeanHandle(eto1.getDisplayName(), t1));
((jmri.implementation.LsDecSignalHead) curS).setGreenState(turnoutStateFromBox(es1Box));
}
Turnout t2 = updateTurnoutFromPanel(eto2, "SignalHead:" + eSysNameLabel.getText() + ":Yellow", ((jmri.implementation.LsDecSignalHead) curS).getYellow().getBean(), ev2Border.getTitle());
if (t2 == null) {
return;
} else {
((jmri.implementation.LsDecSignalHead) curS).setYellow(nbhm.getNamedBeanHandle(eto2.getDisplayName(), t2));
((jmri.implementation.LsDecSignalHead) curS).setYellowState(turnoutStateFromBox(es2Box));
}
Turnout t3 = updateTurnoutFromPanel(eto3, "SignalHead:" + eSysNameLabel.getText() + ":Red", ((jmri.implementation.LsDecSignalHead) curS).getRed().getBean(), ev3Border.getTitle());
if (t3 == null) {
return;
} else {
((jmri.implementation.LsDecSignalHead) curS).setRed(nbhm.getNamedBeanHandle(eto3.getDisplayName(), t3));
((jmri.implementation.LsDecSignalHead) curS).setRedState(turnoutStateFromBox(es3Box));
}
Turnout t4 = updateTurnoutFromPanel(eto4, "SignalHead:" + eSysNameLabel.getText() + ":FlashGreen", ((jmri.implementation.LsDecSignalHead) curS).getFlashGreen().getBean(), ev4Border.getTitle());
if (t4 == null) {
return;
} else {
((jmri.implementation.LsDecSignalHead) curS).setFlashGreen(nbhm.getNamedBeanHandle(eto4.getDisplayName(), t4));
((jmri.implementation.LsDecSignalHead) curS).setFlashGreenState(turnoutStateFromBox(es4Box));
}
Turnout t5 = updateTurnoutFromPanel(eto5, "SignalHead:" + eSysNameLabel.getText() + ":FlashYellow", ((jmri.implementation.LsDecSignalHead) curS).getFlashYellow().getBean(), ev5Border.getTitle());
if (t5 == null) {
return;
} else {
((jmri.implementation.LsDecSignalHead) curS).setFlashYellow(nbhm.getNamedBeanHandle(eto5.getDisplayName(), t5));
((jmri.implementation.LsDecSignalHead) curS).setFlashYellowState(turnoutStateFromBox(es5Box));
}
Turnout t6 = updateTurnoutFromPanel(eto6, "SignalHead:" + eSysNameLabel.getText() + ":FlashRed", ((jmri.implementation.LsDecSignalHead) curS).getFlashRed().getBean(), ev6Border.getTitle());
if (t6 == null) {
return;
} else {
((jmri.implementation.LsDecSignalHead) curS).setFlashRed(nbhm.getNamedBeanHandle(eto6.getDisplayName(), t6));
((jmri.implementation.LsDecSignalHead) curS).setFlashRedState(turnoutStateFromBox(es6Box));
}
Turnout t7 = updateTurnoutFromPanel(eto7, "SignalHead:" + eSysNameLabel.getText() + ":Dark", ((jmri.implementation.LsDecSignalHead) curS).getDark().getBean(), ev7Border.getTitle());
if (t7 == null) {
return;
} else {
((jmri.implementation.LsDecSignalHead) curS).setDark(nbhm.getNamedBeanHandle(eto7.getDisplayName(), t7));
((jmri.implementation.LsDecSignalHead) curS).setDarkState(turnoutStateFromBox(es7Box));
}
} else if (className.equals("jmri.implementation.SE8cSignalHead")) {
handleSE8cUpdatePressed();
} else if (className.equals("jmri.jmrix.grapevine.SerialSignalHead")) {
/*String nam = eUserName.getText();
// check if user name changed
if (!((curS.getUserName()!=null) && (curS.getUserName().equals(nam)))) {
if(checkUserName(nam))
curS.setUserName(nam);
}*/
} else if (className.equals("jmri.jmrix.acela.AcelaSignalHead")) {
/*String nam = eUserName.getText();
// check if user name changed
if (!((curS.getUserName()!=null) && (curS.getUserName().equals(nam)))) {
if(checkUserName(nam))
curS.setUserName(nam);
}*/
AcelaNode tNode = AcelaAddress.getNodeFromSystemName(curS.getSystemName(), jmri.InstanceManager.getDefault(jmri.jmrix.acela.AcelaSystemConnectionMemo.class));
if (tNode == null) {
// node does not exist, ignore call
log.error("Can't find new Acela Signal with name '{}'", curS.getSystemName());
return;
}
int headnumber = Integer.parseInt(curS.getSystemName().substring(2, curS.getSystemName().length()));
tNode.setOutputSignalHeadTypeString(headnumber, estBox.getSelectedItem().toString());
// setSignalheadTypeInBox(estBox, tNode.getOutputSignalHeadType(headnumber), signalheadTypeValues);
// ((jmri.AcelaSignalHead)curS).setDarkState(signalheadTypeFromBox(estBox));
} else if (className.equals("jmri.implementation.MergSD2SignalHead")) {
switch(ukSignalAspectsFromBox(emsaBox)) {
case 4:
Turnout t3 = updateTurnoutFromPanel(eto5, (Bundle.getMessage("OutputComment", Bundle.getMessage("BeanNameSignalHead"), eSysNameLabel.getText(), Bundle.getMessage("InputNum", "3"))), ((jmri.implementation.MergSD2SignalHead) curS).getInput3().getBean(), ev5Border.getTitle());
if (t3 == null) {
return;
} else {
((jmri.implementation.MergSD2SignalHead) curS).setInput3(nbhm.getNamedBeanHandle(eto5.getDisplayName(), t3));
}
// fall through
case 3:
Turnout t2 = updateTurnoutFromPanel(eto4, (Bundle.getMessage("OutputComment", Bundle.getMessage("BeanNameSignalHead"), eSysNameLabel.getText(), Bundle.getMessage("InputNum", "2"))), ((jmri.implementation.MergSD2SignalHead) curS).getInput2().getBean(), ev4Border.getTitle());
if (t2 == null) {
return;
} else {
((jmri.implementation.MergSD2SignalHead) curS).setInput2(nbhm.getNamedBeanHandle(eto4.getDisplayName(), t2));
}
// fall through
case 2:
Turnout t1 = updateTurnoutFromPanel(eto3, (Bundle.getMessage("OutputComment", Bundle.getMessage("BeanNameSignalHead"), eSysNameLabel.getText(), Bundle.getMessage("InputNum", "1"))), ((jmri.implementation.MergSD2SignalHead) curS).getInput1().getBean(), ev3Border.getTitle());
if (t1 == null) {
return;
} else {
((jmri.implementation.MergSD2SignalHead) curS).setInput1(nbhm.getNamedBeanHandle(eto3.getDisplayName(), t1));
}
((jmri.implementation.MergSD2SignalHead) curS).setAspects(ukSignalAspectsFromBox(emsaBox));
if (ukSignalTypeFromBox(emstBox).equals("Distant")) {
((jmri.implementation.MergSD2SignalHead) curS).setHome(false);
} else {
((jmri.implementation.MergSD2SignalHead) curS).setHome(true);
}
default:
break;
}
//Need to add the code here for update!
} else if (className.equals("jmri.implementation.DccSignalHead")) {
for (int i = 0; i < dccAspectEdt.length; i++) {
int number = (Integer) dccAspectEdt[i].getValue();
try {
((DccSignalHead) curS).setOutputForAppearance(((DccSignalHead) curS).getValidStates()[i], number);
} catch (Exception ex) {
//in theory JSpinner should already have caught a number conversion error.
log.error(ex.toString());
}
}
((DccSignalHead) curS).useAddressOffSet(dccOffSetAddressEdt.isSelected());
} else {
log.error("Internal error - cannot update signal of type " + className);
}
// successful
editFrame.setVisible(false);
editingHead = false;
}
use of jmri.Turnout in project JMRI by JMRI.
the class SignalHeadTableAction method handleSE8cOkPressed.
void handleSE8cOkPressed() {
SignalHead s;
/* String msg;
// if no selection was made severeal exeptions as cast to the console, most start in the createbeanpane
try {
if ((to1 == null) || (to2 == null) || (to1.getDisplayName() == null) || (to2.getDisplayName() == null) ||
(to1.getDisplayName().equals("")) || to2.getDisplayName().equals("")) {
return;
}
} catch (NumberFormatException ex) {
if (to1.getDisplayName().equals("")) {
msg = Bundle.getMessage("se8c4SkippingDueToErrorInFirst");
} else {
msg = Bundle.getMessage("se8c4SkippingDueToErrorInSecond");
}
JOptionPane.showMessageDialog(addFrame, msg,
Bundle.getMessage("WarningTitle"), JOptionPane.ERROR_MESSAGE);
log.error("No SE8C Turnouts selected in pane");
return;
}*/
Turnout t1 = getTurnoutFromPanel(to1, "SignalHead:" + systemName.getText() + ":low");
Turnout t2 = getTurnoutFromPanel(to2, "SignalHead:" + systemName.getText() + ":high");
// check validity
if (t1 != null && t2 != null) {
// OK, process
try {
s = new jmri.implementation.SE8cSignalHead(nbhm.getNamedBeanHandle(t1.getSystemName(), t1), nbhm.getNamedBeanHandle(t2.getSystemName(), t2), userName.getText());
} catch (NumberFormatException ex) {
// user input no good
handleCreate2TurnoutException(t1.getSystemName(), t2.getSystemName(), userName.getText());
// without creating any
return;
}
InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
} else {
// couldn't create turnouts, error
String msg;
if (t1 == null) {
msg = Bundle.getMessage("se8c4SkippingDueToErrorInFirst");
} else {
msg = Bundle.getMessage("se8c4SkippingDueToErrorInSecond");
}
JOptionPane.showMessageDialog(addFrame, msg, Bundle.getMessage("WarningTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
}
use of jmri.Turnout in project JMRI by JMRI.
the class SignalHeadTableAction method handleMergSignalDriverOkPressed.
@SuppressWarnings("fallthrough")
@SuppressFBWarnings(value = "SF_SWITCH_FALLTHROUGH")
void handleMergSignalDriverOkPressed() {
SignalHead s;
// Adding Merg Signal Driver.
Turnout t3 = null;
Turnout t2 = null;
Turnout t1 = null;
NamedBeanHandle<Turnout> nbt1 = null;
NamedBeanHandle<Turnout> nbt2 = null;
NamedBeanHandle<Turnout> nbt3 = null;
if (checkBeforeCreating(systemName.getText())) {
switch(ukSignalAspectsFromBox(msaBox)) {
case 4:
t3 = getTurnoutFromPanel(to5, (Bundle.getMessage("OutputComment", Bundle.getMessage("BeanNameSignalHead"), systemName.getText(), Bundle.getMessage("InputNum", "3"))));
if (t3 == null) {
addTurnoutMessage(v5Border.getTitle(), to5.getDisplayName());
log.warn("skipping creation of signal " + systemName.getText() + " due to error");
return;
} else {
nbt3 = nbhm.getNamedBeanHandle(to5.getDisplayName(), t3);
}
// fall through
case 3:
t2 = getTurnoutFromPanel(to4, (Bundle.getMessage("OutputComment", Bundle.getMessage("BeanNameSignalHead"), systemName.getText(), Bundle.getMessage("InputNum", "2"))));
if (t2 == null) {
addTurnoutMessage(v4Border.getTitle(), to4.getDisplayName());
log.warn("skipping creation of signal " + systemName.getText() + " due to error");
return;
} else {
nbt2 = nbhm.getNamedBeanHandle(to4.getDisplayName(), t2);
}
// fall through
case 2:
t1 = getTurnoutFromPanel(to3, (Bundle.getMessage("OutputComment", Bundle.getMessage("BeanNameSignalHead"), systemName.getText(), Bundle.getMessage("InputNum", "1"))));
if (t1 == null) {
addTurnoutMessage(v3Border.getTitle(), to3.getDisplayName());
log.warn("skipping creation of signal " + systemName.getText() + " due to error");
return;
} else {
nbt1 = nbhm.getNamedBeanHandle(to3.getDisplayName(), t1);
}
default:
break;
}
boolean home;
if (ukSignalTypeFromBox(mstBox).equals("Distant")) {
home = false;
} else {
home = true;
}
s = new jmri.implementation.MergSD2SignalHead(systemName.getText(), ukSignalAspectsFromBox(msaBox), nbt1, nbt2, nbt3, false, home);
s.setUserName(userName.getText());
InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
}
}
use of jmri.Turnout in project JMRI by JMRI.
the class PathTurnoutTableModel method setValueAt.
@Override
public void setValueAt(Object value, int row, int col) {
if (_path.getSettings().size() == row) {
switch(col) {
case TURNOUT_NAME_COL:
tempRow[TURNOUT_NAME_COL] = (String) value;
if (tempRow[SETTINGCOLUMN] == null) {
return;
}
break;
case SETTINGCOLUMN:
tempRow[SETTINGCOLUMN] = (String) value;
if (tempRow[TURNOUT_NAME_COL] == null) {
return;
}
break;
case DELETE_COL:
initTempRow();
fireTableRowsUpdated(row, row);
return;
default:
// fall through
break;
}
Turnout t = InstanceManager.turnoutManagerInstance().getTurnout(tempRow[TURNOUT_NAME_COL]);
if (t != null) {
int s = Turnout.UNKNOWN;
if (tempRow[SETTINGCOLUMN].equals(closed)) {
s = Turnout.CLOSED;
} else if (tempRow[SETTINGCOLUMN].equals(thrown)) {
s = Turnout.THROWN;
} else {
JOptionPane.showMessageDialog(null, Bundle.getMessage("TurnoutMustBeSet", closed, thrown), Bundle.getMessage("ErrorTitle"), JOptionPane.WARNING_MESSAGE);
return;
}
BeanSetting bs = new BeanSetting(t, tempRow[TURNOUT_NAME_COL], s);
_path.addSetting(bs);
fireTableRowsUpdated(row, row);
} else {
JOptionPane.showMessageDialog(null, Bundle.getMessage("NoSuchTurnout", tempRow[TURNOUT_NAME_COL]), Bundle.getMessage("ErrorTitle"), JOptionPane.WARNING_MESSAGE);
return;
}
initTempRow();
return;
}
BeanSetting bs = _path.getSettings().get(row);
switch(col) {
case TURNOUT_NAME_COL:
Turnout t = InstanceManager.turnoutManagerInstance().getTurnout((String) value);
if (t != null) {
if (!t.equals(bs.getBean())) {
_path.removeSetting(bs);
_path.addSetting(new BeanSetting(t, (String) value, bs.getSetting()));
}
} else {
JOptionPane.showMessageDialog(null, Bundle.getMessage("NoSuchTurnout", (String) value), Bundle.getMessage("ErrorTitle"), JOptionPane.WARNING_MESSAGE);
return;
}
fireTableDataChanged();
break;
case SETTINGCOLUMN:
String setting = (String) value;
if (setting.equals(closed)) {
//bs.setSetting(Turnout.CLOSED); - This was the form before BeanSetting was returned to Immutable
_path.getSettings().set(row, new BeanSetting(bs.getBean(), bs.getBeanName(), Turnout.CLOSED));
} else if (setting.equals(thrown)) {
//bs.setSetting(Turnout.THROWN);
_path.getSettings().set(row, new BeanSetting(bs.getBean(), bs.getBeanName(), Turnout.THROWN));
} else {
JOptionPane.showMessageDialog(null, Bundle.getMessage("TurnoutMustBeSet", closed, thrown), Bundle.getMessage("ErrorTitle"), JOptionPane.WARNING_MESSAGE);
return;
}
fireTableRowsUpdated(row, row);
break;
case DELETE_COL:
if (JOptionPane.showConfirmDialog(null, Bundle.getMessage("DeleteTurnoutConfirm"), Bundle.getMessage("WarningTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
_path.removeSetting(bs);
fireTableDataChanged();
}
break;
default:
log.warn("Unhandled col: {}", col);
break;
}
}
use of jmri.Turnout in project JMRI by JMRI.
the class AutoTurnouts method turnoutUtil.
/**
* Internal method implementing the above two methods Returns 'true' if
* turnouts are set correctly, 'false' otherwise If 'set' is 'true' this
* routine will attempt to set the turnouts, if 'false' it reports what it
* finds.
*/
private boolean turnoutUtil(Section s, int seqNum, Section nextSection, ActiveTrain at, LayoutEditor le, boolean trustKnownTurnouts, boolean set, Section prevSection) {
// validate input and initialize
Transit tran = at.getTransit();
if ((s == null) || (seqNum > tran.getMaxSequence()) || (!tran.containsSection(s)) || (le == null)) {
log.error("Invalid argument when checking or setting turnouts in Section.");
return false;
}
int direction = at.getAllocationDirectionFromSectionAndSeq(s, seqNum);
if (direction == 0) {
log.error("Invalid Section/sequence arguments when checking or setting turnouts");
return false;
}
// check for no turnouts in this section
if (_dispatcher.getSignalType() == DispatcherFrame.SIGNALHEAD && (s.getForwardEntryPointList().size() <= 1) && (s.getReverseEntryPointList().size() <= 1)) {
log.debug("No entry points lists");
// no possibility of turnouts
return true;
}
// initialize connectivity utilities and beginning block pointers
ConnectivityUtil ct = le.getConnectivityUtil();
EntryPoint entryPt = null;
if (prevSection != null) {
entryPt = s.getEntryPointFromSection(prevSection, direction);
} else if (!s.containsBlock(at.getStartBlock())) {
entryPt = s.getEntryPointFromBlock(at.getStartBlock(), direction);
}
EntryPoint exitPt = null;
if (nextSection != null) {
exitPt = s.getExitPointToSection(nextSection, direction);
}
// must be in the section
Block curBlock = null;
// must start outside the section or be null
Block prevBlock = null;
// sequence number of curBlock in Section
int curBlockSeqNum = -1;
if (entryPt != null) {
curBlock = entryPt.getBlock();
prevBlock = entryPt.getFromBlock();
curBlockSeqNum = s.getBlockSequenceNumber(curBlock);
} else if (!at.isAllocationReversed() && s.containsBlock(at.getStartBlock())) {
curBlock = at.getStartBlock();
curBlockSeqNum = s.getBlockSequenceNumber(curBlock);
//Get the previous block so that we can set the turnouts in the current block correctly.
if (direction == Section.FORWARD) {
prevBlock = s.getBlockBySequenceNumber(curBlockSeqNum - 1);
} else if (direction == Section.REVERSE) {
prevBlock = s.getBlockBySequenceNumber(curBlockSeqNum + 1);
}
} else if (at.isAllocationReversed() && s.containsBlock(at.getEndBlock())) {
curBlock = at.getEndBlock();
curBlockSeqNum = s.getBlockSequenceNumber(curBlock);
//Get the previous block so that we can set the turnouts in the current block correctly.
if (direction == Section.REVERSE) {
prevBlock = s.getBlockBySequenceNumber(curBlockSeqNum + 1);
} else if (direction == Section.FORWARD) {
prevBlock = s.getBlockBySequenceNumber(curBlockSeqNum - 1);
}
} else {
if (_dispatcher.getSignalType() == DispatcherFrame.SIGNALMAST) {
//This can be considered normal where SignalMast Logic is used.
return true;
}
log.error("Error in turnout check/set request - initial Block and Section mismatch");
return false;
}
Block nextBlock = null;
// may be either in the section or the first block in the next section
// sequence number of nextBlock in Section (-1 indicates outside Section)
int nextBlockSeqNum = -1;
if (exitPt != null && curBlock == exitPt.getBlock()) {
// next Block is outside of the Section
nextBlock = exitPt.getFromBlock();
} else {
// next Block is inside the Section
if (direction == Section.FORWARD) {
nextBlock = s.getBlockBySequenceNumber(curBlockSeqNum + 1);
nextBlockSeqNum = curBlockSeqNum + 1;
} else if (direction == Section.REVERSE) {
nextBlock = s.getBlockBySequenceNumber(curBlockSeqNum - 1);
nextBlockSeqNum = curBlockSeqNum - 1;
}
if ((nextBlock == null) && (curBlock != at.getEndBlock())) {
log.error("Error in block sequence numbers when setting/checking turnouts");
return false;
}
}
ArrayList<LayoutTurnout> turnoutList = new ArrayList<LayoutTurnout>();
ArrayList<Integer> settingsList = new ArrayList<Integer>();
// get turnouts by Block
boolean turnoutsOK = true;
while (curBlock != null) {
/*No point in getting the list if the previous block is null as it will return empty and generate an error,
this will only happen on the first run. Plus working on the basis that the turnouts in the current block would have already of
been set correctly for the train to have arrived in the first place.
*/
if (prevBlock != null) {
turnoutList = ct.getTurnoutList(curBlock, prevBlock, nextBlock);
settingsList = ct.getTurnoutSettingList();
}
// loop over turnouts checking and optionally setting turnouts
for (int i = 0; i < turnoutList.size(); i++) {
Turnout to = turnoutList.get(i).getTurnout();
int setting = settingsList.get(i).intValue();
if (turnoutList.get(i) instanceof LayoutSlip) {
setting = ((LayoutSlip) turnoutList.get(i)).getTurnoutState(settingsList.get(i));
}
// check or ignore current setting based on flag, set in Options
if (!trustKnownTurnouts) {
log.debug("{}: setting turnout {} to {}", at.getTrainName(), to.getFullyFormattedDisplayName(), (setting == Turnout.CLOSED ? closedText : thrownText));
to.setCommandedState(setting);
try {
Thread.sleep(100);
} catch (Exception ex) {
}
//TODO: move this to separate thread
} else {
if (to.getKnownState() != setting) {
// turnout is not set correctly
if (set) {
// setting has been requested, is Section free and Block unoccupied
if ((s.getState() == Section.FREE) && (curBlock.getState() != Block.OCCUPIED)) {
// send setting command
log.debug("{}: turnout {} commanded to {}", at.getTrainName(), to.getFullyFormattedDisplayName(), (setting == Turnout.CLOSED ? closedText : thrownText));
to.setCommandedState(setting);
try {
Thread.sleep(100);
} catch (Exception ex) {
}
//TODO: move this to separate thread
} else {
turnoutsOK = false;
}
} else {
turnoutsOK = false;
}
} else {
log.debug("{}: turnout {} already {}, skipping", at.getTrainName(), to.getFullyFormattedDisplayName(), (setting == Turnout.CLOSED ? closedText : thrownText));
}
}
if (turnoutList.get(i) instanceof LayoutSlip) {
//Look at the state of the second turnout in the slip
setting = ((LayoutSlip) turnoutList.get(i)).getTurnoutBState(settingsList.get(i));
to = ((LayoutSlip) turnoutList.get(i)).getTurnoutB();
if (!trustKnownTurnouts) {
to.setCommandedState(setting);
} else if (to.getKnownState() != setting) {
// turnout is not set correctly
if (set) {
// setting has been requested, is Section free and Block unoccupied
if ((s.getState() == Section.FREE) && (curBlock.getState() != Block.OCCUPIED)) {
// send setting command
to.setCommandedState(setting);
} else {
turnoutsOK = false;
}
} else {
turnoutsOK = false;
}
}
}
}
if (turnoutsOK) {
// move to next Block if any
if (nextBlockSeqNum >= 0) {
prevBlock = curBlock;
curBlock = nextBlock;
curBlockSeqNum = nextBlockSeqNum;
if ((exitPt != null) && (curBlock == exitPt.getBlock())) {
// next block is outside of the Section
nextBlock = exitPt.getFromBlock();
nextBlockSeqNum = -1;
} else {
if (direction == Section.FORWARD) {
nextBlockSeqNum++;
} else {
nextBlockSeqNum--;
}
nextBlock = s.getBlockBySequenceNumber(nextBlockSeqNum);
if (nextBlock == null) {
// there is no next Block
nextBlockSeqNum = -1;
}
}
} else {
curBlock = null;
}
} else {
curBlock = null;
}
}
return turnoutsOK;
}
Aggregations