Search in sources :

Example 26 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class AbstractSensorManager 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) {
        jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class).showErrorMessage("Error", "Unable to convert " + curAddress + " to a valid Hardware Address", "" + ex, "", true, false);
        return null;
    }
    Sensor s = getBySystemName(tmpSName);
    if (s == 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;
    }
    //Check to determine if the systemName is in use, return null if it is,
    //otherwise return the next valid address.
    s = getBySystemName(prefix + typeLetter() + iName);
    if (s != null) {
        for (int x = 1; x < 10; x++) {
            iName++;
            s = getBySystemName(prefix + typeLetter() + iName);
            if (s == null) {
                return Integer.toString(iName);
            }
        }
        return null;
    } else {
        return Integer.toString(iName);
    }
}
Also used : JmriException(jmri.JmriException) Sensor(jmri.Sensor)

Example 27 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class JsonRouteSocketServiceTest method testRouteChange.

public void testRouteChange() {
    // the route state on a sensorless route
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1");
        JsonRouteSocketService service = new JsonRouteSocketService(connection);
        RouteManager manager = InstanceManager.getDefault(RouteManager.class);
        Route route1 = manager.provideRoute("IR1", "Route1");
        Sensor sensor1 = InstanceManager.getDefault(SensorManager.class).provideSensor("IS1");
        sensor1.setKnownState(Sensor.UNKNOWN);
        route1.setTurnoutsAlignedSensor(sensor1.getSystemName());
        service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
        // TODO: test that service is listener in RouteManager
        Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        sensor1.setKnownState(Sensor.ACTIVE);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(JSON.ACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        sensor1.setKnownState(Sensor.INACTIVE);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.INACTIVE;
        }, "Route to deactivate");
        Assert.assertEquals(Sensor.INACTIVE, route1.getState());
        Assert.assertEquals(JSON.INACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        service.onClose();
    // TODO: test that service is no longer a listener in RouteManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SensorManager(jmri.SensorManager) JsonMockConnection(jmri.server.json.JsonMockConnection) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Route(jmri.Route) RouteManager(jmri.RouteManager) Sensor(jmri.Sensor)

Example 28 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class JsonSignalMastSocketServiceTest method testSignalMastChange.

@Test
@Ignore("Needs setup completed")
public void testSignalMastChange() {
    try {
        //create a signalmast for testing
        String sysName = "IF$shsm:basic:one-searchlight:SM2";
        String userName = "SM2";
        SignalMastManager manager = InstanceManager.getDefault(SignalMastManager.class);
        SignalMast s = manager.provideSignalMast(sysName);
        s.setUserName(userName);
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName);
        JsonSignalMastSocketService service = new JsonSignalMastSocketService(connection);
        service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
        // TODO: test that service is listener in SignalMastManager
        Assert.assertEquals(JSON.ASPECT_UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
        //change to Approach, and wait for change to show up
        s.setAspect("Approach");
        JUnitUtil.waitFor(() -> {
            return s.getAspect().equals("Approach");
        }, "SignalMast is now Approach");
        Assert.assertEquals("Approach", connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
        //change to Stop, and wait for change to show up
        s.setAspect("Stop");
        JUnitUtil.waitFor(() -> {
            return s.getAspect().equals("Stop");
        }, "SignalMast is now Stop");
        Assert.assertEquals("Stop", connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
        service.onClose();
    //            // TODO: test that service is no longer a listener in SignalMastManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) SignalMastManager(jmri.SignalMastManager) SignalMast(jmri.SignalMast) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 29 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class DestinationPoints method cancelClearInterlock.

void cancelClearInterlock(int cancelClear) {
    if ((cancelClear == EntryExitPairs.EXITROUTE) || (cancelClear == EntryExitPairs.STACKROUTE)) {
        src.pd.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
        point.setNXButtonState(EntryExitPairs.NXBUTTONINACTIVE);
        src.getPoint().getPanel().getGlassPane().setVisible(false);
        if (cancelClear == EntryExitPairs.STACKROUTE) {
            manager.stackNXRoute(this, false);
        }
        return;
    }
    if (cancelClear == EntryExitPairs.CANCELROUTE) {
        if (manager.getDispatcherIntegration() && jmri.InstanceManager.getNullableDefault(jmri.jmrit.dispatcher.DispatcherFrame.class) != null) {
            jmri.jmrit.dispatcher.DispatcherFrame df = jmri.InstanceManager.getDefault(jmri.jmrit.dispatcher.DispatcherFrame.class);
            ActiveTrain at = null;
            for (ActiveTrain atl : df.getActiveTrainsList()) {
                if (atl.getEndBlock() == point.getFacing().getBlock()) {
                    if (atl.getLastAllocatedSection() == atl.getEndBlockSection()) {
                        at = atl;
                        break;
                    }
                }
            }
            if (at != null) {
                jmri.Section sec = null;
                synchronized (this) {
                    if (sml != null && sml.getAssociatedSection((SignalMast) getSignal()) != null) {
                        sec = sml.getAssociatedSection((SignalMast) getSignal());
                    } else {
                        sec = InstanceManager.getDefault(jmri.SectionManager.class).getSection(src.getPoint().getDisplayName() + ":" + point.getDisplayName());
                    }
                }
                if (sec != null) {
                    if (!df.removeFromActiveTrainPath(sec, at, src.getPoint().getPanel())) {
                        log.error("Unable to remove allocation from dispathcer, leave interlock in place");
                        src.pd.cancelNXButtonTimeOut();
                        point.cancelNXButtonTimeOut();
                        src.getPoint().getPanel().getGlassPane().setVisible(false);
                        return;
                    }
                    if (sec.getSectionType() == jmri.Section.DYNAMICADHOC) {
                        sec.removeAllBlocksFromSection();
                    }
                }
            }
        }
    }
    src.setMenuEnabled(false);
    if (src.sourceSignal instanceof SignalMast) {
        SignalMast mast = (SignalMast) src.sourceSignal;
        mast.setAspect(mast.getAppearanceMap().getSpecificAppearance(jmri.SignalAppearanceMap.DANGER));
        mast.setHeld(true);
    } else if (src.sourceSignal instanceof SignalHead) {
        SignalHead head = (SignalHead) src.sourceSignal;
        head.setHeld(true);
    } else {
        log.debug("No signal found");
    }
    //Get rid of the signal mast logic to the destination mast.
    synchronized (this) {
        if ((getSignal() instanceof SignalMast) && (sml != null)) {
            SignalMast mast = (SignalMast) getSignal();
            if (sml.getStoreState(mast) == jmri.SignalMastLogic.STORENONE) {
                sml.removeDestination(mast);
            }
        }
        sml = null;
    }
    if (routeDetails == null) {
        return;
    }
    for (LayoutBlock blk : routeDetails) {
        if ((getEntryExitType() == EntryExitPairs.FULLINTERLOCK)) {
            blk.setUseExtraColor(false);
        }
        // was set against occupancy sensor
        blk.getBlock().removePropertyChangeListener(propertyBlockListener);
    }
    if (cancelClear == EntryExitPairs.CLEARROUTE) {
        if (routeDetails.size() == 0) {
            if (log.isDebugEnabled()) {
                log.debug(mUserName + "  all blocks have automatically been cleared down");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug(mUserName + "  No blocks were cleared down " + routeDetails.size());
            }
            try {
                if (log.isDebugEnabled()) {
                    log.debug(mUserName + "  set first block as active so that we can manually clear this down " + routeDetails.get(0).getBlock().getUserName());
                }
                if (routeDetails.get(0).getOccupancySensor() != null) {
                    routeDetails.get(0).getOccupancySensor().setState(Sensor.ACTIVE);
                } else {
                    routeDetails.get(0).getBlock().goingActive();
                }
                if (src.getStart().getOccupancySensor() != null) {
                    src.getStart().getOccupancySensor().setState(Sensor.INACTIVE);
                } else {
                    src.getStart().getBlock().goingInactive();
                }
            } catch (java.lang.NullPointerException e) {
                log.error("error in clear route A " + e);
            } catch (JmriException e) {
                log.error("error in clear route A " + e);
            }
            if (log.isDebugEnabled()) {
                log.debug(mUserName + "  Going to clear routeDetails down " + routeDetails.size());
                for (int i = 0; i < routeDetails.size(); i++) {
                    log.debug("Block at " + i + " " + routeDetails.get(i).getDisplayName());
                }
            }
            if (routeDetails.size() > 1) {
                //Should we just be usrc.pdating the block status and not the sensor
                for (int i = 1; i < routeDetails.size() - 1; i++) {
                    if (log.isDebugEnabled()) {
                        log.debug(mUserName + " in loop Set active " + routeDetails.get(i).getDisplayName() + " " + routeDetails.get(i).getBlock().getSystemName());
                    }
                    try {
                        if (routeDetails.get(i).getOccupancySensor() != null) {
                            routeDetails.get(i).getOccupancySensor().setState(Sensor.ACTIVE);
                        } else {
                            routeDetails.get(i).getBlock().goingActive();
                        }
                        if (log.isDebugEnabled()) {
                            log.debug(mUserName + " in loop Set inactive " + routeDetails.get(i - 1).getDisplayName() + " " + routeDetails.get(i - 1).getBlock().getSystemName());
                        }
                        if (routeDetails.get(i - 1).getOccupancySensor() != null) {
                            routeDetails.get(i - 1).getOccupancySensor().setState(Sensor.INACTIVE);
                        } else {
                            routeDetails.get(i - 1).getBlock().goingInactive();
                        }
                    } catch (java.lang.NullPointerException e) {
                        log.error("error in clear route b " + e);
                        e.printStackTrace();
                    } catch (JmriException e) {
                        log.error("error in clear route b " + e);
                    }
                }
                try {
                    if (log.isDebugEnabled()) {
                        log.debug(mUserName + " out of loop Set active " + routeDetails.get(routeDetails.size() - 1).getDisplayName() + " " + routeDetails.get(routeDetails.size() - 1).getBlock().getSystemName());
                    }
                    //Get the last block an set it active.
                    if (routeDetails.get(routeDetails.size() - 1).getOccupancySensor() != null) {
                        routeDetails.get(routeDetails.size() - 1).getOccupancySensor().setState(Sensor.ACTIVE);
                    } else {
                        routeDetails.get(routeDetails.size() - 1).getBlock().goingActive();
                    }
                    if (log.isDebugEnabled()) {
                        log.debug(mUserName + " out of loop Set inactive " + routeDetails.get(routeDetails.size() - 2).getUserName() + " " + routeDetails.get(routeDetails.size() - 2).getBlock().getSystemName());
                    }
                    if (routeDetails.get(routeDetails.size() - 2).getOccupancySensor() != null) {
                        routeDetails.get(routeDetails.size() - 2).getOccupancySensor().setState(Sensor.INACTIVE);
                    } else {
                        routeDetails.get(routeDetails.size() - 2).getBlock().goingInactive();
                    }
                } catch (java.lang.NullPointerException e) {
                    log.error("error in clear route c " + e);
                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    log.error("error in clear route c " + e);
                } catch (JmriException e) {
                    log.error("error in clear route c " + e);
                }
            }
        }
    }
    setActiveEntryExit(false);
    setRouteFrom(false);
    setRouteTo(false);
    routeDetails = null;
    synchronized (this) {
        lastSeenActiveBlockObject = null;
    }
    src.pd.cancelNXButtonTimeOut();
    point.cancelNXButtonTimeOut();
    src.getPoint().getPanel().getGlassPane().setVisible(false);
}
Also used : ActiveTrain(jmri.jmrit.dispatcher.ActiveTrain) SignalHead(jmri.SignalHead) JmriException(jmri.JmriException) LayoutBlock(jmri.jmrit.display.layoutEditor.LayoutBlock) SignalMast(jmri.SignalMast)

Example 30 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class PowerManagerButton method setPowerIcons.

protected void setPowerIcons() {
    PowerManager powerMgr = InstanceManager.getNullableDefault(PowerManager.class);
    if (powerMgr == null) {
        return;
    }
    try {
        switch(powerMgr.getPower()) {
            case PowerManager.ON:
                setIcon(getPowerOnIcon());
                setToolTipText(Bundle.getMessage("LayoutPowerOn"));
                if (getFullText()) {
                    setText(Bundle.getMessage("PowerStateOn"));
                }
                break;
            case PowerManager.OFF:
                setIcon(getPowerOffIcon());
                setToolTipText(Bundle.getMessage("LayoutPowerOff"));
                if (getFullText()) {
                    setText(Bundle.getMessage("PowerStateOff"));
                }
                break;
            case PowerManager.UNKNOWN:
                setIcon(getPowerUnknownIcon());
                setToolTipText(Bundle.getMessage("LayoutPowerUnknown"));
                if (getFullText()) {
                    setText(Bundle.getMessage("PowerStateUnknown"));
                }
                break;
            default:
                setIcon(getPowerUnknownIcon());
                setToolTipText(Bundle.getMessage("LayoutPowerUnknown"));
                log.error("Unexpected state value: {}", powerMgr.getPower());
                if (getFullText()) {
                    setText(Bundle.getMessage("PowerStateUnknown"));
                }
                break;
        }
    } catch (JmriException ex) {
        setIcon(getPowerUnknownIcon());
        setToolTipText(Bundle.getMessage("LayoutPowerUnknown"));
        if (getFullText()) {
            setText(Bundle.getMessage("PowerStateUnknown"));
        }
    }
}
Also used : PowerManager(jmri.PowerManager) JmriException(jmri.JmriException)

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