use of jmri.TurnoutManager in project JMRI by JMRI.
the class JsonTurnoutHttpServiceTest method testDoPost.
public void testDoPost() throws JmriException {
ObjectMapper mapper = new ObjectMapper();
JsonTurnoutHttpService service = new JsonTurnoutHttpService(mapper);
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
Turnout turnout1 = manager.provideTurnout("IT1");
JsonNode result;
JsonNode message;
try {
turnout1.setState(Turnout.UNKNOWN);
// set closed
message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.CLOSED);
result = service.doPost(JsonTurnoutServiceFactory.TURNOUT, "IT1", message, Locale.ENGLISH);
Assert.assertEquals(Turnout.CLOSED, turnout1.getState());
Assert.assertNotNull(result);
Assert.assertEquals(JSON.CLOSED, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set thrown
message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.THROWN);
result = service.doPost(JsonTurnoutServiceFactory.TURNOUT, "IT1", message, Locale.ENGLISH);
Assert.assertEquals(Turnout.THROWN, turnout1.getState());
Assert.assertNotNull(result);
Assert.assertEquals(JSON.THROWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set unknown - remains thrown
message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.UNKNOWN);
result = service.doPost(JsonTurnoutServiceFactory.TURNOUT, "IT1", message, Locale.ENGLISH);
Assert.assertEquals(Turnout.THROWN, turnout1.getState());
Assert.assertEquals(JSON.THROWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set invalid state
// Invalid value
message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, 42);
JsonException exception = null;
try {
service.doPost(JsonTurnoutServiceFactory.TURNOUT, "IT1", message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertEquals(Turnout.THROWN, turnout1.getState());
Assert.assertNotNull(exception);
Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.TurnoutManager in project JMRI by JMRI.
the class AbstractTurnoutManagerConfigXML method loadTurnouts.
/**
* Utility method to load the individual Turnout objects. If there's no
* additional info needed for a specific turnout type, invoke this with the
* parent of the set of Turnout elements.
*
* @param shared Element containing the Turnout elements to load.
* @param perNode Element containing per-node Turnout data.
* @return true if succeeded
*/
@SuppressWarnings("unchecked")
public boolean loadTurnouts(Element shared, Element perNode) {
boolean result = true;
List<Element> operationList = shared.getChildren("operations");
if (operationList.size() > 1) {
log.warn("unexpected extra elements found in turnout operations list");
result = false;
}
if (operationList.size() > 0) {
TurnoutOperationManagerXml tomx = new TurnoutOperationManagerXml();
tomx.load(operationList.get(0), null);
}
List<Element> turnoutList = shared.getChildren("turnout");
if (log.isDebugEnabled()) {
log.debug("Found " + turnoutList.size() + " turnouts");
}
TurnoutManager tm = InstanceManager.turnoutManagerInstance();
try {
if (shared.getChild("defaultclosedspeed") != null) {
String closedSpeed = shared.getChild("defaultclosedspeed").getText();
if (closedSpeed != null && !closedSpeed.equals("")) {
tm.setDefaultClosedSpeed(closedSpeed);
}
}
} catch (jmri.JmriException ex) {
log.error(ex.toString());
}
try {
if (shared.getChild("defaultthrownspeed") != null) {
String thrownSpeed = shared.getChild("defaultthrownspeed").getText();
if (thrownSpeed != null && !thrownSpeed.equals("")) {
tm.setDefaultThrownSpeed(thrownSpeed);
}
}
} catch (jmri.JmriException ex) {
log.error(ex.toString());
}
for (int i = 0; i < turnoutList.size(); i++) {
Element elem = turnoutList.get(i);
String sysName = getSystemName(elem);
if (sysName == null) {
log.error("unexpected null in systemName " + elem);
result = false;
break;
}
String userName = getUserName(elem);
checkNameNormalization(sysName, userName, tm);
if (log.isDebugEnabled()) {
log.debug("create turnout: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
}
Turnout t = tm.getBySystemName(sysName);
if (t == null) {
t = tm.newTurnout(sysName, userName);
//Nothing is logged in the console window as the newTurnoutFunction already does this.
} else if (userName != null) {
t.setUserName(userName);
}
// Load common parts
loadCommon(t, elem);
// now add feedback if needed
Attribute a;
a = elem.getAttribute("feedback");
if (a != null) {
try {
t.setFeedbackMode(a.getValue());
} catch (IllegalArgumentException e) {
log.error("Can not set feedback mode: '" + a.getValue() + "' for turnout: '" + sysName + "' user name: '" + (userName == null ? "" : userName) + "'");
result = false;
}
}
a = elem.getAttribute("sensor1");
if (a != null) {
try {
t.provideFirstFeedbackSensor(a.getValue());
} catch (jmri.JmriException e) {
result = false;
}
}
a = elem.getAttribute("sensor2");
if (a != null) {
try {
t.provideSecondFeedbackSensor(a.getValue());
} catch (jmri.JmriException e) {
result = false;
}
}
// check for turnout inverted
t.setInverted(getAttributeBool(elem, "inverted", false));
// check for turnout decoder
a = turnoutList.get(i).getAttribute("decoder");
if (a != null) {
t.setDecoderName(a.getValue());
}
// check for turnout lock mode
a = turnoutList.get(i).getAttribute("lockMode");
if (a != null) {
if (a.getValue().equals("both")) {
t.enableLockOperation(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, true);
}
if (a.getValue().equals("cab")) {
t.enableLockOperation(Turnout.CABLOCKOUT, true);
t.enableLockOperation(Turnout.PUSHBUTTONLOCKOUT, false);
}
if (a.getValue().equals("pushbutton")) {
t.enableLockOperation(Turnout.PUSHBUTTONLOCKOUT, true);
t.enableLockOperation(Turnout.CABLOCKOUT, false);
}
}
// check for turnout locked
a = turnoutList.get(i).getAttribute("locked");
if (a != null) {
t.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, a.getValue().equals("true"));
}
// number of bits, if present - if not, defaults to 1
a = turnoutList.get(i).getAttribute("numBits");
if (a == null) {
t.setNumberOutputBits(1);
} else {
int iNum = Integer.parseInt(a.getValue());
if ((iNum == 1) || (iNum == 2)) {
t.setNumberOutputBits(iNum);
} else {
log.warn("illegal number of output bits for control of turnout " + sysName);
t.setNumberOutputBits(1);
result = false;
}
}
// control type, if present - if not, defaults to 0
a = turnoutList.get(i).getAttribute("controlType");
if (a == null) {
t.setControlType(0);
} else {
int iType = Integer.parseInt(a.getValue());
if (iType >= 0) {
t.setControlType(iType);
} else {
log.warn("illegal control type for control of turnout " + sysName);
t.setControlType(0);
result = false;
}
}
// operation stuff
List<Element> myOpList = turnoutList.get(i).getChildren("operation");
if (myOpList.size() > 0) {
if (myOpList.size() > 1) {
log.warn("unexpected extra elements found in turnout-specific operations");
result = false;
}
TurnoutOperation toper = TurnoutOperationXml.loadOperation(myOpList.get(0));
t.setTurnoutOperation(toper);
} else {
a = turnoutList.get(i).getAttribute("automate");
if (a != null) {
String str = a.getValue();
if (str.equals("Off")) {
t.setInhibitOperation(true);
} else if (!str.equals("Default")) {
t.setInhibitOperation(false);
TurnoutOperation toper = TurnoutOperationManager.getInstance().getOperation(str);
t.setTurnoutOperation(toper);
} else {
t.setInhibitOperation(false);
}
}
}
// set initial state from sensor feedback if appropriate
t.setInitialKnownStateFromFeedback();
try {
t.setDivergingSpeed("Global");
if (elem.getChild("divergingSpeed") != null) {
String speed = elem.getChild("divergingSpeed").getText();
if (speed != null && !speed.equals("") && !speed.contains("Global")) {
t.setDivergingSpeed(speed);
}
}
} catch (jmri.JmriException ex) {
log.error(ex.toString());
}
try {
t.setStraightSpeed("Global");
if (elem.getChild("straightSpeed") != null) {
String speed = elem.getChild("straightSpeed").getText();
if (speed != null && !speed.equals("") && !speed.contains("Global")) {
t.setStraightSpeed(speed);
}
}
} catch (jmri.JmriException ex) {
log.error(ex.toString());
}
}
return result;
}
use of jmri.TurnoutManager in project JMRI by JMRI.
the class XNetTurnoutManagerTest method testAsAbstractFactory.
@Test
public void testAsAbstractFactory() {
// create and register the manager object
XNetTurnoutManager l = new XNetTurnoutManager(lnis, "X");
jmri.InstanceManager.setTurnoutManager(l);
// ask for a Turnout, and check type
TurnoutManager t = jmri.InstanceManager.turnoutManagerInstance();
Turnout o = t.newTurnout("XT21", "my name");
if (log.isDebugEnabled()) {
log.debug("received turnout value " + o);
}
Assert.assertTrue(null != (XNetTurnout) o);
// make sure loaded into tables
if (log.isDebugEnabled()) {
log.debug("by system name: " + t.getBySystemName("XT21"));
}
if (log.isDebugEnabled()) {
log.debug("by user name: " + t.getByUserName("my name"));
}
Assert.assertTrue(null != t.getBySystemName("XT21"));
Assert.assertTrue(null != t.getByUserName("my name"));
}
use of jmri.TurnoutManager in project JMRI by JMRI.
the class ZTC611XNetTurnoutManagerTest method testAsAbstractFactory.
@Test
public void testAsAbstractFactory() {
lnis = new XNetInterfaceScaffold(new LenzCommandStation());
// create and register the manager object
ZTC611XNetTurnoutManager l = new ZTC611XNetTurnoutManager(lnis, "X");
jmri.InstanceManager.setTurnoutManager(l);
// ask for a Turnout, and check type
TurnoutManager t = jmri.InstanceManager.turnoutManagerInstance();
Turnout o = t.newTurnout("XT21", "my name");
if (log.isDebugEnabled()) {
log.debug("received turnout value " + o);
}
Assert.assertTrue(null != (ZTC611XNetTurnout) o);
// make sure loaded into tables
if (log.isDebugEnabled()) {
log.debug("by system name: " + t.getBySystemName("XT21"));
}
if (log.isDebugEnabled()) {
log.debug("by user name: " + t.getByUserName("my name"));
}
Assert.assertTrue(null != t.getBySystemName("XT21"));
Assert.assertTrue(null != t.getByUserName("my name"));
}
use of jmri.TurnoutManager in project JMRI by JMRI.
the class JsonTurnoutSocketServiceTest method testOnMessageChange.
public void testOnMessageChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message;
JsonTurnoutSocketService service = new JsonTurnoutSocketService(connection);
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
Turnout turnout1 = manager.provideTurnout("IT1");
turnout1.setCommandedState(Turnout.UNKNOWN);
// Turnout CLOSED
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.CLOSED);
service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
Assert.assertEquals(Turnout.CLOSED, turnout1.getState());
// Turnout THROWN
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.THROWN);
service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
Assert.assertEquals(Turnout.THROWN, turnout1.getState());
// Turnout UNKNOWN - remains THROWN
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.UNKNOWN);
service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
Assert.assertEquals(Turnout.THROWN, turnout1.getState());
// Turnout Invalid State
// invalid state
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, 42);
JsonException exception = null;
try {
service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertEquals(Turnout.THROWN, turnout1.getState());
Assert.assertNotNull(exception);
Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
Aggregations