use of jmri.Transit 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;
}
use of jmri.Transit in project JMRI by JMRI.
the class ActivateTrainFrame method initializeFreeTransitsCombo.
private void initializeFreeTransitsCombo(ArrayList<Transit> transitList) {
ArrayList<String> allTransits = (ArrayList<String>) _TransitManager.getSystemNameList();
transitSelectBox.removeAllItems();
transitBoxList.clear();
if (transitList.isEmpty()) {
for (int i = 0; i < allTransits.size(); i++) {
String tName = allTransits.get(i);
Transit t = _TransitManager.getBySystemName(tName);
transitList.add(t);
}
}
for (Transit t : transitList) {
boolean free = true;
for (int j = 0; j < _ActiveTrainsList.size(); j++) {
ActiveTrain at = _ActiveTrainsList.get(j);
if (t == at.getTransit()) {
free = false;
}
}
if (free) {
String tName = t.getSystemName();
transitBoxList.add(t);
String uname = t.getUserName();
if ((uname != null) && (!uname.equals("")) && (!uname.equals(tName))) {
tName = tName + "(" + uname + ")";
}
transitSelectBox.addItem(tName);
}
}
if (transitBoxList.size() > 0) {
transitSelectBox.setSelectedIndex(0);
selectedTransit = transitBoxList.get(0);
} else {
selectedTransit = null;
}
}
use of jmri.Transit in project JMRI by JMRI.
the class ActivateTrainFrame method handleTransitSelectionChanged.
private void handleTransitSelectionChanged(ActionEvent e) {
int index = transitSelectBox.getSelectedIndex();
if (index < 0) {
return;
}
Transit t = transitBoxList.get(index);
if ((t != null) && (t != selectedTransit)) {
selectedTransit = t;
initializeStartingBlockCombo();
initializeDestinationBlockCombo();
initiateFrame.pack();
}
}
use of jmri.Transit in project JMRI by JMRI.
the class ActivateTrainFrame method initiateTrain.
/**
* Open up a new train window for a given roster entry located in a
* specific block.
*
* @param e the action event triggering the new window
* @param re the roster entry to open the new window for
* @param b the block where the train is located
*/
public void initiateTrain(ActionEvent e, RosterEntry re, Block b) {
initiateTrain(e);
if (_TrainsFromRoster && re != null) {
setComboBox(trainSelectBox, re.getId());
//Add in some bits of code as some point to filter down the transits that can be used.
}
if (b != null && selectedTransit != null) {
ArrayList<Transit> transitList = _TransitManager.getListUsingBlock(b);
ArrayList<Transit> transitEntryList = _TransitManager.getListEntryBlock(b);
for (Transit t : transitEntryList) {
if (!transitList.contains(t)) {
transitList.add(t);
}
}
transitsFromSpecificBlock = true;
initializeFreeTransitsCombo(transitList);
ArrayList<Block> tmpBlkList = new ArrayList<Block>();
if (selectedTransit.getEntryBlocksList().contains(b)) {
tmpBlkList = selectedTransit.getEntryBlocksList();
inTransitBox.setSelected(false);
} else if (selectedTransit.containsBlock(b)) {
tmpBlkList = selectedTransit.getInternalBlocksList();
inTransitBox.setSelected(true);
}
ArrayList<Integer> tmpSeqList = selectedTransit.getBlockSeqList();
for (int i = 0; i < tmpBlkList.size(); i++) {
if (tmpBlkList.get(i) == b) {
setComboBox(startingBlockBox, getBlockName(b) + "-" + tmpSeqList.get(i));
break;
}
}
}
}
use of jmri.Transit in project JMRI by JMRI.
the class TransitCreationTool method createTransit.
public Transit createTransit() throws JmriException {
TransitManager tm = InstanceManager.getDefault(jmri.TransitManager.class);
String transitName = "From " + list.get(0).getDisplayName() + " to " + list.get(list.size() - 1).getDisplayName();
Transit t = tm.createNewTransit(transitName);
if (t == null) {
log.error("Unable to create transit " + transitName);
throw new JmriException(Bundle.getMessage("TCTErrorUnableToCreate", transitName));
}
if (list.get(0) instanceof SignalMast) {
jmri.SignalMastLogicManager smlm = InstanceManager.getDefault(jmri.SignalMastLogicManager.class);
for (int i = 1; i <= list.size() - 1; i++) {
jmri.SignalMastLogic sml = smlm.getSignalMastLogic((SignalMast) list.get(i - 1));
Section sec = sml.getAssociatedSection((SignalMast) list.get(i));
//In theory sec being null would already have been tested when the signal was added.
if (sec == null) {
String error = Bundle.getMessage("TCTErrorMastPairsNoSection", list.get(i - 1).getDisplayName(), list.get(i).getDisplayName());
log.error(error);
tm.deregister(t);
t.dispose();
cancelTransitCreate();
throw new JmriException(error);
}
t.addTransitSection(new jmri.TransitSection(sec, i, Section.FORWARD));
}
}
//Once created clear the list for a fresh start.
list = new ArrayList<NamedBean>();
return t;
}
Aggregations