Search in sources :

Example 1 with PowerManager

use of jmri.PowerManager in project JMRI by JMRI.

the class JsonPowerHttpService method doGet.

@Override
public JsonNode doGet(String type, String name, Locale locale) throws JsonException {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, POWER);
    ObjectNode data = root.putObject(DATA);
    try {
        PowerManager manager = InstanceManager.getDefault(PowerManager.class);
        if (name != null && !name.isEmpty()) {
            for (PowerManager pm : InstanceManager.getList(PowerManager.class)) {
                if (pm.getUserName().equals(name)) {
                    manager = pm;
                    data.put(NAME, name);
                }
            }
        }
        switch(manager.getPower()) {
            case PowerManager.OFF:
                data.put(STATE, OFF);
                break;
            case PowerManager.ON:
                data.put(STATE, ON);
                break;
            default:
                data.put(STATE, UNKNOWN);
                break;
        }
        data.put(DEFAULT, false);
        if (manager.equals(InstanceManager.getDefault(PowerManager.class))) {
            data.put(DEFAULT, true);
        }
    } catch (JmriException e) {
        log.error("Unable to get Power state.", e);
        throw new JsonException(500, Bundle.getMessage(locale, "ErrorPower"));
    } catch (NullPointerException e) {
        // No PowerManager is defined; just report it as UNKNOWN
        data.put(STATE, UNKNOWN);
    }
    return root;
}
Also used : PowerManager(jmri.PowerManager) JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JmriException(jmri.JmriException)

Example 2 with PowerManager

use of jmri.PowerManager in project JMRI by JMRI.

the class PowerManagerMenu method getManager.

public PowerManager getManager() {
    // start with default
    PowerManager manager = InstanceManager.getNullableDefault(jmri.PowerManager.class);
    if (manager == null) {
        return null;
    }
    String name = manager.getUserName();
    // find active name
    for (JMenuItem item : items) {
        if (item.isSelected()) {
            name = item.getActionCommand();
            break;
        }
    }
    // find PowerManager and return
    List<PowerManager> managers = InstanceManager.getList(PowerManager.class);
    for (PowerManager mgr : managers) {
        if (name.equals(mgr.getUserName())) {
            return mgr;
        }
    }
    // should not happen
    return null;
}
Also used : PowerManager(jmri.PowerManager) JMenuItem(javax.swing.JMenuItem)

Example 3 with PowerManager

use of jmri.PowerManager in project JMRI by JMRI.

the class JsonPowerSocketServiceTest method testPowerChange.

public void testPowerChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().readTree("{}");
        JsonPowerSocketService service = new JsonPowerSocketService(connection);
        PowerManager power = InstanceManager.getDefault(PowerManager.class);
        power.setPower(PowerManager.UNKNOWN);
        service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
        // TODO: test that service is listener in PowerManager
        Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        power.setPower(PowerManager.ON);
        Assert.assertEquals(JSON.ON, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        power.setPower(PowerManager.OFF);
        Assert.assertEquals(JSON.OFF, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        service.onClose();
    // TODO: test that service is no longer a listener in PowerManager
    } catch (IOException | JmriException | JsonException ex) {
        log.error("testPowerChange threw", ex);
        Assert.fail("Unexpected Exception");
    }
}
Also used : PowerManager(jmri.PowerManager) JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 4 with PowerManager

use of jmri.PowerManager in project JMRI by JMRI.

the class JsonPowerHttpServiceTest method testDoPost.

@Test
public void testDoPost() throws JmriException {
    ObjectMapper mapper = new ObjectMapper();
    JsonPowerHttpService service = new JsonPowerHttpService(mapper);
    PowerManager power = InstanceManager.getDefault(PowerManager.class);
    JsonNode result;
    JsonNode message;
    try {
        power.setPower(PowerManager.UNKNOWN);
        message = mapper.createObjectNode().put(JSON.STATE, JSON.ON);
        result = service.doPost(JsonPowerServiceFactory.POWER, null, message, Locale.ENGLISH);
        Assert.assertEquals(PowerManager.ON, power.getPower());
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.ON, result.path(JSON.DATA).path(JSON.STATE).asInt());
        message = mapper.createObjectNode().put(JSON.STATE, JSON.OFF);
        result = service.doPost(JsonPowerServiceFactory.POWER, null, message, Locale.ENGLISH);
        Assert.assertEquals(PowerManager.OFF, power.getPower());
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
        message = mapper.createObjectNode().put(JSON.STATE, JSON.UNKNOWN);
        result = service.doPost(JsonPowerServiceFactory.POWER, null, message, Locale.ENGLISH);
        Assert.assertEquals(PowerManager.OFF, power.getPower());
        Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // Invalid value
        message = mapper.createObjectNode().put(JSON.STATE, 42);
        JsonException exception = null;
        try {
            service.doPost(JsonPowerServiceFactory.POWER, null, message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals(PowerManager.OFF, power.getPower());
        Assert.assertNotNull(exception);
        Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
    } catch (JsonException ex) {
        log.error("Threw", ex);
        Assert.fail("Unexpected Exception");
    }
}
Also used : PowerManager(jmri.PowerManager) JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 5 with PowerManager

use of jmri.PowerManager in project JMRI by JMRI.

the class PowerManagerMenu method setDefault.

void setDefault() {
    // name of default
    PowerManager manager = InstanceManager.getNullableDefault(jmri.PowerManager.class);
    if (manager == null) {
        return;
    }
    String defaultMgr = manager.getUserName();
    for (JMenuItem item : items) {
        if (defaultMgr.equals(item.getActionCommand())) {
            item.setSelected(true);
        }
    }
}
Also used : PowerManager(jmri.PowerManager) JMenuItem(javax.swing.JMenuItem)

Aggregations

PowerManager (jmri.PowerManager)10 JsonException (jmri.server.json.JsonException)6 JmriException (jmri.JmriException)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 IOException (java.io.IOException)2 JMenuItem (javax.swing.JMenuItem)2 JsonMockConnection (jmri.server.json.JsonMockConnection)2 Test (org.junit.Test)2