Search in sources :

Example 1 with Consist

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

Example 2 with Consist

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"));
    }
}
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)

Example 3 with Consist

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);
    }
}
Also used : HashMap(java.util.HashMap) Consist(jmri.Consist) Element(org.jdom2.Element) IOException(java.io.IOException) Document(org.jdom2.Document) File(java.io.File) XmlFile(jmri.jmrit.XmlFile) ProcessingInstruction(org.jdom2.ProcessingInstruction)

Example 4 with Consist

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();
}
Also used : Consist(jmri.Consist) ThrottleFrame(jmri.jmrit.throttle.ThrottleFrame) DccLocoAddress(jmri.DccLocoAddress)

Example 5 with Consist

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();
}
Also used : Consist(jmri.Consist) DccLocoAddress(jmri.DccLocoAddress)

Aggregations

Consist (jmri.Consist)15 DccLocoAddress (jmri.DccLocoAddress)12 JsonException (jmri.server.json.JsonException)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 IOException (java.io.IOException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ArrayList (java.util.ArrayList)2 ConsistFile (jmri.jmrit.consisttool.ConsistFile)2 Element (org.jdom2.Element)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 File (java.io.File)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 NoSuchElementException (java.util.NoSuchElementException)1 InstanceManager (jmri.InstanceManager)1 XmlFile (jmri.jmrit.XmlFile)1 ThrottleFrame (jmri.jmrit.throttle.ThrottleFrame)1 ADDRESS (jmri.server.json.JSON.ADDRESS)1 DATA (jmri.server.json.JSON.DATA)1