Search in sources :

Example 6 with Consist

use of jmri.Consist in project JMRI by JMRI.

the class ConsistToolFrame method restoreButtonActionPerformed.

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

Example 7 with Consist

use of jmri.Consist in project JMRI by JMRI.

the class ConsistController method addLoco.

/**
     * Add a loco or change it's direction. Creates a new consist if one does
     * not already exist
     *
     * @param message RC+<;>consistAddress<;>ID<:>locoAddress<;>directionNormal
     */
private void addLoco(String message) {
    Consist consist;
    List<String> headerAndLoco = Arrays.asList(message.split("<:>"));
    try {
        //  Break out header and either get existing consist or create new
        List<String> headerData = Arrays.asList(headerAndLoco.get(0).split("<;>"));
        consist = manager.getConsist(stringToDcc(headerData.get(1)));
        consist.setConsistID(headerData.get(2));
        List<String> locoData = Arrays.asList(headerAndLoco.get(1).split("<;>"));
        if (consist.isAddressAllowed(stringToDcc(locoData.get(0)))) {
            consist.add(stringToDcc(locoData.get(0)), Boolean.valueOf(locoData.get(1)));
            if (log.isDebugEnabled()) {
                log.debug("add loco: " + locoData.get(0) + ", to consist: " + headerData.get(1));
            }
        }
    } catch (NullPointerException e) {
        log.warn("addLoco error for message: " + message);
        return;
    }
    writeFile();
}
Also used : Consist(jmri.Consist)

Example 8 with Consist

use of jmri.Consist in project JMRI by JMRI.

the class ConsistController method removeConsist.

/**
     * remove a consist by it's Dcc address. Wiil remove all locos in the
     * process.
     *
     * @param message RCR<;>consistAddress
     */
private void removeConsist(String message) {
    List<String> header = Arrays.asList(message.split("<;>"));
    Consist consist = null;
    try {
        consist = manager.getConsist(stringToDcc(header.get(1)));
        while (!consist.getConsistList().isEmpty()) {
            DccLocoAddress loco = consist.getConsistList().get(0);
            if (log.isDebugEnabled()) {
                log.debug("Remove loco: " + loco + ", from consist: " + consist.getConsistAddress().toString());
            }
            consist.remove(loco);
        }
    } catch (NullPointerException noCon) {
        log.warn("Consist: " + header.get(1) + " not found. Cannot delete.");
        return;
    }
    try {
        manager.delConsist(stringToDcc(header.get(1)));
    } catch (NullPointerException noCon) {
        log.warn("Consist: " + header.get(1) + " not found. Cannot delete.");
        return;
    }
    writeFile();
}
Also used : Consist(jmri.Consist) DccLocoAddress(jmri.DccLocoAddress)

Example 9 with Consist

use of jmri.Consist 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"));
    }
}
Also used : JsonException(jmri.server.json.JsonException) 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 10 with Consist

use of jmri.Consist in project JMRI by JMRI.

the class ConsistFile method consistFromXml.

/**
     * Load a Consist from the consist elements in the file.
     *
     * @param consist a JDOM element containing a consist
     */
@SuppressWarnings("unchecked")
private void consistFromXml(Element consist) {
    Attribute type, cnumber, isCLong, cID;
    Consist newConsist;
    // Read the consist address from the file and create the
    // consisit in memory if it doesn't exist already.
    cnumber = consist.getAttribute("consistNumber");
    isCLong = consist.getAttribute("longAddress");
    DccLocoAddress consistAddress;
    if (isCLong != null) {
        log.debug("adding consist {} with longAddress set to {}.", cnumber, isCLong.getValue());
        try {
            int number = Integer.parseInt(cnumber.getValue());
            consistAddress = new DccLocoAddress(number, isCLong.getValue().equals("yes"));
        } catch (NumberFormatException e) {
            log.debug("Consist number not an integer");
            return;
        }
    } else {
        log.debug("adding consist {} with default long address setting.", cnumber);
        consistAddress = new DccLocoAddress(Integer.parseInt(cnumber.getValue()), false);
    }
    newConsist = consistMan.getConsist(consistAddress);
    if (!(newConsist.getConsistList().isEmpty())) {
        log.debug("Consist {} is not empty.  Using version in memory.", consistAddress.toString());
        return;
    }
    // read and set the consist type
    type = consist.getAttribute("type");
    if (type != null) {
        // use the value read from the file
        newConsist.setConsistType((type.getValue().equals("CSAC")) ? Consist.CS_CONSIST : Consist.ADVANCED_CONSIST);
    } else {
        // use the default (DAC)
        newConsist.setConsistType(Consist.ADVANCED_CONSIST);
    }
    // Read the consist ID from the file;
    cID = consist.getAttribute("id");
    if (cID != null) {
        // use the value read from the file
        newConsist.setConsistID(cID.getValue());
    }
    // read each child of locomotive in the consist from the file
    // and restore it's information to memory.
    Iterator<Element> childIterator = consist.getDescendants(new ElementFilter("loco"));
    try {
        Element e;
        do {
            e = childIterator.next();
            Attribute number, isLong, direction, position, rosterId;
            number = e.getAttribute("dccLocoAddress");
            isLong = e.getAttribute("longAddress");
            direction = e.getAttribute("locoDir");
            position = e.getAttribute("locoName");
            rosterId = e.getAttribute("locoRosterId");
            log.debug("adding Loco {}", number);
            // Use restore so we DO NOT cause send any commands
            // to the command station as we recreate the consist.
            DccLocoAddress address;
            if (isLong != null && direction != null) {
                // use the values from the file
                log.debug("using direction from file {}", direction.getValue());
                address = new DccLocoAddress(Integer.parseInt(number.getValue()), isLong.getValue().equals("yes"));
                newConsist.restore(address, direction.getValue().equals("normal"));
            } else if (isLong == null && direction != null) {
                // use the direction from the file
                // but set as long address
                log.debug("using direction from file {}", direction.getValue());
                address = new DccLocoAddress(Integer.parseInt(number.getValue()), true);
                newConsist.restore(address, direction.getValue().equals("normal"));
            } else if (isLong != null && direction == null) {
                // use the default direction
                // but the long/short value from the file
                address = new DccLocoAddress(Integer.parseInt(number.getValue()), isLong.getValue().equals("yes"));
                newConsist.restore(address, true);
            } else {
                // use the default values long address
                // and normal direction
                address = new DccLocoAddress(Integer.parseInt(number.getValue()), true);
                newConsist.restore(address, true);
            }
            if (position != null && !position.getValue().equals("mid")) {
                if (position.getValue().equals("lead")) {
                    newConsist.setPosition(address, Consist.POSITION_LEAD);
                } else if (position.getValue().equals("rear")) {
                    newConsist.setPosition(address, Consist.POSITION_TRAIL);
                }
            } else {
                Attribute midNumber = e.getAttribute("locoMidNumber");
                if (midNumber != null) {
                    int pos = Integer.parseInt(midNumber.getValue());
                    newConsist.setPosition(address, pos);
                }
            }
            if (rosterId != null) {
                newConsist.setRosterId(address, rosterId.getValue());
            }
        } while (true);
    } catch (NoSuchElementException nse) {
        log.debug("end of loco list");
    }
}
Also used : Attribute(org.jdom2.Attribute) Consist(jmri.Consist) ElementFilter(org.jdom2.filter.ElementFilter) Element(org.jdom2.Element) NoSuchElementException(java.util.NoSuchElementException) 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