Search in sources :

Example 21 with SensorManager

use of jmri.SensorManager in project JMRI by JMRI.

the class JsonSensorSocketServiceTest method testSensorChange.

public void testSensorChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1");
        JsonSensorSocketService service = new JsonSensorSocketService(connection);
        SensorManager manager = InstanceManager.getDefault(SensorManager.class);
        Sensor sensor1 = manager.provideSensor("IS1");
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        // TODO: test that service is listener in SensorManager
        // -1 not possible value
        Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
        sensor1.setKnownState(Sensor.ACTIVE);
        JUnitUtil.waitFor(() -> {
            return sensor1.getKnownState() == Sensor.ACTIVE;
        }, "Sensor ACTIVE");
        Assert.assertEquals(JSON.ACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
        sensor1.setKnownState(Sensor.INACTIVE);
        JUnitUtil.waitFor(() -> {
            return sensor1.getKnownState() == Sensor.INACTIVE;
        }, "Sensor INACTIVE");
        Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
        Assert.assertEquals(JSON.INACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
        service.onClose();
    // TODO: test that service is no longer a listener in SensorManager
    } 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) Sensor(jmri.Sensor)

Example 22 with SensorManager

use of jmri.SensorManager in project JMRI by JMRI.

the class JsonSensorHttpServiceTest method testDoGet.

public void testDoGet() throws JmriException {
    JsonSensorHttpService service = new JsonSensorHttpService(new ObjectMapper());
    SensorManager manager = InstanceManager.getDefault(SensorManager.class);
    // no value
    Sensor sensor1 = manager.provideSensor("IS1");
    JsonNode result;
    try {
        result = service.doGet(JsonSensor.SENSOR, "IS1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JsonSensor.SENSOR, result.path(JSON.TYPE).asText());
        Assert.assertEquals("IS1", result.path(JSON.DATA).path(JSON.NAME).asText());
        // -1 is not a possible value
        Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
        sensor1.setKnownState(Sensor.ACTIVE);
        result = service.doGet(JsonSensor.SENSOR, "IS1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.ACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
        sensor1.setKnownState(Sensor.INACTIVE);
        result = service.doGet(JsonSensor.SENSOR, "IS1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(Sensor.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