use of jmri.Consist in project JMRI by JMRI.
the class JsonConsistHttpService 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 JsonNode getConsist(Locale locale, DccLocoAddress address) throws JsonException {
if (this.manager.getConsistList().contains(address)) {
ObjectNode root = mapper.createObjectNode();
root.put(TYPE, CONSIST);
ObjectNode data = root.putObject(DATA);
Consist consist = this.manager.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);
consist.getConsistList().stream().forEach((locomotive) -> {
ObjectNode engine = mapper.createObjectNode();
engine.put(ADDRESS, locomotive.getNumber());
engine.put(IS_LONG_ADDRESS, locomotive.isLongAddress());
engine.put(FORWARD, consist.getLocoDirection(locomotive));
engine.put(POSITION, consist.getPosition(locomotive));
engines.add(engine);
});
data.put(ID, consist.getConsistID());
data.put(SIZE_LIMIT, consist.sizeLimit());
return root;
} else {
// NOI18N
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", CONSIST, address.toString()));
}
}
use of jmri.Consist 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"));
}
}
use of jmri.Consist in project JMRI by JMRI.
the class ConsistFile method writeFile.
/**
* Write all consists to a file.
*
* @param consistList list of consist addresses
* @param fileName path to file
* @throws java.io.IOException if unable to write file
*/
public void writeFile(ArrayList<DccLocoAddress> consistList, String fileName) throws IOException {
// create root element
Element root = new Element("consist-roster-config");
Document doc = newDocument(root, dtdLocation + "consist-roster-config.dtd");
// add XSLT processing instruction
Map<String, String> m = new HashMap<>();
m.put("type", "text/xsl");
m.put("href", xsltLocation + "consistRoster.xsl");
ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
doc.addContent(0, p);
Element roster = new Element("roster");
for (int i = 0; i < consistList.size(); i++) {
Consist newConsist = consistMan.getConsist(consistList.get(i));
roster.addContent(consistToXml(newConsist));
}
root.addContent(roster);
try {
if (!checkFile(fileName)) {
//The file does not exist, create it before writing
File file = new File(fileName);
File parentDir = file.getParentFile();
if (!parentDir.exists()) {
if (!parentDir.mkdir()) {
throw (new IOException());
}
}
if (!file.createNewFile()) {
throw (new IOException());
}
}
writeXML(findFile(fileName), doc);
} catch (IOException ioe) {
log.error("IO Exception " + ioe);
throw (ioe);
}
}
use of jmri.Consist in project JMRI by JMRI.
the class ConsistToolFrame method throttleButtonActionPerformed.
public void throttleButtonActionPerformed(ActionEvent e) {
if (adrSelector.getAddress() == null) {
JOptionPane.showMessageDialog(this, Bundle.getMessage("NoConsistSelectedError"));
return;
}
// make sure any new locomotives are added to the consist.
addLocoButtonActionPerformed(e);
// Create a throttle object with the
ThrottleFrame tf = ThrottleFrameManager.instance().createThrottleFrame();
DccLocoAddress address = adrSelector.getAddress();
/*
* get the lead locomotive from the list of locomotives so we can
* register function button bindings in the throttle.
*/
Consist tempConsist = ConsistMan.getConsist(address);
ArrayList<DccLocoAddress> addressList = tempConsist.getConsistList();
DccLocoAddress locoaddress = addressList.get(0);
if (address != locoaddress) {
if (log.isDebugEnabled()) {
log.debug("Consist Address " + address.toString() + ", Lead Locomoitve " + locoaddress.toString());
}
// the consist address and the lead locomotive address differ,
// register so the function buttons trigger the lead locomotive
tf.getAddressPanel().setCurrentAddress(locoaddress);
}
// Notify the throttle of the selected consist address
tf.getAddressPanel().setConsistAddress(address);
tf.toFront();
}
use of jmri.Consist in project JMRI by JMRI.
the class ConsistToolFrame method recallConsist.
// Recall the consist
private void recallConsist() {
if (adrSelector.getAddress() == null) {
// Clear any consist information that was present
locoSelector.reset();
locoRosterBox.setSelectedIndex(0);
if (consistModel.getConsist() != null) {
consistModel.getConsist().removeConsistListener(this);
_status.setText("Ready");
}
consistModel.setConsist((Consist) null);
canAdd();
return;
}
DccLocoAddress address = adrSelector.getAddress();
if (consistModel.getConsist() != null) {
consistModel.getConsist().removeConsistListener(this);
_status.setText("Ready");
}
Consist selectedConsist = ConsistMan.getConsist(address);
consistModel.setConsist(selectedConsist);
selectedConsist.addConsistListener(this);
// reset the editable locomotive information.
locoSelector.reset();
locoRosterBox.setSelectedIndex(0);
locoDirectionNormal.setSelected(true);
// the user change the direction
if (consistModel.getRowCount() == 0) {
locoDirectionNormal.setEnabled(false);
} else {
locoDirectionNormal.setEnabled(true);
}
if (log.isDebugEnabled()) {
log.debug("Recall Consist " + address);
}
// What type of consist is this?
if (selectedConsist.getConsistType() == Consist.ADVANCED_CONSIST) {
log.debug("Consist type is Advanced Consist ");
isAdvancedConsist.setSelected(true);
isCSConsist.setSelected(false);
_Consist_Type = Consist.ADVANCED_CONSIST;
} else {
// This must be a CS Consist.
log.debug("Consist type is Command Station Consist ");
isAdvancedConsist.setSelected(false);
isCSConsist.setSelected(true);
_Consist_Type = Consist.CS_CONSIST;
}
canAdd();
}
Aggregations