use of jmri.JmriException in project JMRI by JMRI.
the class JsonSignalHeadSocketServiceTest method testSignalHeadChange.
@Test
public void testSignalHeadChange() {
try {
//create a signalhead for testing
String sysName = "IH1";
String userName = "SH1";
SignalHead s = new jmri.implementation.VirtualSignalHead(sysName, userName);
jmri.InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
Assert.assertNotNull(s);
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName);
JsonSignalHeadSocketService service = new JsonSignalHeadSocketService(connection);
service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
//signalhead defaults to Dark
Assert.assertEquals(SignalHead.DARK, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
//change to Green, and wait for change to show up, then verify
s.setAppearance(SignalHead.GREEN);
JUnitUtil.waitFor(() -> {
return s.getState() == SignalHead.GREEN;
}, "SignalHead is now GREEN");
Assert.assertEquals(SignalHead.GREEN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
//change to Red, and wait for change to show up, then verify
s.setAppearance(SignalHead.RED);
JUnitUtil.waitFor(() -> {
return s.getState() == SignalHead.RED;
}, "SignalHead is now RED");
Assert.assertEquals(SignalHead.RED, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
service.onClose();
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.JmriException in project JMRI by JMRI.
the class SerialTurnoutManager 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) {
throw new JmriException("Unable to convert " + curAddress + " to a valid Hardware Address");
}
tmpSName = SerialAddress.makeSystemName("T", nAddress, bitNum);
} else {
tmpSName = prefix + "T" + curAddress;
try {
bitNum = SerialAddress.getBitFromSystemName(tmpSName);
nAddress = SerialAddress.getNodeAddressFromSystemName(tmpSName);
} catch (NumberFormatException ex) {
throw new JmriException("Unable to convert " + curAddress + " to a valid Hardware Address");
}
}
return (tmpSName);
}
use of jmri.JmriException in project JMRI by JMRI.
the class TamsSensorManager method getNextValidAddress.
@Override
public String getNextValidAddress(String curAddress, String prefix) {
String tmpSName = "";
try {
tmpSName = createSystemName(curAddress, prefix);
} catch (JmriException ex) {
jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class).showInfoMessage("Error", "Unable to convert " + curAddress + " to a valid Hardware Address", "" + ex, "", true, false);
return null;
}
//Check to determine if the systemName is in use, return null if it is,
//otherwise return the next valid address.
Sensor s = getBySystemName(tmpSName);
if (s != null) {
port++;
while (port < 17) {
try {
tmpSName = createSystemName(board + ":" + port, prefix);
} catch (Exception e) {
log.error("Error creating system name for " + board + ":" + port);
}
s = getBySystemName(tmpSName);
if (s == null) {
StringBuilder sb = new StringBuilder();
sb.append(board);
sb.append(":");
//Little work around to pad single digit address out.
padPortNumber(port, sb);
return sb.toString();
}
port++;
}
return null;
} else {
StringBuilder sb = new StringBuilder();
sb.append(board);
sb.append(":");
//Little work around to pad single digit address out.
padPortNumber(port, sb);
return sb.toString();
}
}
use of jmri.JmriException in project JMRI by JMRI.
the class JsonPowerHttpService method doGet.
@Override
public JsonNode doGet(String type, String name, Locale locale) throws JsonException {
ObjectNode root = mapper.createObjectNode();
root.put(TYPE, POWER);
ObjectNode data = root.putObject(DATA);
try {
PowerManager manager = InstanceManager.getDefault(PowerManager.class);
if (name != null && !name.isEmpty()) {
for (PowerManager pm : InstanceManager.getList(PowerManager.class)) {
if (pm.getUserName().equals(name)) {
manager = pm;
data.put(NAME, name);
}
}
}
switch(manager.getPower()) {
case PowerManager.OFF:
data.put(STATE, OFF);
break;
case PowerManager.ON:
data.put(STATE, ON);
break;
default:
data.put(STATE, UNKNOWN);
break;
}
data.put(DEFAULT, false);
if (manager.equals(InstanceManager.getDefault(PowerManager.class))) {
data.put(DEFAULT, true);
}
} catch (JmriException e) {
log.error("Unable to get Power state.", e);
throw new JsonException(500, Bundle.getMessage(locale, "ErrorPower"));
} catch (NullPointerException e) {
// No PowerManager is defined; just report it as UNKNOWN
data.put(STATE, UNKNOWN);
}
return root;
}
use of jmri.JmriException in project JMRI by JMRI.
the class JsonMemorySocketServiceTest method testOnMessageChange.
public void testOnMessageChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message;
JsonMemorySocketService service = new JsonMemorySocketService(connection);
MemoryManager manager = InstanceManager.getDefault(MemoryManager.class);
Memory memory1 = manager.provideMemory("IM1");
// Memory "close"
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1").put(JSON.VALUE, "close");
service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
Assert.assertEquals("close", memory1.getValue());
// Memory "throw"
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1").put(JSON.VALUE, "throw");
service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
Assert.assertEquals("throw", memory1.getValue());
// Memory UNKNOWN - remains ON
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1").putNull(JSON.VALUE);
service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
Assert.assertEquals(null, memory1.getValue());
memory1.setValue("throw");
// Memory no value
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1");
JsonException exception = null;
try {
service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertEquals("throw", memory1.getValue());
Assert.assertNull(exception);
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
Aggregations