Search in sources :

Example 11 with JsonException

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

Example 12 with JsonException

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

Example 13 with JsonException

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));
    }
}
Also used : JsonException(jmri.server.json.JsonException) SignalMast(jmri.SignalMast) TrainCommon.splitString(jmri.jmrit.operations.trains.TrainCommon.splitString)

Example 14 with JsonException

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

Example 15 with JsonException

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

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