Search in sources :

Example 6 with SensorManager

use of jmri.SensorManager in project JMRI by JMRI.

the class JsonUtil method getRoute.

@Deprecated
public static JsonNode getRoute(Locale locale, String name) throws JsonException {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, ROUTE);
    ObjectNode data = root.putObject(DATA);
    try {
        Route route = InstanceManager.getDefault(jmri.RouteManager.class).getRoute(name);
        SensorManager s = InstanceManager.sensorManagerInstance();
        data.put(NAME, route.getSystemName());
        data.put(USERNAME, route.getUserName());
        data.put(COMMENT, route.getComment());
        Sensor sensor = s.getSensor(route.getTurnoutsAlignedSensor());
        if (sensor != null) {
            switch(sensor.getKnownState()) {
                case Sensor.ACTIVE:
                    data.put(STATE, ACTIVE);
                    break;
                case Sensor.INACTIVE:
                    data.put(STATE, INACTIVE);
                    break;
                case Sensor.INCONSISTENT:
                    data.put(STATE, INCONSISTENT);
                    break;
                case Sensor.UNKNOWN:
                default:
                    data.put(STATE, UNKNOWN);
                    break;
            }
        } else {
            data.put(STATE, UNKNOWN);
        }
    } catch (NullPointerException e) {
        log.error("Unable to get route [{}].", name);
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", ROUTE, name));
    }
    return root;
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SensorManager(jmri.SensorManager) Route(jmri.Route) Sensor(jmri.Sensor)

Example 7 with SensorManager

use of jmri.SensorManager in project JMRI by JMRI.

the class OBlockTest method testSetErrorSensor.

@Test
public void testSetErrorSensor() throws Exception {
    OBlock b = blkMgr.createNewOBlock("OB101", "b");
    Assert.assertFalse("setErrorSensor foo", b.setErrorSensor("foo"));
    Assert.assertNull("getErrorSensor foo", b.getErrorSensor());
    SensorManager sensorMgr = InstanceManager.getDefault(SensorManager.class);
    Sensor se = sensorMgr.newSensor("ISE1", "error1");
    se.setState(Sensor.ACTIVE);
    Assert.assertTrue("setErrorSensor only", b.setErrorSensor("error1"));
    Assert.assertEquals("getErrorSensor only", se, b.getErrorSensor());
    Assert.assertEquals("state error only", OBlock.TRACK_ERROR | OBlock.UNDETECTED, b.getState());
    Sensor s1 = sensorMgr.newSensor("IS1", "sensor1");
    s1.setState(Sensor.ACTIVE);
    Assert.assertTrue("setSensor", b.setSensor("IS1"));
    Assert.assertEquals("getErrorSensor se", se, b.getErrorSensor());
    Assert.assertEquals("state error", OBlock.TRACK_ERROR | OBlock.OCCUPIED, b.getState());
    Assert.assertTrue("setErrorSensor", b.setErrorSensor("  "));
    Assert.assertNull("getErrorSensor none", b.getErrorSensor());
    Assert.assertEquals("state dark", OBlock.OCCUPIED, b.getState());
}
Also used : SensorManager(jmri.SensorManager) Sensor(jmri.Sensor) Test(org.junit.Test)

Example 8 with SensorManager

use of jmri.SensorManager in project JMRI by JMRI.

the class OBlockTest method testSetSensor.

@Test
public void testSetSensor() throws Exception {
    OBlock b = blkMgr.createNewOBlock("OB100", "a");
    Assert.assertFalse("setSensor", b.setSensor("foo"));
    Assert.assertNull("getSensor", b.getSensor());
    SensorManager sensorMgr = InstanceManager.getDefault(SensorManager.class);
    Sensor s1 = sensorMgr.newSensor("IS1", "sensor1");
    Assert.assertTrue("setSensor", b.setSensor("sensor1"));
    Assert.assertEquals("getSensor", s1, b.getSensor());
    Assert.assertEquals("state unknown", OBlock.UNKNOWN, b.getState());
    Assert.assertTrue("dup setSensor", b.setSensor("IS1"));
    Assert.assertEquals("dup getSensor s1", s1, b.getSensor());
    Assert.assertEquals("dup state unknown", OBlock.UNKNOWN, b.getState());
    Assert.assertTrue("setSensor none", b.setSensor("  "));
    Assert.assertNull("getSensor none", b.getSensor());
    Assert.assertEquals("none state dark", OBlock.UNDETECTED, b.getState());
    Assert.assertTrue("Not Free", b.isFree());
    b.setState(b.getState() | OBlock.ALLOCATED | OBlock.RUNNING);
    Assert.assertFalse("Is Free", b.isFree());
    s1.setState(Sensor.ACTIVE);
    Assert.assertTrue("setSensor sensor1", b.setSensor("sensor1"));
    Assert.assertEquals("state allocated&running", OBlock.OCCUPIED | OBlock.ALLOCATED | OBlock.RUNNING, b.getState());
}
Also used : SensorManager(jmri.SensorManager) Sensor(jmri.Sensor) Test(org.junit.Test)

Example 9 with SensorManager

use of jmri.SensorManager in project JMRI by JMRI.

the class InternalSensorManagerTest method testAsAbstractFactory.

@Test
public void testAsAbstractFactory() {
    // ask for a Sensor, and check type
    SensorManager lm = jmri.InstanceManager.sensorManagerInstance();
    Sensor tl = lm.newSensor("IS21", "my name");
    if (log.isDebugEnabled()) {
        log.debug("received sensor value " + tl);
    }
    Assert.assertTrue(null != tl);
    // make sure loaded into tables
    if (log.isDebugEnabled()) {
        log.debug("by system name: " + lm.getBySystemName("IS21"));
    }
    if (log.isDebugEnabled()) {
        log.debug("by user name:   " + lm.getByUserName("my name"));
    }
    Assert.assertTrue(null != lm.getBySystemName("IS21"));
    Assert.assertTrue(null != lm.getByUserName("my name"));
}
Also used : SensorManager(jmri.SensorManager) Sensor(jmri.Sensor) Test(org.junit.Test)

Example 10 with SensorManager

use of jmri.SensorManager in project JMRI by JMRI.

the class JsonSensorHttpServiceTest method testDoPost.

public void testDoPost() throws JmriException {
    ObjectMapper mapper = new ObjectMapper();
    JsonSensorHttpService service = new JsonSensorHttpService(mapper);
    SensorManager manager = InstanceManager.getDefault(SensorManager.class);
    Sensor sensor1 = manager.provideSensor("IS1");
    JsonNode result;
    JsonNode message;
    try {
        // set ACTIVE
        message = mapper.createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.ACTIVE);
        result = service.doPost(JsonSensor.SENSOR, "IS1", message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
        Assert.assertNotNull(result);
        // -1 is not a possible value
        Assert.assertEquals(JSON.ACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
        // set INACTIVE
        message = mapper.createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INACTIVE);
        result = service.doPost(JsonSensor.SENSOR, "IS1", message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
        // set UNKNOWN
        message = mapper.createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.UNKNOWN);
        result = service.doPost(JsonSensor.SENSOR, "IS1", message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
        Assert.assertEquals(JSON.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
        // set INCONSISTENT
        message = mapper.createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INCONSISTENT);
        result = service.doPost(JsonSensor.SENSOR, "IS1", message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
        Assert.assertEquals(JSON.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SensorManager(jmri.SensorManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Sensor(jmri.Sensor)

Aggregations

SensorManager (jmri.SensorManager)22 Sensor (jmri.Sensor)20 Test (org.junit.Test)9 JsonException (jmri.server.json.JsonException)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 JmriException (jmri.JmriException)4 Element (org.jdom2.Element)3 IOException (java.io.IOException)2 JsonMockConnection (jmri.server.json.JsonMockConnection)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 NamedBeanHandleManager (jmri.NamedBeanHandleManager)1 Route (jmri.Route)1 Positionable (jmri.jmrit.display.Positionable)1 LayoutBlock (jmri.jmrit.display.layoutEditor.LayoutBlock)1 LayoutBlockManager (jmri.jmrit.display.layoutEditor.LayoutBlockManager)1 LayoutEditor (jmri.jmrit.display.layoutEditor.LayoutEditor)1 LnSensor (jmri.jmrix.loconet.LnSensor)1 Document (org.jdom2.Document)1 XMLOutputter (org.jdom2.output.XMLOutputter)1