Search in sources :

Example 11 with Consist

use of jmri.Consist in project JMRI by JMRI.

the class ConsistToolFrame method reverseButtonActionPerformed.

public void reverseButtonActionPerformed(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);
    /*
         * get the array list of the locomotives in the consist
         */
    DccLocoAddress address = adrSelector.getAddress();
    Consist tempConsist = ConsistMan.getConsist(address);
    tempConsist.reverse();
}
Also used : Consist(jmri.Consist) DccLocoAddress(jmri.DccLocoAddress)

Example 12 with Consist

use of jmri.Consist in project JMRI by JMRI.

the class ConsistToolFrame method addLocoButtonActionPerformed.

public void addLocoButtonActionPerformed(ActionEvent e) {
    if (locoSelector.getAddress() == null) {
        return;
    }
    if (_Consist_Type == Consist.ADVANCED_CONSIST && adrSelector.getAddress() == null) {
        JOptionPane.showMessageDialog(this, Bundle.getMessage("NoConsistSelectedError"));
        return;
    } else if (_Consist_Type == Consist.ADVANCED_CONSIST && adrSelector.getAddress().isLongAddress()) {
        JOptionPane.showMessageDialog(this, Bundle.getMessage("RequiresShortConsistError"));
        return;
    } else if (_Consist_Type == Consist.CS_CONSIST && adrSelector.getAddress() == null) {
        if (ConsistMan.csConsistNeedsSeperateAddress()) {
            JOptionPane.showMessageDialog(this, Bundle.getMessage("NoConsistSelectedError"));
            return;
        } else {
            // We need to set an identifier so we can recall the
            // consist.  We're going to use the lead locomotive number
            // for this.
            adrSelector.setAddress(locoSelector.getAddress());
        }
    }
    DccLocoAddress address = adrSelector.getAddress();
    /*
         * Make sure the marked consist type matches the consist type stored for
         * this consist
         */
    if (_Consist_Type != ConsistMan.getConsist(address).getConsistType()) {
        if (log.isDebugEnabled()) {
            if (_Consist_Type == Consist.ADVANCED_CONSIST) {
                log.debug("Setting Consist Type to Advanced Consist");
            } else if (_Consist_Type == Consist.CS_CONSIST) {
                log.debug("Setting Consist Type to Command Station Assisted Consist");
            }
        }
        ConsistMan.getConsist(address).setConsistType(_Consist_Type);
    }
    DccLocoAddress locoaddress = locoSelector.getAddress();
    // consist, and add it to the consist if it is
    if (!ConsistMan.getConsist(address).isAddressAllowed(locoaddress)) {
        JOptionPane.showMessageDialog(this, Bundle.getMessage("AddressNotAllowedError"));
    } else {
        if (ConsistMan.getConsist(address).contains(locoaddress)) {
            JOptionPane.showMessageDialog(this, Bundle.getMessage("AddressAlreadyInConsistError"));
        } else {
            Consist tempConsist = ConsistMan.getConsist(address);
            tempConsist.add(locoaddress, locoDirectionNormal.isSelected());
            if (locoRosterBox.getSelectedRosterEntries().length == 1) {
                tempConsist.setRosterId(locoaddress, locoRosterBox.getSelectedRosterEntries()[0].titleString());
            }
        }
        if (consistAdrBox.getSelectedItem() != adrSelector.getAddress()) {
            initializeConsistBox();
        }
        consistModel.fireTableDataChanged();
        resetLocoButtonActionPerformed(e);
    }
}
Also used : Consist(jmri.Consist) DccLocoAddress(jmri.DccLocoAddress)

Example 13 with Consist

use of jmri.Consist 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 14 with Consist

use of jmri.Consist in project JMRI by JMRI.

the class ConsistController method reorderConsist.

/**
     * Change the sequence of locos in this consist. Reorders the consistList,
     * instead of setting the 'position' value. Lead and Trail are set on first
     * and last locos by DccConsist.
     *
     * @param message RCP<;>consistAddress<:>leadLoco<;>nextLoco<;>...
     * ...<;>nextLoco<;>trailLoco
     */
private void reorderConsist(String message) {
    Consist consist;
    List<String> headerAndLocos = Arrays.asList(message.split("<:>"));
    if (headerAndLocos.size() < 2) {
        log.warn("reorderConsist missing data in message: " + message);
        return;
    }
    try {
        List<String> headerData = Arrays.asList(headerAndLocos.get(0).split("<;>"));
        //  
        consist = manager.getConsist(stringToDcc(headerData.get(1)));
        List<String> locoData = Arrays.asList(headerAndLocos.get(1).split("<;>"));
        /*
             * Reorder the consistList:
             * For each loco sent, remove it from the consistList
             * and reinsert it at the front of the list.
             */
        for (String loco : locoData) {
            ArrayList<DccLocoAddress> conList = consist.getConsistList();
            int index = conList.indexOf(stringToDcc(loco));
            if (index != -1) {
                conList.add(conList.remove(index));
            }
        }
    } catch (NullPointerException e) {
        log.warn("reorderConsist error for message: " + message);
        return;
    }
    writeFile();
}
Also used : Consist(jmri.Consist) DccLocoAddress(jmri.DccLocoAddress)

Example 15 with Consist

use of jmri.Consist in project JMRI by JMRI.

the class ConsistController method removeLoco.

/**
     * remove a loco if it exist in this consist.
     *
     * @param message RC-<;>consistAddress<:>locoAddress
     */
private void removeLoco(String message) {
    Consist consist;
    List<String> headerAndLoco = Arrays.asList(message.split("<:>"));
    if (log.isDebugEnabled()) {
        log.debug("remove loco string: " + message);
    }
    try {
        List<String> headerData = Arrays.asList(headerAndLoco.get(0).split("<;>"));
        consist = manager.getConsist(stringToDcc(headerData.get(1)));
        List<String> locoData = Arrays.asList(headerAndLoco.get(1).split("<;>"));
        DccLocoAddress loco = stringToDcc(locoData.get(0));
        if (checkForBroadcastAddress(loco)) {
            return;
        }
        if (consist.contains(loco)) {
            consist.remove(loco);
            if (log.isDebugEnabled()) {
                log.debug("Remove loco: " + loco + ", from consist: " + headerData.get(1));
            }
        }
    } catch (NullPointerException e) {
        log.warn("removeLoco error for message: " + message);
        return;
    }
    writeFile();
}
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