Search in sources :

Example 76 with JsonException

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

the class JsonLayoutBlockHttpService method doGet.

@Override
public JsonNode doGet(String type, String name, Locale locale) throws JsonException {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, LAYOUTBLOCK);
    ObjectNode data = root.putObject(DATA);
    LayoutBlock layoutBlock = InstanceManager.getDefault(LayoutBlockManager.class).getLayoutBlock(name);
    if (layoutBlock == null) {
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", LAYOUTBLOCK, name));
    }
    data.put(NAME, layoutBlock.getSystemName());
    data.put(USERNAME, layoutBlock.getUserName());
    data.put(COMMENT, layoutBlock.getComment());
    data.put(STATE, layoutBlock.getState());
    data.put(USE_EXTRA_COLOR, layoutBlock.getUseExtraColor());
    data.put(BLOCK_COLOR, jmri.util.ColorUtil.colorToColorName(layoutBlock.getBlockColor()));
    data.put(TRACK_COLOR, jmri.util.ColorUtil.colorToColorName(layoutBlock.getBlockTrackColor()));
    data.put(OCCUPIED_COLOR, jmri.util.ColorUtil.colorToColorName(layoutBlock.getBlockOccupiedColor()));
    data.put(EXTRA_COLOR, jmri.util.ColorUtil.colorToColorName(layoutBlock.getBlockExtraColor()));
    data.put(OCCUPANCY_SENSOR, layoutBlock.getOccupancySensorName());
    data.put(OCCUPIED_SENSE, layoutBlock.getOccupiedSense());
    return root;
}
Also used : LayoutBlock(jmri.jmrit.display.layoutEditor.LayoutBlock) JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) LayoutBlockManager(jmri.jmrit.display.layoutEditor.LayoutBlockManager)

Example 77 with JsonException

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

the class JsonConsistHttpService method doPost.

/**
     * 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 type   the JSON message type
     * @param locale the locale to throw exceptions in
     * @param name   the consist address, ignored if data contains an
     *               {@value jmri.server.json.JSON#ADDRESS} and
     *               {@value jmri.server.json.JSON#IS_LONG_ADDRESS} nodes
     * @param data   the consist as a JsonObject
     * @return the JSON representation of the Consist
     * @throws jmri.server.json.JsonException if there is no consist manager
     *                                        (code 503), the consist does not
     *                                        exist (code 404), or the consist
     *                                        cannot be saved (code 500).
     */
@Override
public JsonNode doPost(String type, String name, JsonNode data, Locale locale) throws JsonException {
    if (!this.manager.isConsistManager()) {
        // NOI18N
        throw new JsonException(503, Bundle.getMessage(locale, "ErrorNoConsistManager"));
    }
    DccLocoAddress address;
    if (data.path(ADDRESS).canConvertToInt()) {
        address = new DccLocoAddress(data.path(ADDRESS).asInt(), data.path(IS_LONG_ADDRESS).asBoolean(false));
    } else {
        address = JsonUtilHttpService.addressForString(data.path(ADDRESS).asText());
    }
    if (!this.manager.getConsistList().contains(address)) {
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", CONSIST, name));
    }
    Consist consist = this.manager.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
        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);
        }
        // remove engines if needed
        ArrayList<DccLocoAddress> consistEngines = new ArrayList<>(consist.getConsistList());
        consistEngines.stream().filter((engineAddress) -> (!engines.contains(engineAddress))).forEach((engineAddress) -> {
            consist.remove(engineAddress);
        });
    }
    try {
        (new ConsistFile()).writeFile(this.manager.getConsistList());
    } catch (IOException ex) {
        throw new JsonException(500, ex.getLocalizedMessage());
    }
    return this.getConsist(locale, address);
}
Also used : JsonException(jmri.server.json.JsonException) InstanceManager(jmri.InstanceManager) ADDRESS(jmri.server.json.JSON.ADDRESS) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CONSIST(jmri.server.json.consist.JsonConsist.CONSIST) IOException(java.io.IOException) DccLocoAddress(jmri.DccLocoAddress) JsonHttpService(jmri.server.json.JsonHttpService) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonException(jmri.server.json.JsonException) FORWARD(jmri.server.json.JSON.FORWARD) ArrayList(java.util.ArrayList) ConsistFile(jmri.jmrit.consisttool.ConsistFile) TYPE(jmri.server.json.JSON.TYPE) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Locale(java.util.Locale) SIZE_LIMIT(jmri.server.json.JSON.SIZE_LIMIT) DATA(jmri.server.json.JSON.DATA) ID(jmri.server.json.JSON.ID) JsonNode(com.fasterxml.jackson.databind.JsonNode) IS_LONG_ADDRESS(jmri.server.json.JSON.IS_LONG_ADDRESS) Consist(jmri.Consist) JsonUtilHttpService(jmri.server.json.util.JsonUtilHttpService) ENGINES(jmri.server.json.JSON.ENGINES) POSITION(jmri.server.json.JSON.POSITION) 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 78 with JsonException

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

the class JsonUtil method getLocation.

public JsonNode getLocation(Locale locale, String id) throws JsonException {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, LOCATION);
    ObjectNode data = root.putObject(DATA);
    try {
        Location location = LocationManager.instance().getLocationById(id);
        data.put(NAME, location.getName());
        data.put(ID, location.getId());
        data.put(LENGTH, location.getLength());
        data.put(COMMENT, location.getComment());
    } catch (NullPointerException e) {
        log.error("Unable to get location id [{}].", id);
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", LOCATION, id));
    }
    return root;
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RouteLocation(jmri.jmrit.operations.routes.RouteLocation) Location(jmri.jmrit.operations.locations.Location)

Example 79 with JsonException

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

the class JsonUtil method getTrain.

public JsonNode getTrain(Locale locale, String id) throws JsonException {
    ObjectNode root = this.mapper.createObjectNode();
    root.put(TYPE, JsonOperations.TRAIN);
    ObjectNode data = root.putObject(JSON.DATA);
    try {
        Train train = TrainManager.instance().getTrainById(id);
        data.put(NAME, train.getName());
        data.put(JSON.ICON_NAME, train.getIconName());
        data.put(ID, train.getId());
        data.put(JSON.DEPARTURE_TIME, train.getFormatedDepartureTime());
        data.put(JSON.DESCRIPTION, train.getDescription());
        data.put(COMMENT, train.getComment());
        if (train.getRoute() != null) {
            data.put(ROUTE, train.getRoute().getName());
            data.put(JSON.ROUTE_ID, train.getRoute().getId());
            data.put(JsonOperations.LOCATIONS, this.getRouteLocationsForTrain(locale, train));
        }
        data.put(JSON.ENGINES, this.getEnginesForTrain(locale, train));
        data.put(JsonOperations.CARS, this.getCarsForTrain(locale, train));
        if (train.getTrainDepartsName() != null) {
            data.put(JSON.DEPARTURE_LOCATION, train.getTrainDepartsName());
        }
        if (train.getTrainTerminatesName() != null) {
            data.put(JSON.TERMINATES_LOCATION, train.getTrainTerminatesName());
        }
        data.put(LOCATION, train.getCurrentLocationName());
        if (train.getCurrentLocation() != null) {
            data.put(JsonOperations.LOCATION_ID, train.getCurrentLocation().getId());
        }
        data.put(JSON.STATUS, train.getStatus(locale));
        data.put(JSON.STATUS_CODE, train.getStatusCode());
        data.put(LENGTH, train.getTrainLength());
        data.put(JsonOperations.WEIGHT, train.getTrainWeight());
        if (train.getLeadEngine() != null) {
            data.put(JsonOperations.LEAD_ENGINE, train.getLeadEngine().toString());
        }
        data.put(JsonOperations.CABOOSE, train.getCabooseRoadAndNumber());
    } catch (NullPointerException e) {
        log.error("Unable to get train id [{}].", id);
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", JsonOperations.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 80 with JsonException

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

the class JsonBlockHttpService method doGet.

@Override
public JsonNode doGet(String type, String name, Locale locale) throws JsonException {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, BLOCK);
    ObjectNode data = root.putObject(DATA);
    Block block = InstanceManager.getDefault(BlockManager.class).getBlock(name);
    if (block == null) {
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", BLOCK, name));
    }
    data.put(NAME, block.getSystemName());
    data.put(USERNAME, block.getUserName());
    data.put(COMMENT, block.getComment());
    if (block.getValue() == null) {
        data.putNull(VALUE);
    } else {
        data.put(VALUE, block.getValue().toString());
    }
    return root;
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BlockManager(jmri.BlockManager) Block(jmri.Block)

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