Search in sources :

Example 1 with SensorManager

use of jmri.SensorManager in project JMRI by JMRI.

the class AbstractTurnout method provideSecondFeedbackSensor.

@Override
public void provideSecondFeedbackSensor(String pName) throws jmri.JmriException, IllegalArgumentException {
    if (InstanceManager.getNullableDefault(SensorManager.class) != null) {
        if (pName == null || pName.equals("")) {
            provideSecondFeedbackNamedSensor(null);
        } else {
            Sensor sensor = InstanceManager.sensorManagerInstance().provideSensor(pName);
            provideSecondFeedbackNamedSensor(jmri.InstanceManager.getDefault(jmri.NamedBeanHandleManager.class).getNamedBeanHandle(pName, sensor));
        }
    } else {
        log.error("No SensorManager for this protocol");
        throw new jmri.JmriException("No Sensor Manager Found");
    }
}
Also used : SensorManager(jmri.SensorManager) JmriException(jmri.JmriException) Sensor(jmri.Sensor)

Example 2 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 3 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 4 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 5 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)

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