Search in sources :

Example 16 with JsonException

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;
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Turnout(jmri.Turnout)

Example 17 with JsonException

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);
}
Also used : JsonException(jmri.server.json.JsonException)

Example 18 with JsonException

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;
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode)

Example 19 with JsonException

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);
}
Also used : JsonException(jmri.server.json.JsonException) SignalMastManager(jmri.SignalMastManager) SignalMast(jmri.SignalMast)

Example 20 with JsonException

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);
    }
}
Also used : JsonException(jmri.server.json.JsonException)

Aggregations

JsonException (jmri.server.json.JsonException)112 JsonNode (com.fasterxml.jackson.databind.JsonNode)65 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)42 Test (org.junit.Test)31 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)25 JmriException (jmri.JmriException)22 IOException (java.io.IOException)21 JsonMockConnection (jmri.server.json.JsonMockConnection)18 Sensor (jmri.Sensor)13 SensorManager (jmri.SensorManager)13 Locale (java.util.Locale)10 Route (jmri.Route)9 RouteManager (jmri.RouteManager)9 Turnout (jmri.Turnout)9 SignalMast (jmri.SignalMast)8 TurnoutManager (jmri.TurnoutManager)8 Light (jmri.Light)7 ReporterManager (jmri.ReporterManager)7 SignalHead (jmri.SignalHead)7 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)6