use of jmri.jmrit.consisttool.ConsistFile 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"));
}
}
use of jmri.jmrit.consisttool.ConsistFile 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);
}
Aggregations