Search in sources :

Example 6 with LightManager

use of jmri.LightManager in project JMRI by JMRI.

the class XNetLightManagerTest method testAsAbstractFactory.

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

Example 7 with LightManager

use of jmri.LightManager in project JMRI by JMRI.

the class JsonLightSocketServiceTest method testLightChange.

public void testLightChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1");
        JsonLightSocketService service = new JsonLightSocketService(connection);
        LightManager manager = InstanceManager.getDefault(LightManager.class);
        Light light1 = manager.provideLight("IL1");
        service.onMessage(JsonLight.LIGHT, message, Locale.ENGLISH);
        // TODO: test that service is listener in LightManager
        Assert.assertEquals(JSON.OFF, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        light1.setState(Light.ON);
        JUnitUtil.waitFor(() -> {
            return light1.getState() == Light.ON;
        }, "Light to throw");
        Assert.assertEquals(JSON.ON, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        light1.setState(Light.OFF);
        JUnitUtil.waitFor(() -> {
            return light1.getState() == Light.OFF;
        }, "Light to close");
        Assert.assertEquals(Light.OFF, light1.getState());
        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 LightManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) Light(jmri.Light) LightManager(jmri.LightManager) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 8 with LightManager

use of jmri.LightManager in project JMRI by JMRI.

the class JsonLightSocketServiceTest method testOnMessageChange.

public void testOnMessageChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message;
        JsonLightSocketService service = new JsonLightSocketService(connection);
        LightManager manager = InstanceManager.getDefault(LightManager.class);
        Light light1 = manager.provideLight("IL1");
        // Light OFF
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.OFF);
        service.onMessage(JsonLight.LIGHT, message, Locale.ENGLISH);
        Assert.assertEquals(Light.OFF, light1.getState());
        // Light ON
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.ON);
        service.onMessage(JsonLight.LIGHT, message, Locale.ENGLISH);
        Assert.assertEquals(Light.ON, light1.getState());
        // Light UNKNOWN - remains ON
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.UNKNOWN);
        service.onMessage(JsonLight.LIGHT, message, Locale.ENGLISH);
        Assert.assertEquals(Light.ON, light1.getState());
        // Light Invalid State
        // invalid state
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, 42);
        JsonException exception = null;
        try {
            service.onMessage(JsonLight.LIGHT, 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 (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) Light(jmri.Light) LightManager(jmri.LightManager) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 9 with LightManager

use of jmri.LightManager in project JMRI by JMRI.

the class JsonLightHttpServiceTest method testDoGet.

public void testDoGet() throws JmriException {
    JsonLightHttpService service = new JsonLightHttpService(new ObjectMapper());
    LightManager manager = InstanceManager.getDefault(LightManager.class);
    Light light1 = manager.provideLight("IL1");
    JsonNode result;
    try {
        result = service.doGet(JsonLight.LIGHT, "IL1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JsonLight.LIGHT, result.path(JSON.TYPE).asText());
        Assert.assertEquals("IL1", result.path(JSON.DATA).path(JSON.NAME).asText());
        Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
        light1.setState(Light.ON);
        result = service.doGet(JsonLight.LIGHT, "IL1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.ON, result.path(JSON.DATA).path(JSON.STATE).asInt());
        light1.setState(Light.OFF);
        result = service.doGet(JsonLight.LIGHT, "IL1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
    } 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 10 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