use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method setSignalHead.
public static void setSignalHead(Locale locale, String name, JsonNode data) throws JsonException {
try {
SignalHead signalHead = InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(name);
if (data.path(USERNAME).isTextual()) {
signalHead.setUserName(data.path(USERNAME).asText());
}
if (data.path(COMMENT).isTextual()) {
signalHead.setComment(data.path(COMMENT).asText());
}
int state = data.path(STATE).asInt(UNKNOWN);
boolean isValid = false;
for (int validState : signalHead.getValidStates()) {
if (state == validState) {
isValid = true;
break;
}
}
if (isValid && state != INCONSISTENT && state != UNKNOWN) {
// TODO: completely insulate JSON state from SignalHead state
signalHead.setAppearance(state);
} else {
throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", SIGNAL_HEAD, state));
}
} catch (NullPointerException e) {
log.error("Unable to get signal head [{}].", name);
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", SIGNAL_HEAD, name));
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method setRoute.
/**
* Routes can be set by passing a JsonNode with the node <em>state</em>
* equal to <em>8</em> (the aspect of {@link jmri.Route#TOGGLE}).
*
* @param locale The locale to throw exceptions in
* @param name The name of the route
* @param data A JsonNode containing route attributes to set
* @throws jmri.server.json.JsonException if the named route does not exist
* or the state is invalid
* @see jmri.Route#TOGGLE
*/
@Deprecated
public static void setRoute(Locale locale, String name, JsonNode data) throws JsonException {
try {
Route route = InstanceManager.getDefault(jmri.RouteManager.class).getRoute(name);
if (data.path(USERNAME).isTextual()) {
route.setUserName(data.path(USERNAME).asText());
}
if (data.path(COMMENT).isTextual()) {
route.setComment(data.path(COMMENT).asText());
}
int state = data.path(STATE).asInt(UNKNOWN);
switch(state) {
case ACTIVE:
case TOGGLE:
route.setRoute();
break;
case INACTIVE:
case UNKNOWN:
// silently ignore
break;
default:
throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", ROUTE, state));
}
} catch (NullPointerException ex) {
log.error("Unable to get route [{}].", name);
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", ROUTE, name));
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method getSignalHead.
public static JsonNode getSignalHead(Locale locale, String name) throws JsonException {
ObjectNode root = mapper.createObjectNode();
root.put(TYPE, SIGNAL_HEAD);
ObjectNode data = root.putObject(DATA);
SignalHead signalHead = InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(name);
try {
data.put(NAME, name);
data.put(USERNAME, signalHead.getUserName());
data.put(COMMENT, signalHead.getComment());
data.put(LIT, signalHead.getLit());
data.put(APPEARANCE, signalHead.getAppearance());
data.put(TOKEN_HELD, signalHead.getHeld());
//state is appearance, plus a flag for held status
if (signalHead.getHeld()) {
data.put(STATE, SignalHead.HELD);
} else {
data.put(STATE, signalHead.getAppearance());
}
data.put(APPEARANCE_NAME, signalHead.getAppearanceName());
} catch (NullPointerException e) {
log.error("Unable to get signalHead [{}].", name);
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", SIGNAL_HEAD, name));
}
return root;
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method getPower.
@Deprecated
public static JsonNode getPower(Locale locale) throws JsonException {
ObjectNode root = mapper.createObjectNode();
root.put(TYPE, POWER);
ObjectNode data = root.putObject(DATA);
try {
switch(InstanceManager.getDefault(jmri.PowerManager.class).getPower()) {
case PowerManager.OFF:
data.put(STATE, OFF);
break;
case PowerManager.ON:
data.put(STATE, ON);
break;
default:
data.put(STATE, UNKNOWN);
break;
}
} 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.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method putConsist.
/**
* Add a consist.
*
* Adds a consist, populating it with information from data.
*
* @param locale The locale to throw exceptions in.
* @param address The address of the new consist.
* @param data The JSON representation of the consist. See
* {@link #getConsist(Locale, jmri.DccLocoAddress) } for the
* JSON structure.
* @throws jmri.server.json.JsonException if no consist manager is available
*/
public static void putConsist(Locale locale, DccLocoAddress address, JsonNode data) throws JsonException {
try {
if (!InstanceManager.getDefault(jmri.ConsistManager.class).getConsistList().contains(address)) {
InstanceManager.getDefault(jmri.ConsistManager.class).getConsist(address);
setConsist(locale, address, data);
}
} catch (NullPointerException ex) {
// NOI18N
throw new JsonException(503, Bundle.getMessage(locale, "ErrorNoConsistManager"));
}
}
Aggregations