Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhwood committed Sep 1, 2019
2 parents 6b4e769 + 159eb9a commit f0e4239
Show file tree
Hide file tree
Showing 6 changed files with 590 additions and 138 deletions.
47 changes: 17 additions & 30 deletions java/src/jmri/implementation/AbstractConsistManager.java
Expand Up @@ -15,14 +15,14 @@
* @author Paul Bender Copyright (C) 2004
* @author Randall Wood Copyright (C) 2013
*/
abstract public class AbstractConsistManager implements ConsistManager {
public abstract class AbstractConsistManager implements ConsistManager {

protected HashMap<LocoAddress, Consist> consistTable = null;
private ArrayList<ConsistListListener> changeListeners = null;

public AbstractConsistManager() {
consistTable = new HashMap<LocoAddress, Consist>();
changeListeners = new ArrayList<ConsistListListener>();
consistTable = new HashMap<>();
changeListeners = new ArrayList<>();
}

/**
Expand All @@ -44,7 +44,7 @@ public Consist getConsist(LocoAddress address) {
* @return a consist at address; this will be the existing consist if a
* consist is already known to exist at address
*/
abstract protected Consist addConsist(LocoAddress address);
protected abstract Consist addConsist(LocoAddress address);

// remove the old Consist
@Override
Expand All @@ -53,64 +53,51 @@ public void delConsist(LocoAddress address) {
consistTable.remove(address);
}

/**
* Does this implementation support a command station consist?
*/
@Override
abstract public boolean isCommandStationConsistPossible();

/**
* Does a CS consist require a separate consist address? (or is the lead
* loco to be used for the consist address)
*/
@Override
abstract public boolean csConsistNeedsSeperateAddress();

/**
* Return the list of consists we know about.
*/
@Override
public ArrayList<LocoAddress> getConsistList() {
return new ArrayList<LocoAddress>(consistTable.keySet());
return new ArrayList<>(consistTable.keySet());
}

@Override
public String decodeErrorCode(int ErrorCode) {
public String decodeErrorCode(int errorCode) {
StringBuilder buffer = new StringBuilder("");
if ((ErrorCode & ConsistListener.NotImplemented) != 0) {
if ((errorCode & ConsistListener.NotImplemented) != 0) {
buffer.append("Not Implemented ");
}
if ((ErrorCode & ConsistListener.OPERATION_SUCCESS) != 0) {
if ((errorCode & ConsistListener.OPERATION_SUCCESS) != 0) {
buffer.append("Operation Completed Successfully ");
}
if ((ErrorCode & ConsistListener.CONSIST_ERROR) != 0) {
if ((errorCode & ConsistListener.CONSIST_ERROR) != 0) {
buffer.append("Consist Error ");
}
if ((ErrorCode & ConsistListener.LOCO_NOT_OPERATED) != 0) {
if ((errorCode & ConsistListener.LOCO_NOT_OPERATED) != 0) {
buffer.append("Address not controled by this device.");
}
if ((ErrorCode & ConsistListener.ALREADY_CONSISTED) != 0) {
if ((errorCode & ConsistListener.ALREADY_CONSISTED) != 0) {
buffer.append("Locomotive already consisted");
}
if ((ErrorCode & ConsistListener.NOT_CONSISTED) != 0) {
if ((errorCode & ConsistListener.NOT_CONSISTED) != 0) {
buffer.append("Locomotive Not Consisted ");
}
if ((ErrorCode & ConsistListener.NONZERO_SPEED) != 0) {
if ((errorCode & ConsistListener.NONZERO_SPEED) != 0) {
buffer.append("Speed Not Zero ");
}
if ((ErrorCode & ConsistListener.NOT_CONSIST_ADDR) != 0) {
if ((errorCode & ConsistListener.NOT_CONSIST_ADDR) != 0) {
buffer.append("Address Not Conist Address ");
}
if ((ErrorCode & ConsistListener.DELETE_ERROR) != 0) {
if ((errorCode & ConsistListener.DELETE_ERROR) != 0) {
buffer.append("Delete Error ");
}
if ((ErrorCode & ConsistListener.STACK_FULL) != 0) {
if ((errorCode & ConsistListener.STACK_FULL) != 0) {
buffer.append("Stack Full ");
}

String retval = buffer.toString();
if (retval.equals("")) {
return "Unknown Status Code: " + ErrorCode;
return "Unknown Status Code: " + errorCode;
} else {
return retval;
}
Expand Down
33 changes: 26 additions & 7 deletions java/src/jmri/implementation/AbstractIdTag.java
@@ -1,11 +1,12 @@
package jmri.implementation;

import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import jmri.IdTag;
import jmri.Reportable;
import jmri.Reporter;

import jmri.*;
import jmri.managers.ProxyIdTagManager;

/**
* Abstract implementation of {@link jmri.IdTag} containing code common to all
Expand All @@ -19,6 +20,7 @@ public abstract class AbstractIdTag extends AbstractNamedBean implements IdTag,
protected Reporter whereLastSeen = null;

protected Date whenLastSeen = null;
protected String prefix = null;

public AbstractIdTag(String systemName) {
super(systemName);
Expand All @@ -31,9 +33,27 @@ public AbstractIdTag(String systemName, String userName) {
@Override
@Nonnull
public String getTagID() {
// TODO: Convert this to allow for >1 char system name length
// Or, is this really necessary as it will always be 'I'nternal???
return this.mSystemName.substring(2);
if(prefix == null) {
try {
prefix = findPrefix();
} catch ( NullPointerException | BadSystemNameException e) {
// if there isn't a ProxyIDTag Manager, assume the first D in the
// system name is the type letter.
return mSystemName.substring(mSystemName.indexOf('D') + 1);

}
}
return mSystemName.substring(prefix.length()+1);
}

private String findPrefix() {
List<Manager<IdTag>> managerList = InstanceManager.getDefault(ProxyIdTagManager.class).getManagerList();
for (Manager<IdTag> m : managerList) {
if (m.getBeanBySystemName(mSystemName) != null) {
return m.getSystemPrefix();
}
}
throw new BadSystemNameException();
}

@Override
Expand Down Expand Up @@ -82,5 +102,4 @@ public String getBeanType() {
return Bundle.getMessage("BeanNameReporter");
}

// private static final Logger log = LoggerFactory.getLogger(AbstractIdTag.class);
}
14 changes: 8 additions & 6 deletions java/src/jmri/implementation/AbstractIdTagReporter.java
Expand Up @@ -35,21 +35,21 @@ public AbstractIdTagReporter(String systemName, String userName) {

@Override
public void notify(IdTag id) {
log.debug("Notify: " + this.mSystemName);
log.debug("Notify: {}",mSystemName);
if (id != null) {
log.debug("Tag: " + id);
log.debug("Tag: {}",id);
AbstractIdTagReporter r;
if ((r = (AbstractIdTagReporter) id.getWhereLastSeen()) != null) {
log.debug("Previous reporter: " + r.mSystemName);
log.debug("Previous reporter: {}",r.mSystemName);
if (!(r.equals(this)) && r.getCurrentReport() == id) {
log.debug("Notify previous");
r.notify(null);
} else {
log.debug("Current report was: " + r.getCurrentReport());
log.debug("Current report was: {}",r.getCurrentReport());
}
}
id.setWhereLastSeen(this);
log.debug("Seen here: " + this.mSystemName);
log.debug("Seen here: {}",this.mSystemName);
}
setReport(id);
setState(id != null ? IdTag.SEEN : IdTag.UNSEEN);
Expand Down Expand Up @@ -86,7 +86,9 @@ public LocoAddress getLocoAddress(String rep) {
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));
if(log.isDebugEnabled()) {
log.debug("Parsed address: {}", m.group(1));
}
// I have no idea what kind of loco address an Ecos reporter uses,
// so we'll default to DCC for now.
return (new DccLocoAddress(Integer.parseInt(m.group(1)), LocoAddress.Protocol.DCC));
Expand Down
57 changes: 23 additions & 34 deletions java/src/jmri/implementation/DccConsist.java
Expand Up @@ -51,7 +51,7 @@ public class DccConsist implements Consist, ProgListener {
protected DccLocoAddress consistAddress = null;
protected String consistID = null;
// data member to hold the throttle listener objects
final private ArrayList<ConsistListener> listeners;
private final ArrayList<ConsistListener> listeners;


private AddressedProgrammerManager opsProgManager = null;
Expand Down Expand Up @@ -88,13 +88,12 @@ public DccConsist(DccLocoAddress address,AddressedProgrammerManager apm) {
@Override
public void dispose() {
if (consistList == null) {
// already disposed;
return;
}
for (int i = (consistList.size() - 1); i >= 0; i--) {
DccLocoAddress loco = consistList.get(i);
if (log.isDebugEnabled()) {
log.debug("Deleting Locomotive: " + loco.toString());
log.debug("Deleting Locomotive: {}",loco);
}
try {
remove(loco);
Expand All @@ -114,11 +113,15 @@ public void setConsistType(int consist_type) {
if (consist_type == ADVANCED_CONSIST) {
consistType = consist_type;
} else {
log.error("Consist Type Not Supported");
notifyConsistListeners(new DccLocoAddress(0, false), ConsistListener.NotImplemented);
notifyUnsupportedConsistType();
}
}

private void notifyUnsupportedConsistType(){
log.error("Consist Type Not Supported");
notifyConsistListeners(new DccLocoAddress(0, false), ConsistListener.NotImplemented);
}

// get the Consist Type
@Override
public int getConsistType() {
Expand Down Expand Up @@ -167,11 +170,9 @@ public ArrayList<DccLocoAddress> getConsistList() {
@Override
public boolean contains(DccLocoAddress address) {
if (consistType == ADVANCED_CONSIST) {
//String Address = Integer.toString(address);
return (consistList.contains(address));
} else {
log.error("Consist Type Not Supported");
notifyConsistListeners(new DccLocoAddress(0, false), ConsistListener.NotImplemented);
notifyUnsupportedConsistType();
}
return false;
}
Expand All @@ -181,12 +182,10 @@ public boolean contains(DccLocoAddress address) {
@Override
public boolean getLocoDirection(DccLocoAddress address) {
if (consistType == ADVANCED_CONSIST) {
//String Address= Integer.toString(address);
Boolean Direction = consistDir.get(address);
return (Direction);
} else {
log.error("Consist Type Not Supported");
notifyConsistListeners(address, ConsistListener.NotImplemented);
notifyUnsupportedConsistType();
}
return false;
}
Expand All @@ -200,7 +199,6 @@ public boolean getLocoDirection(DccLocoAddress address) {
@Override
public void add(DccLocoAddress LocoAddress, boolean directionNormal) {
if (consistType == ADVANCED_CONSIST) {
//String Address= Integer.toString(LocoAddress);
Boolean Direction = directionNormal;
if (!(consistList.contains(LocoAddress))) {
consistList.add(LocoAddress);
Expand All @@ -210,8 +208,7 @@ public void add(DccLocoAddress LocoAddress, boolean directionNormal) {
//set the value in the roster entry for CV19
setRosterEntryCVValue(LocoAddress);
} else {
log.error("Consist Type Not Supported");
notifyConsistListeners(LocoAddress, ConsistListener.NotImplemented);
notifyUnsupportedConsistType();
}
}

Expand All @@ -226,15 +223,13 @@ public void add(DccLocoAddress LocoAddress, boolean directionNormal) {
@Override
public void restore(DccLocoAddress LocoAddress, boolean directionNormal) {
if (consistType == ADVANCED_CONSIST) {
//String Address= Integer.toString(LocoAddress);
Boolean Direction = directionNormal;
if (!(consistList.contains(LocoAddress))) {
consistList.add(LocoAddress);
}
consistDir.put(LocoAddress, Direction);
} else {
log.error("Consist Type Not Supported");
notifyConsistListeners(LocoAddress, ConsistListener.NotImplemented);
notifyUnsupportedConsistType();
}
}

Expand All @@ -247,15 +242,13 @@ public void remove(DccLocoAddress LocoAddress) {
if (consistType == ADVANCED_CONSIST) {
//reset the value in the roster entry for CV19
resetRosterEntryCVValue(LocoAddress);
//String Address= Integer.toString(LocoAddress);
consistDir.remove(LocoAddress);
consistList.remove(LocoAddress);
consistPosition.remove(LocoAddress);
consistRoster.remove(LocoAddress);
removeFromAdvancedConsist(LocoAddress);
} else {
log.error("Consist Type Not Supported");
notifyConsistListeners(LocoAddress, ConsistListener.NotImplemented);
notifyUnsupportedConsistType();
}
}

Expand Down Expand Up @@ -438,7 +431,7 @@ protected void updateRosterCV(DccLocoAddress address,Boolean direction,int value

entry.loadCvModel(varTable, cvTable);
CvValue cv19Value = cvTable.getCvByNumber("19");
cv19Value.setValue((value & 0xff) | (direction?0x00:0x80 ));
cv19Value.setValue((value & 0xff) | (direction.booleanValue()?0x00:0x80 ));

entry.writeFile(cvTable,varTable);
}
Expand All @@ -449,29 +442,29 @@ protected void loadDecoderFromLoco(RosterEntry r,VariableTableModel varTable) {
String decoderModel = r.getDecoderModel();
String decoderFamily = r.getDecoderFamily();
if (log.isDebugEnabled()) {
log.debug("selected loco uses decoder " + decoderFamily + " " + decoderModel);
log.debug("selected loco uses decoder {} {}",decoderFamily,decoderModel);
}
// locate a decoder like that.
List<DecoderFile> l = InstanceManager.getDefault(DecoderIndexFile.class).matchingDecoderList(null, decoderFamily, null, null, null, decoderModel);
if (log.isDebugEnabled()) {
log.debug("found " + l.size() + " matches");
log.debug("found {} matches",l.size());
}
if (l.size() == 0) {
log.debug("Loco uses " + decoderFamily + " " + decoderModel + " decoder, but no such decoder defined");
if (l.isEmpty()) {
log.debug("Loco uses {} {} decoder, but no such decoder defined",decoderFamily,decoderModel );
// fall back to use just the decoder name, not family
l = InstanceManager.getDefault(DecoderIndexFile.class).matchingDecoderList(null, null, null, null, null, decoderModel);
if (log.isDebugEnabled()) {
log.debug("found " + l.size() + " matches without family key");
log.debug("found {} matches without family key",l.size());
}
}
if (l.size() > 0) {
if (!l.isEmpty()) {
DecoderFile d = l.get(0);
loadDecoderFile(d, r, varTable);
} else {
if (decoderModel.equals("")) {
log.debug("blank decoderModel requested, so nothing loaded");
} else {
log.warn("no matching \"" + decoderModel + "\" decoder found for loco, no decoder info loaded");
log.warn("no matching \"{}\" decoder found for loco, no decoder info loaded",decoderModel );
}
}
}
Expand All @@ -497,10 +490,6 @@ protected void loadDecoderFile(DecoderFile df, RosterEntry re,VariableTableModel
df.getProductID();
if(decoderRoot!=null) {
df.loadVariableModel(decoderRoot.getChild("decoder"), variableModel);

// load reset from decoder tree
//df.loadResetModel(decoderRoot.getChild("decoder"), resetModel);

// load function names
re.loadFunctions(decoderRoot.getChild("decoder").getChild("family").getChild("functionlabels"));
}
Expand Down Expand Up @@ -623,7 +612,7 @@ protected void notifyConsistListeners(DccLocoAddress LocoAddress, int ErrorCode)
ErrorCode,
v.size(), LocoAddress);
// forward to all listeners
v.forEach((client) -> {
v.forEach(client -> {
client.consistReply(LocoAddress, ErrorCode);
});
}
Expand All @@ -636,5 +625,5 @@ public void programmingOpReply(int value, int status) {
notifyConsistListeners(new DccLocoAddress(0, false), ConsistListener.OPERATION_SUCCESS);
}

private final static Logger log = LoggerFactory.getLogger(DccConsist.class);
private static final Logger log = LoggerFactory.getLogger(DccConsist.class);
}

0 comments on commit f0e4239

Please sign in to comment.