Search in sources :

Example 86 with DccLocoAddress

use of jmri.DccLocoAddress in project JMRI by JMRI.

the class SprogThrottleManager method requestThrottleSetup.

@Override
public void requestThrottleSetup(LocoAddress a, boolean control) {
    // The SPROG protocol doesn't require an interaction with the command
    // station for this, so set the address and immediately trigger the callback
    // if a throttle is not in use.
    DccLocoAddress address = (DccLocoAddress) a;
    if (!throttleInUse) {
        throttleInUse = true;
        log.debug("new SprogThrottle for " + address);
        String addr = "" + address.getNumber() + (address.isLongAddress() ? " 0" : "");
        SprogMessage m = new SprogMessage(2 + addr.length());
        int i = 0;
        m.setElement(i++, 'A');
        m.setElement(i++, ' ');
        for (int j = 0; j < addr.length(); j++) {
            m.setElement(i++, addr.charAt(j));
        }
        ((SprogSystemConnectionMemo) adapterMemo).getSprogTrafficController().sendSprogMessage(m, null);
        notifyThrottleKnown(new SprogThrottle((SprogSystemConnectionMemo) adapterMemo, address), address);
    } else {
        failedThrottleRequest(address, "Only one Throttle can be in use at anyone time with the Sprog.");
        //javax.swing.JOptionPane.showMessageDialog(null,"Only one Throttle can be in use at anyone time with the Sprog.","Sprog Throttle",javax.swing.JOptionPane.WARNING_MESSAGE);
        log.warn("Single SPROG Throttle already in use");
    }
}
Also used : DccLocoAddress(jmri.DccLocoAddress)

Example 87 with DccLocoAddress

use of jmri.DccLocoAddress in project JMRI by JMRI.

the class SerialThrottleManager method requestThrottleSetup.

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

Example 88 with DccLocoAddress

use of jmri.DccLocoAddress in project JMRI by JMRI.

the class Mx1ThrottleManager method requestThrottleSetup.

@Override
public void requestThrottleSetup(LocoAddress a, boolean control) {
    //We do interact
    DccLocoAddress address = (DccLocoAddress) a;
    //IN18N
    log.debug("new Mx1Throttle for " + address);
    notifyThrottleKnown(new Mx1Throttle((Mx1SystemConnectionMemo) adapterMemo, address), address);
}
Also used : DccLocoAddress(jmri.DccLocoAddress)

Example 89 with DccLocoAddress

use of jmri.DccLocoAddress in project JMRI by JMRI.

the class JsonConsistSocketService method onMessage.

@Override
public void onMessage(String type, JsonNode data, Locale locale) throws IOException, JmriException, JsonException {
    this.locale = locale;
    if (JsonConsist.CONSISTS.equals(type)) {
        this.connection.sendMessage(this.service.doGetList(type, locale));
    } else {
        DccLocoAddress address = new DccLocoAddress(data.path(JSON.ADDRESS).asInt(), data.path(JSON.IS_LONG_ADDRESS).asBoolean());
        String name = address.getNumber() + (address.isLongAddress() ? "L" : "");
        if (data.path(JSON.METHOD).asText().equals(JSON.PUT)) {
            this.connection.sendMessage(this.service.doPut(type, name, data, locale));
        } else {
            this.connection.sendMessage(this.service.doPost(type, name, data, locale));
        }
        if (!this.consists.contains(address)) {
            this.service.manager.getConsist(address).addConsistListener(this.consistListener);
            this.consists.add(address);
        }
    }
}
Also used : DccLocoAddress(jmri.DccLocoAddress)

Example 90 with DccLocoAddress

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

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