Search in sources :

Example 6 with RouteManager

use of jmri.RouteManager in project JMRI by JMRI.

the class SensorGroupFrame method deleteGroup.

void deleteGroup(boolean showMsg) {
    String group = _nameField.getText();
    if (group == null || group.equals("")) {
        if (showMsg) {
            javax.swing.JOptionPane.showMessageDialog(this, "'View' the group or enter the group name in the 'Group Name' field before selecting 'Undo Group'", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
        }
        return;
    }
    String prefix = (namePrefix + group + nameDivider).toUpperCase();
    // remove the old routes
    RouteManager rm = InstanceManager.getDefault(jmri.RouteManager.class);
    List<String> l = rm.getSystemNameList();
    for (int i = 0; i < l.size(); i++) {
        String name = l.get(i);
        if (name.startsWith(prefix)) {
            // OK, kill this one
            Route r = rm.getBySystemName(l.get(i));
            r.deActivateRoute();
            rm.deleteRoute(r);
        }
    }
    String cSystemName = (ConditionalSystemPrefix + group).toUpperCase();
    String cUserName = ConditionalUserPrefix + group;
    Logix logix = getSystemLogix();
    for (int i = 0; i < logix.getNumConditionals(); i++) {
        String name = logix.getConditionalByNumberOrder(i);
        if (cSystemName.equals(name) || cUserName.equals(name)) {
            Conditional c = InstanceManager.getDefault(jmri.ConditionalManager.class).getBySystemName(name);
            if (c == null) {
                log.error("Conditional \"" + name + "\" expected but NOT found in Logix " + logix.getSystemName());
            } else {
                logix.deleteConditional(cSystemName);
                break;
            }
        }
    }
    DefaultListModel<String> model = (DefaultListModel<String>) _sensorGroupList.getModel();
    int index = model.indexOf(group);
    if (index > -1) {
        model.remove(index);
    }
    index = _sensorGroupList.getSelectedIndex();
    if (index > -1) {
        String sysName = ConditionalSystemPrefix + model.elementAt(index);
        String[] msgs = logix.deleteConditional(sysName);
        if (msgs != null) {
            if (showMsg) {
                javax.swing.JOptionPane.showMessageDialog(this, "Conditional " + msgs[0] + " (" + msgs[1] + ") is a Conditional Variable in the Conditional,\n" + msgs[2] + " (" + msgs[3] + "), of Logix, " + msgs[4] + " (" + msgs[5] + ").\nPlease remove that variable first.", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
            }
        } else {
            model.remove(index);
        }
    }
}
Also used : Logix(jmri.Logix) DefaultListModel(javax.swing.DefaultListModel) Conditional(jmri.Conditional) SensorGroupConditional(jmri.implementation.SensorGroupConditional) Route(jmri.Route) RouteManager(jmri.RouteManager)

Example 7 with RouteManager

use of jmri.RouteManager in project JMRI by JMRI.

the class SensorGroup method addPressed.

void addPressed() {
    log.debug("start with " + sensorList.size() + " lines");
    RouteManager rm = InstanceManager.getDefault(jmri.RouteManager.class);
    String group = name.toUpperCase();
    // remove the old routes
    List<String> l = rm.getSystemNameList();
    String prefix = (namePrefix + group + nameDivider).toUpperCase();
    for (int i = 0; i < l.size(); i++) {
        String routeName = l.get(i);
        if (routeName.startsWith(prefix)) {
            // OK, kill this one
            Route r = rm.getBySystemName(l.get(i));
            r.deActivateRoute();
            rm.deleteRoute(r);
        }
    }
    // add the new routes
    for (int i = 0; i < sensorList.size(); i++) {
        String sensor = sensorList.get(i);
        String routeName = namePrefix + group + nameDivider + sensor;
        Route r = new DefaultRoute(routeName);
        // add the control sensor
        r.addSensorToRoute(sensor, Route.ONACTIVE);
        // add the output sensors
        for (int j = 0; j < sensorList.size(); j++) {
            String outSensor = sensorList.get(j);
            int mode = Sensor.INACTIVE;
            if (i == j) {
                mode = Sensor.ACTIVE;
            }
            r.addOutputSensor(outSensor, mode);
        }
        // make it persistant & activate
        r.activateRoute();
        rm.register(r);
    }
}
Also used : DefaultRoute(jmri.implementation.DefaultRoute) Route(jmri.Route) DefaultRoute(jmri.implementation.DefaultRoute) RouteManager(jmri.RouteManager)

Example 8 with RouteManager

use of jmri.RouteManager in project JMRI by JMRI.

the class JsonRouteHttpServiceTest method testDoPostWithRouteSensor.

public void testDoPostWithRouteSensor() throws JmriException {
    log.debug("testDoPostWithRouteSensor");
    ObjectMapper mapper = new ObjectMapper();
    JsonRouteHttpService service = new JsonRouteHttpService(mapper);
    RouteManager manager = InstanceManager.getDefault(RouteManager.class);
    Route route1 = manager.provideRoute("IR1", "Route1");
    Sensor sensor1 = InstanceManager.getDefault(SensorManager.class).provideSensor("IS1");
    Turnout turnout1 = InstanceManager.getDefault(TurnoutManager.class).provideTurnout("IT1");
    turnout1.setCommandedState(Turnout.CLOSED);
    route1.setTurnoutsAlignedSensor(sensor1.getSystemName());
    Assert.assertTrue(route1.addOutputTurnout(turnout1.getSystemName(), Turnout.THROWN));
    route1.activateRoute();
    Assert.assertNotNull(route1.getTurnoutsAlgdSensor());
    JsonNode result;
    JsonNode message;
    try {
        // set ACTIVE
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.ACTIVE);
        Assert.assertEquals(Sensor.INACTIVE, route1.getState());
        result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        Assert.assertNotNull(result);
        // set INACTIVE - remains ACTIVE
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.INACTIVE);
        result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        // not testing results content because result *may* be set before route state changes
        // so its not predictable (this is not unanticipated in the design)
        Assert.assertNotNull(result);
        // set UNKNOWN - remains ACTIVE
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.UNKNOWN);
        result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        Assert.assertNotNull(result);
        // reset to INACTIVE
        turnout1.setCommandedState(Turnout.CLOSED);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.INACTIVE;
        }, "Route to activate");
        Assert.assertEquals(Sensor.INACTIVE, route1.getState());
        // set TOGGLE - becomes ACTIVE
        log.debug("Toggling route in testDoPostWithRouteSensor");
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.TOGGLE);
        result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        Assert.assertNotNull(result);
        // set TOGGLE - remains ACTIVE
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.TOGGLE);
        result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        Assert.assertNotNull(result);
        // set invalid state
        // Invalid value
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, 42);
        JsonException exception = null;
        try {
            service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals(Sensor.ACTIVE, route1.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) SensorManager(jmri.SensorManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) Turnout(jmri.Turnout) TurnoutManager(jmri.TurnoutManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Route(jmri.Route) RouteManager(jmri.RouteManager) Sensor(jmri.Sensor)

Example 9 with RouteManager

use of jmri.RouteManager in project JMRI by JMRI.

the class JsonRouteSocketServiceTest method testOnMessageChange.

public void testOnMessageChange() {
    // the route state on a sensorless route
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message;
        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");
        Turnout turnout1 = InstanceManager.getDefault(TurnoutManager.class).provideTurnout("IT1");
        turnout1.setCommandedState(Turnout.CLOSED);
        route1.setTurnoutsAlignedSensor(sensor1.getSystemName());
        Assert.assertTrue(route1.addOutputTurnout(turnout1.getSystemName(), Turnout.THROWN));
        route1.activateRoute();
        Assert.assertNotNull(route1.getTurnoutsAlgdSensor());
        // Route ACTIVE - becomes ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.ACTIVE);
        service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        // Route INACTIVE - remains ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.INACTIVE);
        service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        // Route UNKNOWN - remains ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.UNKNOWN);
        service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        // Route TOGGLE - remains ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.TOGGLE);
        service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        // Route TOGGLE - becomes ACTIVE
        sensor1.setKnownState(Sensor.INACTIVE);
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.TOGGLE);
        service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
        JUnitUtil.waitFor(() -> {
            return route1.getState() == Sensor.ACTIVE;
        }, "Route to activate");
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        // Route Invalid State
        // invalid state
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, 42);
        JsonException exception = null;
        try {
            service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals(Sensor.ACTIVE, route1.getState());
        Assert.assertNotNull(exception);
        Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) TurnoutManager(jmri.TurnoutManager) SensorManager(jmri.SensorManager) JsonMockConnection(jmri.server.json.JsonMockConnection) Turnout(jmri.Turnout) Route(jmri.Route) RouteManager(jmri.RouteManager) Sensor(jmri.Sensor)

Example 10 with RouteManager

use of jmri.RouteManager in project JMRI by JMRI.

the class JsonRouteHttpServiceTest method testDoGetWithoutRouteSensor.

public void testDoGetWithoutRouteSensor() throws JmriException {
    JsonRouteHttpService service = new JsonRouteHttpService(new ObjectMapper());
    RouteManager manager = InstanceManager.getDefault(RouteManager.class);
    Route route1 = manager.provideRoute("IR1", "Route1");
    JsonNode result;
    try {
        result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals("IR1", result.path(JSON.DATA).path(JSON.NAME).asText());
        Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
        route1.setState(Sensor.ACTIVE);
        result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
        route1.setState(Sensor.INACTIVE);
        result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Route(jmri.Route) RouteManager(jmri.RouteManager)

Aggregations

RouteManager (jmri.RouteManager)16 Route (jmri.Route)11 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 JsonException (jmri.server.json.JsonException)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 Sensor (jmri.Sensor)4 SensorManager (jmri.SensorManager)4 Conditional (jmri.Conditional)3 Logix (jmri.Logix)3 Turnout (jmri.Turnout)3 SensorGroupConditional (jmri.implementation.SensorGroupConditional)3 DefaultRouteManager (jmri.managers.DefaultRouteManager)3 IOException (java.io.IOException)2 DefaultListModel (javax.swing.DefaultListModel)2 JmriException (jmri.JmriException)2 TurnoutManager (jmri.TurnoutManager)2 DefaultRoute (jmri.implementation.DefaultRoute)2 JsonMockConnection (jmri.server.json.JsonMockConnection)2 Element (org.jdom2.Element)2 ActionEvent (java.awt.event.ActionEvent)1