use of jmri.JmriException in project JMRI by JMRI.
the class BeanTableDataModel method setValueAt.
@Override
public void setValueAt(Object value, int row, int col) {
switch(col) {
case USERNAMECOL:
// check to see if user name already exists
if (((String) value).equals("")) {
value = null;
} else {
NamedBean nB = getByUserName((String) value);
if (nB != null) {
log.error("User name is not unique " + value);
String msg = Bundle.getMessage("WarningUserName", new Object[] { ("" + value) });
JOptionPane.showMessageDialog(null, msg, Bundle.getMessage("WarningTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
}
NamedBean nBean = getBySystemName(sysNameList.get(row));
nBean.setUserName((String) value);
if (nbMan.inUse(sysNameList.get(row), nBean)) {
String msg = Bundle.getMessage("UpdateToUserName", new Object[] { getBeanType(), value, sysNameList.get(row) });
int optionPane = JOptionPane.showConfirmDialog(null, msg, Bundle.getMessage("UpdateToUserNameTitle"), JOptionPane.YES_NO_OPTION);
if (optionPane == JOptionPane.YES_OPTION) {
//This will update the bean reference from the systemName to the userName
try {
nbMan.updateBeanFromSystemToUser(nBean);
} catch (JmriException ex) {
//We should never get an exception here as we already check that the username is not valid
}
}
}
fireTableRowsUpdated(row, row);
break;
case COMMENTCOL:
getBySystemName(sysNameList.get(row)).setComment((String) value);
fireTableRowsUpdated(row, row);
break;
case VALUECOL:
// button fired, swap state
NamedBean t = getBySystemName(sysNameList.get(row));
clickOn(t);
break;
case DELETECOL:
// button fired, delete Bean
deleteBean(row, col);
break;
default:
break;
}
}
use of jmri.JmriException in project JMRI by JMRI.
the class JsonSensorHttpService method doPost.
@Override
public JsonNode doPost(String type, String name, JsonNode data, Locale locale) throws JsonException {
Sensor sensor = InstanceManager.getDefault(SensorManager.class).getSensor(name);
if (sensor == null) {
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", SENSOR, name));
}
if (data.path(JSON.USERNAME).isTextual()) {
sensor.setUserName(data.path(JSON.USERNAME).asText());
}
if (data.path(JSON.INVERTED).isBoolean()) {
sensor.setInverted(data.path(JSON.INVERTED).asBoolean());
}
if (data.path(JSON.COMMENT).isTextual()) {
sensor.setComment(data.path(JSON.COMMENT).asText());
}
int state = data.path(JSON.STATE).asInt(JSON.UNKNOWN);
try {
switch(state) {
case JSON.ACTIVE:
sensor.setKnownState(Sensor.ACTIVE);
break;
case JSON.INACTIVE:
sensor.setKnownState(Sensor.INACTIVE);
break;
case JSON.INCONSISTENT:
case JSON.UNKNOWN:
// silently ignore
break;
default:
throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", SENSOR, state));
}
} catch (JmriException ex) {
throw new JsonException(500, ex);
}
return this.doGet(type, name, locale);
}
use of jmri.JmriException in project JMRI by JMRI.
the class WarrantTest method testWarrant.
/**
* tests depend on the order of execution.
* So this will be one large test.
*/
@Test
public void testWarrant() {
_OBlockMgr = InstanceManager.getDefault(OBlockManager.class);
OBlock bWest = _OBlockMgr.createNewOBlock("OB1", "West");
OBlock bEast = _OBlockMgr.createNewOBlock("OB2", "East");
OBlock bNorth = _OBlockMgr.createNewOBlock("OB3", "North");
OBlock bSouth = _OBlockMgr.createNewOBlock("OB4", "South");
Assert.assertEquals("OBlock", bNorth, _OBlockMgr.getOBlock("North"));
Assert.assertEquals("OBlock", bEast, _OBlockMgr.getOBlock("OB2"));
_portalMgr = InstanceManager.getDefault(PortalManager.class);
Portal pNorthWest = _portalMgr.createNewPortal(null, "NorthWest");
pNorthWest.setToBlock(bWest, false);
pNorthWest.setFromBlock(bNorth, false);
Portal pSouthWest = _portalMgr.createNewPortal(null, "SouthWest");
pSouthWest.setToBlock(bWest, false);
pSouthWest.setFromBlock(bSouth, false);
Assert.assertEquals("Portal", pNorthWest, _portalMgr.getPortal("NorthWest"));
Assert.assertEquals("Portal Block", bSouth, _portalMgr.getPortal("SouthWest").getFromBlock());
Assert.assertEquals("Portal", pSouthWest, bSouth.getPortalByName("SouthWest"));
Assert.assertEquals("Portal Block", "West", _portalMgr.getPortal("NorthWest").getToBlockName());
Assert.assertEquals("Portal Block", "North", _portalMgr.getPortal("NorthWest").getFromBlockName());
Portal pNorthEast = _portalMgr.createNewPortal(null, "NorthEast");
pNorthEast.setToBlock(_OBlockMgr.getOBlock("OB2"), false);
pNorthEast.setFromBlock(_OBlockMgr.getOBlock("North"), false);
Portal pSouthEast = _portalMgr.createNewPortal(null, "SouthEast");
OBlock east = _OBlockMgr.getOBlock("OB2");
pSouthEast.setToBlock(east, false);
pSouthEast.setFromBlock(_OBlockMgr.getOBlock("South"), false);
Assert.assertEquals("Portal Block", east, _portalMgr.getPortal("SouthEast").getToBlock());
Assert.assertEquals("Portal Block", "West", _portalMgr.getPortal("NorthWest").getToBlockName());
Assert.assertEquals("Portal Block", _OBlockMgr.getOBlock("South"), _portalMgr.getPortal("SouthWest").getFromBlock());
_turnoutMgr = InstanceManager.turnoutManagerInstance();
Turnout northSwitch = _turnoutMgr.newTurnout("IT1", "NorthSwitch");
ArrayList<BeanSetting> settings = new ArrayList<BeanSetting>();
settings.add(new BeanSetting(northSwitch, "NorthSwitch", Turnout.CLOSED));
OBlock north = _OBlockMgr.getOBlock("North");
OPath path = new OPath("NorthToWest", north, null, _portalMgr.getPortal("NorthWest"), settings);
north.addPath(path);
settings = new ArrayList<BeanSetting>();
settings.add(new BeanSetting(northSwitch, "NorthSwitch", Turnout.THROWN));
path = new OPath("NorthToEast", north, null, _portalMgr.getPortal("NorthEast"), settings);
north.addPath(path);
Assert.assertEquals("Path Block", path, north.getPathByName("NorthToEast"));
Assert.assertEquals("Path Block", "NorthToWest", north.getPathByName("NorthToWest").getName());
Turnout southSwitch = _turnoutMgr.newTurnout("IT2", "SouthSwitch");
OBlock south = _OBlockMgr.getOBlock("South");
settings = new ArrayList<BeanSetting>();
settings.add(new BeanSetting(southSwitch, "SouthSwitch", Turnout.THROWN));
path = new OPath("SouthToEast", south, null, _portalMgr.getPortal("SouthEast"), settings);
south.addPath(path);
settings = new ArrayList<BeanSetting>();
settings.add(new BeanSetting(southSwitch, "SouthSwitch", Turnout.CLOSED));
path = new OPath("SouthToWest", south, null, south.getPortalByName("SouthWest"), settings);
south.addPath(path);
Assert.assertEquals("Path Block", path, south.getPathByName("SouthToWest"));
Assert.assertEquals("Path Block", "SouthToEast", south.getPathByName("SouthToEast").getName());
settings = new ArrayList<BeanSetting>();
OBlock block = _OBlockMgr.getOBlock("West");
path = new OPath("SouthToNorth", block, _portalMgr.getPortal("NorthWest"), _portalMgr.getPortal("SouthWest"), settings);
_OBlockMgr.getOBlock("West").addPath(path);
Assert.assertEquals("Path Block", path, block.getPathByName("SouthToNorth"));
settings = new ArrayList<BeanSetting>();
block = _OBlockMgr.getOBlock("East");
path = new OPath("NorthToSouth", block, south.getPortalByName("SouthEast"), north.getPortalByName("NorthEast"), settings);
_OBlockMgr.getOBlock("East").addPath(path);
Assert.assertEquals("Path Block", path, block.getPathByName("NorthToSouth"));
_sensorMgr = InstanceManager.getDefault(SensorManager.class);
Sensor sWest = _sensorMgr.newSensor("IS1", "WestSensor");
Sensor sEast = _sensorMgr.newSensor("IS2", "EastSensor");
Sensor sNorth = _sensorMgr.newSensor("IS3", "NorthSensor");
Sensor sSouth = _sensorMgr.newSensor("IS4", "SouthSensor");
bWest.setSensor("WestSensor");
bEast.setSensor("IS2");
bNorth.setSensor("NorthSensor");
bSouth.setSensor("IS4");
Assert.assertEquals("Sensor Block", sNorth, bNorth.getSensor());
Assert.assertEquals("Sensor Block", sSouth, bSouth.getSensor());
try {
sWest.setState(Sensor.INACTIVE);
sEast.setState(Sensor.ACTIVE);
sNorth.setState(Sensor.INACTIVE);
sSouth.setState(Sensor.ACTIVE);
} catch (JmriException je) {
}
Assert.assertEquals("Block Detection 1", OBlock.UNOCCUPIED, bWest.getState());
Assert.assertEquals("Block Detection 2", OBlock.OCCUPIED, bEast.getState());
Warrant warrant = new Warrant("IW0", "AllTestWarrant");
bWest.allocate(warrant);
bEast.allocate(warrant);
Assert.assertEquals("Block Detection 3", OBlock.UNOCCUPIED | OBlock.ALLOCATED, bWest.getState());
Assert.assertEquals("Block Detection 4", OBlock.OCCUPIED | OBlock.ALLOCATED, bEast.getState());
try {
sEast.setState(Sensor.INACTIVE);
sSouth.setState(Sensor.INACTIVE);
// start block of warrant
sNorth.setState(Sensor.ACTIVE);
} catch (JmriException je) {
}
bWest.deAllocate(warrant);
bEast.deAllocate(warrant);
Assert.assertEquals("Block Detection 5", OBlock.UNOCCUPIED, bWest.getState());
Assert.assertEquals("Block Detection 6", OBlock.UNOCCUPIED, bEast.getState());
ArrayList<BlockOrder> orders = new ArrayList<BlockOrder>();
orders.add(new BlockOrder(_OBlockMgr.getOBlock("North"), "NorthToWest", "", "NorthWest"));
BlockOrder viaOrder = new BlockOrder(_OBlockMgr.getOBlock("West"), "SouthToNorth", "NorthWest", "SouthWest");
orders.add(viaOrder);
BlockOrder lastOrder = new BlockOrder(_OBlockMgr.getOBlock("South"), "SouthToWest", "SouthWest", null);
orders.add(lastOrder);
warrant.setViaOrder(viaOrder);
warrant.setBlockOrders(orders);
Assert.assertEquals("BlockOrder", warrant.getLastOrder().toString(), lastOrder.toString());
Assert.assertEquals("BlockOrder", warrant.getViaOrder().toString(), viaOrder.toString());
String msg = warrant.allocateRoute(orders);
Assert.assertNull("allocateRoute - " + msg, msg);
warrant.deAllocate();
warrant.setThrottleCommands(new ArrayList<ThrottleSetting>());
warrant.addThrottleCommand(new ThrottleSetting(0, "Speed", "0.0", "North"));
warrant.addThrottleCommand(new ThrottleSetting(10, "Speed", "0.4", "North"));
warrant.addThrottleCommand(new ThrottleSetting(100, "NoOp", "Enter Block", "West"));
warrant.addThrottleCommand(new ThrottleSetting(100, "Speed", "0.5", "West"));
warrant.addThrottleCommand(new ThrottleSetting(100, "NoOp", "Enter Block", "South"));
warrant.addThrottleCommand(new ThrottleSetting(100, "Speed", "0.3", "South"));
warrant.addThrottleCommand(new ThrottleSetting(100, "Speed", "0.0", "South"));
List<ThrottleSetting> list = warrant.getThrottleCommands();
Assert.assertEquals("ThrottleCommands", 7, list.size());
// DccLocoAddress dccAddress = new DccLocoAddress(999, true);
// Assert.assertNotNull("dccAddress", dccAddress);
warrant.setDccAddress("999(L)");
msg = warrant.setRoute(0, orders);
Assert.assertNull("setRoute - " + msg, msg);
msg = warrant.checkStartBlock(Warrant.MODE_RUN);
Assert.assertNull("checkStartBlock - " + msg, msg);
msg = warrant.checkRoute();
Assert.assertNull("checkRoute - " + msg, msg);
warrant.setTrainName("TestTrain");
PropertyChangeListener listener = new WarrantListener(warrant);
Assert.assertNotNull("PropertyChangeListener", listener);
warrant.addPropertyChangeListener(listener);
msg = warrant.setRunMode(Warrant.MODE_RUN, null, null, null, false);
Assert.assertNull("setRunMode - " + msg, msg);
jmri.util.JUnitUtil.waitFor(() -> {
String m = warrant.getRunningMessage();
return m.endsWith("Cmd #2.");
}, "Train starts to move after 2nd command");
// nothing specific to wait for...
jmri.util.JUnitUtil.releaseThread(this);
jmri.util.ThreadingUtil.runOnLayout(() -> {
try {
sWest.setState(Sensor.ACTIVE);
} catch (jmri.JmriException e) {
Assert.fail("Unexpected Exception: " + e);
}
});
jmri.util.JUnitUtil.releaseThread(this);
jmri.util.ThreadingUtil.runOnLayout(() -> {
try {
sSouth.setState(Sensor.ACTIVE);
} catch (jmri.JmriException e) {
Assert.fail("Unexpected Exception: " + e);
}
});
jmri.util.JUnitUtil.releaseThread(this);
// confirm one message logged
jmri.util.JUnitAppender.assertWarnMessage("RosterSpeedProfile not found. Using default ThrottleFactor 0.75");
// wait for done
jmri.util.JUnitUtil.waitFor(() -> {
return warrant.getThrottle() == null;
}, "engineer blocked");
msg = warrant.getRunningMessage();
Assert.assertEquals("getRunningMessage", "Idle", msg);
}
use of jmri.JmriException in project JMRI by JMRI.
the class EntryExitPairs method automaticallyDiscoverEntryExitPairs.
/**
* Discover all possible valid source and destination Signal Mast Logic pairs
* on all Layout Editor panels.
*
* @param editor The Layout Editor panel
* @param interlockType Integer value representing the type of interlocking, one of
* SETUPTURNOUTSONLY, SETUPSIGNALMASTLOGIC or FULLINTERLOCK
* @throws JmriException when an error occurs during discovery
*/
public void automaticallyDiscoverEntryExitPairs(LayoutEditor editor, int interlockType) throws JmriException {
//This is almost a duplicate of that in the DefaultSignalMastLogicManager
runWhenStabilised = false;
jmri.jmrit.display.layoutEditor.LayoutBlockManager lbm = InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class);
if (!lbm.isAdvancedRoutingEnabled()) {
throw new JmriException("advanced routing not enabled");
}
if (!lbm.routingStablised()) {
runWhenStabilised = true;
toUseWhenStable = editor;
interlockTypeToUseWhenStable = interlockType;
log.debug("Layout block routing has not yet stabilised, discovery will happen once it has");
return;
}
Hashtable<NamedBean, ArrayList<NamedBean>> validPaths = lbm.getLayoutBlockConnectivityTools().discoverValidBeanPairs(editor, Sensor.class, LayoutBlockConnectivityTools.SENSORTOSENSOR);
Enumeration<NamedBean> en = validPaths.keys();
EntryExitPairs eep = this;
while (en.hasMoreElements()) {
NamedBean key = en.nextElement();
ArrayList<NamedBean> validDestMast = validPaths.get(key);
if (validDestMast.size() > 0) {
eep.addNXSourcePoint(key, editor);
for (int i = 0; i < validDestMast.size(); i++) {
if (!eep.isDestinationValid(key, validDestMast.get(i), editor)) {
eep.addNXDestination(key, validDestMast.get(i), editor);
eep.setEntryExitType(key, editor, validDestMast.get(i), interlockType);
}
}
}
}
firePropertyChange("autoGenerateComplete", null, null);
}
use of jmri.JmriException in project JMRI by JMRI.
the class SerialSensorManager method createSystemName.
@Override
public String createSystemName(String curAddress, String prefix) throws JmriException {
String tmpSName = "";
if (curAddress.contains(":")) {
//Address format passed is in the form node:address
int seperator = curAddress.indexOf(":");
try {
nAddress = Integer.valueOf(curAddress.substring(0, seperator)).intValue();
bitNum = Integer.valueOf(curAddress.substring(seperator + 1)).intValue();
} catch (NumberFormatException ex) {
log.error("Unable to convert " + curAddress + " Hardware Address to a number");
throw new JmriException("Unable to convert " + curAddress + " to a valid Hardware Address");
}
tmpSName = _memo.makeSystemName("S", nAddress, bitNum);
} else if (curAddress.contains("B") || (curAddress.contains("b"))) {
curAddress = curAddress.toUpperCase();
try {
//We do this to simply check that we have numbers in the correct places ish
Integer.parseInt(curAddress.substring(0, 1));
int b = (curAddress.toUpperCase()).indexOf("B") + 1;
Integer.parseInt(curAddress.substring(b));
} catch (NumberFormatException ex) {
log.error("Unable to convert " + curAddress + " Hardware Address to a number");
throw new JmriException("Unable to convert " + curAddress + " to a valid Hardware Address");
}
tmpSName = prefix + typeLetter() + curAddress;
bitNum = _memo.getBitFromSystemName(tmpSName);
nAddress = _memo.getNodeAddressFromSystemName(tmpSName);
} else {
try {
//We do this to simply check that the value passed is a number!
Integer.parseInt(curAddress);
} catch (NumberFormatException ex) {
log.error("Unable to convert " + curAddress + " Hardware Address to a number");
throw new JmriException("Unable to convert " + curAddress + " to a valid Hardware Address");
}
tmpSName = prefix + typeLetter() + curAddress;
bitNum = _memo.getBitFromSystemName(tmpSName);
nAddress = _memo.getNodeAddressFromSystemName(tmpSName);
}
return tmpSName;
}
Aggregations