Search in sources :

Example 1 with DccLocoAddress

use of jmri.DccLocoAddress in project JMRI by JMRI.

the class DccConsist method programmingOpReply.

// This class is to be registered as a programmer listener, so we
// include the programmingOpReply() function
@Override
public void programmingOpReply(int value, int status) {
    log.debug("Programming Operation reply recieved, value is {}, status is ", value, status);
    notifyConsistListeners(new DccLocoAddress(0, false), ConsistListener.OPERATION_SUCCESS);
}
Also used : DccLocoAddress(jmri.DccLocoAddress)

Example 2 with DccLocoAddress

use of jmri.DccLocoAddress in project JMRI by JMRI.

the class DccConsist method restore.

/*
     * Restore the consist to the command station.
     */
@Override
public void restore() {
    // itterate through the list to re-add the addresses to the 
    // command station.
    java.util.Iterator<DccLocoAddress> i = ConsistList.iterator();
    while (i.hasNext()) {
        DccLocoAddress locoaddress = i.next();
        add(locoaddress, getLocoDirection(locoaddress));
    }
    // notify any listeners that the consist changed
    this.notifyConsistListeners(ConsistAddress, ConsistListener.OK);
}
Also used : DccLocoAddress(jmri.DccLocoAddress)

Example 3 with DccLocoAddress

use of jmri.DccLocoAddress 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 4 with DccLocoAddress

use of jmri.DccLocoAddress in project JMRI by JMRI.

the class RosterEntry method store.

/**
     * Create an XML element to represent this Entry.
     * <p>
     * This member has to remain
     * synchronized with the detailed DTD in roster-config.xml.
     *
     * @return Contents in a JDOM Element
     */
@Override
public Element store() {
    Element e = new Element("locomotive");
    e.setAttribute("id", getId());
    e.setAttribute("fileName", getFileName());
    e.setAttribute("roadNumber", getRoadNumber());
    e.setAttribute("roadName", getRoadName());
    e.setAttribute("mfg", getMfg());
    e.setAttribute("owner", getOwner());
    e.setAttribute("model", getModel());
    e.setAttribute("dccAddress", getDccAddress());
    //e.setAttribute("protocol",""+getProtocol());
    e.setAttribute("comment", getComment());
    e.setAttribute(RosterEntry.MAX_SPEED, (Integer.toString(getMaxSpeedPCT())));
    // file path are saved without default xml config path
    e.setAttribute("imageFilePath", (this.getImagePath() != null) ? FileUtil.getPortableFilename(this.getImagePath()) : "");
    e.setAttribute("iconFilePath", (this.getIconPath() != null) ? FileUtil.getPortableFilename(this.getIconPath()) : "");
    e.setAttribute("URL", getURL());
    e.setAttribute(RosterEntry.SHUNTING_FUNCTION, getShuntingFunction());
    if (_dateUpdated.isEmpty()) {
        // set date updated to now if never set previously
        this.changeDateUpdated();
    }
    e.addContent(new Element("dateUpdated").addContent(this.getDateUpdated()));
    Element d = new Element("decoder");
    d.setAttribute("model", getDecoderModel());
    d.setAttribute("family", getDecoderFamily());
    d.setAttribute("comment", getDecoderComment());
    e.addContent(d);
    if (_dccAddress.isEmpty()) {
        // store a null address
        e.addContent((new jmri.configurexml.LocoAddressXml()).store(null));
    } else {
        e.addContent((new jmri.configurexml.LocoAddressXml()).store(new DccLocoAddress(Integer.parseInt(_dccAddress), _protocol)));
    }
    if (functionLabels != null) {
        d = new Element("functionlabels");
        // loop to copy non-null elements
        for (int i = 0; i <= MAXFNNUM; i++) {
            if (functionLabels[i] != null && !functionLabels[i].isEmpty()) {
                Element fne = new Element(RosterEntry.FUNCTION_LABEL);
                fne.setAttribute("num", "" + i);
                boolean lockable = false;
                if (functionLockables != null) {
                    lockable = functionLockables[i];
                }
                fne.setAttribute("lockable", lockable ? "true" : "false");
                if ((functionImages != null)) {
                    fne.setAttribute("functionImage", (functionImages[i] != null) ? FileUtil.getPortableFilename(functionImages[i]) : "");
                }
                if ((functionSelectedImages != null)) {
                    fne.setAttribute("functionImageSelected", (functionSelectedImages[i] != null) ? FileUtil.getPortableFilename(functionSelectedImages[i]) : "");
                }
                fne.addContent(functionLabels[i]);
                d.addContent(fne);
            }
        }
        e.addContent(d);
    }
    if (soundLabels != null) {
        d = new Element("soundlabels");
        // loop to copy non-null elements
        for (int i = 0; i < MAXSOUNDNUM; i++) {
            if (soundLabels[i] != null && !soundLabels[i].isEmpty()) {
                Element fne = new Element(RosterEntry.SOUND_LABEL);
                fne.setAttribute("num", "" + i);
                fne.addContent(soundLabels[i]);
                d.addContent(fne);
            }
        }
        e.addContent(d);
    }
    if (!getAttributes().isEmpty()) {
        d = new Element("attributepairs");
        for (String key : getAttributes()) {
            d.addContent(new Element("keyvaluepair").addContent(new Element("key").addContent(key)).addContent(new Element("value").addContent(getAttribute(key))));
        }
        e.addContent(d);
    }
    if (_sp != null) {
        _sp.store(e);
    }
    return e;
}
Also used : Element(org.jdom2.Element) DccLocoAddress(jmri.DccLocoAddress)

Example 5 with DccLocoAddress

use of jmri.DccLocoAddress in project JMRI by JMRI.

the class ThrottleController method setLocoForConsistFunctions.

public void setLocoForConsistFunctions(String inPackage) {
    /*
         *      This is used to control speed an direction on the
         *      consist address, but have functions mapped to lead.
         *      Consist address must be set first!
         */
    leadAddress = new DccLocoAddress(Integer.parseInt(inPackage.substring(1)), (inPackage.charAt(0) != 'S'));
    if (log.isDebugEnabled()) {
        log.debug("Setting lead loco address: " + leadAddress.toString() + ", for consist: " + getCurrentAddressString());
    }
    clearLeadLoco();
    leadLocoF = new ConsistFunctionController(this);
    useLeadLocoF = leadLocoF.requestThrottle(leadAddress);
    if (!useLeadLocoF) {
        log.warn("Lead loco address not available.");
        leadLocoF = null;
    }
}
Also used : DccLocoAddress(jmri.DccLocoAddress)

Aggregations

DccLocoAddress (jmri.DccLocoAddress)96 Consist (jmri.Consist)12 ThrottleManager (jmri.ThrottleManager)7 IOException (java.io.IOException)6 Element (org.jdom2.Element)6 Test (org.junit.Test)6 Matcher (java.util.regex.Matcher)5 DccThrottle (jmri.DccThrottle)5 SystemConnectionMemo (jmri.jmrix.SystemConnectionMemo)5 JsonException (jmri.server.json.JsonException)5 Pattern (java.util.regex.Pattern)4 IdTag (jmri.IdTag)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 JTextField (javax.swing.JTextField)2 CommandStation (jmri.CommandStation)2 ProgrammerException (jmri.ProgrammerException)2