Search in sources :

Example 66 with DccLocoAddress

use of jmri.DccLocoAddress in project JMRI by JMRI.

the class NceThrottleManager method requestThrottleSetup.

@Override
public void requestThrottleSetup(LocoAddress a, boolean control) {
    // the NCE protocol doesn't require an interaction with the command
    // station for this, so immediately trigger the callback.
    DccLocoAddress address = (DccLocoAddress) a;
    log.debug("new NceThrottle for " + address);
    notifyThrottleKnown(new NceThrottle((NceSystemConnectionMemo) adapterMemo, address), address);
}
Also used : DccLocoAddress(jmri.DccLocoAddress)

Example 67 with DccLocoAddress

use of jmri.DccLocoAddress in project JMRI by JMRI.

the class OlcbThrottleManager method requestThrottleSetup.

@Override
public void requestThrottleSetup(LocoAddress a, boolean control) {
    // Immediately trigger the callback.
    DccLocoAddress address = (DccLocoAddress) a;
    log.debug("new debug throttle for " + address);
    notifyThrottleKnown(new OlcbThrottle(address, adapterMemo, mgr), a);
}
Also used : DccLocoAddress(jmri.DccLocoAddress)

Example 68 with DccLocoAddress

use of jmri.DccLocoAddress in project JMRI by JMRI.

the class RfidReporter method getLocoAddress.

// Methods to support PhysicalLocationReporter interface
/**
     * getLocoAddress()
     *
     * get the locomotive address we're reporting about from the current report.
     *
     * Note: We ignore the string passed in, because rfid Reporters don't send
     * String type reports.
     */
@Override
public LocoAddress getLocoAddress(String rep) {
    // For now, we assume the current report.
    // IdTag.getTagID() is a system-name-ized version of the loco address. I think.
    // Matcher.group(1) : loco address (I think)
    IdTag cr = (IdTag) this.getCurrentReport();
    ReporterManager rm = InstanceManager.getDefault(jmri.ReporterManager.class);
    Pattern p = Pattern.compile("" + rm.getSystemPrefix() + rm.typeLetter() + "(\\d+)");
    Matcher m = p.matcher(cr.getTagID());
    if (m.find()) {
        log.debug("Parsed address: " + m.group(1));
        // so we'll default to DCC for now.
        return (new DccLocoAddress(Integer.parseInt(m.group(1)), LocoAddress.Protocol.DCC));
    } else {
        return (null);
    }
}
Also used : Pattern(java.util.regex.Pattern) ReporterManager(jmri.ReporterManager) Matcher(java.util.regex.Matcher) IdTag(jmri.IdTag) DccLocoAddress(jmri.DccLocoAddress)

Example 69 with DccLocoAddress

use of jmri.DccLocoAddress in project JMRI by JMRI.

the class RpsReporter method getLocoAddress.

// Methods to support PhysicalLocationReporter interface
/**
     * getLocoAddress()
     *
     * Parses out a (possibly old) RpsReporter-generated report string to
     * extract the address from the front. Assumes the RpsReporter format is
     * "NNNN"
     */
public LocoAddress getLocoAddress(String rep) {
    // The report is a string, that is the ID of the locomotive (I think)
    log.debug("Parsed ID: " + rep);
    // so we'll default to DCC for now.
    if (rep.length() > 0) {
        try {
            int id = Integer.parseInt(rep);
            int addr = Engine.instance().getTransmitter(id).getAddress();
            return (new DccLocoAddress(addr, LocoAddress.Protocol.DCC));
        } catch (NumberFormatException e) {
            return (null);
        }
    } else {
        return (null);
    }
}
Also used : DccLocoAddress(jmri.DccLocoAddress)

Example 70 with DccLocoAddress

use of jmri.DccLocoAddress in project JMRI by JMRI.

the class JsonThrottle method getThrottle.

/**
     * Creates a new JsonThrottle or returns an existing one if the request is
     * for an existing throttle.
     *
     * data can contain either a string {@link jmri.server.json.JSON#ID} node
     * containing the ID of a {@link jmri.jmrit.roster.RosterEntry} or an
     * integer {@link jmri.server.json.JSON#ADDRESS} node. If data contains an
     * ADDRESS, the ID node is ignored. The ADDRESS may be accompanied by a
     * boolean {@link jmri.server.json.JSON#IS_LONG_ADDRESS} node specifying the
     * type of address, if IS_LONG_ADDRESS is not specified, the inverse of {@link jmri.ThrottleManager#canBeShortAddress(int)
     * } is used as the "best guess" of the address length.
     *
     * @param throttleId The client's identity token for this throttle
     * @param data       JSON object containing either an ADDRESS or an ID
     * @param server     The server requesting this throttle on behalf of a
     *                   client
     * @return The throttle
     * @throws jmri.server.json.JsonException if unable to get the requested
     *                                        {@link jmri.Throttle} 
     */
public static JsonThrottle getThrottle(String throttleId, JsonNode data, JsonThrottleSocketService server) throws JsonException {
    DccLocoAddress address = null;
    Locale locale = server.getConnection().getLocale();
    JsonThrottleManager manager = JsonThrottleManager.getDefault();
    if (!data.path(ADDRESS).isMissingNode()) {
        if (manager.canBeLongAddress(data.path(ADDRESS).asInt()) || manager.canBeShortAddress(data.path(ADDRESS).asInt())) {
            address = new DccLocoAddress(data.path(ADDRESS).asInt(), data.path(IS_LONG_ADDRESS).asBoolean(!manager.canBeShortAddress(data.path(ADDRESS).asInt())));
        } else {
            log.warn("Address \"{}\" is not a valid address.", data.path(ADDRESS).asInt());
            // NOI18N
            throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, Bundle.getMessage(locale, "ErrorThrottleInvalidAddress", data.path(ADDRESS).asInt()));
        }
    } else if (!data.path(ID).isMissingNode()) {
        try {
            address = Roster.getDefault().getEntryForId(data.path(ID).asText()).getDccLocoAddress();
        } catch (NullPointerException ex) {
            log.warn("Roster entry \"{}\" does not exist.", data.path(ID).asText());
            // NOI18N
            throw new JsonException(HttpServletResponse.SC_NOT_FOUND, Bundle.getMessage(locale, "ErrorThrottleRosterEntry", data.path(ID).asText()));
        }
    } else {
        log.warn("No address specified");
        // NOI18N
        throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, Bundle.getMessage(locale, "ErrorThrottleNoAddress"));
    }
    if (manager.containsKey(address)) {
        JsonThrottle throttle = manager.get(address);
        manager.put(throttle, server);
        throttle.sendMessage(server.getConnection().getObjectMapper().createObjectNode().put(CLIENTS, manager.getServers(throttle).size()));
        return throttle;
    } else {
        JsonThrottle throttle = new JsonThrottle(address, server);
        if (!manager.requestThrottle(address, throttle)) {
            log.error("Unable to get throttle for \"{}\".", address);
            throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, Bundle.getMessage(server.getConnection().getLocale(), "ErrorThrottleUnableToGetThrottle", address));
        }
        manager.put(address, throttle);
        manager.put(throttle, server);
        manager.attachListener(address, throttle);
        return throttle;
    }
}
Also used : Locale(java.util.Locale) JsonException(jmri.server.json.JsonException) 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