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 JSON#ID} node containing the ID
* of a {@link jmri.jmrit.roster.RosterEntry} or an integer
* {@link JSON#ADDRESS} node. If data contains an ADDRESS, the ID node is
* ignored. The ADDRESS may be accompanied by a boolean
* {@link 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.JmriException if unable to get the requested throttle
* @throws java.io.IOException if unable to communicate with client
*/
public static JsonThrottle getThrottle(String throttleId, JsonNode data, JsonThrottleServer server) throws JmriException, IOException {
DccLocoAddress address = null;
if (!data.path(ADDRESS).isMissingNode()) {
if (InstanceManager.throttleManagerInstance().canBeLongAddress(data.path(ADDRESS).asInt()) || InstanceManager.throttleManagerInstance().canBeShortAddress(data.path(ADDRESS).asInt())) {
address = new DccLocoAddress(data.path(ADDRESS).asInt(), data.path(IS_LONG_ADDRESS).asBoolean(!InstanceManager.throttleManagerInstance().canBeShortAddress(data.path(ADDRESS).asInt())));
} else {
server.sendErrorMessage(-103, Bundle.getMessage(server.connection.getLocale(), "ErrorThrottleInvalidAddress", data.path(ADDRESS).asInt()));
// NOI18N
throw new JmriException("Address " + data.path(ADDRESS).asInt() + " is not a valid address.");
}
} else if (!data.path(ID).isMissingNode()) {
try {
address = Roster.getDefault().getEntryForId(data.path(ID).asText()).getDccLocoAddress();
} catch (NullPointerException ex) {
server.sendErrorMessage(-100, Bundle.getMessage(server.connection.getLocale(), "ErrorThrottleRosterEntry", data.path(ID).asText()));
// NOI18N
throw new JmriException("Roster entry " + data.path(ID).asText() + " does not exist.");
}
} else {
server.sendErrorMessage(-101, Bundle.getMessage(server.connection.getLocale(), "ErrorThrottleNoAddress"));
// NOI18N
throw new JmriException("No address specified.");
}
if (JsonThrottle.throttles == null) {
JsonThrottle.throttles = new HashMap<>();
}
if (JsonThrottle.throttles.containsKey(address)) {
JsonThrottle throttle = JsonThrottle.throttles.get(address);
throttle.servers.add(server);
throttle.sendMessage(throttle.mapper.createObjectNode().put(CLIENTS, throttle.servers.size()));
return throttle;
} else {
JsonThrottle throttle = new JsonThrottle(address, server);
if (!InstanceManager.throttleManagerInstance().requestThrottle(address, throttle)) {
// NOI18N
throw new JmriException("Error getting throttle for " + address);
}
JsonThrottle.throttles.put(address, throttle);
return throttle;
}
}
use of jmri.DccLocoAddress in project JMRI by JMRI.
the class RosterEntryPane method update.
/**
* Fill a RosterEntry object from GUI contents
*
*/
public void update(RosterEntry r) {
r.setId(id.getText());
r.setRoadName(roadName.getText());
r.setRoadNumber(roadNumber.getText());
r.setMfg(mfg.getText());
r.setOwner(owner.getText());
r.setModel(model.getText());
DccLocoAddress a = addrSel.getAddress();
if (a != null) {
r.setDccAddress("" + a.getNumber());
r.setProtocol(a.getProtocol());
}
r.setComment(comment.getText());
JComponent editor = maxSpeedSpinner.getEditor();
r.setMaxSpeedPCT(Integer.parseInt(((JSpinner.DefaultEditor) editor).getTextField().getText()));
r.setDecoderFamily(decoderFamily.getText());
r.setDecoderModel(decoderModel.getText());
r.setDecoderComment(decoderComment.getText());
}
use of jmri.DccLocoAddress in project JMRI by JMRI.
the class RosterEntryPane method setDccAddressLong.
public void setDccAddressLong(boolean m) {
DccLocoAddress addr = addrSel.getAddress();
int n = 0;
if (addr != null) {
//If the protocol is already set to something other than DCC, then do not try to configure it as DCC long or short.
if (addr.getProtocol() != LocoAddress.Protocol.DCC_LONG && addr.getProtocol() != LocoAddress.Protocol.DCC_SHORT && addr.getProtocol() != LocoAddress.Protocol.DCC) {
return;
}
n = addr.getNumber();
}
addrSel.setAddress(new DccLocoAddress(n, m));
}
use of jmri.DccLocoAddress in project JMRI by JMRI.
the class RosterEntryPane method setDccAddress.
public void setDccAddress(String a) {
DccLocoAddress addr = addrSel.getAddress();
LocoAddress.Protocol protocol = addr.getProtocol();
addrSel.setAddress(new DccLocoAddress(Integer.parseInt(a), protocol));
}
use of jmri.DccLocoAddress in project JMRI by JMRI.
the class ConsistController method sendDataForConsist.
//send consist detail record for a single NCE consist, e.g. RCD}|{127(S)}|{2591]\[2591(L)}|{true]\[6318(L)}|{true]\[2608(L)}|{false
/* dboudreau 2/26/2012 added consist manager for NCE
public void sendDataForNCEConsist(NceConsistRosterEntry con){
if (listeners == null) return;
StringBuilder list = new StringBuilder("RCD"); // Roster Consist Data
list.append("}|{");
list.append(con.getConsistNumber()); //address
list.append("(S)}|{"); //consist address is always short
if (con.getId().length() > 0){
list.append(con.getId()); //id
}
//append entries for each loco (if set)
list.append("]\\[");
list.append(con.getLoco1DccAddress()); //loco address
list.append(con.isLoco1LongAddress() ? "(L)" : "(S)"); //include length
list.append("}|{");
list.append((con.getLoco1Direction().equals("normal"))); //forward is true, reverse is false
if (!con.getLoco2DccAddress().equals("")) {
list.append("]\\[");
list.append(con.getLoco2DccAddress());
list.append(con.isLoco2LongAddress() ? "(L)" : "(S)");
list.append("}|{");
list.append((con.getLoco2Direction().equals("normal")));
}
if (!con.getLoco3DccAddress().equals("")) {
list.append("]\\[");
list.append(con.getLoco3DccAddress());
list.append(con.isLoco3LongAddress() ? "(L)" : "(S)");
list.append("}|{");
list.append((con.getLoco3Direction().equals("normal")));
}
if (!con.getLoco4DccAddress().equals("")) {
list.append("]\\[");
list.append(con.getLoco4DccAddress());
list.append(con.isLoco4LongAddress() ? "(L)" : "(S)");
list.append("}|{");
list.append((con.getLoco4Direction().equals("normal")));
}
if (!con.getLoco5DccAddress().equals("")) {
list.append("]\\[");
list.append(con.getLoco5DccAddress());
list.append(con.isLoco5LongAddress() ? "(L)" : "(S)");
list.append("}|{");
list.append((con.getLoco5Direction().equals("normal")));
}
if (!con.getLoco6DccAddress().equals("")) {
list.append("]\\[");
list.append(con.getLoco6DccAddress());
list.append(con.isLoco6LongAddress() ? "(L)" : "(S)");
list.append("}|{");
list.append((con.getLoco6Direction().equals("normal")));
}
String message = list.toString();
for (ControllerInterface listener : listeners){
listener.sendPacketToDevice(message);
}
}
*/
public void sendDataForConsist(Consist con) {
if (listeners == null) {
return;
}
// Roster Consist Data
StringBuilder list = new StringBuilder("RCD");
list.append("}|{");
list.append(con.getConsistAddress());
list.append("}|{");
if (con.getConsistID().length() > 0) {
list.append(con.getConsistID());
}
for (DccLocoAddress loco : con.getConsistList()) {
list.append("]\\[");
list.append(loco.toString());
list.append("}|{");
list.append(con.getLocoDirection(loco));
}
String message = list.toString();
for (ControllerInterface listener : listeners) {
listener.sendPacketToDevice(message);
}
}
Aggregations