Search in sources :

Example 1 with Turnout

use of jmri.Turnout 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());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) TurnoutManager(jmri.TurnoutManager) Turnout(jmri.Turnout) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with Turnout

use of jmri.Turnout in project JMRI by JMRI.

the class XpaTurnoutManager method createNewTurnout.

// Xpa-specific methods
@Override
public Turnout createNewTurnout(String systemName, String userName) {
    int addr = Integer.parseInt(systemName.substring(prefix.length() + 1));
    Turnout t = new XpaTurnout(addr, memo);
    t.setUserName(userName);
    return t;
}
Also used : Turnout(jmri.Turnout)

Example 3 with Turnout

use of jmri.Turnout in project JMRI by JMRI.

the class Mx1TurnoutManager method createNewTurnout.

@Override
public Turnout createNewTurnout(String systemName, String userName) {
    int addr = Integer.valueOf(systemName.substring(getSystemPrefix().length() + 1)).intValue();
    Turnout t = new Mx1Turnout(addr, tc, getSystemPrefix());
    t.setUserName(userName);
    return t;
}
Also used : Turnout(jmri.Turnout)

Example 4 with Turnout

use of jmri.Turnout in project JMRI by JMRI.

the class SerialTurnoutManager method createNewTurnout.

@Override
public Turnout createNewTurnout(String systemName, String userName) {
    // validate the system name, and normalize it
    String sName = SerialAddress.normalizeSystemName(systemName);
    if (sName.equals("")) {
        // system name is not valid
        return null;
    }
    // does this turnout already exist
    Turnout t = getBySystemName(sName);
    if (t != null) {
        return null;
    }
    // check under alternate name
    String altName = SerialAddress.convertSystemNameToAlternate(sName);
    t = getBySystemName(altName);
    if (t != null) {
        return null;
    }
    // create the turnout
    int addr = Integer.valueOf(systemName.substring(2)).intValue();
    t = new SerialTurnout(addr);
    t.setUserName(userName);
    // does system name correspond to configured hardware
    if (!SerialAddress.validSystemNameConfig(sName, 'T')) {
        // system name does not correspond to configured hardware
        log.warn("Turnout '" + sName + "' refers to an undefined Serial Node.");
    }
    return t;
}
Also used : Turnout(jmri.Turnout)

Example 5 with Turnout

use of jmri.Turnout 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;
}
Also used : TurnoutOperation(jmri.TurnoutOperation) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) TurnoutManager(jmri.TurnoutManager) TurnoutOperationManagerXml(jmri.configurexml.TurnoutOperationManagerXml) Turnout(jmri.Turnout)

Aggregations

Turnout (jmri.Turnout)221 Test (org.junit.Test)63 Sensor (jmri.Sensor)26 SignalHead (jmri.SignalHead)20 Element (org.jdom2.Element)20 TurnoutManager (jmri.TurnoutManager)17 JmriException (jmri.JmriException)11 Light (jmri.Light)10 SignalMast (jmri.SignalMast)10 ActionEvent (java.awt.event.ActionEvent)9 NamedBeanHandle (jmri.NamedBeanHandle)9 JsonException (jmri.server.json.JsonException)9 ActionListener (java.awt.event.ActionListener)8 Block (jmri.Block)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 NamedBean (jmri.NamedBean)6 Route (jmri.Route)6 AbstractTurnout (jmri.implementation.AbstractTurnout)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5