use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method getTurnout.
@Deprecated
public static JsonNode getTurnout(Locale locale, String name) throws JsonException {
ObjectNode root = mapper.createObjectNode();
root.put(TYPE, TURNOUT);
ObjectNode data = root.putObject(DATA);
try {
Turnout turnout = InstanceManager.turnoutManagerInstance().getTurnout(name);
data.put(NAME, turnout.getSystemName());
data.put(USERNAME, turnout.getUserName());
data.put(COMMENT, turnout.getComment());
data.put(INVERTED, turnout.getInverted());
switch(turnout.getKnownState()) {
case Turnout.THROWN:
data.put(STATE, THROWN);
break;
case Turnout.CLOSED:
data.put(STATE, CLOSED);
break;
case Turnout.INCONSISTENT:
data.put(STATE, INCONSISTENT);
break;
case Turnout.UNKNOWN:
default:
data.put(STATE, UNKNOWN);
break;
}
} catch (NullPointerException e) {
log.error("Unable to get turnout [{}].", name);
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", TURNOUT, name));
}
return root;
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonSignalHeadServer method parseRequest.
public void parseRequest(Locale locale, JsonNode data) throws JmriException, IOException, JsonException {
String name = data.path(NAME).asText();
int state = data.path(STATE).asInt(UNKNOWN);
if (state == UNKNOWN) {
//if unknown, retrieve current and respond
try {
state = InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(name).getAppearance();
} catch (NullPointerException e) {
log.error("Unable to get signalHead [{}].", name);
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", SIGNAL_HEAD, name));
}
this.sendStatus(name, state);
} else {
//else set the appearance to the state passed-in
this.setSignalHeadAppearance(name, state);
}
this.addSignalHeadToList(name);
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtilHttpService method getMetadata.
/**
* Get a JSON message with a metadata element from {@link jmri.Metadata}.
*
* @param locale The client's Locale.
* @param name The metadata element to get.
* @return JSON metadata element.
* @throws JsonException if name is not a recognized metadata element.
*/
public JsonNode getMetadata(Locale locale, String name) throws JsonException {
String metadata = Metadata.getBySystemName(name);
ObjectNode root;
if (metadata != null) {
root = mapper.createObjectNode();
root.put(JSON.TYPE, JSON.METADATA);
ObjectNode data = root.putObject(JSON.DATA);
data.put(JSON.NAME, name);
data.put(JSON.VALUE, Metadata.getBySystemName(name));
} else {
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", JSON.METADATA, name));
}
return root;
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonSignalMastHttpService method doPost.
@Override
public JsonNode doPost(String type, String name, JsonNode data, Locale locale) throws JsonException {
SignalMast signalMast = InstanceManager.getDefault(jmri.SignalMastManager.class).getSignalMast(name);
this.postNamedBean(signalMast, data, name, type, locale);
if (signalMast != null) {
if (data.path(STATE).isTextual()) {
String aspect = data.path(STATE).asText();
if (aspect.equals("Held")) {
signalMast.setHeld(true);
} else if (signalMast.getValidAspects().contains(aspect)) {
if (signalMast.getHeld()) {
signalMast.setHeld(false);
}
if (signalMast.getAspect() == null || !signalMast.getAspect().equals(aspect)) {
signalMast.setAspect(aspect);
}
} else {
throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", SIGNAL_MAST, aspect));
}
}
}
return this.doGet(type, name, locale);
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonThrottle method notifyFailedThrottleRequest.
@Override
public void notifyFailedThrottleRequest(DccLocoAddress address, String reason) {
JsonThrottleManager manager = JsonThrottleManager.getDefault();
for (JsonThrottleSocketService server : manager.getServers(this).toArray(new JsonThrottleSocketService[manager.getServers(this).size()])) {
this.sendErrorMessage(new JsonException(512, Bundle.getMessage(server.getConnection().getLocale(), "ErrorThrottleRequestFailed", address, reason)), server);
server.release(this);
}
}
Aggregations