Skip to content
Permalink
Browse files

allow any case for hex digits in Z21CanReporter system name.

  • Loading branch information...
pabender committed Aug 27, 2019
1 parent c66b7f7 commit ae6d22c7051b241802c22b82096e0cc3a25e8424
@@ -2,6 +2,7 @@

import jmri.InstanceManager;
import jmri.RailComManager;
import jmri.JmriException;
import jmri.Reporter;
import jmri.jmrix.internal.InternalSystemConnectionMemo;
import org.slf4j.Logger;
@@ -153,6 +154,61 @@ public void enableInternalReporterCreationFromMessages() {
autoCreateInternalReporter = true;
}

synchronized private String createSystemName(String curAddress, String prefix) throws JmriException {
int encoderAddress = 0;
int input = 0;

if (curAddress.contains(":")) {
// Address format passed is in the form of encoderAddress:input or T:turnout address
int seperator = curAddress.indexOf(":");
try {
encoderAddress = Integer.parseInt(curAddress.substring(0, seperator));
input = Integer.parseInt(curAddress.substring(seperator + 1));
} catch (NumberFormatException ex) {
// system name may include hex values for CAN sensors.
try {
encoderAddress = Integer.parseInt(curAddress.substring(0, seperator), 16);
input = Integer.parseInt(curAddress.substring(seperator + 1));
return getSystemPrefix() + typeLetter() + String.format("%4x", encoderAddress) + ":" + input;
} catch (NumberFormatException ex1) {
log.error("Unable to convert {} into the cab and input format of nn:xx", curAddress);
throw new JmriException("Hardware Address passed should be a number");
}
}
iName = ((encoderAddress - 1) * 8) + input;
} else {
// Entered in using the old format
try {
iName = Integer.parseInt(curAddress);
} catch (NumberFormatException ex) {
log.error("Unable to convert {} Hardware Address to a number", curAddress);
throw new JmriException("Hardware Address passed should be a number");
}
}
return getSystemPrefix() + typeLetter() + iName;
}

int iName; // must synchronize to avoid race conditions.

/**
* {@inheritDoc}
*/
@Override
public Reporter getBySystemName(String sName){
Reporter r = super.getBySystemName(sName);
if(r == null && sName.contains(":")) {
// normalize the hex characters in the system name to upper case.
String curAddress = sName.substring(getSystemPrefix().length() +1);
try {
return super.getBySystemName(createSystemName(curAddress, getSystemPrefix()));
} catch (JmriException je) {
// format isn't correct, but s already equals null, so just return
// that.
}
}
return r;
}

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

}
@@ -1,5 +1,6 @@
package jmri.jmrix.roco.z21;

import jmri.Reporter;
import jmri.util.JUnitUtil;
import org.junit.*;

@@ -31,6 +32,36 @@ public void testAutomaticCreateFromCanReply(){
Assert.assertNotNull("Reporter Created via message",zr.getReporter("ZRABCD:1"));
}

@Test
@Override
public void testProvideName() {
// create
Reporter t = l.provide("ZRABCD:1");
// check
Assert.assertTrue("real object returned ", t != null);
Assert.assertEquals("system name correct ", t,l.getBySystemName("ZRABCD:1"));
}

@Test
public void testDefaultSystemNameLowerCase() {
// create
Reporter t = l.provideReporter("ZRabcd:1");
// check
Assert.assertNotNull("real object returned ", t);
Assert.assertEquals("system name same input correct ", t,l.getBySystemName("ZRabcd:1"));
Assert.assertEquals("system name same value correct ", t,l.getBySystemName("ZRABCD:1"));
}

@Test
public void testDefaultSystemMixedDigitx() {
// create
Reporter t = l.provideReporter("ZRa1c3:5");
// check
Assert.assertNotNull("real object returned ", t);
Assert.assertEquals("system name same input correct ", t,l.getBySystemName("ZRa1c3:5"));
Assert.assertEquals("system name same value correct ", t,l.getBySystemName("ZRA1C3:5"));
}

@Before
@Override
public void setUp() {

0 comments on commit ae6d22c

Please sign in to comment.
You can’t perform that action at this time.