use of jmri.jmrit.dispatcher.ActiveTrain in project JMRI by JMRI.
the class DestinationPoints method setRoute.
//For a clear down we need to add a message, if it is a cancel, manual clear down or I didn't mean it.
void setRoute(boolean state) {
if (log.isDebugEnabled()) {
log.debug("Set route " + src.getPoint().getDisplayName());
}
if (disposed) {
log.error("Set route called even though interlock has been disposed of");
return;
}
if (routeDetails == null) {
log.error("No route to set or clear down");
setActiveEntryExit(false);
setRouteTo(false);
setRouteFrom(false);
if ((getSignal() instanceof SignalMast) && (getEntryExitType() != EntryExitPairs.FULLINTERLOCK)) {
SignalMast mast = (SignalMast) getSignal();
mast.setHeld(false);
}
synchronized (this) {
destination = null;
}
return;
}
if (!state) {
switch(manager.getClearDownOption()) {
case EntryExitPairs.PROMPTUSER:
cancelClearOptionBox();
break;
case EntryExitPairs.AUTOCANCEL:
cancelClearInterlock(EntryExitPairs.CANCELROUTE);
break;
case EntryExitPairs.AUTOCLEAR:
cancelClearInterlock(EntryExitPairs.CLEARROUTE);
break;
default:
cancelClearOptionBox();
break;
}
if (log.isDebugEnabled()) {
log.debug("Exit " + src.getPoint().getDisplayName());
}
return;
}
if (manager.isRouteStacked(this, false)) {
manager.cancelStackedRoute(this, false);
}
/* We put the setting of the route into a seperate thread and put a glass pane in front of the layout editor.
The swing thread for flashing the icons will carry on without interuption. */
final ArrayList<Color> realColorStd = new ArrayList<Color>();
final ArrayList<Color> realColorXtra = new ArrayList<Color>();
final ArrayList<LayoutBlock> routeBlocks = new ArrayList<LayoutBlock>();
if (manager.useDifferentColorWhenSetting()) {
for (LayoutBlock lbk : routeDetails) {
routeBlocks.add(lbk);
realColorXtra.add(lbk.getBlockExtraColor());
realColorStd.add(lbk.getBlockTrackColor());
lbk.setBlockExtraColor(manager.getSettingRouteColor());
lbk.setBlockTrackColor(manager.getSettingRouteColor());
}
//Force a redraw, to reflect color change
src.getPoint().getPanel().redrawPanel();
}
ActiveTrain tmpat = null;
if (manager.getDispatcherIntegration() && jmri.InstanceManager.getNullableDefault(jmri.jmrit.dispatcher.DispatcherFrame.class) != null) {
jmri.jmrit.dispatcher.DispatcherFrame df = jmri.InstanceManager.getDefault(jmri.jmrit.dispatcher.DispatcherFrame.class);
for (ActiveTrain atl : df.getActiveTrainsList()) {
if (atl.getEndBlock() == src.getStart().getBlock()) {
if (atl.getLastAllocatedSection() == atl.getEndBlockSection()) {
if (!atl.getReverseAtEnd() && !atl.getResetWhenDone()) {
tmpat = atl;
break;
}
log.warn("Interlock will not be added to existing Active Train as it is set for back and forth operation");
}
}
}
}
final ActiveTrain at = tmpat;
Runnable setRouteRun = new Runnable() {
@Override
public void run() {
src.getPoint().getPanel().getGlassPane().setVisible(true);
try {
Hashtable<Turnout, Integer> turnoutSettings = new Hashtable<Turnout, Integer>();
ConnectivityUtil connection = new ConnectivityUtil(point.getPanel());
// Last block in the route is the one that we are protecting at the last sensor/signalmast
for (int i = 0; i < routeDetails.size(); i++) {
//if we are not using the dispatcher and the signal logic is dynamic, then set the turnouts
if (at == null && isSignalLogicDynamic()) {
if (i > 0) {
ArrayList<LayoutTurnout> turnoutlist;
int nxtBlk = i + 1;
int preBlk = i - 1;
if (i < routeDetails.size() - 1) {
turnoutlist = connection.getTurnoutList(routeDetails.get(i).getBlock(), routeDetails.get(preBlk).getBlock(), routeDetails.get(nxtBlk).getBlock());
ArrayList<Integer> throwlist = connection.getTurnoutSettingList();
for (int x = 0; x < turnoutlist.size(); x++) {
if (turnoutlist.get(x) instanceof LayoutSlip) {
int slipState = throwlist.get(x);
LayoutSlip ls = (LayoutSlip) turnoutlist.get(x);
int taState = ls.getTurnoutState(slipState);
turnoutSettings.put(ls.getTurnout(), taState);
int tbState = ls.getTurnoutBState(slipState);
ls.getTurnoutB().setCommandedState(tbState);
turnoutSettings.put(ls.getTurnoutB(), tbState);
} else {
String t = turnoutlist.get(x).getTurnoutName();
Turnout turnout = InstanceManager.turnoutManagerInstance().getTurnout(t);
turnoutSettings.put(turnout, throwlist.get(x));
if (turnoutlist.get(x).getSecondTurnout() != null) {
turnoutSettings.put(turnoutlist.get(x).getSecondTurnout(), throwlist.get(x));
}
}
}
}
}
}
if ((getEntryExitType() == EntryExitPairs.FULLINTERLOCK)) {
// was set against occupancy sensor
routeDetails.get(i).getBlock().addPropertyChangeListener(propertyBlockListener);
if (i > 0) {
routeDetails.get(i).setUseExtraColor(true);
}
} else {
// was set against occupancy sensor
routeDetails.get(i).getBlock().removePropertyChangeListener(propertyBlockListener);
}
}
if (at == null) {
if (!isSignalLogicDynamic()) {
jmri.SignalMastLogic tmSml = InstanceManager.getDefault(jmri.SignalMastLogicManager.class).getSignalMastLogic((SignalMast) src.sourceSignal);
for (Turnout t : tmSml.getAutoTurnouts((SignalMast) getSignal())) {
turnoutSettings.put(t, tmSml.getAutoTurnoutState(t, (SignalMast) getSignal()));
}
}
for (Map.Entry<Turnout, Integer> entry : turnoutSettings.entrySet()) {
entry.getKey().setCommandedState(entry.getValue());
Runnable r = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(250 + manager.turnoutSetDelay);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
};
Thread thr = new Thread(r, "Entry Exit Route, turnout setting");
thr.start();
try {
thr.join();
} catch (InterruptedException ex) {
// log.info("interrupted at join " + ex);
}
}
}
src.getPoint().getPanel().redrawPanel();
if (getEntryExitType() != EntryExitPairs.SETUPTURNOUTSONLY) {
if (getEntryExitType() == EntryExitPairs.FULLINTERLOCK) {
//If our start block is already active we will set it as our lastSeenActiveBlock.
if (src.getStart().getState() == Block.OCCUPIED) {
src.getStart().removePropertyChangeListener(propertyBlockListener);
lastSeenActiveBlockObject = src.getStart().getBlock().getValue();
log.debug("Last seen value " + lastSeenActiveBlockObject);
}
}
if ((src.sourceSignal instanceof SignalMast) && (getSignal() instanceof SignalMast)) {
SignalMast smSource = (SignalMast) src.sourceSignal;
SignalMast smDest = (SignalMast) getSignal();
synchronized (this) {
sml = InstanceManager.getDefault(jmri.SignalMastLogicManager.class).newSignalMastLogic(smSource);
if (!sml.isDestinationValid(smDest)) {
//if no signalmastlogic existed then created it, but set it not to be stored.
sml.setDestinationMast(smDest);
sml.setStore(jmri.SignalMastLogic.STORENONE, smDest);
}
}
//Remove the first block as it is our start block
routeDetails.remove(0);
synchronized (this) {
smSource.setHeld(false);
//Only change the block and turnout details if this a temp signalmast logic
if (sml.getStoreState(smDest) == jmri.SignalMastLogic.STORENONE) {
LinkedHashMap<Block, Integer> blks = new LinkedHashMap<Block, Integer>();
for (int i = 0; i < routeDetails.size(); i++) {
if (routeDetails.get(i).getBlock().getState() == Block.UNKNOWN) {
routeDetails.get(i).getBlock().setState(Block.UNOCCUPIED);
}
blks.put(routeDetails.get(i).getBlock(), Block.UNOCCUPIED);
}
sml.setAutoBlocks(blks, smDest);
sml.setAutoTurnouts(turnoutSettings, smDest);
sml.initialise(smDest);
}
}
smSource.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
SignalMast source = (SignalMast) e.getSource();
source.removePropertyChangeListener(this);
setRouteFrom(true);
setRouteTo(true);
}
});
src.pd.extendedtime = true;
point.extendedtime = true;
} else {
if (src.sourceSignal instanceof SignalMast) {
SignalMast mast = (SignalMast) src.sourceSignal;
mast.setHeld(false);
} else if (src.sourceSignal instanceof SignalHead) {
SignalHead head = (SignalHead) src.sourceSignal;
head.setHeld(false);
}
setRouteFrom(true);
setRouteTo(true);
}
}
if (manager.useDifferentColorWhenSetting()) {
//final ArrayList<Color> realColorXtra = realColorXtra;
javax.swing.Timer resetColorBack = new javax.swing.Timer(manager.getSettingTimer(), new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
for (int i = 0; i < routeBlocks.size(); i++) {
LayoutBlock lbk = routeBlocks.get(i);
lbk.setBlockExtraColor(realColorXtra.get(i));
lbk.setBlockTrackColor(realColorStd.get(i));
}
src.getPoint().getPanel().redrawPanel();
}
});
resetColorBack.setRepeats(false);
resetColorBack.start();
}
if (at != null) {
jmri.Section sec = null;
if (sml != null && sml.getAssociatedSection((SignalMast) getSignal()) != null) {
sec = sml.getAssociatedSection((SignalMast) getSignal());
} else {
sec = InstanceManager.getDefault(jmri.SectionManager.class).createNewSection(src.getPoint().getDisplayName() + ":" + point.getDisplayName());
if (sec == null) {
//A Section already exists, lets grab it and check that it is one used with the Interlocking, if so carry on using that.
sec = InstanceManager.getDefault(jmri.SectionManager.class).getSection(src.getPoint().getDisplayName() + ":" + point.getDisplayName());
} else {
sec.setSectionType(jmri.Section.DYNAMICADHOC);
}
if (sec.getSectionType() == jmri.Section.DYNAMICADHOC) {
sec.removeAllBlocksFromSection();
for (LayoutBlock key : routeDetails) {
if (key != src.getStart()) {
sec.addBlock(key.getBlock());
}
}
String dir = jmri.Path.decodeDirection(src.getStart().getNeighbourDirection(routeDetails.get(0).getBlock()));
jmri.EntryPoint ep = new jmri.EntryPoint(routeDetails.get(0).getBlock(), src.getStart().getBlock(), dir);
ep.setTypeForward();
sec.addToForwardList(ep);
LayoutBlock proDestLBlock = point.getProtecting().get(0);
if (proDestLBlock != null) {
dir = jmri.Path.decodeDirection(proDestLBlock.getNeighbourDirection(point.getFacing()));
ep = new jmri.EntryPoint(point.getFacing().getBlock(), proDestLBlock.getBlock(), dir);
ep.setTypeReverse();
sec.addToReverseList(ep);
}
}
}
jmri.InstanceManager.getDefault(jmri.jmrit.dispatcher.DispatcherFrame.class).extendActiveTrainsPath(sec, at, src.getPoint().getPanel());
}
src.pd.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
point.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
} catch (RuntimeException ex) {
log.error("An error occured while setting the route");
ex.printStackTrace();
src.pd.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
point.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
if (manager.useDifferentColorWhenSetting()) {
for (int i = 0; i < routeBlocks.size(); i++) {
LayoutBlock lbk = routeBlocks.get(i);
lbk.setBlockExtraColor(realColorXtra.get(i));
lbk.setBlockTrackColor(realColorStd.get(i));
}
}
src.getPoint().getPanel().redrawPanel();
}
src.getPoint().getPanel().getGlassPane().setVisible(false);
//src.setMenuEnabled(true);
}
};
Thread thrMain = new Thread(setRouteRun, "Entry Exit Set Route");
thrMain.start();
try {
thrMain.join();
} catch (InterruptedException e) {
log.error("Interuption exception " + e.toString());
}
if (log.isDebugEnabled()) {
log.debug("finish route " + src.getPoint().getDisplayName());
}
}
use of jmri.jmrit.dispatcher.ActiveTrain in project JMRI by JMRI.
the class DestinationPoints method cancelClearInterlock.
void cancelClearInterlock(int cancelClear) {
if ((cancelClear == EntryExitPairs.EXITROUTE) || (cancelClear == EntryExitPairs.STACKROUTE)) {
src.pd.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
point.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
src.getPoint().getPanel().getGlassPane().setVisible(false);
if (cancelClear == EntryExitPairs.STACKROUTE) {
manager.stackNXRoute(this, false);
}
return;
}
if (cancelClear == EntryExitPairs.CANCELROUTE) {
if (manager.getDispatcherIntegration() && jmri.InstanceManager.getNullableDefault(jmri.jmrit.dispatcher.DispatcherFrame.class) != null) {
jmri.jmrit.dispatcher.DispatcherFrame df = jmri.InstanceManager.getDefault(jmri.jmrit.dispatcher.DispatcherFrame.class);
ActiveTrain at = null;
for (ActiveTrain atl : df.getActiveTrainsList()) {
if (atl.getEndBlock() == point.getFacing().getBlock()) {
if (atl.getLastAllocatedSection() == atl.getEndBlockSection()) {
at = atl;
break;
}
}
}
if (at != null) {
jmri.Section sec = null;
synchronized (this) {
if (sml != null && sml.getAssociatedSection((SignalMast) getSignal()) != null) {
sec = sml.getAssociatedSection((SignalMast) getSignal());
} else {
sec = InstanceManager.getDefault(jmri.SectionManager.class).getSection(src.getPoint().getDisplayName() + ":" + point.getDisplayName());
}
}
if (sec != null) {
if (!df.removeFromActiveTrainPath(sec, at, src.getPoint().getPanel())) {
log.error("Unable to remove allocation from dispathcer, leave interlock in place");
src.pd.cancelNXButtonTimeOut();
point.cancelNXButtonTimeOut();
src.getPoint().getPanel().getGlassPane().setVisible(false);
return;
}
if (sec.getSectionType() == jmri.Section.DYNAMICADHOC) {
sec.removeAllBlocksFromSection();
}
}
}
}
}
src.setMenuEnabled(false);
if (src.sourceSignal instanceof SignalMast) {
SignalMast mast = (SignalMast) src.sourceSignal;
mast.setAspect(mast.getAppearanceMap().getSpecificAppearance(jmri.SignalAppearanceMap.DANGER));
mast.setHeld(true);
} else if (src.sourceSignal instanceof SignalHead) {
SignalHead head = (SignalHead) src.sourceSignal;
head.setHeld(true);
} else {
log.debug("No signal found");
}
//Get rid of the signal mast logic to the destination mast.
synchronized (this) {
if ((getSignal() instanceof SignalMast) && (sml != null)) {
SignalMast mast = (SignalMast) getSignal();
if (sml.getStoreState(mast) == jmri.SignalMastLogic.STORENONE) {
sml.removeDestination(mast);
}
}
sml = null;
}
if (routeDetails == null) {
return;
}
for (LayoutBlock blk : routeDetails) {
if ((getEntryExitType() == EntryExitPairs.FULLINTERLOCK)) {
blk.setUseExtraColor(false);
}
// was set against occupancy sensor
blk.getBlock().removePropertyChangeListener(propertyBlockListener);
}
if (cancelClear == EntryExitPairs.CLEARROUTE) {
if (routeDetails.size() == 0) {
if (log.isDebugEnabled()) {
log.debug(mUserName + " all blocks have automatically been cleared down");
}
} else {
if (log.isDebugEnabled()) {
log.debug(mUserName + " No blocks were cleared down " + routeDetails.size());
}
try {
if (log.isDebugEnabled()) {
log.debug(mUserName + " set first block as active so that we can manually clear this down " + routeDetails.get(0).getBlock().getUserName());
}
if (routeDetails.get(0).getOccupancySensor() != null) {
routeDetails.get(0).getOccupancySensor().setState(Sensor.ACTIVE);
} else {
routeDetails.get(0).getBlock().goingActive();
}
if (src.getStart().getOccupancySensor() != null) {
src.getStart().getOccupancySensor().setState(Sensor.INACTIVE);
} else {
src.getStart().getBlock().goingInactive();
}
} catch (java.lang.NullPointerException e) {
log.error("error in clear route A " + e);
} catch (JmriException e) {
log.error("error in clear route A " + e);
}
if (log.isDebugEnabled()) {
log.debug(mUserName + " Going to clear routeDetails down " + routeDetails.size());
for (int i = 0; i < routeDetails.size(); i++) {
log.debug("Block at " + i + " " + routeDetails.get(i).getDisplayName());
}
}
if (routeDetails.size() > 1) {
//Should we just be usrc.pdating the block status and not the sensor
for (int i = 1; i < routeDetails.size() - 1; i++) {
if (log.isDebugEnabled()) {
log.debug(mUserName + " in loop Set active " + routeDetails.get(i).getDisplayName() + " " + routeDetails.get(i).getBlock().getSystemName());
}
try {
if (routeDetails.get(i).getOccupancySensor() != null) {
routeDetails.get(i).getOccupancySensor().setState(Sensor.ACTIVE);
} else {
routeDetails.get(i).getBlock().goingActive();
}
if (log.isDebugEnabled()) {
log.debug(mUserName + " in loop Set inactive " + routeDetails.get(i - 1).getDisplayName() + " " + routeDetails.get(i - 1).getBlock().getSystemName());
}
if (routeDetails.get(i - 1).getOccupancySensor() != null) {
routeDetails.get(i - 1).getOccupancySensor().setState(Sensor.INACTIVE);
} else {
routeDetails.get(i - 1).getBlock().goingInactive();
}
} catch (java.lang.NullPointerException e) {
log.error("error in clear route b " + e);
e.printStackTrace();
} catch (JmriException e) {
log.error("error in clear route b " + e);
}
}
try {
if (log.isDebugEnabled()) {
log.debug(mUserName + " out of loop Set active " + routeDetails.get(routeDetails.size() - 1).getDisplayName() + " " + routeDetails.get(routeDetails.size() - 1).getBlock().getSystemName());
}
//Get the last block an set it active.
if (routeDetails.get(routeDetails.size() - 1).getOccupancySensor() != null) {
routeDetails.get(routeDetails.size() - 1).getOccupancySensor().setState(Sensor.ACTIVE);
} else {
routeDetails.get(routeDetails.size() - 1).getBlock().goingActive();
}
if (log.isDebugEnabled()) {
log.debug(mUserName + " out of loop Set inactive " + routeDetails.get(routeDetails.size() - 2).getUserName() + " " + routeDetails.get(routeDetails.size() - 2).getBlock().getSystemName());
}
if (routeDetails.get(routeDetails.size() - 2).getOccupancySensor() != null) {
routeDetails.get(routeDetails.size() - 2).getOccupancySensor().setState(Sensor.INACTIVE);
} else {
routeDetails.get(routeDetails.size() - 2).getBlock().goingInactive();
}
} catch (java.lang.NullPointerException e) {
log.error("error in clear route c " + e);
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
log.error("error in clear route c " + e);
} catch (JmriException e) {
log.error("error in clear route c " + e);
}
}
}
}
setActiveEntryExit(false);
setRouteFrom(false);
setRouteTo(false);
routeDetails = null;
synchronized (this) {
lastSeenActiveBlockObject = null;
}
src.pd.cancelNXButtonTimeOut();
point.cancelNXButtonTimeOut();
src.getPoint().getPanel().getGlassPane().setVisible(false);
}
Aggregations