Search in sources :

Example 21 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class SimpleServer method handleClient.

// Handle communication to a client through inStream and outStream
@Override
public void handleClient(DataInputStream inStream, DataOutputStream outStream) throws IOException {
    Scanner inputScanner = new Scanner(new InputStreamReader(inStream, "UTF-8"));
    // Listen for commands from the client until the connection closes
    String cmd;
    // interface components
    SimplePowerServer powerServer = new SimplePowerServer(inStream, outStream);
    SimpleTurnoutServer turnoutServer = new SimpleTurnoutServer(inStream, outStream);
    SimpleLightServer lightServer = new SimpleLightServer(inStream, outStream);
    SimpleSensorServer sensorServer = new SimpleSensorServer(inStream, outStream);
    SimpleSignalHeadServer signalHeadServer = new SimpleSignalHeadServer(inStream, outStream);
    SimpleReporterServer reporterServer = new SimpleReporterServer(inStream, outStream);
    SimpleOperationsServer operationsServer = new SimpleOperationsServer(inStream, outStream);
    // Start by sending a welcome message
    outStream.writeBytes("JMRI " + jmri.Version.name() + " \n");
    outStream.writeBytes("RAILROAD " + WebServerPreferences.getDefault().getRailRoadName() + " \n");
    outStream.writeBytes("NODE " + NodeIdentity.identity() + " \n");
    while (true) {
        // skip any stray end of line characters.
        inputScanner.skip("[\r\n]*");
        // Read the command from the client
        try {
            cmd = inputScanner.nextLine();
        } catch (NoSuchElementException nse) {
            // so break out of the loop.
            break;
        }
        if (log.isDebugEnabled()) {
            log.debug("Received from client: " + cmd);
        }
        if (cmd.startsWith("POWER")) {
            try {
                powerServer.parseStatus(cmd);
                powerServer.sendStatus(InstanceManager.getDefault(jmri.PowerManager.class).getPower());
            } catch (JmriException je) {
                outStream.writeBytes("not supported\n");
            }
        } else if (cmd.startsWith("TURNOUT")) {
            try {
                turnoutServer.parseStatus(cmd);
            } catch (JmriException je) {
                outStream.writeBytes("not supported\n");
            }
        } else if (cmd.startsWith("LIGHT")) {
            try {
                lightServer.parseStatus(cmd);
            } catch (JmriException je) {
                outStream.writeBytes("not supported\n");
            }
        } else if (cmd.startsWith("SENSOR")) {
            try {
                sensorServer.parseStatus(cmd);
            } catch (JmriException je) {
                outStream.writeBytes("not supported\n");
            }
        } else if (cmd.startsWith("SIGNALHEAD")) {
            try {
                signalHeadServer.parseStatus(cmd);
            } catch (JmriException je) {
                outStream.writeBytes("not supported\n");
            }
        } else if (cmd.startsWith("REPORTER")) {
            try {
                reporterServer.parseStatus(cmd);
            } catch (JmriException je) {
                outStream.writeBytes("not supported\n");
            }
        } else if (cmd.startsWith(SimpleOperationsServer.OPERATIONS)) {
            try {
                operationsServer.parseStatus(cmd);
            } catch (JmriException je) {
                outStream.writeBytes("not supported\n");
            }
        } else {
            outStream.writeBytes("Unknown Command " + cmd + "\n");
        }
    }
    inputScanner.close();
}
Also used : Scanner(java.util.Scanner) InputStreamReader(java.io.InputStreamReader) JmriException(jmri.JmriException) NoSuchElementException(java.util.NoSuchElementException)

Example 22 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class TransitCreationTool method createTransit.

public Transit createTransit() throws JmriException {
    TransitManager tm = InstanceManager.getDefault(jmri.TransitManager.class);
    String transitName = "From " + list.get(0).getDisplayName() + " to " + list.get(list.size() - 1).getDisplayName();
    Transit t = tm.createNewTransit(transitName);
    if (t == null) {
        log.error("Unable to create transit " + transitName);
        throw new JmriException(Bundle.getMessage("TCTErrorUnableToCreate", transitName));
    }
    if (list.get(0) instanceof SignalMast) {
        jmri.SignalMastLogicManager smlm = InstanceManager.getDefault(jmri.SignalMastLogicManager.class);
        for (int i = 1; i <= list.size() - 1; i++) {
            jmri.SignalMastLogic sml = smlm.getSignalMastLogic((SignalMast) list.get(i - 1));
            Section sec = sml.getAssociatedSection((SignalMast) list.get(i));
            //In theory sec being null would already have been tested when the signal was added.
            if (sec == null) {
                String error = Bundle.getMessage("TCTErrorMastPairsNoSection", list.get(i - 1).getDisplayName(), list.get(i).getDisplayName());
                log.error(error);
                tm.deregister(t);
                t.dispose();
                cancelTransitCreate();
                throw new JmriException(error);
            }
            t.addTransitSection(new jmri.TransitSection(sec, i, Section.FORWARD));
        }
    }
    //Once created clear the list for a fresh start.
    list = new ArrayList<NamedBean>();
    return t;
}
Also used : NamedBean(jmri.NamedBean) JmriException(jmri.JmriException) Transit(jmri.Transit) Section(jmri.Section) TransitManager(jmri.TransitManager) SignalMast(jmri.SignalMast)

Example 23 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class DefaultSignalMastLogicManager method discoverSignallingDest.

/**
     * Discover valid destination Signal Masts for a given source Signal Mast on a
     * given Layout Editor Panel.
     *
     * @param source Source SignalMast
     * @param layout Layout Editor panel to check.
     */
@Override
public void discoverSignallingDest(SignalMast source, LayoutEditor layout) throws JmriException {
    firePropertyChange("autoSignalMastGenerateStart", null, source.getDisplayName());
    Hashtable<NamedBean, List<NamedBean>> validPaths = new Hashtable<NamedBean, List<NamedBean>>();
    jmri.jmrit.display.layoutEditor.LayoutBlockManager lbm = InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class);
    if (!lbm.isAdvancedRoutingEnabled()) {
        //log.debug("advanced routing not enabled");
        throw new JmriException("advanced routing not enabled");
    }
    if (!lbm.routingStablised()) {
        throw new JmriException("routing not stabilised");
    }
    try {
        validPaths.put(source, lbm.getLayoutBlockConnectivityTools().discoverPairDest(source, layout, SignalMast.class, LayoutBlockConnectivityTools.MASTTOMAST));
    } catch (JmriException e) {
        throw e;
    }
    Enumeration<NamedBean> en = validPaths.keys();
    while (en.hasMoreElements()) {
        SignalMast key = (SignalMast) en.nextElement();
        SignalMastLogic sml = getSignalMastLogic(key);
        if (sml == null) {
            sml = newSignalMastLogic(key);
        }
        List<NamedBean> validDestMast = validPaths.get(key);
        for (int i = 0; i < validDestMast.size(); i++) {
            if (!sml.isDestinationValid((SignalMast) validDestMast.get(i))) {
                try {
                    sml.setDestinationMast((SignalMast) validDestMast.get(i));
                    sml.useLayoutEditorDetails(true, true, (SignalMast) validDestMast.get(i));
                    sml.useLayoutEditor(true, (SignalMast) validDestMast.get(i));
                } catch (JmriException e) {
                    //log.debug("We shouldn't get an exception here");
                    log.error("Exception found when adding pair " + source.getDisplayName() + " to destination " + validDestMast.get(i).getDisplayName() + "\n" + e.toString());
                //throw e;
                }
            }
        }
        if (sml.getDestinationList().size() == 1 && sml.getAutoTurnouts(sml.getDestinationList().get(0)).size() == 0) {
            key.setProperty("intermediateSignal", true);
        } else {
            key.removeProperty("intermediateSignal");
        }
    }
    initialise();
    firePropertyChange("autoSignalMastGenerateComplete", null, source.getDisplayName());
}
Also used : NamedBean(jmri.NamedBean) Hashtable(java.util.Hashtable) JmriException(jmri.JmriException) SignalMastLogic(jmri.SignalMastLogic) DefaultSignalMastLogic(jmri.implementation.DefaultSignalMastLogic) SignalMast(jmri.SignalMast) ArrayList(java.util.ArrayList) List(java.util.List)

Example 24 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class DefaultSignalMastLogicManager method automaticallyDiscoverSignallingPairs.

/**
     * Discover all possible valid source + destination signal mast pairs
     * on all Layout Editor Panels.
     */
@Override
public void automaticallyDiscoverSignallingPairs() throws JmriException {
    runWhenStablised = false;
    jmri.jmrit.display.layoutEditor.LayoutBlockManager lbm = InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class);
    if (!lbm.isAdvancedRoutingEnabled()) {
        throw new JmriException("advanced routing not enabled");
    }
    if (!lbm.routingStablised()) {
        runWhenStablised = true;
        return;
    }
    Hashtable<NamedBean, ArrayList<NamedBean>> validPaths = lbm.getLayoutBlockConnectivityTools().discoverValidBeanPairs(null, SignalMast.class, LayoutBlockConnectivityTools.MASTTOMAST);
    Enumeration<NamedBean> en = validPaths.keys();
    firePropertyChange("autoGenerateUpdate", null, ("Found " + validPaths.size() + " masts as sources for logic"));
    for (NamedBean nb : InstanceManager.getDefault(jmri.SignalMastManager.class).getNamedBeanList()) {
        nb.removeProperty("intermediateSignal");
    }
    while (en.hasMoreElements()) {
        SignalMast key = (SignalMast) en.nextElement();
        SignalMastLogic sml = getSignalMastLogic(key);
        if (sml == null) {
            sml = newSignalMastLogic(key);
        }
        ArrayList<NamedBean> validDestMast = validPaths.get(key);
        for (int i = 0; i < validDestMast.size(); i++) {
            if (!sml.isDestinationValid((SignalMast) validDestMast.get(i))) {
                try {
                    sml.setDestinationMast((SignalMast) validDestMast.get(i));
                    sml.useLayoutEditorDetails(true, true, (SignalMast) validDestMast.get(i));
                    sml.useLayoutEditor(true, (SignalMast) validDestMast.get(i));
                } catch (jmri.JmriException ex) {
                    //log.debug("we shouldn't get an exception here!");
                    log.debug(ex.getLocalizedMessage(), ex);
                }
            }
        }
        if (sml.getDestinationList().size() == 1 && sml.getAutoTurnouts(sml.getDestinationList().get(0)).size() == 0) {
            key.setProperty("intermediateSignal", true);
        }
    }
    initialise();
    firePropertyChange("autoGenerateComplete", null, null);
}
Also used : NamedBean(jmri.NamedBean) JmriException(jmri.JmriException) ArrayList(java.util.ArrayList) JmriException(jmri.JmriException) SignalMastLogic(jmri.SignalMastLogic) DefaultSignalMastLogic(jmri.implementation.DefaultSignalMastLogic) SignalMast(jmri.SignalMast)

Example 25 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class AbstractTurnoutManager method getNextValidAddress.

@Override
public String getNextValidAddress(String curAddress, String prefix) throws JmriException {
    //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) {
        jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class).showErrorMessage("Error", "Unable to convert " + curAddress + " to a valid Hardware Address", "" + ex, "", true, false);
        return null;
    }
    Turnout t = getBySystemName(tmpSName);
    if (t == null) {
        return curAddress;
    }
    // This bit deals with handling the curAddress, and how to get the next address.
    int iName = 0;
    try {
        iName = Integer.parseInt(curAddress);
    } catch (NumberFormatException 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;
    }
    //The Number of Output Bits of the previous turnout will help determine the next
    //valid address.
    iName = iName + t.getNumberOutputBits();
    //Check to determine if the systemName is in use, return null if it is,
    //otherwise return the next valid address.
    t = getBySystemName(prefix + typeLetter() + iName);
    if (t != null) {
        for (int x = 1; x < 10; x++) {
            iName = iName + t.getNumberOutputBits();
            t = getBySystemName(prefix + typeLetter() + iName);
            if (t == null) {
                return Integer.toString(iName);
            }
        }
        return null;
    } else {
        return Integer.toString(iName);
    }
}
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