Search in sources :

Example 6 with JsonException

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

Example 7 with JsonException

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

Example 8 with JsonException

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

Example 9 with JsonException

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

Example 10 with JsonException

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"));
    }
}
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