use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method setLight.
/*
* deprecated in favor of the implementations of the {@code do*} methods in
* {@link jmri.server.json.JsonHttpService}.
* @deprecated since 4.5.1
*/
@Deprecated
public static void setLight(Locale locale, String name, JsonNode data) throws JsonException {
try {
Light light = InstanceManager.lightManagerInstance().getBySystemName(name);
if (data.path(USERNAME).isTextual()) {
light.setUserName(data.path(USERNAME).asText());
}
if (data.path(COMMENT).isTextual()) {
light.setComment(data.path(COMMENT).asText());
}
int state = data.path(STATE).asInt(UNKNOWN);
switch(state) {
case OFF:
InstanceManager.lightManagerInstance().getLight(name).setState(Light.OFF);
break;
case ON:
InstanceManager.lightManagerInstance().getLight(name).setState(Light.ON);
break;
case UNKNOWN:
// silently ignore
break;
default:
throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", LIGHT, state));
}
} catch (NullPointerException e) {
log.error("Unable to get light [{}].", name);
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", LIGHT, name));
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method setSensor.
@Deprecated
public static void setSensor(Locale locale, String name, JsonNode data) throws JsonException {
try {
Sensor sensor = InstanceManager.sensorManagerInstance().getSensor(name);
if (data.path(USERNAME).isTextual()) {
sensor.setUserName(data.path(USERNAME).asText());
}
if (data.path(INVERTED).isBoolean()) {
sensor.setInverted(data.path(INVERTED).asBoolean());
}
if (data.path(COMMENT).isTextual()) {
sensor.setComment(data.path(COMMENT).asText());
}
int state = data.path(STATE).asInt(UNKNOWN);
switch(state) {
case Sensor.ACTIVE:
sensor.setKnownState(Sensor.ACTIVE);
break;
case INACTIVE:
sensor.setKnownState(Sensor.INACTIVE);
break;
case UNKNOWN:
// silently ignore
break;
default:
throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", SENSOR, state));
}
} catch (NullPointerException e) {
log.error("Unable to get sensor [{}].", name);
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", SENSOR, name));
} catch (JmriException ex) {
throw new JsonException(500, ex);
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method setSignalMast.
// TODO: test for HELD and DARK aspects
public static void setSignalMast(Locale locale, String name, JsonNode data) throws JsonException {
try {
SignalMast signalMast = InstanceManager.getDefault(jmri.SignalMastManager.class).getSignalMast(name);
if (data.path(USERNAME).isTextual()) {
signalMast.setUserName(data.path(USERNAME).asText());
}
if (data.path(COMMENT).isTextual()) {
signalMast.setComment(data.path(COMMENT).asText());
}
String aspect = data.path(ASPECT).asText();
if (signalMast.getValidAspects().contains(aspect)) {
signalMast.setAspect(aspect);
} else {
throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", SIGNAL_MAST, aspect));
}
} catch (NullPointerException e) {
log.error("Unable to get signal mast [{}].", name);
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", SIGNAL_MAST, name));
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method getRoute.
@Deprecated
public static JsonNode getRoute(Locale locale, String name) throws JsonException {
ObjectNode root = mapper.createObjectNode();
root.put(TYPE, ROUTE);
ObjectNode data = root.putObject(DATA);
try {
Route route = InstanceManager.getDefault(jmri.RouteManager.class).getRoute(name);
SensorManager s = InstanceManager.sensorManagerInstance();
data.put(NAME, route.getSystemName());
data.put(USERNAME, route.getUserName());
data.put(COMMENT, route.getComment());
Sensor sensor = s.getSensor(route.getTurnoutsAlignedSensor());
if (sensor != null) {
switch(sensor.getKnownState()) {
case Sensor.ACTIVE:
data.put(STATE, ACTIVE);
break;
case Sensor.INACTIVE:
data.put(STATE, INACTIVE);
break;
case Sensor.INCONSISTENT:
data.put(STATE, INCONSISTENT);
break;
case Sensor.UNKNOWN:
default:
data.put(STATE, UNKNOWN);
break;
}
} else {
data.put(STATE, UNKNOWN);
}
} catch (NullPointerException e) {
log.error("Unable to get route [{}].", name);
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", ROUTE, name));
}
return root;
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method getConsist.
/**
* Get the JSON representation of a consist.
*
* The JSON representation is an object with the following data attributes:
* <ul>
* <li>address - integer address</li>
* <li>isLongAddress - boolean true if address is long, false if short</li>
* <li>type - integer, see {@link jmri.Consist#getConsistType() }</li>
* <li>id - string with consist Id</li>
* <li>sizeLimit - the maximum number of locomotives the consist can
* contain</li>
* <li>engines - array listing every locomotive in the consist. Each entry
* in the array contains the following attributes:
* <ul>
* <li>address - integer address</li>
* <li>isLongAddress - boolean true if address is long, false if short</li>
* <li>forward - boolean true if the locomotive running is forward in the
* consists</li>
* <li>position - integer locomotive's position in the consist</li>
* </ul>
* </ul>
*
* @param locale The locale to throw exceptions in.
* @param address The address of the consist to get.
* @return The JSON representation of the consist.
* @throws JsonException This exception has code 404 if the consist does not
* exist.
*/
public static JsonNode getConsist(Locale locale, DccLocoAddress address) throws JsonException {
try {
if (InstanceManager.getDefault(jmri.ConsistManager.class).getConsistList().contains(address)) {
ObjectNode root = mapper.createObjectNode();
root.put(TYPE, CONSIST);
ObjectNode data = root.putObject(DATA);
Consist consist = InstanceManager.getDefault(jmri.ConsistManager.class).getConsist(address);
data.put(ADDRESS, consist.getConsistAddress().getNumber());
data.put(IS_LONG_ADDRESS, consist.getConsistAddress().isLongAddress());
data.put(TYPE, consist.getConsistType());
ArrayNode engines = data.putArray(ENGINES);
for (DccLocoAddress l : consist.getConsistList()) {
ObjectNode engine = mapper.createObjectNode();
engine.put(ADDRESS, l.getNumber());
engine.put(IS_LONG_ADDRESS, l.isLongAddress());
engine.put(FORWARD, consist.getLocoDirection(l));
engine.put(POSITION, consist.getPosition(l));
engines.add(engine);
}
data.put(ID, consist.getConsistID());
data.put(SIZE_LIMIT, consist.sizeLimit());
return root;
} else {
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", CONSIST, address.toString()));
}
} catch (NullPointerException ex) {
// NOI18N
throw new JsonException(503, Bundle.getMessage(locale, "ErrorNoConsistManager"));
}
}
Aggregations