use of jmri.SignalHead in project JMRI by JMRI.
the class AutoTrainAction method executeAction.
// this method is called to execute the action, when the "When" event has happened.
// it is "public" because it may be called from a TransitSectionAction.
// djd debugging - need to check this out - probably useless, but harmless
@SuppressFBWarnings(value = "SWL_SLEEP_WITH_LOCK_HELD", justification = "used only by thread that can be stopped, no conflict with other threads expected")
public synchronized void executeAction(TransitSectionAction tsa) {
if (tsa == null) {
log.error("executeAction called with null TransitSectionAction");
return;
}
Sensor s = null;
float temp = 0.0f;
switch(tsa.getWhatCode()) {
case TransitSectionAction.PAUSE:
// pause for a number of fast minutes--e.g. station stop
if (_autoActiveTrain.getCurrentAllocatedSection().getNextSection() != null) {
// pause train if this is not the last Section
Thread tPause = _autoActiveTrain.pauseTrain(tsa.getDataWhat1());
tsa.setWaitingThread(tPause);
}
break;
case TransitSectionAction.SETMAXSPEED:
// set maximum train speed to value
temp = tsa.getDataWhat1();
_autoActiveTrain.setMaxSpeed(temp * 0.01f);
completedAction(tsa);
break;
case TransitSectionAction.SETCURRENTSPEED:
// set current speed either higher or lower than current value
temp = tsa.getDataWhat1();
float spd = temp * 0.01f;
if (spd > _autoActiveTrain.getMaxSpeed()) {
spd = _autoActiveTrain.getMaxSpeed();
}
_autoActiveTrain.setTargetSpeed(spd * _autoActiveTrain.getSpeedFactor());
if ((_autoActiveTrain.getRampRate() != AutoActiveTrain.RAMP_NONE) && (_autoActiveTrain.getAutoEngineer() != null)) {
// temporarily turn ramping off
_autoActiveTrain.setCurrentRampRate(AutoActiveTrain.RAMP_NONE);
// wait for train to achieve speed in a separate thread which will complete action
Runnable monTrainSpeed = new MonitorTrainSpeed(tsa);
Thread tMonTrainSpeed = new Thread(monTrainSpeed);
tsa.setWaitingThread(tMonTrainSpeed);
tMonTrainSpeed.start();
} else {
completedAction(tsa);
}
break;
case TransitSectionAction.RAMPTRAINSPEED:
// set current speed to target using specified ramp rate
temp = tsa.getDataWhat1();
float spdx = temp * 0.01f;
if (spdx > _autoActiveTrain.getMaxSpeed()) {
spdx = _autoActiveTrain.getMaxSpeed();
}
_autoActiveTrain.setTargetSpeed(spdx * _autoActiveTrain.getSpeedFactor());
completedAction(tsa);
break;
case TransitSectionAction.TOMANUALMODE:
// drop out of automated mode and allow manual throttle control
_autoActiveTrain.initiateWorking();
if ((tsa.getStringWhat() != null) && (!tsa.getStringWhat().equals(""))) {
// optional Done sensor was provided, listen to it
listenToDoneSensor(tsa);
}
completedAction(tsa);
break;
case TransitSectionAction.SETLIGHT:
// set light on or off
if (_autoActiveTrain.getAutoEngineer() != null) {
log.debug("{}: setting light (F0) to {}", _activeTrain.getTrainName(), tsa.getStringWhat());
if (tsa.getStringWhat().equals("On")) {
_autoActiveTrain.getAutoEngineer().setFunction(0, true);
} else if (tsa.getStringWhat().equals("Off")) {
_autoActiveTrain.getAutoEngineer().setFunction(0, false);
} else {
log.error("Incorrect Light ON/OFF setting *" + tsa.getStringWhat() + "*");
}
}
completedAction(tsa);
break;
case TransitSectionAction.STARTBELL:
// start bell (only works with sound decoder)
if (_autoActiveTrain.getSoundDecoder() && (_autoActiveTrain.getAutoEngineer() != null)) {
log.debug("{}: starting bell (F1)", _activeTrain.getTrainName());
_autoActiveTrain.getAutoEngineer().setFunction(1, true);
}
completedAction(tsa);
break;
case TransitSectionAction.STOPBELL:
// stop bell (only works with sound decoder)
if (_autoActiveTrain.getSoundDecoder() && (_autoActiveTrain.getAutoEngineer() != null)) {
log.debug("{}: stopping bell (F1)", _activeTrain.getTrainName());
_autoActiveTrain.getAutoEngineer().setFunction(1, false);
}
completedAction(tsa);
break;
case TransitSectionAction.SOUNDHORN:
// sound horn for specified number of milliseconds - done in separate thread
case TransitSectionAction.SOUNDHORNPATTERN:
// sound horn according to specified pattern - done in separate thread
if (_autoActiveTrain.getSoundDecoder()) {
log.debug("{}: sounding horn as specified in action", _activeTrain.getTrainName());
Runnable rHorn = new HornExecution(tsa);
Thread tHorn = new Thread(rHorn);
tsa.setWaitingThread(tHorn);
tHorn.start();
} else {
completedAction(tsa);
}
break;
case TransitSectionAction.LOCOFUNCTION:
// execute the specified decoder function
if (_autoActiveTrain.getAutoEngineer() != null) {
log.debug("{}: setting function {} to {}", _activeTrain.getTrainName(), tsa.getDataWhat1(), tsa.getStringWhat());
int fun = tsa.getDataWhat1();
if (tsa.getStringWhat().equals("On")) {
_autoActiveTrain.getAutoEngineer().setFunction(fun, true);
} else if (tsa.getStringWhat().equals("Off")) {
_autoActiveTrain.getAutoEngineer().setFunction(fun, false);
}
}
completedAction(tsa);
break;
case TransitSectionAction.SETSENSORACTIVE:
// set specified sensor active
s = InstanceManager.sensorManagerInstance().getSensor(tsa.getStringWhat());
if (s != null) {
// if sensor is already active, set it to inactive first
if (s.getKnownState() == Sensor.ACTIVE) {
try {
s.setState(Sensor.INACTIVE);
} catch (jmri.JmriException reason) {
log.error("Exception when toggling Sensor " + tsa.getStringWhat() + " Inactive - " + reason);
}
}
try {
s.setState(Sensor.ACTIVE);
} catch (jmri.JmriException reason) {
log.error("Exception when setting Sensor " + tsa.getStringWhat() + " Active - " + reason);
}
} else if ((tsa.getStringWhat() != null) && (!tsa.getStringWhat().equals(""))) {
log.error("Could not find Sensor " + tsa.getStringWhat());
} else {
log.error("Sensor not specified for Action");
}
break;
case TransitSectionAction.SETSENSORINACTIVE:
// set specified sensor inactive
s = InstanceManager.sensorManagerInstance().getSensor(tsa.getStringWhat());
if (s != null) {
if (s.getKnownState() == Sensor.ACTIVE) {
try {
s.setState(Sensor.ACTIVE);
} catch (jmri.JmriException reason) {
log.error("Exception when toggling Sensor " + tsa.getStringWhat() + " Active - " + reason);
}
}
try {
s.setState(Sensor.INACTIVE);
} catch (jmri.JmriException reason) {
log.error("Exception when setting Sensor " + tsa.getStringWhat() + " Inactive - " + reason);
}
} else if ((tsa.getStringWhat() != null) && (!tsa.getStringWhat().equals(""))) {
log.error("Could not find Sensor " + tsa.getStringWhat());
} else {
log.error("Sensor not specified for Action");
}
break;
case TransitSectionAction.HOLDSIGNAL:
// set specified signalhead or signalmast to HELD
SignalMast sm = null;
SignalHead sh = null;
String sName = tsa.getStringWhat();
sm = InstanceManager.getDefault(SignalMastManager.class).getSignalMast(sName);
if (sm == null) {
sh = InstanceManager.getDefault(SignalHeadManager.class).getSignalHead(sName);
if (sh == null) {
log.error("{}: Could not find SignalMast or SignalHead named '{}'", _activeTrain.getTrainName(), sName);
} else {
log.debug("{}: setting signalHead '{}' to HELD", _activeTrain.getTrainName(), sName);
sh.setHeld(true);
}
} else {
log.debug("{}: setting signalMast '{}' to HELD", _activeTrain.getTrainName(), sName);
sm.setHeld(true);
}
break;
case TransitSectionAction.RELEASESIGNAL:
// set specified signalhead or signalmast to NOT HELD
sm = null;
sh = null;
sName = tsa.getStringWhat();
sm = InstanceManager.getDefault(SignalMastManager.class).getSignalMast(sName);
if (sm == null) {
sh = InstanceManager.getDefault(SignalHeadManager.class).getSignalHead(sName);
if (sh == null) {
log.error("{}: Could not find SignalMast or SignalHead named '{}'", _activeTrain.getTrainName(), sName);
} else {
log.debug("{}: setting signalHead '{}' to NOT HELD", _activeTrain.getTrainName(), sName);
sh.setHeld(false);
}
} else {
log.debug("{}: setting signalMast '{}' to NOT HELD", _activeTrain.getTrainName(), sName);
sm.setHeld(false);
}
break;
default:
log.error("illegal What code - " + tsa.getWhatCode() + " - in call to executeAction");
break;
}
}
use of jmri.SignalHead in project JMRI by JMRI.
the class SignalHeadIcon method performMouseClicked.
/**
* This was added in so that the layout editor can handle the mouseclicked
* when zoomed in
*/
public void performMouseClicked(java.awt.event.MouseEvent e) {
if (e.isMetaDown() || e.isAltDown()) {
return;
}
if (getSignalHead() == null) {
log.error("No turnout connection, can't process click");
return;
}
switch(clickMode) {
case 0:
switch(getSignalHead().getAppearance()) {
case jmri.SignalHead.RED:
case jmri.SignalHead.FLASHRED:
getSignalHead().setAppearance(jmri.SignalHead.YELLOW);
break;
case jmri.SignalHead.YELLOW:
case jmri.SignalHead.FLASHYELLOW:
getSignalHead().setAppearance(jmri.SignalHead.GREEN);
break;
case jmri.SignalHead.GREEN:
case jmri.SignalHead.FLASHGREEN:
getSignalHead().setAppearance(jmri.SignalHead.RED);
break;
default:
getSignalHead().setAppearance(jmri.SignalHead.RED);
break;
}
return;
case 1:
getSignalHead().setLit(!getSignalHead().getLit());
return;
case 2:
getSignalHead().setHeld(!getSignalHead().getHeld());
return;
case 3:
SignalHead sh = getSignalHead();
int[] states = sh.getValidStates();
int state = sh.getAppearance();
for (int i = 0; i < states.length; i++) {
// if (log.isDebugEnabled()) log.debug("state= "+state+" states["+i+"]= "+states[i]);
if (state == states[i]) {
i++;
if (i >= states.length) {
i = 0;
}
state = states[i];
break;
}
}
sh.setAppearance(state);
if (log.isDebugEnabled()) {
log.debug("Set state= " + state);
}
return;
default:
log.error("Click in mode " + clickMode);
}
}
use of jmri.SignalHead in project JMRI by JMRI.
the class LayoutTurnout method setSignalD2Name.
public void setSignalD2Name(String signalHead) {
if (signalHead == null || signalHead.equals("")) {
signalD2HeadNamed = null;
return;
}
SignalHead head = InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(signalHead);
if (head != null) {
signalD2HeadNamed = InstanceManager.getDefault(jmri.NamedBeanHandleManager.class).getNamedBeanHandle(signalHead, head);
} else {
signalD2HeadNamed = null;
log.error("Signal Head " + signalHead + " Not found for turnout " + getTurnoutName());
}
}
use of jmri.SignalHead in project JMRI by JMRI.
the class LayoutTurnout method setSignalD1Name.
public void setSignalD1Name(String signalHead) {
if (signalHead == null || signalHead.equals("")) {
signalD1HeadNamed = null;
return;
}
SignalHead head = InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(signalHead);
if (head != null) {
signalD1HeadNamed = InstanceManager.getDefault(jmri.NamedBeanHandleManager.class).getNamedBeanHandle(signalHead, head);
} else {
signalD1HeadNamed = null;
log.error("Signal Head " + signalHead + " Not found for turnout " + getTurnoutName());
}
}
use of jmri.SignalHead in project JMRI by JMRI.
the class LayoutTurnout method setSignalB1Name.
public void setSignalB1Name(String signalHead) {
if (signalHead == null || signalHead.equals("")) {
signalB1HeadNamed = null;
return;
}
SignalHead head = InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(signalHead);
if (head != null) {
signalB1HeadNamed = InstanceManager.getDefault(jmri.NamedBeanHandleManager.class).getNamedBeanHandle(signalHead, head);
} else {
signalB1HeadNamed = null;
log.error("Signal Head " + signalHead + " Not found for turnout " + getTurnoutName());
}
}
Aggregations