Search in sources :

Example 11 with LightManager

use of jmri.LightManager in project JMRI by JMRI.

the class InternalLightManagerTest method testAsAbstractFactory.

@Test
public void testAsAbstractFactory() {
    // create and register the manager object
    InternalLightManager alm = new InternalLightManager();
    jmri.InstanceManager.setLightManager(alm);
    // ask for a Light, and check type
    LightManager lm = jmri.InstanceManager.lightManagerInstance();
    Light tl = lm.newLight("IL21", "my name");
    if (log.isDebugEnabled()) {
        log.debug("received light value " + tl);
    }
    Assert.assertTrue(null != tl);
    // make sure loaded into tables
    if (log.isDebugEnabled()) {
        log.debug("by system name: " + lm.getBySystemName("IL21"));
    }
    if (log.isDebugEnabled()) {
        log.debug("by user name:   " + lm.getByUserName("my name"));
    }
    Assert.assertTrue(null != lm.getBySystemName("IL21"));
    Assert.assertTrue(null != lm.getByUserName("my name"));
}
Also used : Light(jmri.Light) LightManager(jmri.LightManager) Test(org.junit.Test)

Example 12 with LightManager

use of jmri.LightManager in project JMRI by JMRI.

the class JsonLightHttpServiceTest method testDoPut.

public void testDoPut() {
    ObjectMapper mapper = new ObjectMapper();
    JsonLightHttpService service = new JsonLightHttpService(mapper);
    LightManager manager = InstanceManager.getDefault(LightManager.class);
    JsonNode message;
    try {
        // add a light
        Assert.assertNull(manager.getLight("IL1"));
        message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, Light.OFF);
        service.doPut(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
        Assert.assertNotNull(manager.getLight("IL1"));
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) LightManager(jmri.LightManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 13 with LightManager

use of jmri.LightManager in project JMRI by JMRI.

the class JsonLightHttpServiceTest method testDoPost.

public void testDoPost() throws JmriException {
    ObjectMapper mapper = new ObjectMapper();
    JsonLightHttpService service = new JsonLightHttpService(mapper);
    LightManager manager = InstanceManager.getDefault(LightManager.class);
    Light light1 = manager.provideLight("IL1");
    JsonNode result;
    JsonNode message;
    try {
        // set off
        message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.OFF);
        result = service.doPost(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
        Assert.assertEquals(Light.OFF, light1.getState());
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // set on
        message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.ON);
        result = service.doPost(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
        Assert.assertEquals(Light.ON, light1.getState());
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.ON, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // set unknown - remains on
        message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.UNKNOWN);
        result = service.doPost(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
        Assert.assertEquals(Light.ON, light1.getState());
        Assert.assertEquals(JSON.ON, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // set invalid state
        // Invalid value
        message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, 42);
        JsonException exception = null;
        try {
            service.doPost(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals(Light.ON, light1.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) Light(jmri.Light) LightManager(jmri.LightManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 14 with LightManager

use of jmri.LightManager in project JMRI by JMRI.

the class JsonLightHttpServiceTest method testDoGetList.

public void testDoGetList() {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonLightHttpService service = new JsonLightHttpService(mapper);
        LightManager manager = InstanceManager.getDefault(LightManager.class);
        JsonNode result;
        result = service.doGetList(JsonLight.LIGHT, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(0, result.size());
        manager.provideLight("IL1");
        manager.provideLight("IL2");
        result = service.doGetList(JsonLight.LIGHT, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(2, result.size());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) LightManager(jmri.LightManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 15 with LightManager

use of jmri.LightManager in project JMRI by JMRI.

the class AbstractLightManagerConfigXML method loadLights.

/**
     * Utility method to load the individual Light objects. If there's no
     * additional info needed for a specific light type, invoke this with the
     * parent of the set of Light elements.
     *
     * @param lights Element containing the Light elements to load.
     */
@SuppressWarnings("unchecked")
public boolean loadLights(Element lights) {
    boolean result = true;
    List<Element> lightList = lights.getChildren("light");
    if (log.isDebugEnabled()) {
        log.debug("Found " + lightList.size() + " lights");
    }
    LightManager tm = InstanceManager.lightManagerInstance();
    for (int i = 0; i < lightList.size(); i++) {
        String sysName = getSystemName(lightList.get(i));
        if (sysName == null) {
            log.warn("unexpected null in systemName " + lightList.get(i) + " " + lightList.get(i).getAttributes());
            result = false;
            break;
        }
        String userName = getUserName(lightList.get(i));
        checkNameNormalization(sysName, userName, tm);
        if (log.isDebugEnabled()) {
            log.debug("create light: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
        }
        Light lgt = null;
        try {
            lgt = tm.newLight(sysName, userName);
        } catch (IllegalArgumentException e) {
            log.error("failed to create Light: " + sysName);
            return false;
        }
        // load common parts
        loadCommon(lgt, lightList.get(i));
        // variable intensity, transition attributes
        double value;
        value = Double.parseDouble(lightList.get(i).getAttribute("minIntensity").getValue());
        lgt.setMinIntensity(value);
        value = Double.parseDouble(lightList.get(i).getAttribute("maxIntensity").getValue());
        lgt.setMaxIntensity(value);
        value = Double.parseDouble(lightList.get(i).getAttribute("transitionTime").getValue());
        lgt.setTransitionTime(value);
        // provide for legacy light control - panel files written by 2.9.5 or before
        if (lightList.get(i).getAttribute("controlType") != null) {
            // this is a legacy Light - create a LightControl from the input
            String temString = lightList.get(i).getAttribute("controlType").getValue();
            int type = Light.NO_CONTROL;
            try {
                type = Integer.parseInt(temString);
            } catch (NumberFormatException e) {
                log.error("error when converting control type in legacy Light load support");
                type = Light.NO_CONTROL;
            }
            if (type != Light.NO_CONTROL) {
                // this legacy light has a control - capture it
                LightControl lc = new LightControl(lgt);
                lc.setControlType(type);
                if (type == Light.SENSOR_CONTROL) {
                    lc.setControlSensorName(lightList.get(i).getAttribute("controlSensor").getValue());
                    try {
                        lc.setControlSensorSense(Integer.parseInt(lightList.get(i).getAttribute("sensorSense").getValue()));
                    } catch (NumberFormatException e) {
                        log.error("error when converting control sensor sense in legacy Light load");
                    }
                } else if (type == Light.FAST_CLOCK_CONTROL) {
                    int onHour = 0;
                    int onMin = 0;
                    int offHour = 0;
                    int offMin = 0;
                    try {
                        onHour = Integer.parseInt(lightList.get(i).getAttribute("fastClockOnHour").getValue());
                        onMin = Integer.parseInt(lightList.get(i).getAttribute("fastClockOnMin").getValue());
                        offHour = Integer.parseInt(lightList.get(i).getAttribute("fastClockOffHour").getValue());
                        offMin = Integer.parseInt(lightList.get(i).getAttribute("fastClockOffMin").getValue());
                    } catch (NumberFormatException e) {
                        log.error("error when converting fast clock items in legacy Light load");
                    }
                    lc.setFastClockControlSchedule(onHour, onMin, offHour, offMin);
                } else if (type == Light.TURNOUT_STATUS_CONTROL) {
                    lc.setControlTurnout(lightList.get(i).getAttribute("controlTurnout").getValue());
                    try {
                        lc.setControlTurnoutState(Integer.parseInt(lightList.get(i).getAttribute("turnoutState").getValue()));
                    } catch (NumberFormatException e) {
                        log.error("error when converting turnout state in legacy Light load");
                    }
                } else if (type == Light.TIMED_ON_CONTROL) {
                    lc.setControlTimedOnSensorName(lightList.get(i).getAttribute("timedControlSensor").getValue());
                    try {
                        lc.setTimedOnDuration(Integer.parseInt(lightList.get(i).getAttribute("duration").getValue()));
                    } catch (NumberFormatException e) {
                        log.error("error when converting timed sensor items in legacy Light load");
                    }
                }
                lgt.addLightControl(lc);
            }
        }
        // load lightcontrol children, if any
        List<Element> lightControlList = lightList.get(i).getChildren("lightcontrol");
        for (int n = 0; n < lightControlList.size(); n++) {
            boolean noErrors = true;
            Element elem = lightControlList.get(n);
            LightControl lc = new LightControl(lgt);
            String tem = elem.getAttribute("controlType").getValue();
            int type = Light.NO_CONTROL;
            try {
                type = Integer.parseInt(tem);
                lc.setControlType(type);
            } catch (NumberFormatException e) {
                log.error("error when converting control type while loading light " + sysName);
                noErrors = false;
            }
            if (type == Light.SENSOR_CONTROL) {
                lc.setControlSensorName(elem.getAttribute("controlSensor").getValue());
                try {
                    lc.setControlSensorSense(Integer.parseInt(elem.getAttribute("sensorSense").getValue()));
                } catch (NumberFormatException e) {
                    log.error("error when converting control sensor sense while loading light " + sysName);
                    noErrors = false;
                }
            } else if (type == Light.FAST_CLOCK_CONTROL) {
                int onHour = 0;
                int onMin = 0;
                int offHour = 0;
                int offMin = 0;
                try {
                    onHour = Integer.parseInt(elem.getAttribute("fastClockOnHour").getValue());
                    onMin = Integer.parseInt(elem.getAttribute("fastClockOnMin").getValue());
                    offHour = Integer.parseInt(elem.getAttribute("fastClockOffHour").getValue());
                    offMin = Integer.parseInt(elem.getAttribute("fastClockOffMin").getValue());
                    lc.setFastClockControlSchedule(onHour, onMin, offHour, offMin);
                } catch (NumberFormatException e) {
                    log.error("error when converting fast clock items while loading light " + sysName);
                    noErrors = false;
                }
            } else if (type == Light.TURNOUT_STATUS_CONTROL) {
                lc.setControlTurnout(elem.getAttribute("controlTurnout").getValue());
                try {
                    lc.setControlTurnoutState(Integer.parseInt(elem.getAttribute("turnoutState").getValue()));
                } catch (NumberFormatException e) {
                    log.error("error when converting turnout state while loading light " + sysName);
                    noErrors = false;
                }
            } else if (type == Light.TIMED_ON_CONTROL) {
                lc.setControlTimedOnSensorName(elem.getAttribute("timedControlSensor").getValue());
                try {
                    lc.setTimedOnDuration(Integer.parseInt(elem.getAttribute("duration").getValue()));
                } catch (NumberFormatException e) {
                    log.error("error when converting timed sensor items while loading light " + sysName);
                    noErrors = false;
                }
            } else if (type == Light.TWO_SENSOR_CONTROL) {
                lc.setControlSensorName(elem.getAttribute("controlSensor").getValue());
                lc.setControlSensor2Name(elem.getAttribute("controlSensor2").getValue());
                try {
                    lc.setControlSensorSense(Integer.parseInt(elem.getAttribute("sensorSense").getValue()));
                } catch (NumberFormatException e) {
                    log.error("error when converting control sensor2 sense while loading light " + sysName);
                    noErrors = false;
                }
            }
            if (noErrors) {
                lgt.addLightControl(lc);
            }
        }
        // done, start it working
        lgt.activateLight();
    }
    return result;
}
Also used : Light(jmri.Light) Element(org.jdom2.Element) LightManager(jmri.LightManager) LightControl(jmri.implementation.LightControl)

Aggregations

LightManager (jmri.LightManager)15 Light (jmri.Light)11 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 JsonException (jmri.server.json.JsonException)6 Test (org.junit.Test)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 IOException (java.io.IOException)3 JmriException (jmri.JmriException)2 LightControl (jmri.implementation.LightControl)2 JsonMockConnection (jmri.server.json.JsonMockConnection)2 Element (org.jdom2.Element)2 GuiLafPreferencesManager (apps.gui.GuiLafPreferencesManager)1 Image (java.awt.Image)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 AbstractCellEditor (javax.swing.AbstractCellEditor)1 ImageIcon (javax.swing.ImageIcon)1 JButton (javax.swing.JButton)1