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"));
}
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());
}
}
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());
}
}
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());
}
}
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;
}
Aggregations