Search in sources :

Example 1 with JsonException

use of jmri.server.json.JsonException in project JMRI by JMRI.

the class JsonUtil method setConsist.

/**
     * Change the properties and locomotives of a consist.
     *
     * This method takes as input the JSON representation of a consist as
     * provided by {@link #getConsist(Locale, jmri.DccLocoAddress) }.
     *
     * If present in the JSON, this method sets the following consist
     * properties:
     * <ul>
     * <li>consistID</li>
     * <li>consistType</li>
     * <li>locomotives (<em>engines</em> in the JSON representation)<br>
     * <strong>NOTE</strong> Since this method adds, repositions, and deletes
     * locomotives, the JSON representation must contain <em>every</em>
     * locomotive that should be in the consist, if it contains the engines
     * node.</li>
     * </ul>
     *
     * @param locale  the locale to throw exceptions in
     * @param address the consist address
     * @param data    the consist as a JsonObject
     * @throws jmri.server.json.JsonException if no ConsistManager is available
     */
public static void setConsist(Locale locale, DccLocoAddress address, JsonNode data) throws JsonException {
    try {
        if (InstanceManager.getDefault(jmri.ConsistManager.class).getConsistList().contains(address)) {
            Consist consist = InstanceManager.getDefault(jmri.ConsistManager.class).getConsist(address);
            if (data.path(ID).isTextual()) {
                consist.setConsistID(data.path(ID).asText());
            }
            if (data.path(TYPE).isInt()) {
                consist.setConsistType(data.path(TYPE).asInt());
            }
            if (data.path(ENGINES).isArray()) {
                ArrayList<DccLocoAddress> engines = new ArrayList<>();
                // add every engine in
                for (JsonNode engine : data.path(ENGINES)) {
                    DccLocoAddress engineAddress = new DccLocoAddress(engine.path(ADDRESS).asInt(), engine.path(IS_LONG_ADDRESS).asBoolean());
                    if (!consist.contains(engineAddress)) {
                        consist.add(engineAddress, engine.path(FORWARD).asBoolean());
                    }
                    consist.setPosition(engineAddress, engine.path(POSITION).asInt());
                    engines.add(engineAddress);
                }
                @SuppressWarnings("unchecked") ArrayList<DccLocoAddress> consistEngines = (ArrayList<DccLocoAddress>) consist.getConsistList().clone();
                for (DccLocoAddress engineAddress : consistEngines) {
                    if (!engines.contains(engineAddress)) {
                        consist.remove(engineAddress);
                    }
                }
            }
            try {
                (new ConsistFile()).writeFile(InstanceManager.getDefault(jmri.ConsistManager.class).getConsistList());
            } catch (IOException ex) {
                throw new JsonException(500, ex.getLocalizedMessage());
            }
        }
    } catch (NullPointerException ex) {
        // NOI18N
        throw new JsonException(503, Bundle.getMessage(locale, "ErrorNoConsistManager"));
    }
}
Also used : JsonException(jmri.server.json.JsonException) ConsistFile(jmri.jmrit.consisttool.ConsistFile) Consist(jmri.Consist) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) DccLocoAddress(jmri.DccLocoAddress)

Example 2 with JsonException

use of jmri.server.json.JsonException in project JMRI by JMRI.

the class JsonUtil method setTurnout.

@Deprecated
public static void setTurnout(Locale locale, String name, JsonNode data) throws JsonException {
    try {
        Turnout turnout = InstanceManager.turnoutManagerInstance().getTurnout(name);
        if (data.path(USERNAME).isTextual()) {
            turnout.setUserName(data.path(USERNAME).asText());
        }
        if (data.path(INVERTED).isBoolean()) {
            turnout.setInverted(data.path(INVERTED).asBoolean());
        }
        if (data.path(COMMENT).isTextual()) {
            turnout.setComment(data.path(COMMENT).asText());
        }
        int state = data.path(STATE).asInt(UNKNOWN);
        switch(state) {
            case THROWN:
                turnout.setCommandedState(Turnout.THROWN);
                break;
            case CLOSED:
                turnout.setCommandedState(Turnout.CLOSED);
                break;
            case UNKNOWN:
                // leave state alone in this case
                break;
            default:
                throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", TURNOUT, state));
        }
    } catch (NullPointerException ex) {
        log.error("Unable to get turnout [{}].", name);
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", TURNOUT, name));
    }
}
Also used : JsonException(jmri.server.json.JsonException) Turnout(jmri.Turnout)

Example 3 with JsonException

use of jmri.server.json.JsonException in project JMRI by JMRI.

the class JsonUtil method getTrain.

public static JsonNode getTrain(Locale locale, String id) throws JsonException {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, TRAIN);
    ObjectNode data = root.putObject(DATA);
    try {
        Train train = TrainManager.instance().getTrainById(id);
        data.put(NAME, train.getName());
        data.put(ICON_NAME, train.getIconName());
        data.put(ID, train.getId());
        data.put(DEPARTURE_TIME, train.getFormatedDepartureTime());
        data.put(DESCRIPTION, train.getDescription());
        data.put(COMMENT, train.getComment());
        data.put(ROUTE, train.getRoute().getName());
        data.put(ROUTE_ID, train.getRoute().getId());
        data.put(LOCATIONS, getRouteLocationsForTrain(locale, train));
        data.put(ENGINES, getEnginesForTrain(locale, train));
        data.put(CARS, getCarsForTrain(locale, train));
        if (train.getTrainDepartsName() != null) {
            data.put(DEPARTURE_LOCATION, train.getTrainDepartsName());
        }
        if (train.getTrainTerminatesName() != null) {
            data.put(TERMINATES_LOCATION, train.getTrainTerminatesName());
        }
        data.put(LOCATION, train.getCurrentLocationName());
        if (train.getCurrentLocation() != null) {
            data.put(LOCATION_ID, train.getCurrentLocation().getId());
        }
        data.put(STATUS, train.getStatus(locale));
        data.put(STATUS_CODE, train.getStatusCode());
        data.put(LENGTH, train.getTrainLength());
        data.put(WEIGHT, train.getTrainWeight());
        if (train.getLeadEngine() != null) {
            data.put(LEAD_ENGINE, train.getLeadEngine().toString());
        }
        data.put(CABOOSE, train.getCabooseRoadAndNumber());
    } catch (NullPointerException e) {
        log.error("Unable to get train id [{}].", id);
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", TRAIN, id));
    }
    return root;
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Train(jmri.jmrit.operations.trains.Train)

Example 4 with JsonException

use of jmri.server.json.JsonException in project JMRI by JMRI.

the class JsonUtil method getSensor.

@Deprecated
public static JsonNode getSensor(Locale locale, String name) throws JsonException {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, SENSOR);
    ObjectNode data = root.putObject(DATA);
    try {
        Sensor sensor = InstanceManager.sensorManagerInstance().getSensor(name);
        data.put(NAME, name);
        data.put(USERNAME, sensor.getUserName());
        data.put(COMMENT, sensor.getComment());
        data.put(INVERTED, sensor.getInverted());
        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;
        }
    } catch (NullPointerException e) {
        log.error("Unable to get sensor [{}].", name);
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", SENSOR, name));
    }
    return root;
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Sensor(jmri.Sensor)

Example 5 with JsonException

use of jmri.server.json.JsonException in project JMRI by JMRI.

the class JsonUtil method getSignalMast.

public static JsonNode getSignalMast(Locale locale, String name) throws JsonException {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, SIGNAL_MAST);
    ObjectNode data = root.putObject(DATA);
    SignalMast signalMast = InstanceManager.getDefault(jmri.SignalMastManager.class).getSignalMast(name);
    try {
        data.put(NAME, name);
        data.put(USERNAME, signalMast.getUserName());
        if (signalMast.getComment() != null) {
            data.put(COMMENT, signalMast.getComment());
        }
        String aspect = signalMast.getAspect();
        if (aspect == null) {
            //if null, set aspect to "Unknown"   
            aspect = ASPECT_UNKNOWN;
        }
        data.put(ASPECT, aspect);
        data.put(LIT, signalMast.getLit());
        data.put(TOKEN_HELD, signalMast.getHeld());
        //state is appearance, plus flags for held and dark statii
        if ((signalMast.getHeld()) && (signalMast.getAppearanceMap().getSpecificAppearance(jmri.SignalAppearanceMap.HELD) != null)) {
            data.put(STATE, ASPECT_HELD);
        } else if ((!signalMast.getLit()) && (signalMast.getAppearanceMap().getSpecificAppearance(jmri.SignalAppearanceMap.DARK) != null)) {
            data.put(STATE, ASPECT_DARK);
        } else {
            data.put(STATE, aspect);
        }
    } catch (NullPointerException e) {
        log.error("Unable to get signalMast [{}].", name);
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", SIGNAL_MAST, name));
    }
    return root;
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SignalMast(jmri.SignalMast) TrainCommon.splitString(jmri.jmrit.operations.trains.TrainCommon.splitString)

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