Search in sources :

Example 11 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class JsonUtil method setSensor.

@Deprecated
public static void setSensor(Locale locale, String name, JsonNode data) throws JsonException {
    try {
        Sensor sensor = InstanceManager.sensorManagerInstance().getSensor(name);
        if (data.path(USERNAME).isTextual()) {
            sensor.setUserName(data.path(USERNAME).asText());
        }
        if (data.path(INVERTED).isBoolean()) {
            sensor.setInverted(data.path(INVERTED).asBoolean());
        }
        if (data.path(COMMENT).isTextual()) {
            sensor.setComment(data.path(COMMENT).asText());
        }
        int state = data.path(STATE).asInt(UNKNOWN);
        switch(state) {
            case Sensor.ACTIVE:
                sensor.setKnownState(Sensor.ACTIVE);
                break;
            case INACTIVE:
                sensor.setKnownState(Sensor.INACTIVE);
                break;
            case UNKNOWN:
                // silently ignore
                break;
            default:
                throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", SENSOR, state));
        }
    } catch (NullPointerException e) {
        log.error("Unable to get sensor [{}].", name);
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", SENSOR, name));
    } catch (JmriException ex) {
        throw new JsonException(500, ex);
    }
}
Also used : JsonException(jmri.server.json.JsonException) JmriException(jmri.JmriException) Sensor(jmri.Sensor)

Example 12 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class LayoutBlockConnectivityTools method discoverPairDest.

ArrayList<NamedBean> discoverPairDest(NamedBean source, LayoutBlock lProtecting, LayoutBlock lFacing, ArrayList<FacingProtecting> blockList, int pathMethod) throws JmriException {
    LayoutBlockManager lbm = InstanceManager.getDefault(LayoutBlockManager.class);
    if (!lbm.isAdvancedRoutingEnabled()) {
        throw new JmriException("advanced routing not enabled");
    }
    if (!lbm.routingStablised()) {
        throw new JmriException("routing not stabilised");
    }
    ArrayList<NamedBean> validDestBean = new ArrayList<NamedBean>();
    for (int j = 0; j < blockList.size(); j++) {
        if (blockList.get(j).getBean() != source) {
            NamedBean destObj = blockList.get(j).getBean();
            if (log.isDebugEnabled()) {
                log.debug("looking for pair " + source.getDisplayName() + " " + destObj.getDisplayName());
            }
            try {
                if (checkValidDest(lFacing, lProtecting, blockList.get(j), pathMethod)) {
                    if (log.isDebugEnabled()) {
                        log.debug("Valid pair " + source.getDisplayName() + " " + destObj.getDisplayName());
                    }
                    LayoutBlock ldstBlock = lbm.getLayoutBlock(blockList.get(j).getFacing());
                    try {
                        ArrayList<LayoutBlock> lblks = getLayoutBlocks(lFacing, ldstBlock, lProtecting, true, pathMethod);
                        if (log.isDebugEnabled()) {
                            log.debug("Adding block " + destObj.getDisplayName() + " to paths, current size " + lblks.size());
                        }
                        validDestBean.add(destObj);
                    } catch (jmri.JmriException e) {
                        // Considered normal if route not found.
                        log.debug("not a valid route through " + source.getDisplayName() + " - " + destObj.getDisplayName());
                    }
                }
            } catch (jmri.JmriException ex) {
                log.debug(ex.toString());
            }
        }
    }
    return validDestBean;
}
Also used : NamedBean(jmri.NamedBean) JmriException(jmri.JmriException) ArrayList(java.util.ArrayList) JmriException(jmri.JmriException)

Example 13 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class SerialTurnoutManager method getNextValidAddress.

/**
     * A method that returns the next valid free turnout hardware address
     */
@Override
public String getNextValidAddress(String curAddress, String prefix) throws JmriException {
    String tmpSName = "";
    try {
        tmpSName = createSystemName(curAddress, prefix);
    } catch (JmriException ex) {
        throw ex;
    }
    //If the hardware address part does not already exist then this can
    //be considered the next valid address.
    Turnout t = getBySystemName(tmpSName);
    if (t == null) {
        /*We look for the last instance of T, as the hardware address side 
             of the system name should not contain the letter, however parts of the prefix might */
        int seperator = tmpSName.lastIndexOf("T") + 1;
        curAddress = tmpSName.substring(seperator);
        return curAddress;
    }
    //The Number of Output Bits of the previous turnout will help determine the next
    //valid address.
    bitNum = bitNum + t.getNumberOutputBits();
    //Check to determine if the systemName is in use, return null if it is,
    //otherwise return the next valid address.
    tmpSName = _memo.makeSystemName("T", nAddress, bitNum);
    t = getBySystemName(tmpSName);
    if (t != null) {
        for (int x = 1; x < 10; x++) {
            bitNum = bitNum + t.getNumberOutputBits();
            //System.out.println("This should increment " + bitNum);
            tmpSName = _memo.makeSystemName("T", nAddress, bitNum);
            t = getBySystemName(tmpSName);
            if (t == null) {
                int seperator = tmpSName.lastIndexOf("T") + 1;
                curAddress = tmpSName.substring(seperator);
                return curAddress;
            }
        }
        return null;
    } else {
        int seperator = tmpSName.lastIndexOf("T") + 1;
        curAddress = tmpSName.substring(seperator);
        return curAddress;
    }
}
Also used : JmriException(jmri.JmriException) Turnout(jmri.Turnout)

Example 14 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class SerialSensorManager method getNextValidAddress.

@Override
public String getNextValidAddress(String curAddress, String prefix) {
    //If the hardware address past does not already exist then this can
    //be considered the next valid address.
    String tmpSName = "";
    try {
        tmpSName = createSystemName(curAddress, prefix);
    } catch (JmriException ex) {
        log.error("Unable to convert " + curAddress + " Hardware Address to a number");
        jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class).showErrorMessage("Error", "Unable to convert " + curAddress + " to a valid Hardware Address", "" + ex, "", true, false);
        return null;
    }
    //Check to determine if the systemName is in use, return null if it is,
    //otherwise return the next valid address.
    Sensor s = getBySystemName(tmpSName);
    if (s != null) {
        for (int x = 1; x < 10; x++) {
            bitNum++;
            tmpSName = _memo.makeSystemName("S", nAddress, bitNum);
            s = getBySystemName(tmpSName);
            if (s == null) {
                int seperator = tmpSName.lastIndexOf("S") + 1;
                curAddress = tmpSName.substring(seperator);
                return curAddress;
            }
        }
        return null;
    } else {
        int seperator = tmpSName.lastIndexOf("S") + 1;
        curAddress = tmpSName.substring(seperator);
        return curAddress;
    }
}
Also used : JmriException(jmri.JmriException) Sensor(jmri.Sensor)

Example 15 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class SerialTurnoutManager method getNextValidAddress.

/**
     * A method that returns the next valid free turnout hardware address
     */
@Override
public String getNextValidAddress(String curAddress, String prefix) throws JmriException {
    String tmpSName = "";
    try {
        tmpSName = createSystemName(curAddress, prefix);
    } catch (JmriException ex) {
        throw ex;
    }
    //If the hardware address past does not already exist then this can
    //be considered the next valid address.
    Turnout t = getBySystemName(tmpSName);
    if (t == null) {
        return Integer.toString(nNode) + Integer.toString((nCard + bitNum));
    //return ""+nNode+(nCard+bitNum);
    }
    //The Number of Output Bits of the previous turnout will help determine the next
    //valid address.
    bitNum = bitNum + t.getNumberOutputBits();
    //Check to determine if the systemName is in use, return null if it is,
    //otherwise return the next valid address.
    tmpSName = prefix + "T" + nNode + (nCard + bitNum);
    t = getBySystemName(tmpSName);
    if (t != null) {
        for (int x = 1; x < 10; x++) {
            bitNum = bitNum + t.getNumberOutputBits();
            tmpSName = prefix + "T" + nNode + (nCard + bitNum);
            t = getBySystemName(tmpSName);
            if (t == null) {
                return Integer.toString(nNode) + Integer.toString((nCard + bitNum));
            }
        //return ""+nNode+(nCard+bitNum);
        }
        return null;
    } else {
        return Integer.toString(nNode) + Integer.toString((nCard + bitNum));
    }
}
Also used : JmriException(jmri.JmriException) Turnout(jmri.Turnout)

Aggregations

JmriException (jmri.JmriException)80 Sensor (jmri.Sensor)22 JsonException (jmri.server.json.JsonException)22 IOException (java.io.IOException)20 JsonNode (com.fasterxml.jackson.databind.JsonNode)19 JsonMockConnection (jmri.server.json.JsonMockConnection)18 ArrayList (java.util.ArrayList)10 NamedBean (jmri.NamedBean)10 Turnout (jmri.Turnout)10 Test (org.junit.Test)7 SignalMast (jmri.SignalMast)6 ConfigureManager (jmri.ConfigureManager)5 SensorManager (jmri.SensorManager)5 File (java.io.File)4 PowerManager (jmri.PowerManager)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Light (jmri.Light)3 SignalHead (jmri.SignalHead)3 FileNotFoundException (java.io.FileNotFoundException)2 Hashtable (java.util.Hashtable)2